Friday, 12 April 2019

openCMIS: Relationship object


A relation ship object is used to specify the relation ship between two objects (source and target objects). The source object and the target object MUST both be independent objects, such as a document object, a folder object, a policy object, or an item object.

How to check whether repository supports relationship object or not?
If 'getTypeChildren' api return 'cmis:relationship', then repository supports relation ships, else not.

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

  ItemIterable<ObjectType> objectTypes = session.getTypeChildren(null, false);

  for (ObjectType objectType : objectTypes) {
   System.out.println(objectType.getId());
  }

 }

}

Output
cmis:secondary
cmis:item
cmis:relationship
cmis:policy
cmis:folder
cmis:document

Is creating a relationship changes source (or) destination object?
No

Is removing a relationship changes source (or) destination object?
No

Is relationship between two objects version-specific?
It depends on the repository. The binding of a relationship object to a source document object or to a target document object MAY be either version-specific or version-independent. This version sensitivity is repository-specific.

Can an object participate in more than one relationship?
An independent object MAY participate in any number of explicit relationships, as the source object for some and as the target object for others.

Can multiple relationships exist between same source and target?
Multiple relationships MAY exist between the same pair of source and target objects.

Is relationship deleted on deletion of source (or) target object?
If the source or the target object of a relationship is deleted, the repository MAY automatically delete the relationship object.

Can a relationship specify source and target types?
Yes, a relationship object-type MAY specify that only objects of a specific object-type can participate as the source object or target object for relationship objects of that type. If no such constraints are specified, then an independent object of any type MAY be the source or the target of a relationship object of that type.

How to get relationships associated with an object?
CmisObject interface provides ‘getRelationShips()’ method to get all the relation ships associated with an object.

Example
                  List<Relationship> relationShips = cmisObject.getRelationships();
                  if(relationShips == null){
                           System.out.println("No relation ships exist");
                           return;
                  }


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.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.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();

  CmisObject cmisObject = session.getObjectByPath("/My_Document-0-0");
  
  List<Relationship> relationShips = cmisObject.getRelationships();
  if(relationShips == null){
   System.out.println("No relation ships exist");
   return;
  }
  
  for(Relationship relationship: relationShips){
   System.out.println(relationship);
  }

 }

}




Previous                                                 Next                                                 Home

No comments:

Post a Comment