Monday, 15 April 2019

openCMIS: Set secondary type


You can create secondary type at the time of creating the cmis object (or) you can add secondary type to the existing cmis object.

Setting secondary type at the time of creating cmis object
Below statements are used to set secondary type ‘abc:secondaryType’ and add custom properties ‘ab:contactUs’ and ‘abc:help’ to the document.

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

                  List<String> secondaryTypes = new ArrayList<String>();
                  secondaryTypes.add("abc:secondaryType");
                  properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);

                  properties.put("abc:contactUs", "https://self-learning-java-tutorial.blogspot.com");
                  properties.put("abc:help", "Java Tutorial blogspot");
                 
                  Document document = rootFolder.createDocument(properties, null, null);

‘abc:secondaryType’ is the custom secondary type that I created in my previous post.

TestCmis.java
package com.sample.util;

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

import org.apache.chemistry.opencmis.client.api.Document;
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.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(Document document) {
  String contactUs = document.getPropertyValue("abc:contactUs");
  String help = document.getPropertyValue("abc:help");

  System.out.println("name : " + document.getName());
  System.out.println("id : " + document.getId());
  System.out.println("version series id : " + document.getVersionSeriesId());
  System.out.println("Version Label : " + document.getVersionLabel());
  System.out.println("Checked out by : " + document.getVersionSeriesCheckedOutBy());
  System.out.println("Content length : " + document.getContentStreamLength());

  System.out.println("contact us : " + contactUs);
  System.out.println("Help : " + help);
 }

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

  Session session = getSession();

  Folder rootFolder = session.getRootFolder();

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

  List<String> secondaryTypes = new ArrayList<String>();
  secondaryTypes.add("abc:secondaryType");
  properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, secondaryTypes);

  properties.put("abc:contactUs", "https://self-learning-java-tutorial.blogspot.com");
  properties.put("abc:help", "Java Tutorial blogspot");

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

}


Output
name : emptyDocument.txt
id : 136
version series id : 136
Version Label : null
Checked out by : null
Content length : -1
contact us : https://self-learning-java-tutorial.blogspot.com
Help : Java Tutorial blogspot


From workbench, you can able to see the properties of the document ‘emptyDocument.txt’.

Update secondary type of an object
You can also update the secondary type (or) properties of an object after its creation.

For example, I created one more secondary type ‘abc:confidentialDocs’ and going to add this secondary type to the document I created above.


confidentialDocs.json
{
 "id": "abc:confidentialDocs",
 "localName": "abc:confidentialDocs",
 "localNamespace": "http:\/\/apache.org",
 "displayName": "abc:confidentialDocs",
 "queryName": "abc:confidentialDocs",
 "description": "Builtin InMemory type definition abc:confidentialDocs",
 "baseId": "cmis:secondary",
 "parentId": "cmis:secondary",
 "creatable": false,
 "fileable": false,
 "queryable": true,
 "fulltextIndexed": false,
 "includedInSupertypeQuery": true,
 "controllablePolicy": false,
 "controllableACL": false,
 "typeMutability": {
  "create": true,
  "update": true,
  "delete": true
 },
 "propertyDefinitions": {
  "abc:confidentialLevel": {
   "id": "abc:confidentialLevel",
   "localName": "abc:confidentialLevel",
   "displayName": "Confidentiality of the document",
   "queryName": "abc:confidentialLevel",
   "description": "Contact us for any queries",
   "propertyType": "integer",
   "cardinality": "single",
   "updatability": "readwrite",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": true,
   "openChoice": false
  }
 }
}

Below statements add new secondary type type ‘abc:confidentialDocs’ to the existing document ‘emptyDocument.txt’ and add new custom property ‘abc:confidentialLevel’ to it.

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

                  /* Get current secondary types */
                  List<String> secondaryTypes = cmisObject.getPropertyValue(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);
                  if (secondaryTypes == null) {
                           secondaryTypes = new ArrayList<String>();
                  }

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

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

                  /* set secondary type property */
                  properties.put("abc:confidentialLevel", 5);

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


TestCmis.java
package com.sample.util;

import java.io.IOException;
import java.util.ArrayList;
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(Document document) {
  String contactUs = document.getPropertyValue("abc:contactUs");
  String help = document.getPropertyValue("abc:help");
  
  System.out.println("name : " + document.getName());
  System.out.println("id : " + document.getId());
  System.out.println("version series id : " + document.getVersionSeriesId());
  System.out.println("Version Label : " + document.getVersionLabel());
  System.out.println("Checked out by : " + document.getVersionSeriesCheckedOutBy());
  System.out.println("Content length : " + document.getContentStreamLength());

  System.out.println("contact us : " + contactUs);
  System.out.println("Help : " + help);
  System.out.println("confidentialValue : " + document.getPropertyValue("abc:confidentialLevel"));
 }

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

  Session session = getSession();

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

  /* Get current secondary types */
  List<String> secondaryTypes = cmisObject.getPropertyValue(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);
  if (secondaryTypes == null) {
   secondaryTypes = new ArrayList<String>();
  }

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

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

  /* set secondary type property */
  properties.put("abc:confidentialLevel", 5);

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

  printDocument((Document)cmisObject);
 }

}


Output
name : emptyDocument.txt
id : 136
version series id : 136
Version Label : null
Checked out by : null
Content length : -1
contact us : https://self-learning-java-tutorial.blogspot.com
Help : Java Tutorial blogspot
confidentialValue : 5


From workbench, you can observe that the properties and secondary types are added.



Previous                                                 Next                                                 Home

No comments:

Post a Comment