/* (non-Javadoc)
* @see org.jahia.services.search.SearchProvider#search(org.jahia.services.search.SearchCriteria, org.jahia.params.ProcessingContext)
*/
public SearchResponse search(SearchCriteria criteria, RenderContext context) {
SearchResponse response = new SearchResponse();
List<Hit<?>> results = new LinkedList<Hit<?>>();
Set<String> addedHits = new HashSet<String>();
Set<String> addedNodes = new HashSet<String>();
try {
JCRSessionWrapper session = ServicesRegistry
.getInstance()
.getJCRStoreService()
.getSessionFactory()
.getCurrentUserSession(context.getMainResource().getWorkspace(),
context.getMainResource().getLocale(), context.getFallbackLocale());
Query query = buildQuery(criteria, session);
if (query != null) {
if (logger.isDebugEnabled()) {
logger.debug("Executing search query [{}]", query.getStatement());
}
QueryResult queryResult = query.execute();
RowIterator it = queryResult.getRows();
Set<String> languages = new HashSet<String>();
if (it.hasNext()) {
if (!criteria.getLanguages().isEmpty()) {
for (String languageCode : criteria.getLanguages().getValues()) {
if (!StringUtils.isEmpty(languageCode)) {
languages.add(languageCode);
}
}
} else {
if (session.getLocale() != null) {
languages.add(session.getLocale().toString());
}
}
}
while (it.hasNext()) {
try {
Row row = it.nextRow();
JCRNodeWrapper node = (JCRNodeWrapper) row.getNode();
if (node.isNodeType(Constants.JAHIANT_TRANSLATION)
|| Constants.JCR_CONTENT.equals(node.getName())) {
node = node.getParent();
}
if (addedNodes.add(node.getIdentifier()) && !isNodeToSkip(node, criteria, languages)) {
Hit<?> hit = buildHit(row, node, context);
SearchServiceImpl.executeURLModificationRules(hit, context);
if (addedHits.add(hit.getLink())) {
results.add(hit);
}
}
} catch (Exception e) {
logger.warn("Error resolving search hit", e);
}
}
}
} catch (RepositoryException e) {
if (e.getMessage() != null && e.getMessage().contains(ParseException.class.getName())) {
logger.warn(e.getMessage());
if (logger.isDebugEnabled()) {
logger.debug(e.getMessage(), e);
}
} else {
logger.error("Error while trying to perform a search", e);
}
} catch (Exception e) {
logger.error("Error while trying to perform a search", e);
}
if (logger.isDebugEnabled()) {
logger.debug("Search query has {} results", results.size());
}
response.setResults(results);
return response;
}