Notice: This Wiki is now read only and edits are no longer 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 (Updated the alignment) |
|||
Line 28: | Line 28: | ||
} | } | ||
− | private static Document createDoc(DocumentManagementService dms, File file) { | + | private static Document '''createDoc'''(DocumentManagementService dms, File file) { |
DocumentInfo documentInfo = constructDocInfo(file); | DocumentInfo documentInfo = constructDocInfo(file); | ||
try { | try { | ||
Line 34: | Line 34: | ||
return (dms.createDocument("/", documentInfo, documentContent, null)); | return (dms.createDocument("/", documentInfo, documentContent, null)); | ||
} catch (IOException e) { | } catch (IOException e) { | ||
− | // TODO Auto-generated catch block | + | // TODO Auto-generated catch block |
− | e.printStackTrace(); | + | e.printStackTrace(); |
} | } | ||
return null; | return null; | ||
Line 41: | Line 41: | ||
} | } | ||
− | private static Document updateDoc(DocumentManagementService dms, Document doc) { | + | private static Document '''updateDoc'''(DocumentManagementService dms, Document doc) { |
doc.setDescription("This is the UPDATED Description of the document"); | doc.setDescription("This is the UPDATED Description of the document"); | ||
return (dms.updateDocument(doc, readFileContent(), "UTF-8", true,"TOBEREVISED", false)); | return (dms.updateDocument(doc, readFileContent(), "UTF-8", true,"TOBEREVISED", false)); | ||
} | } | ||
− | private static byte[] readFileContent() { | + | 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; | byte[] bytes = null; | ||
try { | try { | ||
Line 53: | Line 72: | ||
int size = (int) file.length(); | int size = (int) file.length(); | ||
if (size > Integer.MAX_VALUE) { | if (size > Integer.MAX_VALUE) { | ||
− | |||
System.out.println("File is to larger"); | System.out.println("File is to larger"); | ||
− | |||
} | } | ||
bytes = new byte[size]; | bytes = new byte[size]; | ||
Line 69: | Line 86: | ||
} | } | ||
} catch (Exception e) { | } catch (Exception e) { | ||
− | e.getMessage(); | + | e.getMessage(); |
} | } | ||
return bytes; | return bytes; | ||
} | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
Line 95: | Line 96: | ||
</pre> | </pre> | ||
− | <u>The output of the above program is like:</u> | + | <u>The output of the above program is like:</u> |
<br>Id of CREATED Document is : {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>Description from CREATED Document : This is a txt document.<br>Content of CREATED Document : These are the contents of Sample File1.<br>======================================================<br>Id of UPDATED Document is : {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>======================================================<br>Id of Fetched Document is : {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>Description from Fetched Document : This is the UPDATED Description of the document<br>Contents of UPDATED/Fetched Document : These are the contents of Sample File1.<br> | <br>Id of CREATED Document is : {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>Description from CREATED Document : This is a txt document.<br>Content of CREATED Document : These are the contents of Sample File1.<br>======================================================<br>Id of UPDATED Document is : {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>======================================================<br>Id of Fetched Document is : {jcrUuid}cbe663c6-c6fb-432b-a260-b6c4d18e597a<br>Description from Fetched Document : This is the UPDATED Description of the document<br>Contents of UPDATED/Fetched Document : These are the contents of Sample File1.<br> |
Revision as of 04:18, 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.