Wednesday 27 March 2019

openCMIS: Working with folders


A folder in CMIS is just like a folder in file system, it is a collection of file-able objects. Every CMIS repository supplies root folder if with its RepositoryInfo object, this is the starting point to navigate across the folder tree.

Points to remember
a.   Every folder object has unique id associated with it.
b.   Every folder other than root folder has unique parent associated with them

Is Folder object versionable?
No

Is Folder object have content stream associated with it?
No, a folder object do not have content stream associated with it.

Is folder object versionable?
No, a folder object is not versionable.

 I will explain more about the Folder API in my later posts. Right now, go through following program, which prints the folder hierarchy starts from root folder.

If you have the session instance, you can get the root folder by using following snippet.

Folder rootFolder = session.getRootFolder();

Following application travers through entire 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.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.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 printFolder(Folder folder, String tab) {
  System.out.println(tab + folder.getName());

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

  for (CmisObject cmisObject : cmisObjects) {
   if (cmisObject instanceof Folder) {
    printFolder((Folder) cmisObject, tab + " ");
   }
   System.out.println(" " + tab + cmisObject.getName());
  }

 }

 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();
   printFolder(rootFolder, "");

  }
 }
}


Sample Output
RootFolder
 My_Document-0-0
 My_Document-0-1
 My_Document-0-2
 My_Folder-0-0
  My_Document-1-0
  My_Document-1-1
  My_Document-1-2
  My_Folder-1-0
   My_Document-2-0
   My_Document-2-1
   My_Document-2-2
   My_Folder-2-0
   My_Folder-2-0
   My_Folder-2-1
   My_Folder-2-1
  My_Folder-1-0
  My_Folder-1-1
   My_Document-2-0
   My_Document-2-1
   My_Document-2-2
   My_Folder-2-0
   My_Folder-2-0
   My_Folder-2-1
   My_Folder-2-1
  My_Folder-1-1
 My_Folder-0-0
 My_Folder-0-1
  My_Document-1-0
  My_Document-1-1
  My_Document-1-2
  My_Folder-1-0
   My_Document-2-0
   My_Document-2-1
   My_Document-2-2
   My_Folder-2-0
   My_Folder-2-0
   My_Folder-2-1
   My_Folder-2-1
  My_Folder-1-0
  My_Folder-1-1
   My_Document-2-0
   My_Document-2-1
   My_Document-2-2
   My_Folder-2-0
   My_Folder-2-0
   My_Folder-2-1
   My_Folder-2-1
  My_Folder-1-1
 My_Folder-0-1


Previous                                                 Next                                                 Home

No comments:

Post a Comment