Friday, 12 April 2019

openCMIS: Create custom item type


Creating custom item type is very simple using CMIS workbench. First you should download existing custom type, update that as per your needs and update this new type using cmis workbench.

Below step-by-step procedure explains this.

Step1: Open cmis work bench and connect to the server.

Step 2: Click on the button ‘Types’ from the toolbar.


Step 3: Navigate to the Item type. Click on the button ‘Save Type Definition’-> ‘Save Type Definition as JSON’.



MyItemType is defined like below.

cmis_item.json
{
 "id": "cmis:item",
 "localName": "cmis:item",
 "localNamespace": "http:\/\/apache.org",
 "displayName": "Item",
 "queryName": "cmis:item",
 "description": "Item",
 "baseId": "cmis:item",
 "creatable": true,
 "fileable": true,
 "queryable": true,
 "fulltextIndexed": false,
 "includedInSupertypeQuery": true,
 "controllablePolicy": true,
 "controllableACL": true,
 "typeMutability": {
  "create": true,
  "update": false,
  "delete": false
 },
 "propertyDefinitions": {
  "cmis:name": {
   "id": "cmis:name",
   "localName": "cmis:name",
   "displayName": "Name",
   "queryName": "cmis:name",
   "description": "Name",
   "propertyType": "string",
   "cardinality": "single",
   "updatability": "readwrite",
   "inherited": false,
   "required": true,
   "queryable": true,
   "orderable": true
  },
  "cmis:description": {
   "id": "cmis:description",
   "localName": "cmis:description",
   "displayName": "Description",
   "queryName": "cmis:description",
   "description": "Description",
   "propertyType": "string",
   "cardinality": "single",
   "updatability": "readwrite",
   "inherited": false,
   "required": false,
   "queryable": false,
   "orderable": false
  },
  "cmis:objectId": {
   "id": "cmis:objectId",
   "localName": "cmis:objectId",
   "displayName": "Object Id",
   "queryName": "cmis:objectId",
   "description": "Object Id",
   "propertyType": "id",
   "cardinality": "single",
   "updatability": "readonly",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": false
  },
  "cmis:baseTypeId": {
   "id": "cmis:baseTypeId",
   "localName": "cmis:baseTypeId",
   "displayName": "Base Type Id",
   "queryName": "cmis:baseTypeId",
   "description": "Base Type Id",
   "propertyType": "id",
   "cardinality": "single",
   "updatability": "readonly",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": false
  },
  "cmis:objectTypeId": {
   "id": "cmis:objectTypeId",
   "localName": "cmis:objectTypeId",
   "displayName": "Object Type Id",
   "queryName": "cmis:objectTypeId",
   "description": "Object Type Id",
   "propertyType": "id",
   "cardinality": "single",
   "updatability": "oncreate",
   "inherited": false,
   "required": true,
   "queryable": true,
   "orderable": false
  },
  "cmis:secondaryObjectTypeIds": {
   "defaultValue": [],
   "id": "cmis:secondaryObjectTypeIds",
   "localName": "cmis:secondaryObjectTypeIds",
   "displayName": "Secondary Type Ids",
   "queryName": "cmis:secondaryObjectTypeIds",
   "description": "Secondary Type Ids",
   "propertyType": "id",
   "cardinality": "multi",
   "updatability": "readwrite",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": false
  },
  "cmis:createdBy": {
   "id": "cmis:createdBy",
   "localName": "cmis:createdBy",
   "displayName": "Created By",
   "queryName": "cmis:createdBy",
   "description": "Created By",
   "propertyType": "string",
   "cardinality": "single",
   "updatability": "readonly",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": true
  },
  "cmis:creationDate": {
   "id": "cmis:creationDate",
   "localName": "cmis:creationDate",
   "displayName": "Creation Date",
   "queryName": "cmis:creationDate",
   "description": "Creation Date",
   "propertyType": "datetime",
   "cardinality": "single",
   "updatability": "readonly",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": true
  },
  "cmis:lastModifiedBy": {
   "id": "cmis:lastModifiedBy",
   "localName": "cmis:lastModifiedBy",
   "displayName": "Last Modified By",
   "queryName": "cmis:lastModifiedBy",
   "description": "Last Modified By",
   "propertyType": "string",
   "cardinality": "single",
   "updatability": "readonly",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": true
  },
  "cmis:lastModificationDate": {
   "id": "cmis:lastModificationDate",
   "localName": "cmis:lastModificationDate",
   "displayName": "Last Modification Date",
   "queryName": "cmis:lastModificationDate",
   "description": "Last Modification Date",
   "propertyType": "datetime",
   "cardinality": "single",
   "updatability": "readonly",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": true
  },
  "cmis:changeToken": {
   "id": "cmis:changeToken",
   "localName": "cmis:changeToken",
   "displayName": "Change Token",
   "queryName": "cmis:changeToken",
   "description": "Change Token",
   "propertyType": "string",
   "cardinality": "single",
   "updatability": "readonly",
   "inherited": false,
   "required": false,
   "queryable": false,
   "orderable": false
  }
 }
}


