/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
package com.perforce.maven.scm.provider.p4.command.checkin;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmVersion;
import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
import org.apache.maven.scm.command.checkin.CheckInScmResult;
import org.apache.maven.scm.provider.ScmProviderRepository;
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.command.P4Executor;
import com.perforce.maven.scm.provider.p4.repository.P4ScmProviderRepository;
import com.perforce.p4java.core.ChangelistStatus;
import com.perforce.p4java.core.IChangelist;
import com.perforce.p4java.core.file.FileSpecBuilder;
import com.perforce.p4java.core.file.IFileSpec;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.impl.generic.core.Changelist;
import com.perforce.p4java.impl.mapbased.server.Server;
import com.perforce.p4java.option.changelist.SubmitOptions;
import com.perforce.p4java.option.client.ReopenFilesOptions;
/**
* The class uses the Perforce submit command to checkin files with a changelist
* and its associated jobs.
*/
public class P4CheckInCommand extends AbstractCheckInCommand implements
P4Command {
/** The Constant command. */
public static final String command = "Perforce submit command";
/**
* @see org.apache.maven.scm.command.checkin.AbstractCheckInCommand
* executeCheckInCommand
* (org.apache.maven.scm.provider.ScmProviderRepository,
* org.apache.maven.scm.ScmFileSet, java.lang.String,
* org.apache.maven.scm.ScmVersion)
*/
protected CheckInScmResult executeCheckInCommand(
ScmProviderRepository repo, ScmFileSet files, String message,
ScmVersion version) throws ScmException {
P4CheckInResult p4Result = new P4CheckInResult();
P4ScmProviderRepository p4Repo = (P4ScmProviderRepository) repo;
P4ClientExecutor executor = new P4ClientExecutor(p4Repo, files,
getLogger());
// Get the jobs for submit
List<String> jobs = getJobs(System
.getProperty(P4Executor.P4_JOBS_PROPERTY));
List<IFileSpec> fileSpecs = null;
@SuppressWarnings("unchecked")
List<File> fileList = files.getFileList();
if (fileList != null) {
List<String> filePaths = new ArrayList<String>();
for (File f : fileList) {
if (!f.getName().contentEquals(".")) {
filePaths.add(P4Executor.encodeWildcards(f
.getAbsolutePath()));
}
}
if (!filePaths.isEmpty()) {
fileSpecs = FileSpecBuilder.makeFileSpecList(filePaths
.toArray(new String[filePaths.size()]));
}
}
if (fileSpecs == null || fileSpecs.isEmpty()) {
if (P4Executor.isEmpty(files.getIncludes())
&& P4Executor.isEmpty(files.getExcludes())) {
StringBuffer filePath = new StringBuffer();
filePath.append("...");
fileSpecs = FileSpecBuilder
.makeFileSpecList(new String[] { filePath.toString() });
}
}
if (fileSpecs != null && !fileSpecs.isEmpty()) {
try {
Changelist newChangelistImpl = new Changelist(
IChangelist.UNKNOWN, executor.getClient()
.getName(), executor.getP4User(),
ChangelistStatus.NEW, new Date(), message, false,
(Server) executor.getServer());
IChangelist retChangelist = executor.getClient()
.createChangelist(newChangelistImpl);
ReopenFilesOptions reopenFilesOptions = new ReopenFilesOptions();
reopenFilesOptions.setChangelistId(retChangelist.getId());
List<IFileSpec> retFileSpecs = executor.getClient()
.reopenFiles(fileSpecs, reopenFilesOptions);
// Refresh the changelist
if (retChangelist.canRefresh()) {
retChangelist.refresh();
}
SubmitOptions submitOptions = new SubmitOptions();
if (jobs != null) {
submitOptions.setJobIds(jobs);
}
retFileSpecs = retChangelist.submit(submitOptions);
p4Result.process(retFileSpecs);
} catch (P4JavaException e) {
if (getLogger().isErrorEnabled()) {
getLogger().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
}
return new CheckInScmResult(
command,
p4Result.isSuccess() ? "Submit successful" : "Unable to submit",
p4Result.getCommandOutput(), p4Result.isSuccess());
}
/**
* Gets the tokenized (by whitespace) jobs.
*
* @param jobs
* the jobs
* @return the jobs
*/
private List<String> getJobs(String jobs) {
if (jobs != null) {
String[] tokens = jobs.trim().split("\\s+");
if (tokens != null && tokens.length > 0) {
return new ArrayList<String>(Arrays.asList(tokens));
}
}
return null;
}
}