001    package org.apache.maven.scm.provider.clearcase.command.add;
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    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.ScmResult;
025    import org.apache.maven.scm.command.add.AbstractAddCommand;
026    import org.apache.maven.scm.command.status.StatusScmResult;
027    import org.apache.maven.scm.provider.ScmProviderRepository;
028    import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
029    import org.apache.maven.scm.provider.clearcase.command.edit.ClearCaseEditCommand;
030    import org.codehaus.plexus.util.cli.CommandLineException;
031    import org.codehaus.plexus.util.cli.CommandLineUtils;
032    import org.codehaus.plexus.util.cli.Commandline;
033    
034    import java.io.File;
035    
036    /**
037     * @author <a href="mailto:wim.deblauwe@gmail.com">Wim Deblauwe</a>
038     * @version $Id: ClearCaseAddCommand.java 1054126 2010-12-31 15:18:11Z olamy $
039     */
040    public class ClearCaseAddCommand
041        extends AbstractAddCommand
042        implements ClearCaseCommand
043    {
044        /** {@inheritDoc} */
045        protected ScmResult executeAddCommand( ScmProviderRepository scmProviderRepository, ScmFileSet scmFileSet,
046                                               String string, boolean b )
047            throws ScmException
048        {
049            if ( getLogger().isDebugEnabled() )
050            {
051                getLogger().debug( "executing add command..." );
052            }
053            Commandline cl = createCommandLine( scmFileSet );
054    
055            ClearCaseAddConsumer consumer = new ClearCaseAddConsumer( getLogger() );
056    
057            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
058    
059            int exitCode;
060    
061            try
062            {
063                // First we need to 'check out' the current directory
064                Commandline checkoutCurrentDirCommandLine =
065                    ClearCaseEditCommand.createCheckoutCurrentDirCommandLine( scmFileSet );
066                if ( getLogger().isDebugEnabled() )
067                {
068                    getLogger().debug(
069                                       "Executing: "
070                                           + checkoutCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
071                                           + ">>" + checkoutCurrentDirCommandLine.toString() );
072                }
073                exitCode = CommandLineUtils.executeCommandLine( checkoutCurrentDirCommandLine,
074                                                                new CommandLineUtils.StringStreamConsumer(), stderr );
075    
076                if ( exitCode == 0 )
077                {
078                    // Then we add the file
079                    if ( getLogger().isDebugEnabled() )
080                    {
081                        getLogger().debug(
082                                           "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>"
083                                               + cl.toString() );
084                    }
085                    exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
086    
087                    if ( exitCode == 0 )
088                    {
089                        // Then we check in the current directory again.
090                        Commandline checkinCurrentDirCommandLine =
091                            ClearCaseEditCommand.createCheckinCurrentDirCommandLine( scmFileSet );
092                        if ( getLogger().isDebugEnabled() )
093                        {
094                            getLogger().debug(
095                                               "Executing: "
096                                                   + checkinCurrentDirCommandLine.getWorkingDirectory().getAbsolutePath()
097                                                   + ">>" + checkinCurrentDirCommandLine.toString() );
098                        }
099                        exitCode = CommandLineUtils.executeCommandLine( checkinCurrentDirCommandLine,
100                                                                        new CommandLineUtils.StringStreamConsumer(),
101                                                                        stderr );
102                    }
103                }
104            }
105            catch ( CommandLineException ex )
106            {
107                throw new ScmException( "Error while executing clearcase command.", ex );
108            }
109    
110            if ( exitCode != 0 )
111            {
112                return new StatusScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
113            }
114    
115            return new StatusScmResult( cl.toString(), consumer.getAddedFiles() );
116        }
117    
118        // ----------------------------------------------------------------------
119        //
120        // ----------------------------------------------------------------------
121    
122        public static Commandline createCommandLine( ScmFileSet scmFileSet )
123        {
124            Commandline command = new Commandline();
125    
126            File workingDirectory = scmFileSet.getBasedir();
127    
128            command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
129    
130            command.setExecutable( "cleartool" );
131    
132            command.createArg().setValue( "mkelem" );
133    
134            command.createArg().setValue( "-c" );
135    
136            command.createArg().setValue( "new file" );
137    
138            command.createArg().setValue( "-nco" );
139    
140            for ( File file : scmFileSet.getFileList() )
141            {
142                command.createArg().setValue( file.getName() );
143            }
144    
145            return command;
146        }
147    
148    }