Friday 12 April 2019

openCMIS: Creating a relationship


Session interface provides below methods to establish a relationship between two cmis objects.

ObjectId createRelationship(Map<String, ?> properties);
ObjectId createRelationship(Map<String, ?> properties, List<Policy> policies, List<Ace> addAces,List<Ace> removeAces);

How to create a relationship?
         private static ObjectId createRelationShip(Session session, String sourceId, String targetId){
                  Map<String, Object> properties = new HashMap<String, Object>();
                  properties.put(PropertyIds.NAME, "a new relationship");
                  properties.put(PropertyIds.OBJECT_TYPE_ID, "CrossReferenceType");
                  properties.put(PropertyIds.SOURCE_ID, sourceId);
                  properties.put(PropertyIds.TARGET_ID, targetId);

                  ObjectId newRelId = session.createRelationship(properties);

                  return newRelId;
         }

TestCmis.java
package com.sample.util;

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

import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.ObjectId;
import org.apache.chemistry.opencmis.client.api.Relationship;
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 ObjectId createRelationShip(Session session, String sourceId, String targetId){
  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put(PropertyIds.NAME, "a new relationship");
  properties.put(PropertyIds.OBJECT_TYPE_ID, "CrossReferenceType");
  properties.put(PropertyIds.SOURCE_ID, sourceId);
  properties.put(PropertyIds.TARGET_ID, targetId);

  ObjectId newRelId = session.createRelationship(properties);

  return newRelId;
 }
 
 private static void printRelationShip(Relationship relationship){
  String createdBy = relationship.getCreatedBy();
  GregorianCalendar creationDate = relationship.getCreationDate();
  String description = relationship.getDescription();
  String sourceName = relationship.getSource().getName();
  String targetName = relationship.getTarget().getName();
  
  
  System.out.println("createdBy : " + createdBy);
  System.out.println("creationDate : " + creationDate);
  System.out.println("description : " + description);
  System.out.println("sourceName : " + sourceName);
  System.out.println("targetName : " + targetName);
  
 }
 
 public static void main(String args[]) throws IOException {

  Session session = getSession();

  CmisObject folder1 = session.getObjectByPath("/My_Folder-0-0");
  CmisObject folder2 = session.getObjectByPath("/My_Folder-0-1");
  
  ObjectId relationShipId = createRelationShip(session, folder1.getId(), folder2.getId());
  
  Relationship relationShip = (Relationship)session.getObject(relationShipId);
  
  printRelationShip(relationShip);
 
  
 }

}

Output
createdBy :
creationDate : java.util.GregorianCalendar[time=1498063315984,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT",offset=0,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=5,WEEK_OF_YEAR=25,WEEK_OF_MONTH=4,DAY_OF_MONTH=21,DAY_OF_YEAR=172,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=3,AM_PM=1,HOUR=4,HOUR_OF_DAY=16,MINUTE=41,SECOND=55,MILLISECOND=984,ZONE_OFFSET=0,DST_OFFSET=0]
description : null
sourceName : My_Folder-0-0
targetName : My_Folder-0-1


Previous                                                 Next                                                 Home

No comments:

Post a Comment