Skip to content

Instantly share code, notes, and snippets.

@ramannanda9
Created February 26, 2015 14:26
Show Gist options
  • Save ramannanda9/c85e52225645e3b9db4c to your computer and use it in GitHub Desktop.
Save ramannanda9/c85e52225645e3b9db4c to your computer and use it in GitHub Desktop.
CMIS Query with Apache Chemistry and alfresco
import org.apache.chemistry.opencmis.client.api.*;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by Ramandeep on 10/2/15.
*/
public class CMISQueryTest {
public static void main(String [] args){
Logger logger= LoggerFactory.getLogger(CMISQueryTest.class);
SessionFactory sessionFactory = SessionFactoryImpl.newInstance();
Map<String,String> parameter = new HashMap<String,String>();
parameter.put(SessionParameter.USER, "admin");
//the binding type to use
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
parameter.put(SessionParameter.PASSWORD, "admin");
//the endpoint
parameter.put(SessionParameter.ATOMPUB_URL, "http://cmis.alfresco.com/s/cmis");
parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
//fetch the list of repositories
List<Repository> repositories = sessionFactory.getRepositories(parameter);
//establish a session with the first ?
Session session = repositories.get(0).createSession();
//demonstrates a join
QueryStatement qs=session.createQueryStatement("SELECT D.*, O.* FROM cmis:document AS D JOIN cm:ownable AS O ON D.cmis:objectId = O.cmis:objectId " +
" where " +
" D.cmis:name in (?)" +
" and " +
" D.cmis:creationDate > TIMESTAMP ? " +
" order by cmis:creationDate desc");
String documentNames[]= new String[]{"Project Objectives.ppt","Project Overview.ppt"};
qs.setString(1, documentNames);
Calendar now = Calendar.getInstance();
//subtract 5 year for viewing documents for last 5 year
now.add(Calendar.YEAR, -5);
qs.setDateTime(2, now);
//get the first 50 records only.
ItemIterable<QueryResult> results = session.query(qs.toQueryString(), false).getPage(50);
for(QueryResult result: results) {
Object documentName=result.getPropertyByQueryName("D.cmis:name").getFirstValue();
logger.info("D.cmis:name " + ": " + documentName);
//
Object documentReference=result.getPropertyByQueryName("D.cmis:objectId").getFirstValue();
logger.info("--------------------------------------");
logger.info("Content URL: http://cmis.alfresco.com/service/cmis/content?conn=default&id="+documentReference);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment