001package org.apache.maven.scm.provider.integrity.command.mkdir;
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 org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFile;
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmFileStatus;
028import org.apache.maven.scm.ScmResult;
029import org.apache.maven.scm.command.mkdir.AbstractMkdirCommand;
030import org.apache.maven.scm.command.mkdir.MkdirScmResult;
031import org.apache.maven.scm.provider.ScmProviderRepository;
032import org.apache.maven.scm.provider.integrity.ExceptionHandler;
033import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
034
035import java.io.File;
036import java.util.ArrayList;
037import java.util.Iterator;
038import java.util.List;
039
040/**
041 * MKS Integrity implementation of Maven's AbstractMkdirCommand
042 * <br>This command will execute an 'si createsubproject' for the relative path
043 * represented in the fileSet.getFileList().iterator().next() entry.
044 * <br>A single subproject is created required for every directory encountered
045 * in the relative path.
046 *
047 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
048 * @since 1.6
049 */
050public class IntegrityMkdirCommand
051    extends AbstractMkdirCommand
052{
053    /**
054     * Creates a subproject in the Integrity repository.
055     * <br>However, since the subproject automatically creates a folder in
056     * the local sandbox, the createInLocal argument will be ignored
057     */
058    @Override
059    public MkdirScmResult executeMkdirCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message,
060                                               boolean createInLocal )
061        throws ScmException
062    {
063        String dirPath = "";
064        Iterator<File> fit = fileSet.getFileList().iterator();
065        if ( fit.hasNext() )
066        {
067            dirPath = fit.next().getPath().replace( '\\', '/' );
068        }
069        if ( dirPath.isEmpty() )
070        {
071            throw new ScmException( "A relative directory path is required to execute this command!" );
072        }
073        getLogger().info( "Creating subprojects one per directory, as required for " + dirPath );
074        MkdirScmResult result;
075        IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
076        try
077        {
078            Response res = iRepo.getSandbox().createSubproject( dirPath );
079            String subProject = res.getWorkItems().next().getResult().getField( "resultant" ).getItem().getDisplayId();
080            List<ScmFile> createdDirs = new ArrayList<ScmFile>();
081            createdDirs.add( new ScmFile( subProject, ScmFileStatus.ADDED ) );
082            int exitCode = res.getExitCode();
083            boolean success = ( exitCode == 0 ? true : false );
084            getLogger().info( "Successfully created subproject " + subProject );
085            result = new MkdirScmResult( createdDirs,
086                                         new ScmResult( res.getCommandString(), "", "Exit Code: " + exitCode,
087                                                        success ) );
088        }
089        catch ( APIException aex )
090        {
091            ExceptionHandler eh = new ExceptionHandler( aex );
092            getLogger().error( "MKS API Exception: " + eh.getMessage() );
093            getLogger().info( eh.getCommand() + " exited with return code " + eh.getExitCode() );
094            result = new MkdirScmResult( eh.getCommand(), eh.getMessage(), "Exit Code: " + eh.getExitCode(), false );
095        }
096
097        return result;
098    }
099
100}