0

Can someone post the OpenCMIS code necessary to create a new document and then update that document by updating its content stream? I don't want to lose the original document--I want to maintain the version history as new documents are updated. I am using Alfresco but this should be applicable to any CMIS repository.

Jeff Potts
  • 10,468
  • 17
  • 40
user2106213
  • 73
  • 1
  • 4
  • what do you mean with "maintain the version"? Like, you want to persist or control the version number? – skuro Feb 25 '13 at 12:16
  • when I update the Document with document.setContentStream(..), it should change the version by it self and change the content of document and keep both the document in version History. I want like this – user2106213 Feb 25 '13 at 12:19
  • could you please post code for create document?? with version control? – user2106213 Feb 25 '13 at 12:31

1 Answers1

7

In Alfresco, to create a new version, just get the Private Working Copy that is returned to you after a checkout, update the PWC's content stream, then check it back in. Alfresco will manage the versions for you. Here is an example.

Folder folder = (Folder) getSession().getObjectByPath("/cmis-demo");

String timeStamp = new Long(System.currentTimeMillis()).toString();
String filename = "cmis-demo-doc (" + timeStamp + ")";

// Create a doc
Map <String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:document");
properties.put(PropertyIds.NAME, filename);
String docText = "This is a sample document";
byte[] content = docText.getBytes();
InputStream stream = new ByteArrayInputStream(content);
ContentStream contentStream = getSession().getObjectFactory().createContentStream(filename, Long.valueOf(content.length), "text/plain", stream);

Document doc = folder.createDocument(
           properties,
           contentStream,
           VersioningState.MAJOR);

System.out.println("Created: " + doc.getId());
System.out.println("Content Length: " + doc.getContentStreamLength());
System.out.println("Version label:" + doc.getVersionLabel());

// Now update it with a new version
if (doc.getAllowableActions().getAllowableActions().contains(org.apache.chemistry.opencmis.commons.enums.Action.CAN_CHECK_OUT)) {
   doc.refresh();
   String testName = doc.getContentStream().getFileName();
   ObjectId idOfCheckedOutDocument = doc.checkOut();
   Document pwc = (Document) session.getObject(idOfCheckedOutDocument);
   docText = "This is a sample document with an UPDATE";
   content = docText.getBytes();
   stream = new ByteArrayInputStream(content);         
   contentStream = getSession().getObjectFactory().createContentStream(filename, Long.valueOf(content.length), "text/plain", stream);          
   ObjectId objectId = pwc.checkIn(false, null, contentStream, "just a minor change");
   doc = (Document) session.getObject(objectId);
   System.out.println("Version label is now:" + doc.getVersionLabel());
}

When run, it outputs this:

Created: workspace://SpacesStore/d6f3fca2-bf9c-4a0e-8141-088d07d45359;1.0
Content Length: 25
Version label:1.0
Version label is now:1.1
Done
Jeff Potts
  • 10,468
  • 17
  • 40
  • Thank You very much... I want to know one more thing like could we change the label as 2.0 instead of 1.1?? – user2106213 Feb 26 '13 at 07:04
  • Yes, you'd just change the major flag from false to true, so you'd do this: ObjectId objectId = pwc.checkIn(true, null, contentStream, "this one is a major change"); – Jeff Potts Feb 27 '13 at 01:22