Monday 29 April 2019

Cmis: Get property level extensions


At the server end, server should add property level extensions. While sending the response to the client, server should add property level extensions to object data.

         private static void setExtensions(ObjectData objData) {
                  CmisExtensionElement leafExtension1 = new CmisExtensionElementImpl("", "confidentialLevel", null,
                                    "confidential");
                  CmisExtensionElement leafExtension2 = new CmisExtensionElementImpl("", "rootFolderId", null, "folder_1234");
                  CmisExtensionElement leafExtension3 = new CmisExtensionElementImpl("", "classification", null, "public");

                  List<CmisExtensionElement> list = new ArrayList<>();
                  list.add(leafExtension1);
                  list.add(leafExtension2);
                  list.add(leafExtension3);

                  CmisExtensionElement parentExtension = new CmisExtensionElementImpl("", "parentExtension", null, list);

                  List<CmisExtensionElement> extensions = Collections.singletonList(parentExtension);

                  Properties props = objData.getProperties();

                  props.setExtensions(extensions);

         }
Above snipprt adds the extensions to object at property level.

How to read the property level extensions?
Below snippet is used to get the property level extensions of cmis object.

Session session = getSession();
CmisObject cmisObj = session.getObject("L0hlbGxvLnR4dA==");
List<CmisExtensionElement> extensions = cmisObj.getExtensions(ExtensionLevel.PROPERTIES);

Test.java

package cmis_client.cmis_client;

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.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.CmisExtensionElement;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.ExtensionLevel;

public class Test {

	private static String serverURL = "http://localhost:8081/server/browser";
	private static String repositoryId = "test";

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

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

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

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

	private static void printExtensions(List<CmisExtensionElement> extensions, String space) {

		for (CmisExtensionElement extension : extensions) {
			List<CmisExtensionElement> extensionChildren = extension.getChildren();

			if (extensionChildren == null || extensionChildren.isEmpty()) {
				String name = extension.getName();
				String value = extension.getValue();
				System.out.println(name + " : " + value);

			} else {
				printExtensions(extensionChildren, space + " ");
			}
		}
	}

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

		CmisObject cmisObj = session.getObject("L0hlbGxvLnR4dA==");

		List<CmisExtensionElement> extensions = cmisObj.getExtensions(ExtensionLevel.PROPERTIES);

		printExtensions(extensions, " ");

	}
}




Previous                                                 Next                                                 Home

No comments:

Post a Comment