Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Created June 27, 2016 19:43
Show Gist options
  • Save bdkosher/8908c7db5c4e5214085e6530e1aff385 to your computer and use it in GitHub Desktop.
Save bdkosher/8908c7db5c4e5214085e6530e1aff385 to your computer and use it in GitHub Desktop.
Some code I use to iterate through CMIS query results.
import java.util.ArrayList;
import java.util.List;
import org.apache.chemistry.opencmis.client.api.ItemIterable;
import org.apache.chemistry.opencmis.client.api.OperationContext;
import org.apache.chemistry.opencmis.client.api.QueryResult;
import org.apache.chemistry.opencmis.client.api.Session;
/**
* See the CMIS query API for an understanding of items per page and all versions.
*/
public static List<QueryResult> queryAll(Session session, String query, int itemsPerPage, boolean allVersions) {
OperationContext context = session.createOperationContext();
context.setMaxItemsPerPage(itemsPerPage);
ItemIterable<QueryResult> results = session.query(query, allVersions, context);
List<QueryResult> allResults = new ArrayList<>();
for (long i = 0;; ++i) {
ItemIterable<QueryResult> pageResults = results.skipTo(itemsPerPage * i).getPage();
for (QueryResult result : pageResults) {
allResults.add(result);
}
/*
* The getHasMoreItems method call _should_ be sufficient; however, I have observed in Chemistry 0.7.0
* instances of "infinite looping" in certain circumstances, and adding this additional check that
* the number of page items is less than 1 seems will prevent this from happening.
*/
if (!pageResults.getHasMoreItems() || pageResults.getPageNumItems() < 1) {
break;
}
}
return allResults;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment