Tuesday 2 April 2019

openCMIS: Working with Documents


CMIS Documents are just like other documents in the file system, they can be like .txt files. Ppt files (or) some video files etc., Documents stored inside a folder. Every document has some properties associated with. Following application prints all the cmis properties of the first document, while traversing the root folder hierarchy.

TestCmis.java
package com.sample.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.Property;
import org.apache.chemistry.opencmis.client.api.Repository;
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.enums.BindingType;

public class TestCmis {

 public static List<Repository> getRepositories(String serverURL) {
  Map<String, String> parameters = new HashMap<>();
  parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());

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

  parameters.put(SessionParameter.BROWSER_URL, serverURL);

  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
  List<Repository> repositories = sessionFactory.getRepositories(parameters);
  return repositories;
 }

 public static void printFirstDocumentProperties(Folder folder) {

  ItemIterable<CmisObject> cmisObjects = folder.getChildren();

  for (CmisObject cmisObject : cmisObjects) {
   if (!(cmisObject instanceof Document)) {
    continue;
   }

   Document document = (Document) cmisObject;
   List<Property<?>> properties = document.getProperties();

   for (Property property : properties) {
    if (property.getId().startsWith("cmis:"))
     System.out.println(property.getId() + " " + property.getValueAsString());
   }
   return;
  }

 }

 public static void main(String args[]) {
  String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
  List<Repository> repositories = getRepositories(serverURL);

  for (Repository repository : repositories) {
   Session session = repository.createSession();
   Folder rootFolder = session.getRootFolder();
   printFirstDocumentProperties(rootFolder);

  }
 }
}


Sample Output
cmis:isImmutable false
cmis:objectTypeId ComplexType
cmis:versionLabel null
cmis:description null
cmis:createdBy unknown
cmis:checkinComment null
cmis:creationDate Fri May 19 21:22:57 IST 2017
cmis:contentStreamFileName data.txt
cmis:isMajorVersion true
cmis:name My_Document-0-0
cmis:isLatestVersion true
cmis:lastModificationDate Fri May 19 21:22:57 IST 2017
cmis:contentStreamLength 34113
cmis:objectId 133
cmis:lastModifiedBy unknown
cmis:secondaryObjectTypeIds null
cmis:contentStreamId null
cmis:contentStreamMimeType text/plain
cmis:baseTypeId cmis:document
cmis:changeToken 1495209177473
cmis:isPrivateWorkingCopy false
cmis:isVersionSeriesCheckedOut false
cmis:versionSeriesCheckedOutBy null
cmis:versionSeriesId 133
cmis:isLatestMajorVersion true
cmis:versionSeriesCheckedOutId null

Every document has some contents associated with it. We can get the contents of a document using the content stream associated with it. Following application prints first document content to the console while traversing through root folder hierarchy.


TestCmis.java
package com.sample.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.Repository;
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 {

 public static List<Repository> getRepositories(String serverURL) {
  Map<String, String> parameters = new HashMap<>();
  parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());

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

  parameters.put(SessionParameter.BROWSER_URL, serverURL);

  SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
  List<Repository> repositories = sessionFactory.getRepositories(parameters);
  return repositories;
 }

 public static void printFirstDocumentContent(Folder folder) {

  ItemIterable<CmisObject> cmisObjects = folder.getChildren();

  for (CmisObject cmisObject : cmisObjects) {
   if (!(cmisObject instanceof Document)) {
    continue;
   }

   Document document = (Document) cmisObject;

   String mimeType = document.getContentStreamMimeType();

   if (mimeType == null)
    continue;

   if (!mimeType.startsWith("text"))
    continue;

   long contentStreamLength = document.getContentStreamLength();

   if (contentStreamLength == 0)
    continue;

   System.out.println("Name Of the Document : " + document.getName());
   printDocumentContent(document);
   return;
  }

 }

 private static void printDocumentContent(Document document) {
  ContentStream contentStream = document.getContentStream();
  String fileNameOfTheStream = contentStream.getFileName();
  System.out.println("file name of the stream : " + fileNameOfTheStream);
  try (InputStream inputStream = contentStream.getStream()) {
   int data;

   while ((data = inputStream.read()) != -1) {
    System.out.print((char) data);
   }
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

 public static void main(String args[]) {
  String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
  List<Repository> repositories = getRepositories(serverURL);

  for (Repository repository : repositories) {
   Session session = repository.createSession();
   Folder rootFolder = session.getRootFolder();
   printFirstDocumentContent(rootFolder);

  }
 }
}

Depending on the object type definition of the document, a document can be

a.   Version-able: Can able to perform versioning services like Checkout and Checkin of a document.
b.   File-able: Can be filed in zero, one, or more than one folder via the Multi-filing Services.
c.    Query-able: Can be located via the CMIS query
d.   Controllable-Policy: Can have policies applied to it.
e.   Controllable-ACL: Can have an ACL applied to it.

Example
                  DocumentType documentType = document.getDocumentType();

                  System.out.println("Is Document Versionable : " + documentType.isVersionable());
                  System.out.println("Is Document controllable by ALC : " + documentType.isControllableAcl());
                  System.out.println("Is Document controllable by policy : " + documentType.isControllablePolicy());
                  System.out.println("Is Document fileable : " + documentType.isFileable());
                  System.out.println("Is Document queryable : " + documentType.isQueryable());


TestCmis.java
package com.sample.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.DocumentType;
import org.apache.chemistry.opencmis.client.api.Folder;
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.enums.BindingType;

public class TestCmis {

 private static String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
 private static String repositoryId = "A1";

 public static Session getSession() {
  Map<String, String> parameters = new HashMap<>();
  parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());

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

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

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

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

  Session session = getSession();

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

  Folder rootFolder = session.getRootFolder();

  Document document = rootFolder.createDocument(properties, null, null);

  DocumentType documentType = document.getDocumentType();

  System.out.println("Is Document Versionable : " + documentType.isVersionable());
  System.out.println("Is Document controllable by ALC : " + documentType.isControllableAcl());
  System.out.println("Is Document controllable by policy : " + documentType.isControllablePolicy());
  System.out.println("Is Document fileable : " + documentType.isFileable());
  System.out.println("Is Document queryable : " + documentType.isQueryable());

 }

}


Output

Is Document Versionable : false
Is Document controllable by ALC : true
Is Document controllable by policy : true
Is Document fileable : true
Is Document queryable : true  



Previous                                                 Next                                                 Home

No comments:

Post a Comment