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