package com.perforce.jbrown.examples;
import com.perforce.jbrown.utils.LameServerFactory;
import com.perforce.jbrown.utils.LogCallBack;
import com.perforce.p4java.client.IClient;
import com.perforce.p4java.client.IClientSummary;
import com.perforce.p4java.exception.P4JavaException;
import com.perforce.p4java.impl.generic.client.ClientView;
import com.perforce.p4java.impl.generic.client.ClientView.ClientViewMapping;
import com.perforce.p4java.server.IOptionsServer;
/**
* Example showing creating or updating a Client spec.
*
* @author jbrown
*
*/
public class ClientCreate {
public static void main(String[] args) {
// TODO Auto-generated method stub
com.perforce.p4java.Log.setLogCallback(new LogCallBack());
try {
IOptionsServer server = LameServerFactory.getServer();
String resultMsg = createClassicClient(server, "TestBranch-classic-client",
"testBranch");
System.out.println(resultMsg);
resultMsg = createStreamClient(server, "TestBranch-main-stream-client",
"testBranch", "main");
System.out.println(resultMsg);
} catch (Throwable e) {
System.out.println("Exeception: " + e.getMessage());
e.printStackTrace(System.out);
}
}
/**
* Creates or updates a client with a predefined View mapping
*
* @param server valid IOptionsServer instance
* @param clientName name of client to create or update
* @param branchName name of branch
* @return Message returned from server
* @throws P4JavaException
* @throws AppException client not created or updated.
*/
private static String createClassicClient(IOptionsServer server,
String clientName, String branchName)
throws P4JavaException, AppException {
IClient client;
boolean alreadyExists = false;
client = server.getClient(clientName);
if (client != null) {
alreadyExists = true;
} else {
client = server.getClientTemplate(clientName);
}
// set the view mapping. This overwrites anything existing.
client.setClientView(getViewMapping(clientName, branchName));
client.setDescription(
"Classic Client for branch " + branchName + ".\nCreated by " + server.getUserName());
client.setLineEnd(IClientSummary.ClientLineEnd.LOCAL);
// leave the options (rmdir, allwrite, etc) unchanged
String resultMsg;
if (alreadyExists) {
resultMsg = server.updateClient(client);
} else {
resultMsg = server.createClient(client);
}
String saveMsg = "Client " + clientName + " saved";
String saveMsg2 = "Client " + clientName + " not changed";
if (!resultMsg.contains(saveMsg) && !resultMsg.contains(saveMsg2)) {
String msg = "Failed to create or update client '" + clientName
+ "': "
+ resultMsg;
throw new AppException(msg);
}
return resultMsg;
}
/**
* Creates or updates a stream client.
* The stream is named "//streams/" + branchName + stream and must
* already exist.
*
* @param server valid IOptionsServer instance
* @param clientName name of client to create or update
* @param branchName branch.
* @param stream stream. For example: main, rel1.0, dev
* @return Message returned from server
* @throws P4JavaException
* @throws AppException client not created or updated.
*/
private static String createStreamClient(IOptionsServer server,
String clientName, String branchName, String stream)
throws P4JavaException, AppException {
IClient client;
boolean alreadyExists = false;
client = server.getClient(clientName);
if (client != null) {
alreadyExists = true;
} else {
client = server.getClientTemplate(clientName);
}
String streamName = "//streams/" + branchName + "/" + stream; // Stream must already exist
client.setStream(streamName);
client.setDescription(
"Stream Client for branch " + branchName
+ ".\nCreated by " + server.getUserName());
client.setLineEnd(IClientSummary.ClientLineEnd.LOCAL);
// leave the options (rmdir, allwrite, etc) unchanged
String resultMsg;
if (alreadyExists) {
resultMsg = server.updateClient(client);
} else {
resultMsg = server.createClient(client);
}
String saveMsg = "Client " + clientName + " saved";
String saveMsg2 = "Client " + clientName + " not changed";
if (!resultMsg.contains(saveMsg) && !resultMsg.contains(saveMsg2)) {
String msg = "Failed to create or update client '" + clientName
+ "': "
+ resultMsg;
throw new AppException(msg);
}
return resultMsg;
}
/**
* Create the client ViewMapping instance.
*
* @param clientName name of client
* @param branchName name of branch
* @return
*/
private static ClientView getViewMapping(String clientName, String branchName) {
ClientView clientView = new ClientView();
ClientViewMapping clientViewMapping;
String depotPath, clientPath;
int viewCnt = 0;
depotPath = "//depot/main/" + branchName + "/src/...";
clientPath = "//" + clientName + "/main/src/...";
clientViewMapping = new ClientViewMapping(viewCnt++, depotPath,
clientPath);
clientView.addEntry(clientViewMapping);
depotPath = "//depot/dev/" + branchName + "/src/...";
clientPath = "//" + clientName + "/dev/src/...";
clientViewMapping = new ClientViewMapping(viewCnt++, depotPath,
clientPath);
clientView.addEntry(clientViewMapping);
return clientView;
}
}