001    package org.apache.maven.scm.provider.starteam.command.changelog;
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    
022    import org.apache.maven.scm.ScmBranch;
023    import org.apache.maven.scm.ScmException;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
026    import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
027    import org.apache.maven.scm.command.changelog.ChangeLogSet;
028    import org.apache.maven.scm.provider.ScmProviderRepository;
029    import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
030    import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
031    import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
032    import org.codehaus.plexus.util.StringUtils;
033    import org.codehaus.plexus.util.cli.CommandLineException;
034    import org.codehaus.plexus.util.cli.CommandLineUtils;
035    import org.codehaus.plexus.util.cli.Commandline;
036    
037    import java.util.Date;
038    
039    /**
040     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
041     * @author Olivier Lamy
042     * @version $Id: StarteamChangeLogCommand.java 1054408 2011-01-02 14:05:25Z olamy $
043     */
044    public class StarteamChangeLogCommand
045        extends AbstractChangeLogCommand
046        implements StarteamCommand
047    {
048        // ----------------------------------------------------------------------
049        // AbstractChangeLogCommand Implementation
050        // ----------------------------------------------------------------------
051    
052        /** {@inheritDoc} */
053        protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
054                                                              Date startDate, Date endDate, ScmBranch branch,
055                                                              String datePattern )
056            throws ScmException
057        {
058            if ( ( branch != null || StringUtils.isNotEmpty( ( branch == null ) ? null : branch.getName() ) )
059                && ( getLogger().isWarnEnabled() ) )
060            {
061                getLogger().warn( "This provider doesn't support changelog with on a given branch." );
062            }
063    
064            StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
065    
066            // TODO: revision
067            Commandline cl = createCommandLine( repository, fileSet, startDate );
068    
069            StarteamChangeLogConsumer consumer =
070                new StarteamChangeLogConsumer( fileSet.getBasedir(), getLogger(), startDate, endDate, datePattern );
071    
072            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
073    
074            int exitCode;
075    
076            try
077            {
078                exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
079            }
080            catch ( CommandLineException ex )
081            {
082                throw new ScmException( "Error while executing command.", ex );
083            }
084    
085            if ( exitCode != 0 )
086            {
087                return new ChangeLogScmResult( cl.toString(), "The 'stcmd' command failed.", stderr.getOutput(), false );
088            }
089    
090            return new ChangeLogScmResult( cl.toString(),
091                                           new ChangeLogSet( consumer.getModifications(), startDate, endDate ) );
092        }
093    
094        // ----------------------------------------------------------------------
095        //
096        // ----------------------------------------------------------------------
097    
098        public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet workingDirectory,
099                                                     Date startDate )
100        {
101            //return the full history since we dont know to get hist log from
102            //  creation date to a specific date yet
103            return StarteamCommandLineUtils.createStarteamCommandLine( "hist", null, workingDirectory, repo );
104    
105            /**
106             * unfortunately the below option only gives the hist from view creation date to
107             * the specified date.  What good is that?????
108             */
109    
110            /*
111            if ( startDate != null )
112            {
113                SimpleDateFormat localFormat = new SimpleDateFormat();
114    
115                cl.createArg().setValue( "-cfgd" );
116    
117                cl.createArg().setValue( localFormat.format( startDate ).toString() );
118            }
119            */
120    
121        }
122    }