/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
package com.perforce.maven.scm.provider.p4.command.changelog;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.maven.scm.ChangeFile;
import org.apache.maven.scm.ChangeSet;
import org.apache.maven.scm.ScmBranch;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmVersion;
import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
import org.apache.maven.scm.command.changelog.ChangeLogSet;
import org.apache.maven.scm.provider.ScmProviderRepository;
import org.codehaus.plexus.util.StringUtils;
import com.perforce.maven.scm.provider.p4.command.P4Command;
import com.perforce.p4java.client.IClient;
import com.perforce.p4java.core.IChangelist;
import com.perforce.p4java.core.IChangelistSummary;
import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.option.server.GetChangelistsOptions;
import com.perforce.p4java.server.IOptionsServer;
/**
* Use Perforce changes command to get a list of submitted changelists. For each changelist it will issue the Perforce
* describe command to get the changelist details.
*/
public class P4ChangeLogCommand
extends AbstractChangeLogCommand
implements P4Command
{
/** The Constant command. */
public static final String command = "Perforce changes command";
private IClient client;
public void setClient( IClient client )
{
this.client = client;
}
/**
* @see org.apache.maven.scm.command.changelog.AbstractChangeLogCommand executeChangeLogCommand
* (org.apache.maven.scm.provider.ScmProviderRepository, org.apache.maven.scm.ScmFileSet,
* org.apache.maven.scm.ScmVersion, org.apache.maven.scm.ScmVersion, java.lang.String)
*/
protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
ScmVersion startVersion, ScmVersion endVersion,
String datePattern )
throws ScmException
{
return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
}
/**
* @see org.apache.maven.scm.command.changelog.AbstractChangeLogCommand executeChangeLogCommand
* (org.apache.maven.scm.provider.ScmProviderRepository, org.apache.maven.scm.ScmFileSet, java.util.Date,
* java.util.Date, org.apache.maven.scm.ScmBranch, java.lang.String)
*/
protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
Date startDate, Date endDate, ScmBranch branch,
String datePattern )
throws ScmException
{
if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
{
throw new ScmException( "This SCM doesn't support branches." );
}
return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
}
/**
* Execute change log command.
*
* @param repo the repo
* @param fileSet the file set
* @param startDate the start date
* @param endDate the end date
* @param branch the branch
* @param datePattern the date pattern
* @param startVersion the start version
* @param endVersion the end version
* @return the change log scm result
* @throws ScmException the scm exception
*/
protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
Date startDate, Date endDate, ScmBranch branch,
String datePattern, ScmVersion startVersion,
ScmVersion endVersion )
throws ScmException
{
DateFormat dateFormat = new SimpleDateFormat( "yyyy/MM/dd HH:mm:ss" );
if ( datePattern != null )
{
dateFormat = new SimpleDateFormat( datePattern );
}
// Construct file path revision suffix with start date
StringBuilder filePathSuffix = new StringBuilder();
if ( startDate != null )
{
filePathSuffix.append( "@" ).append( dateFormat.format( startDate ) ).append( "," );
if ( endDate != null )
{
filePathSuffix.append( dateFormat.format( endDate ) );
}
else
{
filePathSuffix.append( "now" );
}
}
// Construct file path revision suffix with start version
if ( startVersion != null )
{
filePathSuffix.append( "@" ).append( startVersion.getName() ).append( "," );
if ( endVersion != null )
{
filePathSuffix.append( endVersion.getName() );
}
else
{
filePathSuffix.append( "now" );
}
}
// Construct file path
String filePath = fileSet.getBasedir() + "/..." + filePathSuffix.toString();
List<IFileSpec> fileSpecs = FileSpecBuilder.makeFileSpecList( filePath );
List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
try
{
GetChangelistsOptions getChangelistsOptions = new GetChangelistsOptions();
IOptionsServer server = (IOptionsServer) client.getServer();
List<IChangelistSummary> retChangelistSummaries = server.getChangelists( fileSpecs, getChangelistsOptions );
// Convert result changelists to changesets
for ( IChangelistSummary changelistSummary : retChangelistSummaries )
{
IChangelist changelist = server.getChangelist( changelistSummary.getId() );
List<IFileSpec> files = changelist.getFiles( true );
ChangeSet changeSet = new ChangeSet();
changeSet.setAuthor( changelist.getUsername() );
changeSet.setComment( changelist.getDescription().trim() );// p4 adds extra /n and fails TCK
changeSet.setDate( changelist.getDate() );
changeSet.setRevision( String.valueOf( changelistSummary.getId() ) );
if ( files != null )
{
for ( IFileSpec file : files )
{
ChangeFile changeFile =
new ChangeFile( file.getDepotPath().getPathString(), String.valueOf( file.getEndRevision() ) );
changeSet.addFile( changeFile );
}
}
changeSets.add( changeSet );
}
}
catch ( P4JavaException e )
{
throw new ScmException( e.getLocalizedMessage(), e );
}
ChangeLogSet cls = new ChangeLogSet( changeSets, null, null );
cls.setStartVersion( startVersion );
cls.setEndVersion( endVersion );
return new ChangeLogScmResult( command, cls );
}
}