Saturday 20 April 2019

OpenCMIS: Querying programmatically


In my previous post, I explained how to query types using cmis workbench. Most of the time, we want to embed this CMIS query support in our applications. In this post, I am going to explain how can you query repository programmatically.

Once you got the reference to Session object, by calling the query method of session object, you can execute a query.

For example, following snippet gets you all the records in the type ‘cmis;document’.

String query = "SELECT * FROM cmis:document";
ItemIterable<QueryResult> queryResults = session.query(query, true);

Find the following complete working application.

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.ItemIterable;
import org.apache.chemistry.opencmis.client.api.QueryResult;
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.data.PropertyData;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParseException;

public class TestCmis {

 private static String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
 private static String repositoryId = "A1";

 public 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, JSONParseException {
  Session session = getSession();

  String query = "SELECT * FROM cmis:document";

  ItemIterable<QueryResult> queryResults = session.query(query, true);

  for (QueryResult queryResult : queryResults) {
   List<PropertyData<?>> propertiesData = queryResult.getProperties();

   System.out.println("*********************************************************");
   for (PropertyData<?> propData : propertiesData) {
    System.out.println(propData.getId() + " : " + propData.getValues());
   }
   System.out.println("*********************************************************\n\n");
  }

 }
}


Output
*********************************************************
cmis:isImmutable : [false]
cmis:objectTypeId : [ComplexType]
cmis:versionLabel : []
cmis:description : []
cmis:createdBy : [unknown]
cmis:checkinComment : []
cmis:creationDate : [java.util.GregorianCalendar[time=1495546132425,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=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=143,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=28,SECOND=52,MILLISECOND=425,ZONE_OFFSET=0,DST_OFFSET=0]]
cmis:contentStreamFileName : [data.txt]
cmis:isMajorVersion : [true]
cmis:name : [My_Document-0-0]
cmis:isLatestVersion : [true]
cmis:lastModificationDate : [java.util.GregorianCalendar[time=1495546132425,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=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=143,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=28,SECOND=52,MILLISECOND=425,ZONE_OFFSET=0,DST_OFFSET=0]]
cmis:contentStreamLength : [33871]
cmis:objectId : [133]
cmis:lastModifiedBy : [unknown]
cmis:secondaryObjectTypeIds : []
cmis:contentStreamId : []
cmis:contentStreamMimeType : [text/plain]
cmis:baseTypeId : [cmis:document]
cmis:changeToken : [1495546132425]
cmis:isPrivateWorkingCopy : [false]
cmis:isVersionSeriesCheckedOut : [false]
cmis:versionSeriesCheckedOutBy : []
cmis:versionSeriesId : [133]
cmis:isLatestMajorVersion : [true]
cmis:versionSeriesCheckedOutId : []
*********************************************************


*********************************************************
cmis:isImmutable : [false]
cmis:objectTypeId : [ComplexType]
cmis:versionLabel : []
cmis:description : []
cmis:createdBy : [unknown]
cmis:checkinComment : []
cmis:creationDate : [java.util.GregorianCalendar[time=1495546132617,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=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=143,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=28,SECOND=52,MILLISECOND=617,ZONE_OFFSET=0,DST_OFFSET=0]]
cmis:contentStreamFileName : [data.txt]
cmis:isMajorVersion : [true]
cmis:name : [My_Document-0-1]
cmis:isLatestVersion : [true]
cmis:lastModificationDate : [java.util.GregorianCalendar[time=1495546132617,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=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=143,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=28,SECOND=52,MILLISECOND=617,ZONE_OFFSET=0,DST_OFFSET=0]]
cmis:contentStreamLength : [33878]
cmis:objectId : [134]
cmis:lastModifiedBy : [unknown]
cmis:secondaryObjectTypeIds : []
cmis:contentStreamId : []
cmis:contentStreamMimeType : [text/plain]
cmis:baseTypeId : [cmis:document]
cmis:changeToken : [1495546132617]
cmis:isPrivateWorkingCopy : [false]
cmis:isVersionSeriesCheckedOut : [false]
cmis:versionSeriesCheckedOutBy : []
cmis:versionSeriesId : [134]
cmis:isLatestMajorVersion : [true]
cmis:versionSeriesCheckedOutId : []
*********************************************************


