Search in sources :

Example 51 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit by apache.

the class QueryResultTest method testIteratorNext.

public void testIteratorNext() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000]";
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        int size = 0;
        for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
            it.nextNode();
            size++;
        }
        assertEquals("Wrong size of NodeIterator in result", INITIAL_NODE_NUM - i, size);
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager)

Example 52 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit by apache.

the class GQL method executeJcrQuery.

private RowIterator executeJcrQuery(String jcrQuery, String jcrQueryLanguage) {
    try {
        QueryManager qm = session.getWorkspace().getQueryManager();
        RowIterator nodes = qm.createQuery(jcrQuery, jcrQueryLanguage).execute().getRows();
        if (filter != null) {
            nodes = new FilteredRowIterator(nodes);
        }
        if (offset > 0) {
            try {
                nodes.skip(offset);
            } catch (NoSuchElementException e) {
                return RowIteratorAdapter.EMPTY;
            }
        }
        if (numResults == Integer.MAX_VALUE) {
            return new RowIterAdapter(nodes, nodes.getSize());
        }
        List<Row> resultRows = new ArrayList<Row>();
        while (numResults-- > 0 && nodes.hasNext()) {
            resultRows.add(nodes.nextRow());
        }
        return new RowIterAdapter(resultRows, resultRows.size());
    } catch (RepositoryException e) {
        // in case of error return empty result
        return RowIteratorAdapter.EMPTY;
    }
}
Also used : RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Row(javax.jcr.query.Row) NoSuchElementException(java.util.NoSuchElementException)

Example 53 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit by apache.

the class SearchResourceImpl method getQueryGrammerSet.

//-------------------------------------------< SearchResource interface >---
/**
     * @see SearchResource#getQueryGrammerSet()
     */
public QueryGrammerSet getQueryGrammerSet() {
    QueryGrammerSet qgs = new QueryGrammerSet();
    try {
        QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();
        String[] langs = qMgr.getSupportedQueryLanguages();
        for (String lang : langs) {
            // Note: Existing clients already assume that the
            // query languages returned in the DASL header are
            // not prefixed with any namespace, so we probably
            // shouldn't use an explicit namespace here.
            qgs.addQueryLanguage(lang, Namespace.EMPTY_NAMESPACE);
        }
    } catch (RepositoryException e) {
        log.debug(e.getMessage());
    }
    return qgs;
}
Also used : QueryGrammerSet(org.apache.jackrabbit.webdav.search.QueryGrammerSet) QueryManager(javax.jcr.query.QueryManager) RepositoryException(javax.jcr.RepositoryException)

Example 54 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit by apache.

the class SearchResourceImpl method getQuery.

/**
     * Create a query from the information present in the <code>sInfo</code>
     * object.<br>The following JCR specific logic is applied:
     * <ul>
     * <li>If the requested resource represents a node with nodetype nt:query, the
     * request body is ignored and the query defined with the node is executed
     * instead.</li>
     * <li>If the requested resource does not represent an existing item, the
     * specified query is persisted by calling {@link Query#storeAsNode(String)}.</li>
     * </ul>
     * @param sInfo defining the query to be executed
     * @return <code>Query</code> object.
     * @throws javax.jcr.query.InvalidQueryException if the query defined by <code>sInfo</code> is invalid
     * @throws RepositoryException the query manager cannot be accessed or if
     * another error occurs.
     * @throws DavException if <code>sInfo</code> is <code>null</code> and
     * the underlying repository item is not an nt:query node or if an error
     * occurs when calling {@link Query#storeAsNode(String)}/
     */
