Perforce Public Knowledge Base - Common Usage Patterns for P4Java
Reset Search
 

 

Article

Common Usage Patterns for P4Java

« Go Back

Information

 
Problem

There are common usage patterns you may use regularly when coding with P4Java.

Solution
Setting your application's program name and version:
IOptionsServer server = 
   ServerFactory.getOptionsServer("p4java://perforce:1666", null, 
      new UsageOptions(props).setProgramName("JavaBuilder").setProgramVersion("1.2"));

Setting P4CLIENT:
IClient client = server.getClient("myclient");
server.setCurrentClient(client);

Using FileSpecBuilder to create IFileSpec Lists:
FileSpecBuilder.makeFileSpecList("//...@2009/05/10:00:00,2010/05/10:05:00");

FileSpecBuilder.makeFileSpecList("//depot/dev/...@release_12.1_label");

Using chainable setters when using Options objects:
List<IFileSpec> syncFiles = 
   client.sync(FileSpecBuilder.makeFileSpecList("//depot/..."), 
      new SyncOptions().setNoUpdate(true));
List<IFileSpec> openedFiles = 
   server.getOpenedFiles(FileSpecBuilder.makeFileSpecList("//..."), 
      new OpenedFilesOptions().setUserName(user).setClientName(client);

Using the for-each loop to iterate through a List:
for (IFileSpec file : openedFiles) {
   System.out.println(file.getDepotPathString());
   checkStatus(file);
}

Checking the status of an IFileSpec:
public void checkStatus(IFileSpec file) {
   switch (file.getOpStatus()) {
      case VALID:
         continue;
      case CLIENT_ERROR:
         System.out.println("CLIENT_ERROR: " + file.getStatusMessage());
      case ERROR:
         System.out.println("ERROR: " + file.getStatusMessage());
      case INFO:
         System.out.println("INFO: " + file.getStatusMessage());
      default:
      case UNKNOWN:
         System.out.println("UNKOWN EXCEPTION");
   }
}

Create, populate, and submit a changelist:
Changelist changeListImpl = new Changelist(
   IChangelist.UNKNOWN,
   client.getName(),
   userName,
   ChangelistStatus.NEW,
   new Date(),
   "New changelist",
   false,
   (OptionsServer) server
   );
   IChangelist changelist = client.createChangelist(changeListImpl);
   EditFilesOptions editFilesOptions = new EditFilesOptions().setChangelistId(changelist.getId());
				
   List editList = client.editFiles(
      FileSpecBuilder.makeFileSpecList("SampleText.txt"), editFilesOptions);
				
   changelist.update();
   List submitFiles = changelist.submit(false);
}

     

    Related Links

    Feedback

     

    Was this article helpful?


       

    Feedback

    Please tell us how we can make this article more useful.

    Characters Remaining: 255