001    package org.apache.maven.scm.provider.jazz.command.update;
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.ScmFile;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.ScmVersion;
026    import org.apache.maven.scm.command.changelog.ChangeLogCommand;
027    import org.apache.maven.scm.command.update.AbstractUpdateCommand;
028    import org.apache.maven.scm.command.update.UpdateScmResult;
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.changelog.JazzChangeLogCommand;
033    import org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer;
034    
035    //
036    // The Maven SCM Plugin "update" goal is equivalent to the RTC "accept" command.
037    //
038    // NOTE: What is not clear from the docs, is that the accept command will also
039    // update the sandbox with the changes that have been accepted into the repository.
040    // However, I have checked with Rational Support, and this is indeed the expected
041    // behaviour. This may come from the fact that you can accept changes into a
042    // repository workspace, without having a sandbox loaded; though this makes no
043    // sense to us from a maven usage context (as we only work in a sandbox).
044    //
045    // See the following links for additional information on the RTC "create snapshot" command:
046    // RTC 2.0.0.2:
047    // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_accept.html
048    // RTC 3.0:
049    // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_accept.html
050    // RTC 3.0.1:
051    // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_accept.html
052    //
053    
054    /**
055     * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
056     */
057    public class JazzUpdateCommand
058        extends AbstractUpdateCommand
059    {
060        /**
061         * {@inheritDoc}
062         */
063        protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
064            throws ScmException
065        {
066            if ( getLogger().isDebugEnabled() )
067            {
068                getLogger().debug( "Executing update command..." );
069            }
070    
071            JazzUpdateConsumer updateConsumer = new JazzUpdateConsumer( repo, getLogger() );
072            ErrorConsumer err = new ErrorConsumer( getLogger() );
073    
074            JazzScmCommand updateCmd = createAcceptCommand( repo, fileSet );
075            int status = updateCmd.execute( updateConsumer, err );
076    
077            if ( status != 0 || err.hasBeenFed() )
078            {
079                return new UpdateScmResult( updateCmd.getCommandString(),
080                                            "Error code for Jazz SCM update command - " + status, err.getOutput(), false );
081            }
082    
083            if ( getLogger().isDebugEnabled() )
084            {
085                if ( !updateConsumer.getUpdatedFiles().isEmpty() )
086                {
087                    getLogger().debug( "Iterating over \"Update\" results" );
088                    for ( ScmFile file : updateConsumer.getUpdatedFiles() )
089                    {
090                        getLogger().debug( file.getPath() + " : " + file.getStatus() );
091                    }
092                }
093                else
094                {
095                    getLogger().debug( "There are no updated files" );
096                }
097            }
098    
099            // Now, just (re)load the workspace into the sand box.
100            // We can use the checkout directory for this.
101            return new UpdateScmResult( updateCmd.getCommandString(), updateConsumer.getUpdatedFiles() );
102        }
103    
104        public JazzScmCommand createAcceptCommand( ScmProviderRepository repo, ScmFileSet fileSet )
105        {
106            JazzScmCommand command = new JazzScmCommand( JazzConstants.CMD_ACCEPT, repo, fileSet, getLogger() );
107    
108            command.addArgument( JazzConstants.ARG_FLOW_COMPONENTS );
109    
110            return command;
111        }
112    
113        /**
114         * {@inheritDoc}
115         */
116        protected ChangeLogCommand getChangeLogCommand()
117        {
118            JazzChangeLogCommand command = new JazzChangeLogCommand();
119    
120            command.setLogger( getLogger() );
121    
122            return command;
123        }
124    
125    }