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 * @version $Id: IntegrityExportCommand.java 1.3 2011/08/22 13:06:26EDT Cletus D'Souza (dsouza) Exp  $
050 * @since 1.6
051 */
052public class IntegrityExportCommand
053    extends AbstractExportCommand
054{
055    /**
056     * {@inheritDoc}
057     */
058    @Override
059    public ExportScmResult executeExportCommand( ScmProviderRepository repository, ScmFileSet fileSet,
060                                                 ScmVersion scmVersion, String outputDirectory )
061        throws ScmException
062    {
063        // First lets figure out where we need to export files to...
064        String exportDir = outputDirectory;
065        exportDir =
066            ( ( null != exportDir && exportDir.length() > 0 ) ? exportDir : fileSet.getBasedir().getAbsolutePath() );
067        // Let the user know where we're going to be exporting the files...
068        getLogger().info( "Attempting to export files to " + exportDir );
069        ExportScmResult result;
070        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
071        try
072        {
073            // Lets set our overall export success flag
074            boolean exportSuccess = true;
075            // Perform a fresh checkout of each file in the member list...
076            List<Member> projectMembers = iRepo.getProject().listFiles( exportDir );
077            // Initialize the list of files we actually exported...
078            List<ScmFile> scmFileList = new ArrayList<ScmFile>();
079            for ( Iterator<Member> it = projectMembers.iterator(); it.hasNext(); )
080            {
081                Member siMember = it.next();
082                try
083                {
084                    getLogger().info( "Attempting to export file: " + siMember.getTargetFilePath() + " at revision "
085                                          + siMember.getRevision() );
086                    siMember.checkout( iRepo.getAPISession() );
087                    scmFileList.add( new ScmFile( siMember.getTargetFilePath(), ScmFileStatus.UNKNOWN ) );
088                }
089                catch ( APIException ae )
090                {
091                    exportSuccess = false;
092                    ExceptionHandler eh = new ExceptionHandler( ae );
093                    getLogger().error( "MKS API Exception: " + eh.getMessage() );
094                    getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
095                }
096            }
097            // Lets advice the user that we've checked out all the members
098            getLogger().info(
099                "Exported " + scmFileList.size() + " files out of a total of " + projectMembers.size() + " files!" );
100            if ( exportSuccess )
101            {
102                result = new ExportScmResult( "si co", scmFileList );
103            }
104            else
105            {
106                result = new ExportScmResult( "si co", "Failed to export all files!", "", exportSuccess );
107            }
108        }
109        catch ( APIException aex )
110        {
111            ExceptionHandler eh = new ExceptionHandler( aex );
112            getLogger().error( "MKS API Exception: " + eh.getMessage() );
113            getLogger().debug( eh.getCommand() + " exited with return code " + eh.getExitCode() );
114            result = new ExportScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
115        }
116
117        return result;
118    }
119
120}