Wednesday 17 April 2019

openCMIS: Remove secondary type from the object


In my previous post, I showed you how to add secondary type to a cmis object. CMIS provides a way to remove the secondary properties assigned to an object.


From the above image, you can observe that the document ‘emptyDocument.txt’ has two secondary types associated with it.
a.   abc:secondaryType
b.   abc:confidentialDocs

Below statements are used to remove the secondary type ‘abc:confidentialDocs’.

                  /* Get current secondary types */
                  List<String> secondaryTypes = cmisObject.getPropertyValue(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);
                  if (secondaryTypes == null) {
                           System.out.println("No secondary types associated to this object");
                           return;
                  }

                  Map<String, Object> properties = new HashMap<String, Object>();

                  /* Remove the new secondary type */
                  secondaryTypes.remove("abc:confidentialDocs");
                  properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);

                  /* update properties */
                  cmisObject.updateProperties(properties);

Find the below working application.

TestCmis.java
package com.sample.util;

import java.io.IOException;
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.Session;
import org.apache.chemistry.opencmis.client.api.SessionFactory;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
import org.apache.chemistry.opencmis.commons.PropertyIds;
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";

 private 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);
 }

 private static void printDocument(CmisObject cmisObject) {
  System.out.println("***********************************************");
  System.out.println("contact us : " + cmisObject.getPropertyValue("abc:contactUs"));
  System.out.println("Help : " + cmisObject.getPropertyValue("abc:help"));
  System.out.println("confidentialValue : " + cmisObject.getPropertyValue("abc:confidentialLevel"));

  List<String> secondaryTypes = cmisObject.getPropertyValue(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);

  System.out.println("Secondary types are");
  for (String secondaryType : secondaryTypes) {
   System.out.println(secondaryType);
  }
 }

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

  Session session = getSession();

  CmisObject cmisObject = session.getObjectByPath("/emptyDocument.txt");

  System.out.println("Before removing secondary type 'abc:confidentialDocs'");
  printDocument(cmisObject);
  
  /* Get current secondary types */
  List<String> secondaryTypes = cmisObject.getPropertyValue(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);
  if (secondaryTypes == null) {
   System.out.println("No secondary types associated to this object");
   return;
  }

  Map<String, Object> properties = new HashMap<String, Object>();

  /* Remove the new secondary type */
  secondaryTypes.remove("abc:confidentialDocs");
  properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);

  /* update properties */
  cmisObject.updateProperties(properties);

  System.out.println("\nAfter removing secondary type 'abc:confidentialDocs'");
  printDocument((Document) cmisObject);
 }

}


Output
Before removing secondary type 'abc:confidentialDocs'
***********************************************
contact us : https://self-learning-java-tutorial.blogspot.com
Help : Java Tutorial blogspot
confidentialValue : 5
Secondary types are
abc:secondaryType
abc:confidentialDocs

After removing secondary type 'abc:confidentialDocs'
***********************************************
contact us : https://self-learning-java-tutorial.blogspot.com
Help : Java Tutorial blogspot
confidentialValue : null
Secondary types are
abc:secondaryType


As you see the output, the secondary type ‘abc:confidentialDocs’ is removed for the cmis object. You can observe the same in below workbench image.




Previous                                                 Next                                                 Home

No comments:

Post a Comment