001package org.apache.maven.scm.provider.integrity.command.export;
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 org.apache.maven.scm.ScmException;
024import org.apache.maven.scm.ScmFile;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmFileStatus;
027import org.apache.maven.scm.ScmVersion;
028import org.apache.maven.scm.command.export.AbstractExportCommand;
029import org.apache.maven.scm.command.export.ExportScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.integrity.ExceptionHandler;
032import org.apache.maven.scm.provider.integrity.Member;
033import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
034
035import java.util.ArrayList;
036import java.util.Iterator;
037import java.util.List;
038
039/**
040 * MKS Integrity implementation for Maven's AbstractExportCommand
041 * <br>Since the IntegrityCheckoutCommand creates a fresh Sandbox in the checkoutDirectory,
042 * it does not make sense for the IntegrityExportCommand to essentially do the same thing.
043 * <br>Hence, this command does not create a Sandbox, instead the entire project contents
044 * are exported to the exportDirectory using the 'si projectco' command.
045 * <br>This gives the user the option of exporting a fresh copy of the repository void of
046 * any project.pj files
047 *
048 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
049 * @since 1.6
050 */
051public class IntegrityExportCommand
052    extends AbstractExportCommand
053{
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    public ExportScmResult executeExportCommand( ScmProviderRepository repository, ScmFileSet fileSet,
059                                                 ScmVersion scmVersion, String outputDirectory )
060        throws ScmException
061    {
062        // First lets figure out where we need to export files to...
063        String exportDir = outputDirectory;
064        exportDir =
065            ( ( null != exportDir && exportDir.length() > 0 ) ? exportDir : fileSet.getBasedir().getAbsolutePath() );
066        // Let the user know where we're going to be exporting the files...
067        getLogger().info( "Attempting to export files to " + exportDir );
068        ExportScmResult result;
069        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
070        try
071        {
072            // Lets set our overall export success flag
073            boolean exportSuccess = true;
074            // Perform a fresh checkout of each file in the member list...
075            List<Member> projectMembers = iRepo.getProject().listFiles( exportDir );
076            // Initialize the list of files we actually exported...
077            List<ScmFile> scmFileList = new ArrayList<ScmFile>();
078            for ( Iterator<Member> it = projectMembers.iterator(); it.hasNext(); )
079            {
080                Member siMember = it.next();
081                try
082                {
083                    getLogger().info( "Attempting to export file: " + siMember.getTargetFilePath() + " at revision "
084                                          + siMember.getRevision() );
085                    siMember.checkout( iRepo.getAPISession() );
086                    scmFileList.add( new ScmFile( siMember.getTargetFilePath(), ScmFileStatus.UNKNOWN ) );
087                }
088                catch ( APIException ae )
089                {
090                    exportSuccess = false;
091                    ExceptionHandler eh = new ExceptionHandler( ae );
092                    getLogger().error( "MKS API Exception: " + eh.getMessage() );
093                    getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
094                }
095            }
096            // Lets advice the user that we've checked out all the members
097            getLogger().info(
098                "Exported " + scmFileList.size() + " files out of a total of " + projectMembers.size() + " files!" );
099            if ( exportSuccess )
100            {
101                result = new ExportScmResult( "si co", scmFileList );
102            }
103            else
104            {
105                result = new ExportScmResult( "si co", "Failed to export all files!", "", exportSuccess );
106            }
107        }
108        catch ( APIException aex )
109        {
110            ExceptionHandler eh = new ExceptionHandler( aex );
111            getLogger().error( "MKS API Exception: " + eh.getMessage() );
112            getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
113            result = new ExportScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
114        }
115
116        return result;
117    }
118
119}