001package org.apache.maven.scm.provider.integrity.command.status;
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.Item;
024import com.mks.api.response.WorkItem;
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmFile;
027import org.apache.maven.scm.ScmFileSet;
028import org.apache.maven.scm.ScmFileStatus;
029import org.apache.maven.scm.ScmResult;
030import org.apache.maven.scm.command.status.AbstractStatusCommand;
031import org.apache.maven.scm.command.status.StatusScmResult;
032import org.apache.maven.scm.provider.ScmProviderRepository;
033import org.apache.maven.scm.provider.integrity.ExceptionHandler;
034import org.apache.maven.scm.provider.integrity.Sandbox;
035import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
036
037import java.io.File;
038import java.util.ArrayList;
039import java.util.Iterator;
040import java.util.List;
041
042/**
043 * MKS Integrity implementation for Maven's AbstractStatusCommand
044 * <br>This command will execute a 'si viewsandbox' and report on all
045 * changed and dropped working files.  Additionally, this command
046 * will also run a 'si viewnonmembers' command to report on all new
047 * files added in the sandbox directory
048 *
049 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
050 * @since 1.6
051 */
052public class IntegrityStatusCommand
053    extends AbstractStatusCommand
054{
055    /**
056     * {@inheritDoc}
057     */
058    @Override
059    public StatusScmResult executeStatusCommand( ScmProviderRepository repository, ScmFileSet fileSet )
060        throws ScmException
061    {
062        StatusScmResult result;
063        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
064        getLogger().info( "Status of files changed in sandbox " + fileSet.getBasedir().getAbsolutePath() );
065        try
066        {
067            // Initialize the list of ScmFile objects for the StatusScmResult
068            List<ScmFile> scmFileList = new ArrayList<ScmFile>();
069
070            // Get a listing for all the changes in the sandbox
071            Sandbox siSandbox = iRepo.getSandbox();
072            // Get the excludes and includes list from the configuration
073            String excludes = Sandbox.formatFilePatterns( fileSet.getExcludes() );
074            String includes = Sandbox.formatFilePatterns( fileSet.getIncludes() );
075
076            // Get the new members found in the sandbox
077            List<ScmFile> newMemberList = siSandbox.getNewMembers( excludes, includes );
078            // Update the scmFileList with our updates
079            scmFileList.addAll( newMemberList );
080
081            // Get the changed/dropped members from the sandbox
082            List<WorkItem> changeList = siSandbox.getChangeList();
083            for ( Iterator<WorkItem> wit = changeList.iterator(); wit.hasNext(); )
084            {
085                WorkItem wi = wit.next();
086                File memberFile = new File( wi.getField( "name" ).getValueAsString() );
087                // Separate the changes into files that have been updated and deleted files
088                if ( siSandbox.hasWorkingFile( (Item) wi.getField( "wfdelta" ).getValue() ) )
089                {
090                    scmFileList.add( new ScmFile( memberFile.getAbsolutePath(), ScmFileStatus.UPDATED ) );
091                }
092                else
093                {
094                    scmFileList.add( new ScmFile( memberFile.getAbsolutePath(), ScmFileStatus.DELETED ) );
095                }
096            }
097
098            if ( scmFileList.size() == 0 )
099            {
100                getLogger().info( "No local changes found!" );
101            }
102            result = new StatusScmResult( scmFileList, new ScmResult( "si viewsandbox", "", "", true ) );
103        }
104        catch ( APIException aex )
105        {
106            ExceptionHandler eh = new ExceptionHandler( aex );
107            getLogger().error( "MKS API Exception: " + eh.getMessage() );
108            getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
109            result = new StatusScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
110        }
111
112        return result;
113    }
114
115}