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