001package org.apache.maven.scm.provider.clearcase.command.remove;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
023import java.util.List;
024
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmResult;
028import org.apache.maven.scm.command.remove.AbstractRemoveCommand;
029import org.apache.maven.scm.command.status.StatusScmResult;
030import org.apache.maven.scm.log.ScmLogger;
031import org.apache.maven.scm.provider.ScmProviderRepository;
032import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
033import org.apache.maven.scm.provider.clearcase.command.edit.ClearCaseEditCommand;
034import org.codehaus.plexus.util.cli.CommandLineException;
035import org.codehaus.plexus.util.cli.CommandLineUtils;
036import org.codehaus.plexus.util.cli.Commandline;
037
038/**
039 * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
040 * @author Olivier Lamy
041 *
042 */
043public class ClearCaseRemoveCommand
044    extends AbstractRemoveCommand
045    implements ClearCaseCommand
046{
047    /** {@inheritDoc} */
048    protected ScmResult executeRemoveCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
049                                              String string )
050        throws ScmException
051    {
052        if ( getLogger().isDebugEnabled() )
053        {
054            getLogger().debug( "executing remove command..." );
055        }
056        Commandline cl = createCommandLine( getLogger(), scmFileSet );
057
058        ClearCaseRemoveConsumer consumer = new ClearCaseRemoveConsumer( getLogger() );
059
060        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
061
062        int exitCode;
063        try
064        {
065            // First we need to 'check out' the current directory
066            Commandline checkoutCurrentDirCommandLine =
067                ClearCaseEditCommand.createCheckoutCurrentDirCommandLine( scmFileSet );
068
069            if ( getLogger().isDebugEnabled() )
070            {
071                getLogger().debug(
072                                   "Executing: "
073                                       + checkoutCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
074                                       + ">>" + checkoutCurrentDirCommandLine.toString() );
075            }
076            exitCode =
077                CommandLineUtils.executeCommandLine( checkoutCurrentDirCommandLine,
078                                                     new CommandLineUtils.StringStreamConsumer(), stderr );
079
080            if ( exitCode == 0 )
081            {
082                // Then we add the file
083                if ( getLogger().isDebugEnabled() )
084                {
085                    getLogger().debug(
086                                       "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
087                                           + cl.toString() );
088                }
089                exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
090
091                if ( exitCode == 0 )
092                {
093                    // Then we check in the current directory again.
094                    Commandline checkinCurrentDirCommandLine =
095                        ClearCaseEditCommand.createCheckinCurrentDirCommandLine( scmFileSet );
096                    if ( getLogger().isDebugEnabled() )
097                    {
098                        getLogger().debug(
099                                           "Executing: "
100                                               + checkinCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
101                                               + ">>" + checkinCurrentDirCommandLine.toString() );
102                    }
103                    exitCode = CommandLineUtils.executeCommandLine( checkinCurrentDirCommandLine,
104                                                                    new CommandLineUtils.StringStreamConsumer(),
105                                                                    stderr );
106                }
107            }
108        }
109        catch ( CommandLineException ex )
110        {
111            throw new ScmException( "Error while executing clearcase command.", ex );
112        }
113
114        if ( exitCode != 0 )
115        {
116            return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
117        }
118
119        return new StatusScmResult( cl.toString(), consumer.getRemovedFiles() );
120    }
121
122    // ----------------------------------------------------------------------
123    //
124    // ----------------------------------------------------------------------
125
126    public static Commandline createCommandLine( ScmLogger logger, ScmFileSet scmFileSet )
127    {
128        Commandline command = new Commandline();
129
130        File workingDirectory = scmFileSet.getBasedir();
131
132        command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
133
134        command.setExecutable( "cleartool" );
135
136        command.createArg().setValue( "rmname" );
137
138        command.createArg().setValue( "-nc" );
139
140        List<File> files = scmFileSet.getFileList();
141        for ( File file : files )
142        {
143            if ( logger.isInfoEnabled() )
144            {
145                logger.info( "Deleting file: " + file.getAbsolutePath() );
146            }
147            command.createArg().setValue( file.getName() );
148        }
149
150        return command;
151    }
152}