001    package org.apache.maven.scm.provider.jazz.command.checkout;
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.ScmBranch;
023    import org.apache.maven.scm.ScmException;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.ScmTag;
026    import org.apache.maven.scm.ScmVersion;
027    import org.apache.maven.scm.command.checkout.AbstractCheckOutCommand;
028    import org.apache.maven.scm.command.checkout.CheckOutScmResult;
029    import org.apache.maven.scm.provider.ScmProviderRepository;
030    import org.apache.maven.scm.provider.jazz.command.JazzConstants;
031    import org.apache.maven.scm.provider.jazz.command.JazzScmCommand;
032    import org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer;
033    import org.apache.maven.scm.provider.jazz.repository.JazzScmProviderRepository;
034    import org.codehaus.plexus.util.StringUtils;
035    
036    //
037    // The Maven SCM plugin "checkout" goal is equivalent to the RTC "load" command.
038    //
039    // See the following links for additional information on the RTC "load" command:
040    // RTC 2.0.0.2:
041    // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_load.html
042    // RTC 3.0:
043    // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_load.html
044    // RTC 3.0.1:
045    // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_load.html
046    //
047    
048    /**
049     * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
050     */
051    public class JazzCheckOutCommand
052        extends AbstractCheckOutCommand
053    {
054        /**
055         * {@inheritDoc}
056         */
057        protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet fileSet,
058                                                            ScmVersion scmVersion, boolean recursive )
059            throws ScmException
060        {
061            // TODO - Figure out how this recursive boolean impacts Jazz SCM "checkout" (load).
062    
063            if ( getLogger().isDebugEnabled() )
064            {
065                getLogger().debug( "Executing checkout command..." );
066            }
067    
068            JazzScmProviderRepository jazzRepo = (JazzScmProviderRepository) repo;
069    
070            JazzScmCommand checkoutCmd = createJazzLoadCommand( jazzRepo, fileSet, scmVersion );
071            JazzCheckOutConsumer checkoutConsumer = new JazzCheckOutConsumer( repo, getLogger() );
072            ErrorConsumer errConsumer = new ErrorConsumer( getLogger() );
073    
074            int status = checkoutCmd.execute( checkoutConsumer, errConsumer );
075            if ( status != 0 || errConsumer.hasBeenFed() )
076            {
077                return new CheckOutScmResult( checkoutCmd.getCommandString(),
078                                              "Error code for Jazz SCM checkout (load) command - " + status,
079                                              errConsumer.getOutput(), false );
080            }
081    
082            return new CheckOutScmResult( checkoutCmd.getCommandString(), checkoutConsumer.getCheckedOutFiles() );
083        }
084    
085        public JazzScmCommand createJazzLoadCommand( JazzScmProviderRepository repo, ScmFileSet fileSet,
086                                                     ScmVersion scmVersion )
087        {
088            JazzScmCommand command =
089                new JazzScmCommand( JazzConstants.CMD_LOAD, JazzConstants.ARG_FORCE, repo, fileSet, getLogger() );
090    
091            if ( fileSet != null )
092            {
093                command.addArgument( JazzConstants.ARG_LOCAL_WORKSPACE_PATH );
094                command.addArgument( fileSet.getBasedir().getAbsolutePath() );
095            }
096    
097            // This works in tandem with the Tag Command.
098            // Currently, RTC can not check out directly from a snapshot.
099            // So, as a work around, the Tag Command creates a workspace name of the same name as the snapshot.
100            // The functionality here (in using the ScmTag or ScmBranch) assumes that the workspace has been
101            // created as a part of the Tag Command. 
102    
103            String workspace = repo.getRepositoryWorkspace();
104            if ( scmVersion != null && StringUtils.isNotEmpty( scmVersion.getName() ) )
105            {
106                // Just in case we ever do something different for Tags (snapshots) and Branches (streams)
107                if ( scmVersion instanceof ScmTag )
108                {
109                    workspace = scmVersion.getName();
110                }
111                else if ( scmVersion instanceof ScmBranch )
112                {
113                    workspace = scmVersion.getName();
114                }
115            }
116    
117            command.addArgument( workspace );
118    
119            return command;
120        }
121    }