Search in sources :

Example 11 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class DefaultWikiDescriptorDocumentHelper method getAllXWikiServerClassDocumentNames.

@Override
public List<String> getAllXWikiServerClassDocumentNames() throws WikiManagerException {
    WikiDescriptorManager wikiDescriptorManager = wikiDescriptorManagerProvider.get();
    try {
        Query query = this.queryManager.createQuery("from doc.object(XWiki.XWikiServerClass) as descriptor where doc.name like 'XWikiServer%' " + "and doc.fullName <> 'XWiki.XWikiServerClassTemplate'", Query.XWQL);
        query.setWiki(wikiDescriptorManager.getMainWikiId());
        query.addFilter(componentManager.<QueryFilter>getInstance(QueryFilter.class, "unique"));
        return query.execute();
    } catch (Exception e) {
        throw new WikiManagerException("Failed to locate XWiki.XWikiServerClass documents", e);
    }
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) Query(org.xwiki.query.Query) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) XWikiException(com.xpn.xwiki.XWikiException) QueryException(org.xwiki.query.QueryException) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException)

Example 12 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class HqlQueryExecutor method addEscapeLikeParametersFilter.

private void addEscapeLikeParametersFilter(Query query) {
    if (!hasQueryParametersType(query)) {
        return;
    }
    // Find the component class for the "escapeLikeParameters" filter
    QueryFilter escapeFilter;
    try {
        escapeFilter = this.componentManagerProvider.get().getInstance(QueryFilter.class, ESCAPE_LIKE_PARAMETERS_FILTER);
    } catch (ComponentLookupException e) {
        // Shouldn't happen!
        throw new RuntimeException(String.format("Failed to locate [%s] Query Filter", ESCAPE_LIKE_PARAMETERS_FILTER), e);
    }
    boolean found = false;
    for (QueryFilter filter : query.getFilters()) {
        if (escapeFilter.getClass().getName().equals(filter.getClass().getName())) {
            found = true;
            break;
        }
    }
    if (!found) {
        query.addFilter(escapeFilter);
    }
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException)

Example 13 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class HqlQueryExecutor method execute.

@Override
public <T> List<T> execute(final Query query) throws QueryException {
    // Make sure the query is allowed in the current context
    checkAllowed(query);
    String oldDatabase = getContext().getWikiId();
    try {
        this.progress.startStep(query, "query.hql.progress.execute", "Execute HQL query [{}]", query);
        if (query.getWiki() != null) {
            getContext().setWikiId(query.getWiki());
        }
        return getStore().executeRead(getContext(), new HibernateCallback<List<T>>() {

            @SuppressWarnings("unchecked")
            @Override
            public List<T> doInHibernate(Session session) {
                org.hibernate.Query hquery = createHibernateQuery(session, query);
                List<T> results = hquery.list();
                if (query.getFilters() != null && !query.getFilters().isEmpty()) {
                    for (QueryFilter filter : query.getFilters()) {
                        results = filter.filterResults(results);
                    }
                }
                return results;
            }
        });
    } catch (XWikiException e) {
        throw new QueryException("Exception while executing query", query, e);
    } finally {
        getContext().setWikiId(oldDatabase);
        this.progress.endStep(query);
    }
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) WrappingQuery(org.xwiki.query.WrappingQuery) SecureQuery(org.xwiki.query.SecureQuery) List(java.util.List) XWikiException(com.xpn.xwiki.XWikiException) Session(org.hibernate.Session)

Example 14 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class SpacesResourceImpl method getSpaces.

