package com.perforce.maven.mojo.jobs;
/**
* Copyright (c) 2010 Perforce Software. All rights reserved.
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.scm.ScmException;
import org.apache.maven.scm.ScmFileSet;
import org.apache.maven.scm.ScmResult;
import org.apache.maven.scm.provider.ScmProvider;
import org.apache.maven.scm.provider.ScmProviderRepository;
import org.apache.maven.scm.repository.ScmRepository;
import com.perforce.maven.mojo.P4Mojo;
import com.perforce.maven.scm.provider.p4.command.P4ClientExecutor;
import com.perforce.maven.scm.provider.p4.command.P4Executor;
import com.perforce.maven.scm.provider.p4.repository.P4ScmProviderRepository;
import com.perforce.p4java.core.IJob;
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.GetJobsOptions;
/**
* Perforce jobs command.
*
* @goal jobs
*/
public class P4JobsMojo extends P4Mojo {
/** The Constant command. */
public static final String command = "Perforce jobs command";
@Override
public ScmResult executeP4Command() throws MojoExecutionException {
try {
ScmRepository repository = getScmRepository();
P4ScmProviderRepository providerRepository = (P4ScmProviderRepository) repository
.getProviderRepository();
ScmProvider scmProvider = scmManager
.getProviderByRepository(repository);
ScmFileSet fileSet = new ScmFileSet(scmDirectory, includes,
excludes);
ScmResult scmResult = executeJobsCommand(providerRepository,
fileSet);
return scmResult;
} catch (ScmException e) {
if (getLog().isErrorEnabled()) {
getLog().error(e);
}
throw new MojoExecutionException("Couldn't run jobs.", e);
} catch (IOException e) {
if (getLog().isErrorEnabled()) {
getLog().error(e);
}
throw new MojoExecutionException(
"Problem occurred while running jobs.", e);
}
}
protected ScmResult executeJobsCommand(ScmProviderRepository repo,
ScmFileSet files) throws ScmException {
P4JobsResult p4Result = new P4JobsResult();
P4ScmProviderRepository p4Repo = (P4ScmProviderRepository) repo;
P4ClientExecutor executor = new P4ClientExecutor(p4Repo, files,
getLogger());
boolean shouldRun = true;
List<IFileSpec> fileSpecs = null;
// The default behavior of ScmFileSet: (includes = **) (excludes =
// <default exclusion patterns>). Override this behavior: only
// include files if includes/excludes is set.
if (!P4Executor.isEmpty(includes) || !P4Executor.isEmpty(excludes)) {
List<String> filePaths = new ArrayList<String>();
List<File> fileList = files.getFileList();
if (fileList != null) {
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(includes) || !P4Executor.isEmpty(excludes)) {
shouldRun = false;
}
}
if (shouldRun) {
try {
GetJobsOptions getJobsOptions = new GetJobsOptions();
List<IJob> retJobs = executor.getServer().getJobs(fileSpecs,
getJobsOptions);
((P4JobsResult) p4Result).processJobs(retJobs);
} catch (P4JavaException e) {
if (getLog().isErrorEnabled()) {
getLog().error(e);
}
throw new ScmException(e.getLocalizedMessage(), e);
}
}
if (getLog().isInfoEnabled()) {
getLog().info(p4Result.getCommandOutput());
}
return new ScmResult(command,
p4Result.isSuccess() ? "Jobs command successful"
: "Unable to run jobs", p4Result.getCommandOutput(),
p4Result.isSuccess());
}
}