Friday, 12 April 2019

openCMIS: Create an item object


Just like other cmis object (documents, policies) items can be filed (or) unfiled. You can create unfiled items using Session object, where as you can create filed items using Folder object.

What is filed item?
An item called filed, if it resides in one or more folders.

What is unfiled item?
An item called unfiled, if it do not reside in any folder.

How to create unfiled item?
Session interface provides below methods to create an item.
          ObjectId createItem(Map<String, ?> properties, ObjectId folderId);
          ObjectId createItem(Map<String, ?> properties, ObjectId folderId, List<Policy> policies, List<Ace> addAces,List<Ace> removeAces);
          
You should pass folderId as null to create unfiled items.

Below statements are used to create unfiled item.

                  Map<String, Object> properties = new HashMap<String, Object>();
                  properties.put(PropertyIds.NAME, "My Demo unfiled item");
                  properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:item");

                  ObjectId itemId = session.createItem(properties, null);

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

  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put(PropertyIds.NAME, "My Demo unfiled item");
  properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:item");

  ObjectId itemId = session.createItem(properties, null);

  Item item = (Item) session.getObject(itemId);

  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 Demo unfiled item
cmis:objectTypeId: cmis:item

How to create filed item?
Folder interface provides 'createItem' method to create item.

Item createItem(Map<String, ?> properties);
Item createItem(Map<String, ?> properties, List<Policy> policies, List<Ace> addAces, List<Ace> removeAces,OperationContext context);

Below statements create a filed item.

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

                  Map<String, Object>  properties = new HashMap<String, Object>();
                  properties.put(PropertyIds.NAME, "My Demo filed item");
                  properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:item");

                  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 Demo filed item");
  properties.put(PropertyIds.OBJECT_TYPE_ID, "cmis:item");

  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 Demo filed item
cmis:objectTypeId: cmis:item


After running the application, you can able to see the item ‘My Demo filed item’ in work bench by navigating through the folder ‘My_Folder-0-0’.


Previous                                                 Next                                                 Home

No comments:

Post a Comment