Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Stardust/Knowledge Base/Java API/Document Management"

m (Added the sample Output for a code)
m
 
(17 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
__NOTOC__
 
== '''Updating a document'''  ==
 
== '''Updating a document'''  ==
  
==== <u>'''Scenario:'''</u>&nbsp; The document was created using DocumentManagementService’s '''createDocument() '''method and then how to update a document if we pass the updated byte[] of the same document. ====
+
<u>'''Scenario:'''</u>&nbsp; The document was created using DocumentManagementService’s '''createDocument() '''method and then how to update a document if we pass the updated byte[] of the same document.  
  
The sample code would look like:<br>
+
The sample code would look like:<br>  
<pre>public class DMSClient {
+
 
 +
<source lang="Java">
 +
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.Map;
 +
import ag.carnot.workflow.runtime.DmsUtils;
 +
import ag.carnot.workflow.runtime.Document;
 +
import ag.carnot.workflow.runtime.DocumentInfo;
 +
import ag.carnot.workflow.runtime.DocumentManagementService;
 +
import ag.carnot.workflow.runtime.ServiceFactory;
 +
import ag.carnot.workflow.runtime.ServiceFactoryLocator;
 +
public class DMSClient {
 
public static void main(String[] args) {
 
public static void main(String[] args) {
  
Line 38: Line 54:
 
}
 
}
 
return null;
 
return null;
 
 
}
 
}
  
Line 55: Line 70:
  
 
// Return ServiceFactory
 
// Return ServiceFactory
private static ServiceFactory getServiceFactory(String partition,String domain, String realm,  
+
private static ServiceFactory getServiceFactory(String partition,String domain, String realm, String user, String password) {
          String user, String password) {
+
 
Map properties = new HashMap();
 
Map properties = new HashMap();
properties.put("partition", partition);
+
properties.put(SecurityProperties.PARTITION, partition);
properties.put("domain", domain);
+
properties.put(SecurityProperties.DOMAIN, domain);
properties.put("realm", realm);
+
properties.put(SecurityProperties.REALM, realm);
 
return ServiceFactoryLocator.get(user, password, properties);
 
return ServiceFactoryLocator.get(user, password, properties);
 
}
 
}
 
  
 
private static byte[] readFileContent() {
 
private static byte[] readFileContent() {
Line 90: Line 103:
 
return bytes;
 
return bytes;
 
}
 
}
 
 
}
 
}
 
+
</source>  
 
+
 
+
</pre>
+
 
<u>The output of the above program is like:</u>  
 
<u>The output of the above program is like:</u>  
 
