/**
* 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.P4ClientExecutor;
import com.perforce.maven.scm.provider.p4.command.P4Command;
import com.perforce.maven.scm.provider.p4.repository.P4ScmProviderRepository;
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;
/**
* 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";
/**
* @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 {
P4ScmProviderRepository p4Repo = (P4ScmProviderRepository) repo;
P4ClientExecutor executor = new P4ClientExecutor(p4Repo, fileSet,
getLogger());
// Construct file path revision suffix with start date
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd:HH:mm:ss");
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
StringBuffer filePath = new StringBuffer();
filePath.append("...").append(filePathSuffix.toString());
List<IFileSpec> fileSpecs = FileSpecBuilder
.makeFileSpecList(new String[] { filePath.toString() });
List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
try {
GetChangelistsOptions getChangelistsOptions = new GetChangelistsOptions();
if (executor.getP4Client() != null) {
getChangelistsOptions.setClientName(executor.getP4Client());
}
List<IChangelistSummary> retChangelistSummaries = executor
.getServer().getChangelists(fileSpecs,
getChangelistsOptions);
// Convert result changelists to changesets
if (retChangelistSummaries != null) {
for (IChangelistSummary changelistSummary : retChangelistSummaries) {
IChangelist changelist = executor.getServer()
.getChangelist(changelistSummary.getId());
List<IFileSpec> files = changelist.getFiles(true);
ChangeSet changeSet = new ChangeSet();
changeSet.setAuthor(changelistSummary.getUsername());
changeSet.setComment(changelistSummary.getDescription());
changeSet.setDate(changelistSummary.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) {
if (getLogger().isErrorEnabled()) {
getLogger().error(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);
}
}