private Query getQuery(SearchInfo sInfo) throws InvalidQueryException, RepositoryException, DavException {
    Session session = getRepositorySession();
    NamespaceRegistry nsReg = session.getWorkspace().getNamespaceRegistry();
    Node rootNode = session.getRootNode();
    QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();
    // test if query is defined by requested repository node
    String itemPath = locator.getRepositoryPath();
    if (itemPath != null && !rootNode.getPath().equals(itemPath)) {
        String qNodeRelPath = itemPath.substring(1);
        if (rootNode.hasNode(qNodeRelPath)) {
            Node qNode = rootNode.getNode(qNodeRelPath);
            if (qNode.isNodeType(JcrConstants.NT_QUERY)) {
                return qMgr.getQuery(qNode);
            }
        }
    }
    Query q;
    if (sInfo != null) {
        // apply namespace mappings to session
        Map<String, String> namespaces = sInfo.getNamespaces();
        try {
            for (Map.Entry<String, String> entry : namespaces.entrySet()) {
                String prefix = entry.getKey();
                String uri = entry.getValue();
                session.setNamespacePrefix(prefix, uri);
            }
            q = qMgr.createQuery(sInfo.getQuery(), sInfo.getLanguageName());
            if (SearchInfo.NRESULTS_UNDEFINED != sInfo.getNumberResults()) {
                q.setLimit(sInfo.getNumberResults());
            }
            if (SearchInfo.OFFSET_UNDEFINED != sInfo.getOffset()) {
                q.setOffset(sInfo.getOffset());
            }
        } finally {
            // reset namespace mappings
            for (String uri : namespaces.values()) {
                try {
                    session.setNamespacePrefix(nsReg.getPrefix(uri), uri);
                } catch (RepositoryException e) {
                    log.warn("Unable to reset mapping of namespace: " + uri);
                }
            }
        }
    } else {
        throw new DavException(DavServletResponse.SC_BAD_REQUEST, locator.getResourcePath() + " is not a nt:query node -> searchRequest body required.");
    }
    /* test if resource path does not exist -> thus indicating that
        the query must be made persistent by calling Query.save(String) */
    if (itemPath != null && !getRepositorySession().itemExists(itemPath)) {
        try {
            q.storeAsNode(itemPath);
        } catch (RepositoryException e) {
            // ItemExistsException should never occur.
            throw new JcrDavException(e);
        }
    }
    return q;
}
Also used : JcrDavException(org.apache.jackrabbit.webdav.jcr.JcrDavException) NamespaceRegistry(javax.jcr.NamespaceRegistry) Query(javax.jcr.query.Query) DavException(org.apache.jackrabbit.webdav.DavException) JcrDavException(org.apache.jackrabbit.webdav.jcr.JcrDavException) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) RepositoryException(javax.jcr.RepositoryException) Map(java.util.Map) JcrDavSession(org.apache.jackrabbit.webdav.jcr.JcrDavSession) Session(javax.jcr.Session)

Example 55 with QueryManager

use of javax.jcr.query.QueryManager in project jackrabbit-oak by apache.

the class LuceneIndexDescendantSuggestionTest method getSuggestions.

private Set<String> getSuggestions(String query) throws Exception {
    QueryManager queryManager = session.getWorkspace().getQueryManager();
    QueryResult result = queryManager.createQuery(query, Query.JCR_SQL2).execute();
    RowIterator rows = result.getRows();
    Set<String> suggestions = newHashSet();
    while (rows.hasNext()) {
        suggestions.add(rows.nextRow().getValue("suggestion").getString());
    }
    return suggestions;
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager)

Aggregations

QueryManager (javax.jcr.query.QueryManager)103 Query (javax.jcr.query.Query)69 Node (javax.jcr.Node)61 QueryResult (javax.jcr.query.QueryResult)55 Session (javax.jcr.Session)54 NodeIterator (javax.jcr.NodeIterator)34 RowIterator (javax.jcr.query.RowIterator)27 Test (org.junit.Test)27 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)22 RepositoryException (javax.jcr.RepositoryException)18 Row (javax.jcr.query.Row)14 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)14 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)9 ValueFactory (javax.jcr.ValueFactory)7 NoSuchElementException (java.util.NoSuchElementException)6 InvalidItemStateException (javax.jcr.InvalidItemStateException)3 Value (javax.jcr.Value)3 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2