Apache CMIS: swap request result

I recently started using Apache CMIS and read the official documentation and examples. I didn't notice anything about the search results.

There is an example showing how to enumerate folder items by setting maxItemsPerPage using operationContext, but it seems that operationContext can be used inside the getChilder method:

int maxItemsPerPage = 5;
int skipCount = 10;
CmisObject object = session.getObject(session.createObjectId(folderId));
Folder folder = (Folder) object;
OperationContext operationContext = session.createOperationContext();
operationContext.setMaxItemsPerPage(maxItemsPerPage);
ItemIterable<CmisObject> children = folder.getChildren(operationContext);
ItemIterable<CmisObject> page = children.skipTo(skipCount).getPage();

      

This is ok when it comes to listing the u folder. But my business is getting the results of a custom search query. Basic approach:

String myType = "my:documentType";
ObjectType type = session.getTypeDefinition(myType);
PropertyDefinition<?> objectIdPropDef = type.getPropertyDefinitions().get(PropertyIds.OBJECT_ID);
String objectIdQueryName = objectIdPropDef.getQueryName();
String queryString = "SELECT " + objectIdQueryName + " FROM " + type.getQueryName();
ItemIterable<QueryResult> results = session.query(queryString, false);
for (QueryResult qResult : results) {
    String objectId = qResult.getPropertyValueByQueryName(objectIdQueryName);
    Document doc = (Document) session.getObject(session.createObjectId(objectId));
}

      

This approach will fetch all documents in queryResult, but I would like to include startIndex and limit. The idea would be as follows:

ItemIterable<QueryResult> results = session.query(queryString, false).skipTo(startIndex).getPage(limit);

      

I'm not sure about this part: getPage (limit). Is this the correct swap approach? Also I would like to get the total number of items, so I might know how to set up the maximum items in the grid where my items will appear. There is a method, but something strange is written in the docs, for example sometimes the repository cannot know about the maximum items. This is the method:

results.getTotalNumItems();

      

I've tried something like:

SELECT COUNT(*)...

      

but it did not help:)

Could you please give me some advice on how to make the correct paging from the query result?

Thanks in advance.

+1


source to share


1 answer


Query returns the same ItemIterable as getChildren, so you can display the result set returned by the query, just as you can create the result set returned by getChildren.

Let's say you have a results page that shows 20 items per page. Consider this snippet that I run in the Groovy Console in OpenCMIS Workbench for a folder with 149 files named testN.txt:

int PAGE_NUM = 1
int PAGE_SIZE = 20
String queryString = "SELECT cmis:name FROM cmis:document where cmis:name like 'test%.txt'"

ItemIterable<QueryResult> results = session.query(queryString, false, operationContext).skipTo(PAGE_NUM * PAGE_SIZE).getPage(PAGE_SIZE)

println "Total items:" + results.getTotalNumItems()

for (QueryResult result : results) {
   println result.getPropertyValueByQueryName("cmis:name")
}

println results.getHasMoreItems()

      



When you run it with PAGE_NUM = 1, you get 20 results and the last println statement will return true. Also note that the first println will print 149, the total number of documents matching the search term, but as you point out, not all servers know how to return this.

If you rerun this with PAGE_NUM = 7 you get 9 results and the last println will return false because you are at the end of the list.

If you want to see a working search page that uses OpenCMIS and simple servlets and JSP pages, check out the SearchServlet class in The Blend , a sample web page application that ships with the CMIS book and Apache Chemistry in Action.

+6


source







All Articles