The following example shows code to create a new version by updating a document and subsequently retrieving all the versions of the document.
package com.sungard.bootcamp.client;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.stardust.engine.api.runtime.DmsUtils;
import org.eclipse.stardust.engine.api.runtime.Document;
import org.eclipse.stardust.engine.api.runtime.DocumentInfo;
import org.eclipse.stardust.engine.api.runtime.DocumentManagementService;
import org.eclipse.stardust.engine.api.runtime.ServiceFactory;
import org.eclipse.stardust.engine.api.runtime.ServiceFactoryLocator;
public class DMSClient{
public static void main(String[] args) {
// Obtain a reference to the ServiceFactory
ServiceFactory sf = getServiceFactory(null, null, null, "motu", "motu");
DocumentManagementService dms = sf.getDocumentManagementService();
Document doc = createDoc(dms, new File("D:/MySampleFile.txt"));
System.out.println("Id of CREATED Document is : " + (doc).getId());
System.out.println("Description from CREATED Document : "+ doc.getDescription());
System.out.println("Content of CREATED Document : " + new String (dms.retrieveDocumentContent(doc.getId())));
// We need to call dms.versionDocument(doc.getId(), "0") and set parameter versionLabel to 0
dms.versionDocument(doc.getId(), "0");
System.out.println("======================================================");
//update the document
Document updatedDoc = updateDoc(dms, doc,"D:/UpdatedContentFile.txt","UPDATED Description");
System.out.println("Id of UPDATED Document is : "+ (updatedDoc).getId());
//fetch the updated document to display the details of the document
Document fetchedDocument = dms.getDocument(updatedDoc.getId());
System.out.println("Contents of UPDATED/Fetched Document : " + new String (
dms.retrieveDocumentContent(fetchedDocument.getRevisionId())));
System.out.println("Description from Fetched Document : "+ fetchedDocument.getDescription());
System.out.println("#######################################################");
//update the document again -- to create sufficient document revisions
Document updatedDoc2 = updateDoc(dms, doc,"D:/UpdatedContentFile2.txt","Again Updated Description");
System.out.println("Id of UPDATED Document is : "+ (updatedDoc2).getId());
Document fetchedDocument2 = dms.getDocument(updatedDoc2.getId());
System.out.println("Contents of UPDATED/Fetched Document : " + new String (
dms.retrieveDocumentContent(fetchedDocument2.getRevisionId())));
System.out.println("Description from Fetched Document : "+ fetchedDocument2.getDescription());
List <Document> docVersions= dms.getDocumentVersions(fetchedDocument.getId());
if(docVersions!= null){
int size = docVersions.size();
System.out.println("Number of Document Versions :: "+size);
for(int i=0;i<size;i++ ){
System.out.println("------------Below are the Document Versions' Details--------------");
Document document = (Document)docVersions.get(i);
System.out.println("Revision Name :: "+document.getRevisionName());
System.out.println("Description :: "+document.getDescription());
System.out.println("Document Contents :: " +new String(
dms.retrieveDocumentContent(document.getRevisionId())));
}
}
}
private static Document createDoc(DocumentManagementService dms, File file) {
DocumentInfo documentInfo = constructDocInfo(file);
try {
byte[] documentContent = FileUploadUtils.getBytesFromFile(file);
return (dms.createDocument("/documents/", documentInfo, documentContent, null));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static Document updateDoc(DocumentManagementService dms,Document doc, String fileName,
String description) {
doc.setDescription(description);
System.out.println("Calling updateDocument() with updated Contents and Description for Document '"+
fileName +"'");
return (dms.updateDocument(doc, readFileContent(fileName), "UTF-8", true,"TOBEREVISED", false));
}
private static byte[] readFileContent(String fileName) {
byte[] bytes = null;
try {
File file = new File(fileName);
// File length
int size = (int) file.length();
if (size > Integer.MAX_VALUE) {
System.out.println("File is too large");
}
bytes = new byte[size];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
int read = 0;
int numRead = 0;
while (read < bytes.length&& (numRead = dis.read(bytes, read, bytes.length - read)) >= 0) {
read = read + numRead;
}
// Ensure all the bytes have been read in
if (read < bytes.length) {
System.out.println("Could not completely read: "+ file.getName());
}
} catch (Exception e) {
e.getMessage();
}
return bytes;
}
}
private static DocumentInfo constructDocInfo(File file) {
DocumentInfo documentInfo = DmsUtils.createDocumentInfo(file.getName());
documentInfo.setContentType("text/plain");
documentInfo.setDescription("This is a txt document.");
return documentInfo;
}
private static ServiceFactory getServiceFactory(String partition,String domain, String realm, String user, String password) {
Map properties = new HashMap();
properties.put(SecurityProperties.PARTITION, partition);
properties.put(SecurityProperties.DOMAIN, domain);
properties.put(SecurityProperties.REALM, realm);
return ServiceFactoryLocator.get(user, password, properties);
}
The following API is used in this example:
The output of the above program could look similar like below:
Id of CREATED Document is : {jcrUuid}e95892b1-263f-440f-be1f-e7622f0617a1
Description from CREATED Document : This is a txt document.
Content of CREATED Document : These are the contents of 'MySampleFile'
======================================================
Calling updateDocument() with updated Contents and Description for Document 'D:/UpdatedContentFile.txt'
Id of UPDATED Document is : {jcrUuid}e95892b1-263f-440f-be1f-e7622f0617a1
Contents of UPDATED/Fetched Document : These are the contents of 'UpdatedContentFile'.
Description from Fetched Document : UPDATED Description
#######################################################
Calling updateDocument() with updated Contents and Description for Document 'D:/UpdatedContentFile2.txt'
Id of UPDATED Document is : {jcrUuid}e95892b1-263f-440f-be1f-e7622f0617a1
Contents of UPDATED/Fetched Document : The document contents are Again UPDATED.
Description from Fetched Document : Again Updated Description
Number of Document Versions :: 3
------------Below are the Document Versions' Details--------------
Revision Name :: 1.0
Description :: This is a txt document.
Document Contents :: These are the contents of 'MySampleFile'
------------Below are the Document Versions' Details--------------
Revision Name :: 1.1
Description :: UPDATED Description
Document Contents :: These are the contents of 'UpdatedContentFile'.
------------Below are the Document Versions' Details--------------
Revision Name :: 1.2
Description :: Again Updated Description
Document Contents :: The document contents are Again UPDATED.