@Override
public Spaces getSpaces(String wikiName, Integer start, Integer number) throws XWikiRestException {
    Spaces spaces = objectFactory.createSpaces();
    try {
        List<String> spaceNames = queryManager.getNamedQuery("getSpaces").addFilter(componentManager.<QueryFilter>getInstance(QueryFilter.class, "hidden")).setOffset(start).setLimit(number).setWiki(wikiName).execute();
        for (String spaceName : spaceNames) {
            List<String> spaceList = Utils.getSpacesFromSpaceId(spaceName);
            String homeId = Utils.getPageId(wikiName, spaceList, "WebHome");
            Document home = null;
            XWiki xwikiApi = Utils.getXWikiApi(componentManager);
            if (xwikiApi.hasAccessLevel("view", homeId)) {
                if (xwikiApi.exists(homeId)) {
                    home = Utils.getXWikiApi(componentManager).getDocument(homeId);
                }
                spaces.getSpaces().add(DomainObjectFactory.createSpace(objectFactory, uriInfo.getBaseUri(), wikiName, spaceList, home));
            }
        }
    } catch (Exception e) {
        throw new XWikiRestException(e);
    }
    return spaces;
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) XWikiRestException(org.xwiki.rest.XWikiRestException) Spaces(org.xwiki.rest.model.jaxb.Spaces) XWiki(com.xpn.xwiki.api.XWiki) Document(com.xpn.xwiki.api.Document) XWikiRestException(org.xwiki.rest.XWikiRestException)

Example 15 with QueryFilter

use of org.xwiki.query.QueryFilter in project xwiki-platform by xwiki.

the class PagesResourceImpl method getPages.

@Override
public Pages getPages(String wikiName, String spaceName, Integer start, Integer number, String parentFilterExpression, String order, Boolean withPrettyNames) throws XWikiRestException {
    String database = Utils.getXWikiContext(componentManager).getWikiId();
    List<String> spaces = parseSpaceSegments(spaceName);
    String spaceId = Utils.getLocalSpaceId(spaces);
    Pages pages = objectFactory.createPages();
    try {
        Utils.getXWikiContext(componentManager).setWikiId(wikiName);
        Query query = ("date".equals(order)) ? queryManager.createQuery("select doc.name from Document doc where doc.space=:space and language='' order by doc.date desc", "xwql") : queryManager.getNamedQuery("getSpaceDocsName");
        /* Use an explicit query to improve performance */
        List<String> pageNames = query.addFilter(componentManager.<QueryFilter>getInstance(QueryFilter.class, "hidden")).bindValue("space", spaceId).setOffset(start).setLimit(number).execute();
        Pattern parentFilter = null;
        if (parentFilterExpression != null) {
            if (parentFilterExpression.equals("null")) {
                parentFilter = Pattern.compile("");
            } else {
                parentFilter = Pattern.compile(parentFilterExpression);
            }
        }
        for (String pageName : pageNames) {
            String pageFullName = Utils.getPageId(wikiName, spaces, pageName);
            if (!Utils.getXWikiApi(componentManager).exists(pageFullName)) {
                getLogger().warn("Page [{}] appears to be in space [{}] but no information is available.", pageName, spaceId);
            } else {
                Document doc = Utils.getXWikiApi(componentManager).getDocument(pageFullName);
                /* We only add pages we have the right to access */
                if (doc != null) {
                    boolean add = true;
                    Document parent = Utils.getParentDocument(doc, Utils.getXWikiApi(componentManager));
                    if (parentFilter != null) {
                        String parentId = "";
                        if (parent != null && !parent.isNew()) {
                            parentId = parent.getPrefixedFullName();
                        }
                        add = parentFilter.matcher(parentId).matches();
                    }
                    if (add) {
                        pages.getPageSummaries().add(DomainObjectFactory.createPageSummary(objectFactory, uriInfo.getBaseUri(), doc, Utils.getXWikiApi(componentManager), withPrettyNames));
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new XWikiRestException(e);
    } finally {
        Utils.getXWikiContext(componentManager).setWikiId(database);
    }
    return pages;
}
Also used : Pages(org.xwiki.rest.model.jaxb.Pages) Pattern(java.util.regex.Pattern) QueryFilter(org.xwiki.query.QueryFilter) Query(org.xwiki.query.Query) XWikiRestException(org.xwiki.rest.XWikiRestException) Document(com.xpn.xwiki.api.Document) XWikiRestException(org.xwiki.rest.XWikiRestException)

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