001package org.apache.maven.scm.provider.starteam.command.unedit;
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.unedit.AbstractUnEditCommand;
026import org.apache.maven.scm.command.unedit.UnEditScmResult;
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 * @author Olivier Lamy
041 *
042 */
043public class StarteamUnEditCommand
044    extends AbstractUnEditCommand
045    implements StarteamCommand
046{
047    // ----------------------------------------------------------------------
048    // AbstractEditCommand Implementation
049    // ----------------------------------------------------------------------
050
051    /** {@inheritDoc} */
052    protected ScmResult executeUnEditCommand( ScmProviderRepository repo, ScmFileSet fileSet )
053        throws ScmException
054    {
055        if ( getLogger().isInfoEnabled() )
056        {
057            getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
058        }
059
060        StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
061
062        StarteamUnEditConsumer consumer = new StarteamUnEditConsumer( getLogger(), fileSet.getBasedir() );
063
064        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
065
066        List<File> unlockFiles = fileSet.getFileList();
067
068        if ( unlockFiles.size() == 0 )
069        {
070            Commandline cl = createCommandLine( repository, fileSet );
071
072            int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
073
074            if ( exitCode != 0 )
075            {
076                return new UnEditScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
077            }
078        }
079        else
080        {
081            //edit only interested files already on the local disk
082            for ( int i = 0; i < unlockFiles.size(); ++i )
083            {
084                ScmFileSet unlockFile = new ScmFileSet( fileSet.getBasedir(), (File) unlockFiles.get( i ) );
085                Commandline cl = createCommandLine( repository, unlockFile );
086
087                int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
088
089                if ( exitCode != 0 )
090                {
091                    return new UnEditScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(),
092                                                false );
093                }
094            }
095        }
096
097        return new UnEditScmResult( null, consumer.getUnEditFiles() );
098
099    }
100
101    public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet dirOrFile )
102    {
103        List<String> args = new ArrayList<String>();
104        args.add( "-u" );
105
106        return StarteamCommandLineUtils.createStarteamCommandLine( "lck", args, dirOrFile, repo );
107    }
108}