Sunday 21 April 2019

openCMIS: isGetDescendantsSupported : Check whether we can get children of a folder


RepositoryCapabilities interface provides 'isGetDescendantsSupported' to check whether an application can able to enumerate the descendants of a folder via the getDescendants service

Following application prints complte folder hierarchy, if repository supports isGetDescendantsSupported.

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.FileableCmisObject;
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.api.Tree;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.data.RepositoryInfo;
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 printHierarchy(List<Tree<FileableCmisObject>> objects, String space) {

  for (Tree<FileableCmisObject> obj : objects) {
   FileableCmisObject fileableObj = obj.getItem();

   System.out.println(space + fileableObj.getName());
   printHierarchy(obj.getChildren(), " " + space);
  }
 }

 public static void main(String args[]) {
  Session session = getSession();

  RepositoryInfo repoInfo = session.getRepositoryInfo();

  boolean isGetDescendantsSupported = repoInfo.getCapabilities().isGetDescendantsSupported();

  if (!isGetDescendantsSupported) {
   System.out.println("Repository do not support descendants");
   return;
  }

  Folder rootFolder = session.getRootFolder();

  List<Tree<FileableCmisObject>> fileableCmisObjects = rootFolder.getDescendants(-1);

  System.out.println(rootFolder.getName());
  printHierarchy(fileableCmisObjects, " ");

 }
}


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-1
  My_Folder-1-1
   My_Document-2-0
   My_Document-2-1
   My_Document-2-2
   My_Folder-2-0
   My_Folder-2-1
 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-1
  My_Folder-1-1
   My_Document-2-0
   My_Document-2-1
   My_Document-2-2
   My_Folder-2-0
   My_Folder-2-1



Previous                                                 Next                                                 Home

No comments:

Post a Comment