use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class XWiki method refreshLinks.
public void refreshLinks(XWikiContext context) throws XWikiException {
try {
// refreshes all Links of each doc of the wiki
@SuppressWarnings("deprecation") List<String> docs = getStore().getQueryManager().getNamedQuery("getAllDocuments").addFilter(Utils.<QueryFilter>getComponent(QueryFilter.class, "hidden")).execute();
for (int i = 0; i < docs.size(); i++) {
XWikiDocument myDoc = this.getDocument(docs.get(i), context);
myDoc.getStore().saveLinks(myDoc, context, true);
}
} catch (QueryException ex) {
throw new XWikiException(0, 0, ex.getMessage(), ex);
}
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class ScriptQuery method count.
/**
* Allow to retrieve the total count of items for the given query instead of the actual results. This method will
* only work for queries selecting document full names, see {@link CountDocumentFilter} for more information.
*
* @return the total number of results for this query.
*/
public long count() {
long result = -1;
try {
// Create a copy of the wrapped query.
QueryManager queryManager = (QueryManager) this.componentManager.getInstance(QueryManager.class);
Query countQuery = queryManager.createQuery(getStatement(), getLanguage());
countQuery.setWiki(getWiki());
for (Map.Entry<Integer, Object> entry : getPositionalParameters().entrySet()) {
countQuery.bindValue(entry.getKey(), entry.getValue());
}
for (Map.Entry<String, Object> entry : getNamedParameters().entrySet()) {
countQuery.bindValue(entry.getKey(), entry.getValue());
}
for (QueryFilter filter : getFilters()) {
countQuery.addFilter(filter);
}
// Add the count filter to it.
countQuery.addFilter(this.componentManager.<QueryFilter>getInstance(QueryFilter.class, "count"));
// Execute and retrieve the count result.
List<Long> results = countQuery.execute();
result = results.get(0);
} catch (Exception e) {
LOGGER.warn("Failed to create count query for query [{}]", getStatement());
e.printStackTrace();
}
return result;
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class ScriptQuery method addFilter.
/**
* Set a filter in the wrapped query from the filter component hint.
*
* @param filter the hint of the component filter to set in the wrapped query.
* @return this query object.
*/
public Query addFilter(String filter) {
if (!StringUtils.isBlank(filter)) {
try {
QueryFilter queryFilter = this.componentManager.getInstance(QueryFilter.class, filter);
addFilter(queryFilter);
} catch (ComponentLookupException e) {
// We need to avoid throwing exceptions in the wiki if the filter does not exist.
LOGGER.warn("Failed to load QueryFilter with component hint [{}]", filter);
}
}
return this;
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class TagQueryUtils method getTagCountForQuery.
/**
* Get cardinality map of tags matching a parameterized hql query.
*
* @param fromHql the <code>from</code> fragment of the hql query
* @param whereHql the <code>where</code> fragment of the hql query
* @param parameterValues list of parameter values for the query
* @param context XWiki context.
* @return map of tags (alphabetical order) with their occurrences counts.
* @throws XWikiException if search query fails (possible failures: DB access problems, etc).
* @since 1.18
* @see TagPluginApi#getTagCountForQuery(String, String, java.util.List)
*/
public static Map<String, Integer> getTagCountForQuery(String fromHql, String whereHql, List<?> parameterValues, XWikiContext context) throws XWikiException {
List<String> results = null;
Map<String, Integer> tagCount = new TreeMap<String, Integer>(String.CASE_INSENSITIVE_ORDER);
String from = "select elements(prop.list) from XWikiDocument as doc, BaseObject as tagobject, " + "DBStringListProperty as prop";
String where = " where tagobject.name=doc.fullName and tagobject.className='XWiki.TagClass' and " + "tagobject.id=prop.id.id and prop.id.name='tags' and doc.translation=0";
// If at least one of the fragments is passed, the query should be matching XWiki documents
if (!StringUtils.isBlank(fromHql) || !StringUtils.isBlank(whereHql)) {
from += fromHql;
}
if (!StringUtils.isBlank(whereHql)) {
where += " and " + whereHql;
}
List<?> params = parameterValues;
if (params == null) {
params = new ArrayList<String>();
}
String hql = from + where;
try {
Query query = context.getWiki().getStore().getQueryManager().createQuery(hql, Query.HQL);
query.bindValues((List<Object>) params);
query.addFilter(Utils.<QueryFilter>getComponent(QueryFilter.class, HiddenDocumentFilter.HINT));
results = query.execute();
} catch (QueryException e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, String.format("Failed to get tag count for query [%s], with parameters [%s]", hql, params.toString()), e);
}
Collections.sort(results, String.CASE_INSENSITIVE_ORDER);
Map<String, String> processedTags = new HashMap<String, String>();
// We have to manually build a cardinality map since we have to ignore tags case.
for (String result : results) {
// This key allows to keep track of the case variants we've encountered.
String lowerTag = result.toLowerCase();
// We store the first case variant to reuse it in the final result set.
if (!processedTags.containsKey(lowerTag)) {
processedTags.put(lowerTag, result);
}
String tagCountKey = processedTags.get(lowerTag);
int tagCountForTag = 0;
if (tagCount.get(tagCountKey) != null) {
tagCountForTag = tagCount.get(tagCountKey);
}
tagCount.put(tagCountKey, tagCountForTag + 1);
}
return tagCount;
}
use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.
the class TagQueryUtils method getAllTags.
/**
* Get all tags within the wiki.
*
* @param context XWiki context.
* @return list of tags (alphabetical order).
* @throws com.xpn.xwiki.XWikiException if search query fails (possible failures: DB access problems, etc).
*/
public static List<String> getAllTags(XWikiContext context) throws XWikiException {
List<String> results;
String hql = "select distinct elements(prop.list) from XWikiDocument as doc, BaseObject as obj, " + "DBStringListProperty as prop where obj.name=doc.fullName and obj.className='XWiki.TagClass' and " + "obj.id=prop.id.id and prop.id.name='tags'";
try {
Query query = context.getWiki().getStore().getQueryManager().createQuery(hql, Query.HQL);
query.addFilter(Utils.<QueryFilter>getComponent(QueryFilter.class, HiddenDocumentFilter.HINT));
results = query.execute();
} catch (QueryException e) {
throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_UNKNOWN, String.format("Failed to get all tags", hql), e);
}
Collections.sort(results, String.CASE_INSENSITIVE_ORDER);
return results;
}
Aggregations