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