001package org.apache.maven.scm.provider.starteam.command.remove;
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.remove.AbstractRemoveCommand;
026import org.apache.maven.scm.command.remove.RemoveScmResult;
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.command.checkin.StarteamCheckInConsumer;
031import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.io.File;
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 StarteamRemoveCommand
044    extends AbstractRemoveCommand
045    implements StarteamCommand
046{
047    /** {@inheritDoc} */
048    protected ScmResult executeRemoveCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message )
049        throws ScmException
050    {
051        if ( getLogger().isInfoEnabled() )
052        {
053            getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
054        }
055
056        StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
057
058        StarteamCheckInConsumer consumer = new StarteamCheckInConsumer( getLogger(), fileSet.getBasedir() );
059
060        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
061
062        List<File> remvoveFiles = fileSet.getFileList();
063
064        if ( remvoveFiles.size() == 0 )
065        {
066            Commandline cl = createCommandLine( repository, fileSet );
067
068            int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
069
070            if ( exitCode != 0 )
071            {
072                return new RemoveScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
073            }
074        }
075        else
076        {
077            //update only interested files already on the local disk
078            for ( int i = 0; i < remvoveFiles.size(); ++i )
079            {
080                File fileToBeRemoved = (File) remvoveFiles.get( i );
081                ScmFileSet scmFileSet = new ScmFileSet( fileSet.getBasedir(), fileToBeRemoved );
082                Commandline cl = createCommandLine( repository, scmFileSet );
083
084                int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
085
086                if ( exitCode != 0 )
087                {
088                    return new RemoveScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(),
089                                                false );
090                }
091            }
092        }
093
094        return new RemoveScmResult( null, consumer.getCheckedInFiles() );
095
096    }
097
098    public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet dirOrFile )
099    {
100        return StarteamCommandLineUtils.createStarteamCommandLine( "remove", null, dirOrFile, repo );
101    }
102}