*********************************************************
cmis:isImmutable : [false]
cmis:objectTypeId : [ComplexType]
cmis:versionLabel : []
cmis:description : []
cmis:createdBy : [unknown]
cmis:checkinComment : []
cmis:creationDate : [java.util.GregorianCalendar[time=1495546132770,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=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=143,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=28,SECOND=52,MILLISECOND=770,ZONE_OFFSET=0,DST_OFFSET=0]]
cmis:contentStreamFileName : [data.txt]
cmis:isMajorVersion : [true]
cmis:name : [My_Document-0-2]
cmis:isLatestVersion : [true]
cmis:lastModificationDate : [java.util.GregorianCalendar[time=1495546132770,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=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=143,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=1,HOUR_OF_DAY=13,MINUTE=28,SECOND=52,MILLISECOND=770,ZONE_OFFSET=0,DST_OFFSET=0]]
cmis:contentStreamLength : [33844]
cmis:objectId : [135]
cmis:lastModifiedBy : [unknown]
cmis:secondaryObjectTypeIds : []
cmis:contentStreamId : []
cmis:contentStreamMimeType : [text/plain]
cmis:baseTypeId : [cmis:document]
cmis:changeToken : [1495546132770]
cmis:isPrivateWorkingCopy : [false]
cmis:isVersionSeriesCheckedOut : [false]
cmis:versionSeriesCheckedOutBy : []
cmis:versionSeriesId : [135]
cmis:isLatestMajorVersion : [true]
cmis:versionSeriesCheckedOutId : []
*********************************************************


*********************************************************
cmis:isImmutable : [false]
cmis:objectTypeId : [cmis:document]
cmis:versionLabel : []
cmis:description : []
cmis:createdBy : []
cmis:checkinComment : []
cmis:creationDate : [java.util.GregorianCalendar[time=1495550196056,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=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=143,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=36,SECOND=36,MILLISECOND=56,ZONE_OFFSET=0,DST_OFFSET=0]]
cmis:contentStreamFileName : [a.txt]
cmis:isMajorVersion : [true]
cmis:name : [a.txt]
cmis:isLatestVersion : [true]
cmis:lastModificationDate : [java.util.GregorianCalendar[time=1495550196056,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=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=143,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=36,SECOND=36,MILLISECOND=56,ZONE_OFFSET=0,DST_OFFSET=0]]
cmis:contentStreamLength : [0]
cmis:objectId : [137]
cmis:lastModifiedBy : []
cmis:secondaryObjectTypeIds : []
cmis:contentStreamId : []
cmis:contentStreamMimeType : [application/octet-stream]
cmis:baseTypeId : [cmis:document]
cmis:changeToken : [1495550196056]
cmis:isPrivateWorkingCopy : [false]
cmis:isVersionSeriesCheckedOut : [false]
cmis:versionSeriesCheckedOutBy : []
cmis:versionSeriesId : [137]
cmis:isLatestMajorVersion : [true]
cmis:versionSeriesCheckedOutId : []
*********************************************************

Is all repositories support queries capability?

Not necessarily. You can check whether the repository support query capability or not by using following statement.
CapabilityQuery queryCapability = session.getRepositoryInfo().getCapabilities().getQueryCapability();
                 
if(queryCapability == null){
         System.out.println("Repository don't support querys");
         return;
}
System.out.println("Query capability supported");


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.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;
import org.apache.chemistry.opencmis.commons.enums.CapabilityQuery;
import org.apache.chemistry.opencmis.commons.impl.json.parser.JSONParseException;

public class TestCmis {

 private static String serverURL = "http://localhost:8080/chemistry-opencmis-server-inmemory-1.1.0/browser";
 private static String repositoryId = "A1";

 public 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, JSONParseException {
  Session session = getSession();

  CapabilityQuery queryCapability = session.getRepositoryInfo().getCapabilities().getQueryCapability();
  
  if(queryCapability == null){
   System.out.println("Repository don't support querys");
   return;
  }
  System.out.println("Query capability supported");
  System.out.println("Query capability : " + queryCapability);
 }
}

Output
Query capability : BOTHCOMBINED

Is all the types are queryable?
Nope, for example CMIS specification has a type ‘cmis:relationship’, it is used to establish a relationship between two objects. It is not queryable.

Can I query on every properties of object types?
No, not all the properties are queryable. If the property has ‘queryable’ attribute set to true, then you can query, otherwise not.


For example, observe following image, ‘Queryable’ attribute of the property ‘Created By’ is set to true. So you can query on the property ‘Created By’.


Observe following image, ‘Queryable’ attribute of the property ‘MIME Type’ is set to false. So you can not query on the property ‘MIME Type’.




Previous                                                 Next                                                 Home

No comments:

Post a Comment