001package 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
022import org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFile;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.ScmVersion;
026import org.apache.maven.scm.command.changelog.ChangeLogCommand;
027import org.apache.maven.scm.command.update.AbstractUpdateCommand;
028import org.apache.maven.scm.command.update.UpdateScmResult;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.jazz.command.JazzConstants;
031import org.apache.maven.scm.provider.jazz.command.JazzScmCommand;
032import org.apache.maven.scm.provider.jazz.command.changelog.JazzChangeLogCommand;
033import 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 */
057public 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 errConsumer = new ErrorConsumer( getLogger() );
073
074        JazzScmCommand updateCmd = createAcceptCommand( repo, fileSet );
075        int status = updateCmd.execute( updateConsumer, errConsumer );
076
077        if ( status != 0 )
078        {
079            return new UpdateScmResult( updateCmd.getCommandString(),
080                                        "Error code for Jazz SCM update command - " + status, errConsumer.getOutput(), 
081                                        false );
082        }
083
084        if ( getLogger().isDebugEnabled() )
085        {
086            if ( !updateConsumer.getUpdatedFiles().isEmpty() )
087            {
088                getLogger().debug( "Iterating over \"Update\" results" );
089                for ( ScmFile file : updateConsumer.getUpdatedFiles() )
090                {
091                    getLogger().debug( file.getPath() + " : " + file.getStatus() );
092                }
093            }
094            else
095            {
096                getLogger().debug( "There are no updated files" );
097            }
098        }
099
100        // Now, just (re)load the workspace into the sand box.
101        // We can use the checkout directory for this.
102        return new UpdateScmResult( updateCmd.getCommandString(), updateConsumer.getUpdatedFiles() );
103    }
104
105    public JazzScmCommand createAcceptCommand( ScmProviderRepository repo, ScmFileSet fileSet )
106    {
107        JazzScmCommand command = new JazzScmCommand( JazzConstants.CMD_ACCEPT, repo, fileSet, getLogger() );
108
109        command.addArgument( JazzConstants.ARG_FLOW_COMPONENTS );
110
111        return command;
112    }
113
114    /**
115     * {@inheritDoc}
116     */
117    protected ChangeLogCommand getChangeLogCommand()
118    {
119        JazzChangeLogCommand command = new JazzChangeLogCommand();
120
121        command.setLogger( getLogger() );
122
123        return command;
124    }
125
126}