Search in sources :

Example 51 with QueryException

use of org.xwiki.query.QueryException 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 52 with QueryException

use of org.xwiki.query.QueryException 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)

Example 53 with QueryException

use of org.xwiki.query.QueryException in project celements-blog by celements.

the class BlogCacheTest method expectXWQL.

private void expectXWQL(WikiReference wikiRef, List<DocumentReference> ret) throws Exception {
    Query queryMock = createMockAndAddToDefault(Query.class);
    String xwql = "select distinct doc.fullName from Document doc, doc.object(" + "Celements2.BlogConfigClass) as obj";
    expect(queryManagerMock.createQuery(eq(xwql), eq(Query.XWQL))).andReturn(queryMock).once();
    expect(queryMock.setWiki(eq(wikiRef.getName()))).andReturn(queryMock).once();
    IExpectationSetters<List<DocumentReference>> expSetter = expect(queryExecServiceMock.executeAndGetDocRefs(same(queryMock)));
    if (ret != null) {
        expSetter.andReturn(ret).once();
    } else {
        expSetter.andThrow(new QueryException("", null, null)).once();
    }
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) List(java.util.List)

Example 54 with QueryException

use of org.xwiki.query.QueryException in project celements-blog by celements.

the class BlogService method getArticles.

@Override
public List<Article> getArticles(DocumentReference blogConfDocRef, ArticleLoadParameter param) throws ArticleLoadException {
    try {
        if (param == null) {
            param = new ArticleLoadParameter();
        }
        param.setExecutionDate(new Date());
        param.setBlogDocRef(blogConfDocRef);
        param.setSubscribedToBlogs(getSubribedToBlogs(blogConfDocRef));
        List<Article> articles = getArticleEngine().getArticles(param);
        LOGGER.info("getArticles: for " + param + " got " + articles.size() + " articles");
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("getArticles: for " + param + " got: " + articles);
        }
        return Collections.unmodifiableList(articles);
    } catch (XWikiException xwe) {
        throw new ArticleLoadException("Error for '" + blogConfDocRef + "'", xwe);
    } catch (QueryException qexc) {
        throw new ArticleLoadException("Error for '" + blogConfDocRef + "'", qexc);
    }
}
Also used : ArticleLoadParameter(com.celements.blog.article.ArticleLoadParameter) QueryException(org.xwiki.query.QueryException) Article(com.celements.blog.article.Article) ArticleLoadException(com.celements.blog.article.ArticleLoadException) Date(java.util.Date) XWikiException(com.xpn.xwiki.XWikiException)

Example 55 with QueryException

use of org.xwiki.query.QueryException in project celements-blog by celements.

the class BlogPlugin method getBlogArticles.

/**
 * @deprecated since 1.32 instead use
 *             {@link BlogService#getArticles(DocumentReference, ArticleLoadParameter)}
 * @param blogArticleSpace
 *          Space where the blog's articles are saved.
 * @param subscribedBlogsStr
 *          Comma separated String with all the blog article spaces the blog has subscribed to.
 * @param language
 *          default language
 * @param archiveOnly
 *          Only get articles from the archive (archivedate < now)
 * @param futurOnly
 *          Only get articles that are not yet published (publishdate > now)
 * @param subscribableOnly
 *          Only get articles from subscribed blogs, but not the ones from the blog the user is
 *          on.
 * @param withArchive
 *          Include archived articles in the answer. Has no effect if archiveOnly = true.
 * @param withFutur
 *          Include not yet published articles. Only possible if the page has been saved with
 *          programmingrights or the user has edit right on the article. Has no effect if
 *          futurOnly = true.
 * @param withSubscribable
 *          Include articles from subscribed blogs.
 * @param withSubscribed
 *          Include articles the blog has subscribed to.
 * @param withUnsubscribed
 *          Include articles the blog has unsubscribed from. Only works with edit rights or
 *          programmingrights.
 * @param withUndecided
 *          Include articles the blog has not yet desided about a subscription. Only works with
 *          edit rights or programmingrights.
 * @param checkAccessRights
 *          Do pay attention to the rights. Default = true if no programmingrights.
 * @param context
 * @return
 * @throws XWikiException
 */
@Deprecated
public List<Article> getBlogArticles(String blogArticleSpace, String subscribedBlogsStr, String language, boolean archiveOnly, boolean futurOnly, boolean subscribableOnly, boolean withArchive, boolean withFutur, boolean withSubscribable, boolean withSubscribed, boolean withUnsubscribed, boolean withUndecided, boolean checkAccessRights, XWikiContext context) throws ArticleLoadException {
    try {
        SpaceReference spaceRef = new SpaceReference(blogArticleSpace, new WikiReference(context.getDatabase()));
        DocumentReference blogConfDocRef = getBlogService().getBlogConfigDocRef(spaceRef);
        ArticleLoadParameter param = new ArticleLoadParameter();
        param.setBlogDocRef(blogConfDocRef);
        param.setWithBlogArticles(!subscribableOnly);
        param.setLanguage(language);
        if (withSubscribable) {
            param.setSubscriptionModes(getSubsModes(withSubscribed, withUnsubscribed, withUndecided));
        }
        param.setDateModes(getDateModes(archiveOnly, futurOnly, withArchive, withFutur));
        LOGGER.debug("Got " + param + "' for: blogArticleSpace=" + blogArticleSpace + ", subscribedBlogs=" + subscribedBlogsStr + ", language=" + language + ", archiveOnly=" + archiveOnly + ", futurOnly=" + futurOnly + ", withArchive=" + withArchive + ", withFutur=" + withFutur + ", subscribableOnly=" + subscribableOnly + ", withSubscribable=" + withSubscribable + ", withSubscribed=" + withSubscribed + ", withUnsubscribed=" + withUnsubscribed + ", withUndecided=" + withUndecided + ", checkAccessRights=" + checkAccessRights);
        return getBlogService().getArticles(blogConfDocRef, param);
    } catch (XWikiException xwe) {
        throw new ArticleLoadException(xwe);
    } catch (QueryException qexc) {
        throw new ArticleLoadException(qexc);
    }
}
Also used : ArticleLoadParameter(com.celements.blog.article.ArticleLoadParameter) QueryException(org.xwiki.query.QueryException) SpaceReference(org.xwiki.model.reference.SpaceReference) WikiReference(org.xwiki.model.reference.WikiReference) ArticleLoadException(com.celements.blog.article.ArticleLoadException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException)

Aggregations

QueryException (org.xwiki.query.QueryException)57 Query (org.xwiki.query.Query)32 DocumentReference (org.xwiki.model.reference.DocumentReference)19 XWikiException (com.xpn.xwiki.XWikiException)18 ArrayList (java.util.ArrayList)14 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 BaseObject (com.xpn.xwiki.objects.BaseObject)8 Test (org.junit.Test)8 QueryFilter (org.xwiki.query.QueryFilter)7 XWikiContext (com.xpn.xwiki.XWikiContext)6 HashMap (java.util.HashMap)6 XWikiRestException (org.xwiki.rest.XWikiRestException)6 Event (org.xwiki.eventstream.Event)5 WikiReference (org.xwiki.model.reference.WikiReference)5 Date (java.util.Date)4 List (java.util.List)4 IconException (org.xwiki.icon.IconException)4 QueryManager (org.xwiki.query.QueryManager)4 SecureQuery (org.xwiki.query.SecureQuery)4 XWiki (com.xpn.xwiki.XWiki)3