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