001package org.apache.maven.scm.provider.starteam.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 org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmVersion;
025import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
026import org.apache.maven.scm.command.checkin.CheckInScmResult;
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.StringUtils;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.io.File;
036import java.util.ArrayList;
037import java.util.List;
038
039/**
040 * @author <a href="mailto:dantran@gmail.com">Dan T. Tran</a>
041 * @author Olivier Lamy
042 *
043 */
044public class StarteamCheckInCommand
045    extends AbstractCheckInCommand
046    implements StarteamCommand
047{
048    // ----------------------------------------------------------------------
049    // AbstractCheckInCommand Implementation
050    // ----------------------------------------------------------------------
051
052    /** {@inheritDoc} */
053    protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
054                                                      ScmVersion version )
055        throws ScmException
056    {
057
058        //work around until maven-scm-api allow this
059        String issueType = System.getProperty( "maven.scm.issue.type" );
060        String issueValue = System.getProperty( "maven.scm.issue.value" );
061        String deprecatedIssue = System.getProperty( "maven.scm.issue" );
062
063        if ( deprecatedIssue != null && deprecatedIssue.trim().length() > 0 )
064        {
065            issueType = "cr";
066            issueValue = deprecatedIssue;
067        }
068
069        if ( getLogger().isInfoEnabled() )
070        {
071            getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
072        }
073
074        StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
075
076        StarteamCheckInConsumer consumer = new StarteamCheckInConsumer( getLogger(), fileSet.getBasedir() );
077
078        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
079
080        List<File> checkInFiles = fileSet.getFileList();
081
082        if ( checkInFiles.isEmpty() )
083        {
084            Commandline cl = createCommandLine( repository, fileSet, message, version, issueType, issueValue );
085
086            int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
087
088            if ( exitCode != 0 )
089            {
090                return new CheckInScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
091            }
092        }
093        else
094        {
095            //update only interested files already on the local disk
096            for ( int i = 0; i < checkInFiles.size(); ++i )
097            {
098                ScmFileSet checkInFile = new ScmFileSet( fileSet.getBasedir(), (File) checkInFiles.get( i ) );
099
100                Commandline cl = createCommandLine( repository, checkInFile, message, version, issueType, issueValue );
101
102                int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
103
104                if ( exitCode != 0 )
105                {
106                    return new CheckInScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(),
107                                                 false );
108                }
109            }
110        }
111
112        return new CheckInScmResult( null, consumer.getCheckedInFiles() );
113
114    }
115
116
117    public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet fileSet, String message,
118                                                 ScmVersion version, String issueType, String issueValue )
119    {
120        List<String> args = new ArrayList<String>();
121
122        if ( message != null && message.length() != 0 )
123        {
124            args.add( "-r" );
125            args.add( message );
126        }
127
128        if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
129        {
130            args.add( "-vl" );
131            args.add( version.getName() );
132        }
133
134        if ( issueType != null && issueType.trim().length() > 0 )
135        {
136            args.add( "-" + issueType.trim() );
137            if ( issueValue != null && issueValue.trim().length() > 0 )
138            {
139                args.add( issueValue.trim() );
140            }
141        }
142
143        boolean checkinDirectory = fileSet.getFileList().size() == 0;
144        if ( !checkinDirectory )
145        {
146            if ( fileSet.getFileList().size() != 0 )
147            {
148                File subFile = (File) fileSet.getFileList().get( 0 );
149                checkinDirectory = subFile.isDirectory();
150            }
151        }
152
153        if ( checkinDirectory )
154        {
155            args.add( "-f" );
156            args.add( "NCI" );
157        }
158
159        StarteamCommandLineUtils.addEOLOption( args );
160
161        return StarteamCommandLineUtils.createStarteamCommandLine( "ci", args, fileSet, repo );
162
163    }
164
165}