001package org.apache.maven.scm.provider.accurev.command.checkin;
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 java.io.File;
023import java.util.Iterator;
024import java.util.List;
025
026import org.apache.maven.scm.CommandParameter;
027import org.apache.maven.scm.CommandParameters;
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.ScmFileStatus;
031import org.apache.maven.scm.ScmResult;
032import org.apache.maven.scm.command.checkin.CheckInScmResult;
033import org.apache.maven.scm.log.ScmLogger;
034import org.apache.maven.scm.provider.ScmProviderRepository;
035import org.apache.maven.scm.provider.accurev.AccuRev;
036import org.apache.maven.scm.provider.accurev.AccuRevException;
037import org.apache.maven.scm.provider.accurev.AccuRevInfo;
038import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
039import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommand;
040
041public class AccuRevCheckInCommand
042    extends AbstractAccuRevCommand
043{
044
045    public AccuRevCheckInCommand( ScmLogger logger )
046    {
047        super( logger );
048    }
049
050    @Override
051    protected ScmResult executeAccurevCommand( AccuRevScmProviderRepository repository, ScmFileSet fileSet,
052                                               CommandParameters parameters )
053        throws ScmException, AccuRevException
054    {
055
056        AccuRev accuRev = repository.getAccuRev();
057
058        String message = parameters.getString( CommandParameter.MESSAGE );
059        List<File> promotedFiles = null;
060
061        File basedir = fileSet.getBasedir();
062        List<File> fileList = fileSet.getFileList();
063
064        if ( fileList.isEmpty() )
065        {
066            // TODO the above test will be matched by a fileset where excludes and includes produce a set with no files.
067            // This is
068            // NOT the same as a fileset created with only a base directory. Raise maven-scm JIRA for this.
069            AccuRevInfo info = accuRev.info( basedir );
070
071            if ( repository.isWorkSpaceRoot( info ) )
072            {
073                promotedFiles = accuRev.promoteAll( basedir, message );
074            }
075            else
076            {
077                throw new ScmException( String.format( "Unsupported recursive checkin for %s. Not the workspace root",
078                                                       basedir.getAbsolutePath() ) );
079            }
080        }
081        else
082        {
083            promotedFiles = accuRev.promote( basedir, fileList, message );
084        }
085
086
087        if ( promotedFiles != null )
088        {
089            Iterator<File> iter = promotedFiles.iterator();
090            while ( iter.hasNext() )
091            {
092                if ( new File( basedir, iter.next().getPath() ).isDirectory() )
093                {
094                    iter.remove();
095                }
096            }
097            // TODO capture the transaction id from the promote
098            return new CheckInScmResult( accuRev.getCommandLines(), getScmFiles( promotedFiles,
099                                                                                 ScmFileStatus.CHECKED_IN ) );
100        }
101        else
102        {
103            return new CheckInScmResult( accuRev.getCommandLines(), "AccuRev Error", accuRev.getErrorOutput(), false );
104        }
105    }
106
107    public CheckInScmResult checkIn( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters )
108        throws ScmException
109    {
110        return (CheckInScmResult) execute( repository, fileSet, parameters );
111    }
112
113}