Wednesday, 3 April 2019

openCMIS: Versioning


Now a days almost all the repositories started supporting versioning. By using versioning, we can synchronize the data updations and maintain the history about a document.

Versioning flow looks like below.
a.   User ‘A’ first checkout a document and get the PWC (Private Working copy)
b.   Id the document is checked out by user ‘A’, no other user can able to check out and edit the document. User ‘A’ updates the private working copy.
c.    After successful updates, user ‘A’ check in the document. Now the latest version of the document is available to all other users.

What is Version Label?
If repository supports versioning, each version in the version history is identified by a number, it is called version label. For example, version labels are like 1.2, 1.6, 1.8, 2.0, 2.5, 3.0 etc.,

Version numbers like 1.2, 1.6, 1.8 and 2.0 are called minor versions, where as version numbers like 2.0,3.0 are called major versions. While checking in a document to the repository, you can tell to cmis, whether you want to check in the document as minor version (or) major version.

Following step-bystep procedure explain simple checkout-checkin flow.

a.   Create a document ‘sample.txt’ directly under root folder
b.   Checkout the document ‘sample.txt’ and get the reference to PWC
c.    Update the private working copy
d.   Checkin the document

Since in memory server is not supporting versioning, I am going to use Alfresco server. You can go through my previous post to know how to connect to alfresco server using CMIS workbench.

Create a document ‘sample.txt’
         private static Document createDocument() throws IOException {
                  File inputFile = new File(inputFile1);

                  String mimeType = Files.probeContentType(inputFile.toPath());

                  Session session = getSession();
                  try (InputStream inputStream = new FileInputStream(inputFile)) {
                           ContentStream contentStream = session.getObjectFactory().createContentStream(inputFile.getName(),
                                             inputFile.length(), mimeType, inputStream);

                           Map<String, String> properties = new HashMap<>();
                           properties.put("cmis:objectTypeId", "cmis:document");
                           properties.put("cmis:name", documentName);

                           Folder rootFolder = session.getRootFolder();
                           Document document = rootFolder.createDocument(properties, contentStream, null);

                           return document;

                  }
         }

Checkout the Document
                  boolean isVersionable = document.isVersionable();

                  if (!isVersionable) {
                           System.out.println("Document is not versionalbe");
                           return;
                  }

                  ObjectId objectId = document.checkOut();

CheckIn the document
         private static Document updateAndCheckInDocument(ObjectId objectId) throws IOException {
                  Session session = getSession();
                  Document document = (Document) session.getObject(objectId);

                  File inputFile = new File(inputFile2);
                  String mimeType = Files.probeContentType(inputFile.toPath());

                  try (InputStream inputStream = new FileInputStream(inputFile)) {
                           ContentStream contentStream = session.getObjectFactory().createContentStream(inputFile.getName(),
                                             inputFile.length(), mimeType, inputStream);

                           document.setContentStream(contentStream, true, true);
                           ObjectId objId = document.checkIn(true, null, contentStream, "Checking in document");
                           return (Document) session.getObject(objId);
                  }

         }

Find the following complete working application.

TestCmis.java
package com.sample.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.ContentStream;
import org.apache.chemistry.opencmis.commons.enums.BindingType;

public class TestCmis {

 private static String serverURL = "http://cmis.alfresco.com/cmisatom";
 private static String repositoryId = "9232b3be-da4f-420a-a89b-d25e71d257f5";

 private static String inputFile1 = "C:\\Users\\Krishna\\Documents\\data1.txt";
 private static String inputFile2 = "C:\\Users\\Krishna\\Documents\\data2.txt";
 private static String documentName = "sample1.txt";
 private static Session session = null;

 public static Session getSession() {
  if (session != null) {
   return session;
  }
  Map<String, String> parameters = new HashMap<>();
  parameters.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());

  parameters.put(SessionParameter.USER, "admin");
  parameters.put(SessionParameter.PASSWORD, "admin");

  parameters.put(SessionParameter.REPOSITORY_ID, repositoryId);
  parameters.put(SessionParameter.ATOMPUB_URL, serverURL);

  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
  session = sessionFactory.createSession(parameters);
  return session;
 }

 private static Document createDocument() throws IOException {
  File inputFile = new File(inputFile1);

  String mimeType = Files.probeContentType(inputFile.toPath());

  Session session = getSession();
  try (InputStream inputStream = new FileInputStream(inputFile)) {
   ContentStream contentStream = session.getObjectFactory().createContentStream(inputFile.getName(),
     inputFile.length(), mimeType, inputStream);

   Map<String, String> properties = new HashMap<>();
   properties.put("cmis:objectTypeId", "cmis:document");
   properties.put("cmis:name", documentName);

   Folder rootFolder = session.getRootFolder();
   Document document = rootFolder.createDocument(properties, contentStream, null);

   return document;

  }
 }

 private static void printDocumentDetails(Document document) {
  System.out.println("name : " + document.getName());
  System.out.println("id : " + document.getId());
  System.out.println("version series id : " + document.getVersionSeriesId());
  System.out.println("Version Label : " + document.getVersionLabel());
  System.out.println("Checked out by : " + document.getVersionSeriesCheckedOutBy());
  System.out.println("Content length : " + document.getContentStreamLength());
 }

 private static Document updateAndCheckInDocument(ObjectId objectId) throws IOException {
  Session session = getSession();
  Document document = (Document) session.getObject(objectId);

  File inputFile = new File(inputFile2);
  String mimeType = Files.probeContentType(inputFile.toPath());

  try (InputStream inputStream = new FileInputStream(inputFile)) {
   ContentStream contentStream = session.getObjectFactory().createContentStream(inputFile.getName(),
     inputFile.length(), mimeType, inputStream);

   document.setContentStream(contentStream, true, true);
   ObjectId objId = document.checkIn(true, null, contentStream, "Checking in document");
   return (Document) session.getObject(objId);
  }

 }

 public static void main(String args[]) throws IOException {

  Document document = createDocument();

  System.out.println("*****************************************");
  System.out.println("Before checking in the document");
  System.out.println("*****************************************");

  printDocumentDetails(document);

  boolean isVersionable = document.isVersionable();

  if (!isVersionable) {
   System.out.println("Document is not versionalbe");
   return;
  }

  ObjectId objectId = document.checkOut();
  Document checkdInDocument = updateAndCheckInDocument(objectId);

  System.out.println("*****************************************");
  System.out.println("After checking in the document");
  System.out.println("*****************************************");
  printDocumentDetails(checkdInDocument);

 }
}


Sample Output
*****************************************
Before checking in the document
*****************************************
name : sample1.txt
id : workspace://SpacesStore/0d743e0b-fda3-457a-895d-d2848847d741;1.0
version series id : workspace://SpacesStore/0d743e0b-fda3-457a-895d-d2848847d741
Version Label : 1.0
Checked out by : null
Content length : 11
*****************************************
After checking in the document
*****************************************
name : sample1.txt
id : workspace://SpacesStore/0d743e0b-fda3-457a-895d-d2848847d741;2.0
version series id : workspace://SpacesStore/0d743e0b-fda3-457a-895d-d2848847d741
Version Label : 2.0
Checked out by : null
Content length : 0


As you observe the output, object id of the document is changed after cheked in, where as version series id is unique across all the checkins.


Previous                                                 Next                                                 Home

No comments:

Post a Comment