public Suggestion suggest(String originalQuery, String siteKey, Locale locale) {
if (StringUtils.isBlank(originalQuery)) {
return null;
}
Suggestion suggestion = null;
JCRSessionWrapper session;
try {
session = ServicesRegistry.getInstance().getJCRStoreService().getSessionFactory().getCurrentUserSession(
null, locale);
QueryManager qm = session.getWorkspace().getQueryManager();
StringBuilder xpath = new StringBuilder(64);
xpath.append("/jcr:root[rep:spellcheck(").append(stringToJCRSearchExp(originalQuery)).append(")");
if (locale != null) {
xpath.append(" or @jcr:language='" + locale + "'");
}
xpath.append("]");
if (siteKey != null) {
xpath.append("/sites/").append(siteKey);
}
xpath.append("/(rep:spellcheck())");
Query query = qm.createQuery(xpath.toString(), Query.XPATH);
RowIterator rows = query.execute().getRows();
if (rows.hasNext()) {
Row r = rows.nextRow();
Value v = r.getValue("rep:spellcheck()");
if (v != null) {
suggestion = new Suggestion(originalQuery, v.getString());
}
if (logger.isDebugEnabled()) {
logger.debug("Making spell check suggestion for '" + originalQuery + "' site '"
+ siteKey + "' and locale '" + locale + "' using XPath query ["
+ xpath.toString() + "]. Result suggestion: " + suggestion);