001package org.apache.maven.scm.provider.starteam.command.edit;
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 org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmResult;
025import org.apache.maven.scm.command.edit.AbstractEditCommand;
026import org.apache.maven.scm.command.edit.EditScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
029import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
030import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
031import org.codehaus.plexus.util.cli.CommandLineUtils;
032import org.codehaus.plexus.util.cli.Commandline;
033
034import java.io.File;
035import java.util.ArrayList;
036import java.util.List;
037
038/**
039 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
040 *
041 */
042public class StarteamEditCommand
043    extends AbstractEditCommand
044    implements StarteamCommand
045{
046    // ----------------------------------------------------------------------
047    // AbstractEditCommand Implementation
048    // ----------------------------------------------------------------------
049
050    /** {@inheritDoc} */
051    protected ScmResult executeEditCommand( ScmProviderRepository repo, ScmFileSet fileSet )
052        throws ScmException
053    {
054        if ( getLogger().isInfoEnabled() )
055        {
056            getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
057        }
058
059        StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
060
061        StarteamEditConsumer consumer = new StarteamEditConsumer( getLogger(), fileSet.getBasedir() );
062
063        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
064
065        List<File> editFiles = fileSet.getFileList();
066
067        if ( editFiles.size() == 0 )
068        {
069            Commandline cl = createCommandLine( repository, fileSet );
070
071            int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
072
073            if ( exitCode != 0 )
074            {
075                return new EditScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
076            }
077        }
078        else
079        {
080            //edit only interested files already on the local disk
081            for ( int i = 0; i < editFiles.size(); ++i )
082            {
083                ScmFileSet editFile = new ScmFileSet( fileSet.getBasedir(), (File) editFiles.get( i ) );
084                Commandline cl = createCommandLine( repository, editFile );
085
086                int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
087
088                if ( exitCode != 0 )
089                {
090                    return new EditScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(),
091                                              false );
092                }
093            }
094        }
095
096        return new EditScmResult( null, consumer.getEditedFiles() );
097
098    }
099
100    public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet dirOrFile )
101    {
102        List<String> args = new ArrayList<String>();
103        args.add( "-l" );
104
105        return StarteamCommandLineUtils.createStarteamCommandLine( "lck", args, dirOrFile, repo );
106    }
107}