I added two extra property definitions to the above json.

 "abc:contactUs": {
   "id": "abc:contactUs",
   "localName": "abc:contactUs",
   "displayName": "contact us",
   "queryName": "abc:contactUs",
   "description": "Contact url.",
   "propertyType": "string",
   "cardinality": "single",
   "updatability": "readwrite",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": true,
   "openChoice": false
  },
  "abc:help": {
   "id": "abc:help",
   "localName": "abc:help",
   "displayName": "Help",
   "queryName": "abc:help",
   "description": "Documentation url.",
   "propertyType": "string",
   "cardinality": "single",
   "updatability": "readwrite",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": true,
   "openChoice": false
  }

My updated json file looks like below.


abcConfigurationsItem.json
{
 "id": "abc:configurationsItem",
 "localName": "abc:configurationsItem",
 "localNamespace": "http:\/\/apache.org",
 "displayName": "abc:configurationsItem",
 "queryName": "abc:configurationsItem",
 "description": "Builtin InMemory type definition abc:configurationsItem",
 "baseId": "cmis:item",
 "parentId": "cmis:item",
 "creatable": true,
 "fileable": true,
 "queryable": true,
 "fulltextIndexed": false,
 "includedInSupertypeQuery": true,
 "controllablePolicy": true,
 "controllableACL": true,
 "typeMutability": {
  "create": true,
  "update": true,
  "delete": true
 },
 "propertyDefinitions": {
  "abc:contactUs": {
   "id": "abc:contactUs",
   "localName": "abc:contactUs",
   "displayName": "contact us",
   "queryName": "abc:contactUs",
   "description": "Contact url.",
   "propertyType": "string",
   "cardinality": "single",
   "updatability": "readwrite",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": true,
   "openChoice": false
  },
  "abc:help": {
   "id": "abc:help",
   "localName": "abc:help",
   "displayName": "Help",
   "queryName": "abc:help",
   "description": "Documentation url.",
   "propertyType": "string",
   "cardinality": "single",
   "updatability": "readwrite",
   "inherited": false,
   "required": false,
   "queryable": true,
   "orderable": true,
   "openChoice": false
  }
 }
}

Step 3: Upload the json ‘abcConfigurationsItem.json’ using workbench.


Click on the button ‘Create Type’ -> Load Type Definition from JSON.

Browse the file ‘abcConfigurationsItem.json’ and save. If your repository supports custom type definitions, then it will be uploaded successfully and create new type definition.


Now, you can able to see new item type in the workbench.


Now you can create items using this item type ‘abc:configurationsItem’.

Below snippet creates custom item of type ‘abc:configurationsItem’.

                  Folder folder = (Folder)session.getObjectByPath("/My_Folder-0-0");

                  Map<String, Object>  properties = new HashMap<String, Object>();
                  properties.put(PropertyIds.NAME, "My custom filed item");
                  properties.put(PropertyIds.OBJECT_TYPE_ID, "abc:configurationsItem");
                  properties.put("abc:contactUs", "self-learning-java-tutorial.blogspot.in");
                  properties.put("abc:help", "Comment on any one of the post");

                  Item item = folder.createItem(properties);

TestCmis.java
package com.sample.util;

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

import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Item;
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);
 }

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

  Session session = getSession();
  
  Folder folder = (Folder)session.getObjectByPath("/My_Folder-0-0");

  Map<String, Object>  properties = new HashMap<String, Object>();
  properties.put(PropertyIds.NAME, "My custom filed item");
  properties.put(PropertyIds.OBJECT_TYPE_ID, "abc:configurationsItem");
  properties.put("abc:contactUs", "self-learning-java-tutorial.blogspot.in");
  properties.put("abc:help", "Comment on any one of the post");

  Item item = folder.createItem(properties);

  System.out.println((PropertyIds.NAME + ": " + item.getPropertyValue(PropertyIds.NAME )));
  System.out.println((PropertyIds.OBJECT_TYPE_ID + ": " + item.getPropertyValue(PropertyIds.OBJECT_TYPE_ID)));

 }

}

Output
cmis:name: My custom filed item
cmis:objectTypeId: abc:configurationsItem


On successful run of the application, open workbench and navigate to the folder ‘My_Folder-0-0’, you can able to see the item ‘My custom filed item’ of type ‘abc:configurationItem’ is created.

Previous                                                 Next                                                 Home

No comments:

Post a Comment