package com.perforce.jbrown.examples;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import com.perforce.jbrown.utils.DumpUtils;
import com.perforce.jbrown.utils.LameServerFactory;
import com.perforce.jbrown.utils.LogCallBack;
import com.perforce.p4java.client.IClient;
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.AddFilesOptions;
import com.perforce.p4java.server.IOptionsServer;
/**
* Example on how to add one new file to the depot.
*
* @author jbrown
*
*/
public class AddFile {
public static void main(String[] args) {
com.perforce.p4java.Log.setLogCallback(new LogCallBack());
try {
IOptionsServer server = LameServerFactory.getServer();
List<IFileSpec> submittedFiles = submitSampleFile(server,
"//depot/test/sampleFile.txt", "j20152");
// print out results of submit. You'll need to check the results to
// find out what was submitted. We'll just print it out here.
DumpUtils.dumpFileSpecs("Submitted results", submittedFiles);
} catch (Throwable e) {
System.out.println("Exception: " + e.getMessage());
e.printStackTrace(System.out);
}
}
/**
* submit a sample file
*
* @param server
* - valid IOptionsServer instance
* @param depotFile
* - file name to create, depot syntax
* @param clientName
* - exisitng client. The client's view must map the provide
* depotFile
* @throws IOException
*/
private static List<IFileSpec> submitSampleFile(IOptionsServer server,
String depotFile, String clientName)
throws P4JavaException, IOException, AppException {
// set the client
IClient client = server.getClient(clientName);
if (client == null) {
throw new RuntimeException(
"Client '" + clientName + "' not found: create it.");
}
server.setCurrentClient(client);
// create a changelist
IChangelist changeList = client.createChangelist(
new Changelist(IChangelist.UNKNOWN, client.getName(),
client.getOwnerName(), ChangelistStatus.NEW, null,
"Test of adding file for " + depotFile, false,
(Server) server));
// add the file to the changelist
List<IFileSpec> addFileDepotFileSpecs = client
.addFiles(FileSpecBuilder.makeFileSpecList(depotFile),
new AddFilesOptions()
.setChangelistId(changeList.getId())
.setFileType("text"));
// Check that the file was successfully added.
String failMsg = null;
for (IFileSpec fspec : addFileDepotFileSpecs) {
// we only added one file, so abort if the first row shows an error.
if (fspec.getClientPathString() == null) {
// sample fail message:
// "Could not add file: //depot/test/sampleFile.txt - can't add
// existing file"
failMsg = fspec.getStatusMessage();
break;
} else {
// Now write out a dummy file if the file does not
// already exist.
File file = new File(fspec.getClientPathString());
if (!file.exists()) {
file.getParentFile().mkdirs();
FileOutputStream fos = new FileOutputStream(file, false);
try {
fos.write(new byte[]{'a', '\n', 'b', '\n'});
} finally {
if (fos != null) {
fos.close();
}
}
}
}
}
if (failMsg != null) {
// failed adding at least one file: skip the submit.
throw new AppException("Could not add file: " + failMsg);
}
// submit
List<IFileSpec> submittedFiles = changeList.submit(new SubmitOptions());
return submittedFiles;
}
}