+
<p>Id of CREATED Document is&nbsp;: {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>Description from CREATED Document&nbsp;: This is a txt document.<br>Content of CREATED Document&nbsp;: These are the contents of Sample File1.<br>======================================================<br>Id of UPDATED Document is&nbsp;: {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>======================================================<br>Id of Fetched Document is&nbsp;: {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>Description from Fetched Document&nbsp;: This is the UPDATED Description of the document<br>Contents of UPDATED/Fetched Document&nbsp;: These are the contents of Sample File1.</p>
<br>Id of CREATED Document is&nbsp;: {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>Description from CREATED Document&nbsp;: This is a txt document.<br>Content of CREATED Document&nbsp;: These are the contents of Sample File1.<br>======================================================<br>Id of UPDATED Document is&nbsp;: {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>======================================================<br>Id of Fetched Document is&nbsp;: {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>Description from Fetched Document&nbsp;: This is the UPDATED Description of the document<br>Contents of UPDATED/Fetched Document&nbsp;: These are the contents of Sample File1.<br>
+
 
+
&nbsp;
+
 
+
&nbsp;
+
  
 
== '''Retrieve Contents of a Document Revision'''  ==
 
== '''Retrieve Contents of a Document Revision'''  ==
  
==== '''Scenario:'''&nbsp;There are 'n' number of versions of a document in the repository. Now how&nbsp;to retrieve the contents of a particular revision of&nbsp;that document. ====
+
'''Scenario:'''&nbsp;There are 'n' number of versions of a document in the repository. Now how&nbsp;to retrieve the contents of a particular revision of&nbsp;that document.  
  
<u>'''&nbsp;Solution:'''</u>'''&nbsp; '''The contents of a particular revision can be retrieved by passing the RevisionId in the '''retrieveDocumentContent()''' method. A sample code look like below:
+
<u>'''Solution:'''</u>'''&nbsp; '''The contents of a particular revision can be retrieved by passing the RevisionId in the '''retrieveDocumentContent()''' method. A sample code look like below:  
<pre>package com.sungard.bootcamp.client;
+
<source lang="Java">
 +
package com.sungard.bootcamp.client;
 
import java.io.File;
 
import java.io.File;
 
import java.io.IOException;
 
import java.io.IOException;
Line 122: Line 127:
 
import ag.carnot.workflow.runtime.ServiceFactoryLocator;
 
import ag.carnot.workflow.runtime.ServiceFactoryLocator;
 
import ag.carnot.workflow.runtime.beans.removethis.SecurityProperties;</pre><pre>public class DMSClient {
 
import ag.carnot.workflow.runtime.beans.removethis.SecurityProperties;</pre><pre>public class DMSClient {
 +
 
public static void main(String[] args) {
 
public static void main(String[] args) {
 
// Obtain a reference to the ServiceFactory
 
// Obtain a reference to the ServiceFactory
Line 142: Line 148:
 
String domain, String realm, String user, String password) {
 
String domain, String realm, String user, String password) {
 
Map properties = new HashMap();
 
Map properties = new HashMap();
properties.put(SecurityProperties.CRED_PARTITION, partition);
+
properties.put(SecurityProperties.PARTITION, partition);
properties.put(SecurityProperties.CRED_DOMAIN, domain);
+
properties.put(SecurityProperties.DOMAIN, domain);
properties.put(SecurityProperties.CRED_REALM, realm);
+
properties.put(SecurityProperties.REALM, realm);
 
return ServiceFactoryLocator.get(user, password, properties);
 
return ServiceFactoryLocator.get(user, password, properties);
 
}
 
}
 
}
 
}
  
</pre>
+
The output of the program is something like below (assuming that there are 2 version of that document):
The output of the program is something like below:
+
</source>
 +
<source lang="Java">
 +
DocumentID&nbsp;:: {jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3
 +
Document RevisionId&nbsp;:: {jcrRev}eb025fef-5609-4f42-911c-abd0ba1125bb
 +
Document RevisionName&nbsp;:: 1.0
 +
Document Contents&nbsp;:: This is a test document..
 +
</pre><pre>DocumentID&nbsp;:: {jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3
 +
Document RevisionId&nbsp;:: {jcrRev}1cc5d11a-9dbd-4733-b451-c0732458c36f
 +
Document RevisionName&nbsp;:: 1.1
 +
Document Contents&nbsp;:: This is UPDATED document..
 +
</source>
 +
 
 +
 
 +
== '''Create a new version'''  ==
 +
 
 +
 
 +
==== Scenario: Sample code to update a document (to create a new version) and then retrieving all the versions of the document  ====
 +
<source lang="Java">
 +
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 ag.carnot.workflow.runtime.DmsUtils;
 +
import ag.carnot.workflow.runtime.Document;
 +
import ag.carnot.workflow.runtime.DocumentInfo;
 +
import ag.carnot.workflow.runtime.DocumentManagementService;
 +
import ag.carnot.workflow.runtime.ServiceFactory;
 +
import ag.carnot.workflow.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&nbsp;: " + (doc).getId());
 +
System.out.println("Description from CREATED Document&nbsp;: "+ doc.getDescription());
 +
System.out.println("Content of CREATED Document&nbsp;: " + new String (dms.retrieveDocumentContent(doc.getId())));
 +
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&nbsp;: "+ (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&nbsp;: " + new String (
 +
dms.retrieveDocumentContent(fetchedDocument.getRevisionId())));
 +
System.out.println("Description from Fetched Document&nbsp;: "+ 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&nbsp;: "+ (updatedDoc2).getId());
 +
 
 +
Document fetchedDocument2 = dms.getDocument(updatedDoc2.getId());
 +
System.out.println("Contents of UPDATED/Fetched Document&nbsp;: " + new String (
 +
dms.retrieveDocumentContent(fetchedDocument2.getRevisionId())));
 +
System.out.println("Description from Fetched Document&nbsp;: "+ fetchedDocument2.getDescription());
 +
 
 +
List &lt;Document&gt; docVersions= dms.getDocumentVersions(fetchedDocument.getId());
 +
if(docVersions!= null){
 +
int size = docVersions.size();
 +
System.out.println("Number of Document Versions&nbsp;:: "+size);
 +
 
 +
for(int i=0;i&lt;size;i++ ){
 +
System.out.println("------------Below are the Document Versions' Details--------------");
 +
Document document = (Document)docVersions.get(i);
 +
System.out.println("Revision Name&nbsp;:: "+document.getRevisionName());
 +
System.out.println("Description&nbsp;:: "+document.getDescription());
 +
System.out.println("Document Contents&nbsp;:: " +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) {
 +
// same code as in above example
 +
}
 +
 
 +
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) {
 +
// same code as in above example
 +
}
 +
}
 +
 
 +
**Note - We need to call dms.versionDocument(doc.getId(), "0") and set the versionLabel as “0”.
 +
 
 +
</source>
 +
<br>The output of the program looks like below:<br>Id of CREATED Document is&nbsp;: {jcrUuid}e95892b1-263f-440f-be1f-e7622f0617a1<br>Description from CREATED Document&nbsp;: This is a txt document.<br>Content of CREATED Document&nbsp;: These are the contents of 'MySampleFile'
 +
 
 +
======================================================<br>Calling updateDocument() with updated Contents and Description for Document 'D:/UpdatedContentFile.txt'<br>Id of UPDATED Document is&nbsp;: {jcrUuid}e95892b1-263f-440f-be1f-e7622f0617a1<br>Contents of UPDATED/Fetched Document&nbsp;: These are the contents of 'UpdatedContentFile'.
 +
 
 +
Description from Fetched Document&nbsp;: UPDATED Description<br>#######################################################<br>Calling updateDocument() with updated Contents and Description for Document 'D:/UpdatedContentFile2.txt'<br>Id of UPDATED Document is&nbsp;: {jcrUuid}e95892b1-263f-440f-be1f-e7622f0617a1<br>Contents of UPDATED/Fetched Document&nbsp;: The document contents are Again UPDATED.
 +
 
 +
<br>Description from Fetched Document&nbsp;: Again Updated Description<br>Number of Document Versions&nbsp;:: 3<br>------------Below are the Document Versions' Details--------------<br>Revision Name&nbsp;:: 1.0<br>Description&nbsp;:: This is a txt document.<br>Document Contents&nbsp;:: These are the contents of 'MySampleFile'
 +
 
 +
----
 +
 
 +
Below are the Document Versions' Details--------------<br>Revision Name&nbsp;:: 1.1<br>Description&nbsp;:: UPDATED Description<br>Document Contents&nbsp;:: These are the contents of 'UpdatedContentFile'.
  
DocumentID :: {jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3<br>Document RevisionId :: {jcrRev}eb025fef-5609-4f42-911c-abd0ba1125bb<br>Document RevisionName :: 1.0<br>Document Contents :: This is a test document..
+
----
  
DocumentID :: {jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3<br>Document RevisionId :: {jcrRev}1cc5d11a-9dbd-4733-b451-c0732458c36f<br>Document RevisionName :: 1.1<br>Document Contents :: This is UPDATED document.. <br>
+
Below are the Document Versions' Details--------------<br>Revision Name&nbsp;:: 1.2<br>Description&nbsp;:: Again Updated Description<br>Document Contents&nbsp;:: The document contents are Again UPDATED.<br>

Latest revision as of 00:27, 3 December 2013

Updating a document

Scenario:  The document was created using DocumentManagementService’s createDocument() method and then how to update a document if we pass the updated byte[] of the same document.

The sample code would look like:

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.Map;
import ag.carnot.workflow.runtime.DmsUtils;
import ag.carnot.workflow.runtime.Document;
import ag.carnot.workflow.runtime.DocumentInfo;
import ag.carnot.workflow.runtime.DocumentManagementService;
import ag.carnot.workflow.runtime.ServiceFactory;
import ag.carnot.workflow.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:/SampleFile1.txt"));
 
System.out.println("Id of CREATED Document is&nbsp;: " + (doc).getId());
System.out.println("Description from CREATED Document&nbsp;: " + doc.getDescription()); 
System.out.println("Content of CREATED Document&nbsp;: " + new String (dms.retrieveDocumentContent(doc.getId())));
System.out.println("======================================================");
 
Document updatedDoc = updateDoc(dms, doc);
System.out.println("Id of UPDATED Document is&nbsp;: "+ (updatedDoc).getId()); 
System.out.println("======================================================");
 
Document fetchedDocument = dms.getDocument(updatedDoc.getId());
System.out.println("Id of Fetched Document is&nbsp;: " + fetchedDocument.getId());
System.out.println("Description from Fetched Document&nbsp;: "+ fetchedDocument.getDescription()); 
System.out.println("Contents of UPDATED/Fetched Document&nbsp;: " + 
                                         new String (dms.retrieveDocumentContent(fetchedDocument.getId())));
}
 
private static Document createDoc(DocumentManagementService dms, File file) {
DocumentInfo documentInfo = constructDocInfo(file);
try {
byte[] documentContent = FileUploadUtils.getBytesFromFile(file); 
return (dms.createDocument("/", documentInfo, documentContent, null));
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
return null;
}
 
private static Document updateDoc(DocumentManagementService dms, Document doc) {
doc.setDescription("This is the UPDATED Description of the document");
return (dms.updateDocument(doc, readFileContent(), "UTF-8", true,"TOBEREVISED", false));
}
 
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;
}
 
 
// Return ServiceFactory
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);
}
 
private static byte[] readFileContent() {
byte[] bytes = null;
try {
File file = new File("D:/SampleFile1.txt");
// File length
int size = (int) file.length();
if (size &gt; Integer.MAX_VALUE) {
System.out.println("File is to larger");
}
bytes = new byte[size];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
int read = 0;
int numRead = 0;
while (read &lt; bytes.length&amp;&amp; (numRead = dis.read(bytes, read, bytes.length - read)) &gt;= 0) {
read = read + numRead;
} 
// Ensure all the bytes have been read in
if (read &lt; bytes.length) {
System.out.println("Could not completely read: "+ file.getName());
}
} catch (Exception e) {
   e.getMessage();
}
return bytes;
}
}

The output of the above program is like:

Id of CREATED Document is : {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a
Description from CREATED Document : This is a txt document.
Content of CREATED Document : These are the contents of Sample File1.
======================================================
Id of UPDATED Document is : {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a
======================================================
Id of Fetched Document is : {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a
Description from Fetched Document : This is the UPDATED Description of the document
Contents of UPDATED/Fetched Document : These are the contents of Sample File1.

Retrieve Contents of a Document Revision

Scenario: There are 'n' number of versions of a document in the repository. Now how to retrieve the contents of a particular revision of that document.

Solution:  The contents of a particular revision can be retrieved by passing the RevisionId in the retrieveDocumentContent() method. A sample code look like below:

package com.sungard.bootcamp.client;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ag.carnot.workflow.runtime.DmsUtils;
import ag.carnot.workflow.runtime.Document;
import ag.carnot.workflow.runtime.DocumentInfo;
import ag.carnot.workflow.runtime.DocumentManagementService;
import ag.carnot.workflow.runtime.ServiceFactory;
import ag.carnot.workflow.runtime.ServiceFactoryLocator;
import ag.carnot.workflow.runtime.beans.removethis.SecurityProperties;</pre><pre>public class DMSClient {
 
public static void main(String[] args) {
// Obtain a reference to the ServiceFactory
ServiceFactory sf = getServiceFactory(null, null, null, "motu", "motu");
DocumentManagementService dmsService = sf.getDocumentManagementService(); 
// pass the documentId to get the document versions of that document 
List&lt;Document&gt; list = dmsService.getDocumentVersions("{jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3"); 
for(int i=0;i&lt;list.size();i++){
Document document = (Document)list.get(i);
System.out.println("DocumentID&nbsp;:: " + document.getId()); 
System.out.println("Document RevisionId&nbsp;:: " +document.getRevisionId());
System.out.println("Document RevisionName&nbsp;:: "+ document.getRevisionName());
System.out.println("Document Contents&nbsp;:: " + new String(
dmsService.retrieveDocumentContent(document.getRevisionId())));
} 
}
 
// Return ServiceFactory
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 output of the program is something like below (assuming that there are 2 version of that document):
DocumentID&nbsp;:: {jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3
Document RevisionId&nbsp;:: {jcrRev}eb025fef-5609-4f42-911c-abd0ba1125bb
Document RevisionName&nbsp;:: 1.0
Document Contents&nbsp;:: This is a test document.. 
</pre><pre>DocumentID&nbsp;:: {jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3
Document RevisionId&nbsp;:: {jcrRev}1cc5d11a-9dbd-4733-b451-c0732458c36f
Document RevisionName&nbsp;:: 1.1
Document Contents&nbsp;:: This is UPDATED document..


Create a new version

Scenario: Sample code to update a document (to create a new version) and then 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 ag.carnot.workflow.runtime.DmsUtils;
import ag.carnot.workflow.runtime.Document;
import ag.carnot.workflow.runtime.DocumentInfo;
import ag.carnot.workflow.runtime.DocumentManagementService;
import ag.carnot.workflow.runtime.ServiceFactory;
import ag.carnot.workflow.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&nbsp;: " + (doc).getId());
System.out.println("Description from CREATED Document&nbsp;: "+ doc.getDescription()); 
System.out.println("Content of CREATED Document&nbsp;: " + new String (dms.retrieveDocumentContent(doc.getId()))); 
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&nbsp;: "+ (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&nbsp;: " + new String (
dms.retrieveDocumentContent(fetchedDocument.getRevisionId())));
System.out.println("Description from Fetched Document&nbsp;: "+ 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&nbsp;: "+ (updatedDoc2).getId());
 
Document fetchedDocument2 = dms.getDocument(updatedDoc2.getId()); 
System.out.println("Contents of UPDATED/Fetched Document&nbsp;: " + new String (
dms.retrieveDocumentContent(fetchedDocument2.getRevisionId())));
System.out.println("Description from Fetched Document&nbsp;: "+ fetchedDocument2.getDescription());
 
List &lt;Document&gt; docVersions= dms.getDocumentVersions(fetchedDocument.getId());
if(docVersions!= null){
int size = docVersions.size();
System.out.println("Number of Document Versions&nbsp;:: "+size);
 
for(int i=0;i&lt;size;i++ ){
System.out.println("------------Below are the Document Versions' Details--------------");
Document document = (Document)docVersions.get(i); 
System.out.println("Revision Name&nbsp;:: "+document.getRevisionName());
System.out.println("Description&nbsp;:: "+document.getDescription()); 
System.out.println("Document Contents&nbsp;:: " +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) {
// same code as in above example
}
 
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) {
// same code as in above example
}
}
 
**Note - We need to call dms.versionDocument(doc.getId(), "0") and set the versionLabel as “0”.


The output of the program looks 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.

Back to the top