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.
Difference between revisions of "Stardust/Knowledge Base/Java API/Document Management"
m |
m (Added the sample Output for a code) |
||
Line 106: | Line 106: | ||
== '''Retrieve Contents of a Document Revision''' == | == '''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. ==== | + | ==== '''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. ==== |
− | | + | <u>''' Solution:'''</u>''' '''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; | <pre>package com.sungard.bootcamp.client; | ||
import java.io.File; | import java.io.File; | ||
Line 130: | Line 130: | ||
for(int i=0;i<list.size();i++){ | for(int i=0;i<list.size();i++){ | ||
Document document = (Document)list.get(i); | Document document = (Document)list.get(i); | ||
− | System.out.println("DocumentID :: " + document.getId()); | + | System.out.println("DocumentID :: " + document.getId()); |
− | System.out.println("Document RevisionId :: " +document.getRevisionId()); | + | System.out.println("Document RevisionId :: " +document.getRevisionId()); |
− | System.out.println("Document RevisionName :: "+ document.getRevisionName()); | + | System.out.println("Document RevisionName :: "+ document.getRevisionName()); |
− | System.out.println("Document Contents :: " + new String( | + | System.out.println("Document Contents :: " + new String( |
dmsService.retrieveDocumentContent(document.getRevisionId()))); | dmsService.retrieveDocumentContent(document.getRevisionId()))); | ||
} | } | ||
Line 148: | Line 148: | ||
} | } | ||
} | } | ||
+ | |||
</pre> | </pre> | ||
+ | The output of the program is something like below: | ||
+ | |||
+ | 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> |
Revision as of 04:36, 30 November 2011
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:
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 : " + (doc).getId()); System.out.println("Description from CREATED Document : " + doc.getDescription()); System.out.println("Content of CREATED Document : " + new String (dms.retrieveDocumentContent(doc.getId()))); System.out.println("======================================================"); Document updatedDoc = updateDoc(dms, doc); System.out.println("Id of UPDATED Document is : "+ (updatedDoc).getId()); System.out.println("======================================================"); Document fetchedDocument = dms.getDocument(updatedDoc.getId()); System.out.println("Id of Fetched Document is : " + fetchedDocument.getId()); System.out.println("Description from Fetched Document : "+ fetchedDocument.getDescription()); System.out.println("Contents of UPDATED/Fetched Document : " + 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("partition", partition); properties.put("domain", domain); properties.put("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 > 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 < 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; } }
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;
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<Document> list = dmsService.getDocumentVersions("{jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3"); for(int i=0;i<list.size();i++){ Document document = (Document)list.get(i); System.out.println("DocumentID :: " + document.getId()); System.out.println("Document RevisionId :: " +document.getRevisionId()); System.out.println("Document RevisionName :: "+ document.getRevisionName()); System.out.println("Document Contents :: " + 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.CRED_PARTITION, partition); properties.put(SecurityProperties.CRED_DOMAIN, domain); properties.put(SecurityProperties.CRED_REALM, realm); return ServiceFactoryLocator.get(user, password, properties); } }
The output of the program is something like below:
DocumentID :: {jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3
Document RevisionId :: {jcrRev}eb025fef-5609-4f42-911c-abd0ba1125bb
Document RevisionName :: 1.0
Document Contents :: This is a test document..
DocumentID :: {jcrUuid}05044280-8411-4fd8-8927-99b23560e4c3
Document RevisionId :: {jcrRev}1cc5d11a-9dbd-4733-b451-c0732458c36f
Document RevisionName :: 1.1
Document Contents :: This is UPDATED document..