* @return the {@link Query} instance, created from the provided search criteria
* @throws RepositoryException
* @throws InvalidQueryException
*/
private Query createQuery(final GWTJahiaSearchQuery gwtQuery, int limit, int offset, JCRSessionWrapper session) throws InvalidQueryException, RepositoryException {
SearchCriteria criteria = new SearchCriteria();
if (offset > 0) {
criteria.setOffset(offset);
}
if (limit > 0) {
criteria.setLimit(limit);
}
// page path
if (gwtQuery.getPages() != null && !gwtQuery.getPages().isEmpty()) {
criteria.getPagePath().setValue(gwtQuery.getPages().get(0).getPath());
criteria.getPagePath().setIncludeChildren(true);
}
// nodeType
if (gwtQuery.getNodeTypes() != null && gwtQuery.getNodeTypes().size() == 1) {
criteria.setNodeType(gwtQuery.getNodeTypes().get(0));
gwtQuery.getNodeTypes().clear();
}
// language
if (gwtQuery.getLanguage() != null && gwtQuery.getLanguage().getLanguage() != null) {
criteria.getLanguages().setValue(gwtQuery.getLanguage().getLanguage());
}
// // category
// if (gwtQuery.getCategories() != null && !gwtQuery.getCategories().isEmpty()) {
// criteria.getLanguages().setValue(gwtQuery.getLanguage().getLanguage());
// }
//
// query string
if (gwtQuery.getQuery() != null && gwtQuery.getQuery().length() > 0) {
criteria.getTerms().get(0).setTerm(gwtQuery.getQuery());
SearchFields fields = criteria.getTerms().get(0).getFields();
fields.setSiteContent(gwtQuery.isInContents());
fields.setFilename(gwtQuery.isInName());
fields.setFileContent(gwtQuery.isInFiles());
fields.setTitle(gwtQuery.isInMetadatas());
fields.setDescription(gwtQuery.isInMetadatas());
fields.setKeywords(gwtQuery.isInMetadatas());
fields.setTags(gwtQuery.isInTags());
}
if (gwtQuery.getOriginSiteUuid() != null) {
String siteKey = JCRTemplate.getInstance().doExecuteWithSystemSession(new JCRCallback<String>() {
public String doInJCR(JCRSessionWrapper session) throws RepositoryException {
try {
JCRNodeWrapper nodeWrapper = session.getNodeByIdentifier(gwtQuery.getOriginSiteUuid());
return nodeWrapper.getName();
} catch (ItemNotFoundException e) {
logger.error("Unable for find site node by UUID: " + gwtQuery.getOriginSiteUuid(), e);
}
return null;
}
});
if (siteKey != null) {
criteria.setOriginSiteKey(siteKey);
}
}
return jcrSearchProvider.buildQuery(criteria, session);