Search in sources :

Example 16 with QueryFilter

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);
    }
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) ParseGroovyFromString(com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString) IncludeServletAsString(com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)

Example 17 with QueryFilter

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;
}
Also used : Query(org.xwiki.query.Query) SecureQuery(org.xwiki.query.SecureQuery) QueryException(org.xwiki.query.QueryException) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) QueryFilter(org.xwiki.query.QueryFilter) QueryManager(org.xwiki.query.QueryManager) Map(java.util.Map)

Example 18 with QueryFilter

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;
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 19 with QueryFilter

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;
}
Also used : Query(org.xwiki.query.Query) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) XWikiException(com.xpn.xwiki.XWikiException)

Example 20 with QueryFilter

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;
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

QueryFilter (org.xwiki.query.QueryFilter)20 Query (org.xwiki.query.Query)15 Test (org.junit.Test)8 QueryException (org.xwiki.query.QueryException)7 SQLQuery (org.hibernate.SQLQuery)5 WrappingQuery (org.xwiki.query.WrappingQuery)5 XWikiException (com.xpn.xwiki.XWikiException)4 Session (org.hibernate.Session)4 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 SecureQuery (org.xwiki.query.SecureQuery)4 QueryManager (org.xwiki.query.QueryManager)3 DefaultQuery (org.xwiki.query.internal.DefaultQuery)3 Document (com.xpn.xwiki.api.Document)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)2 Pair (org.apache.commons.lang3.tuple.Pair)2 XWikiRestException (org.xwiki.rest.XWikiRestException)2