001    package org.apache.maven.scm.provider.integrity.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 com.mks.api.response.APIException;
023    import com.mks.api.response.Response;
024    import com.mks.api.response.Result;
025    import com.mks.api.response.WorkItem;
026    import com.mks.api.response.WorkItemIterator;
027    import com.mks.api.si.SIModelTypeName;
028    import org.apache.maven.scm.ScmException;
029    import org.apache.maven.scm.ScmFile;
030    import org.apache.maven.scm.ScmFileSet;
031    import org.apache.maven.scm.ScmFileStatus;
032    import org.apache.maven.scm.ScmVersion;
033    import org.apache.maven.scm.command.changelog.ChangeLogCommand;
034    import org.apache.maven.scm.command.update.AbstractUpdateCommand;
035    import org.apache.maven.scm.command.update.UpdateScmResult;
036    import org.apache.maven.scm.provider.ScmProviderRepository;
037    import org.apache.maven.scm.provider.integrity.ExceptionHandler;
038    import org.apache.maven.scm.provider.integrity.Sandbox;
039    import org.apache.maven.scm.provider.integrity.command.changelog.IntegrityChangeLogCommand;
040    import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
041    
042    import java.util.ArrayList;
043    import java.util.List;
044    
045    /**
046     * MKS Integrity implementation of Maven's AbstractUpdateCommand
047     * <br>This command will execute a 'si resync' to synchronize the sandbox with the server revisions.
048     *
049     * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
050     * @version $Id: IntegrityUpdateCommand.java 1.3 2011/08/22 13:06:42EDT Cletus D'Souza (dsouza) Exp  $
051     * @since 1.6
052     */
053    public class IntegrityUpdateCommand
054        extends AbstractUpdateCommand
055    {
056        /**
057         * {@inheritDoc}
058         */
059        @Override
060        public UpdateScmResult executeUpdateCommand( ScmProviderRepository repository, ScmFileSet fileSet,
061                                                     ScmVersion scmVersion )
062            throws ScmException
063        {
064            getLogger().info( "Attempting to synchronize sandbox in " + fileSet.getBasedir().getAbsolutePath() );
065            List<ScmFile> updatedFiles = new ArrayList<ScmFile>();
066            IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
067            Sandbox siSandbox = iRepo.getSandbox();
068            try
069            {
070                // Make sure we've got a valid sandbox, otherwise create it...
071                if ( siSandbox.create() )
072                {
073                    Response res = siSandbox.resync();
074                    // Lets capture what we got from running this resync
075                    WorkItemIterator wit = res.getWorkItems();
076                    while ( wit.hasNext() )
077                    {
078                        WorkItem wi = wit.next();
079                        if ( wi.getModelType().equals( SIModelTypeName.MEMBER ) )
080                        {
081                            Result message = wi.getResult();
082                            getLogger().debug( wi.getDisplayId() + " " + ( null != message ? message.getMessage() : "" ) );
083                            if ( null != message && message.getMessage().length() > 0 )
084                            {
085                                updatedFiles.add( new ScmFile( wi.getDisplayId(),
086                                                               message.getMessage().equalsIgnoreCase( "removed" )
087                                                                   ? ScmFileStatus.DELETED
088                                                                   : ScmFileStatus.UPDATED ) );
089                            }
090                        }
091                    }
092                    return new UpdateScmResult( res.getCommandString(), updatedFiles );
093                }
094                else
095                {
096                    return new UpdateScmResult( "si resync", "Failed to synchronize workspace", "", false );
097                }
098            }
099            catch ( APIException aex )
100            {
101                ExceptionHandler eh = new ExceptionHandler( aex );
102                getLogger().error( "MKS API Exception: " + eh.getMessage() );
103                getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
104                return new UpdateScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
105            }
106        }
107    
108        /**
109         * {@inheritDoc}
110         */
111        @Override
112        protected ChangeLogCommand getChangeLogCommand()
113        {
114            IntegrityChangeLogCommand command = new IntegrityChangeLogCommand();
115            command.setLogger( getLogger() );
116            return command;
117        }
118    }