Package javax.jcr.query

Examples of javax.jcr.query.QueryManager


            Node rootNode = writer.getRootNode();
            Node node = rootNode.addNode("test", "nt:unstructured");
            node.addNode(".tokens");
            writer.save();

            QueryManager qm = reader.getWorkspace().getQueryManager();
            Query q = qm.createQuery("/jcr:root//*[_x002e_tokens/@jcr:primaryType]", Query.XPATH);
            NodeIterator res = q.execute().getNodes();
            assertEquals(1, res.getSize());
        } finally {
            if (reader != null) {
                reader.logout();
View Full Code Here


            Node rootNode = writer.getRootNode();
            Node node = rootNode.addNode("test", "nt:unstructured");
            node.setProperty("text", "find me");
            writer.save();

            QueryManager qm = reader.getWorkspace().getQueryManager();
            Query q = qm.createQuery("select * from 'nt:base' where contains(*, 'find me')", Query.JCR_SQL2);
            NodeIterator res = q.execute().getNodes();
            assertEquals("False amount of hits", 1, res.getSize());
        } finally {
            if (reader != null) {
                reader.logout();
View Full Code Here

   
    @Test
    @Ignore("OAK-1155")
    public void nodeType() throws Exception {
        Session session = getAdminSession();
        QueryManager qm = session.getWorkspace().getQueryManager();
        Node testRootNode = session.getRootNode().addNode("testroot");
        Node n1 = testRootNode.addNode("node1");
        Node n2 = n1.addNode("node2");
        n2.addNode("node3");
        session.save();
      
        String sql2 = "select [jcr:path] as [path] from [nt:base] " +
                "where [node2/node3/jcr:primaryType] is not null";
       
        Query q;
        QueryResult result;
        RowIterator it;
       
        q = qm.createQuery("explain " + sql2, Query.JCR_SQL2);
        result = q.execute();
        it = result.getRows();
        assertTrue(it.hasNext());
        String plan = it.nextRow().getValue("plan").getString();
        // should not use the index on "jcr:primaryType"
        assertEquals("[nt:base] as [nt:base] /* traverse \"*\" " +
                "where [nt:base].[node2/node3/jcr:primaryType] is not null */",
                plan);
       
        // verify the result
        q = qm.createQuery(sql2, Query.JCR_SQL2);
        result = q.execute();
        it = result.getRows();
        assertTrue(it.hasNext());
        String path = it.nextRow().getValue("path").getString();
        assertEquals("/testroot/node1", path);
View Full Code Here

    }
   
    @Test
    public void excerpt() throws Exception {
        Session session = getAdminSession();
        QueryManager qm = session.getWorkspace().getQueryManager();
        Node testRootNode = session.getRootNode().addNode("testroot");
        Node n1 = testRootNode.addNode("node1");
        n1.setProperty("text", "hello world");
        n1.setProperty("desc", "description");
        Node n2 = testRootNode.addNode("node2");
        n2.setProperty("text", "Hello World");
        n2.setProperty("desc", "Description");
        session.save();

        Query q;
        RowIterator it;
        Row row;
        String s;
       
        String xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by jcr:path descending";
       
        q = qm.createQuery(xpath, "xpath");
        it = q.execute().getRows();
        row = it.nextRow();
        s = row.getValue("rep:excerpt(.)").getString();
        assertTrue(s, s.indexOf("<strong>hello</strong> world") >= 0);
        assertTrue(s, s.indexOf("description") >= 0);
        row = it.nextRow();
        s = row.getValue("rep:excerpt(.)").getString();
        // TODO is this expected?
        assertTrue(s, s.indexOf("Hello World") >= 0);
        assertTrue(s, s.indexOf("Description") >= 0);
       
        xpath = "//*[jcr:contains(., 'hello')]/rep:excerpt(.) order by jcr:path descending";

        q = qm.createQuery(xpath, "xpath");
        it = q.execute().getRows();
        row = it.nextRow();
        s = row.getValue("rep:excerpt(text)").getString();
        assertTrue(s, s.indexOf("<strong>hello</strong> world") >= 0);
        assertTrue(s, s.indexOf("description") < 0);
View Full Code Here

    }
   
    @Test
    public void fulltextOrWithinText() throws Exception {
        Session session = getAdminSession();
        QueryManager qm = session.getWorkspace().getQueryManager();
        Node testRootNode = session.getRootNode().addNode("testroot");
        Node n1 = testRootNode.addNode("node1");
        n1.setProperty("text", "hello");
        Node n2 = testRootNode.addNode("node2");
        n2.setProperty("text", "hallo");
        Node n3 = testRootNode.addNode("node3");
        n3.setProperty("text", "hello hallo");
        session.save();
      
        String sql2 = "select [jcr:path] as [path] from [nt:base] " +
                "where contains([text], 'hello OR hallo') order by [jcr:path]";
       
        Query q;
       
        q = qm.createQuery("explain " + sql2, Query.JCR_SQL2);

        assertEquals("[nt:base] as [nt:base] /* traverse \"*\" " +
                "where contains([nt:base].[text], cast('hello OR hallo' as string)) */",
                getResult(q.execute(), "plan"));
       
        // verify the result
        // uppercase "OR" mean logical "or"
        q = qm.createQuery(sql2, Query.JCR_SQL2);
        assertEquals("/testroot/node1, /testroot/node2, /testroot/node3",
                getResult(q.execute(), "path"));

        // lowercase "or" mean search for the term "or"
        sql2 = "select [jcr:path] as [path] from [nt:base] " +
                "where contains([text], 'hello or hallo') order by [jcr:path]";
        q = qm.createQuery(sql2, Query.JCR_SQL2);
        assertEquals("",
                getResult(q.execute(), "path"));

    }
View Full Code Here

        tmpl.addEntry(EveryonePrincipal.getInstance(), new Privilege[]{acMgr.privilegeFromName(Privilege.JCR_READ)}, true, restrictions);
        acMgr.setPolicy(tmpl.getPath(), tmpl);
        adminSession.save();

        Session anonymousSession = getRepository().login(new GuestCredentials());
        QueryManager qm = anonymousSession.getWorkspace().getQueryManager();
        Query q = qm.createQuery("/jcr:root/home//social/relationships/following//*[id='aaron.mcdonald@mailinator.com']", Query.XPATH);
        QueryResult r = q.execute();
        RowIterator it = r.getRows();
        Assert.assertTrue(it.hasNext());
    }
View Full Code Here

     * @see SearchResource#getQueryGrammerSet()
     */
    public QueryGrammerSet getQueryGrammerSet()  {
        QueryGrammerSet qgs = new QueryGrammerSet();
        try {
            QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();
            String[] langs = qMgr.getSupportedQueryLanguages();
            for (int i = 0; i < langs.length; i++) {
                // todo: define proper namespace
                qgs.addQueryLanguage(langs[i], Namespace.EMPTY_NAMESPACE);
            }
        } catch (RepositoryException e) {
View Full Code Here

     */
    private Query getQuery(SearchInfo sInfo)
            throws InvalidQueryException, RepositoryException, DavException {

        Node rootNode = getRepositorySession().getRootNode();
        QueryManager qMgr = getRepositorySession().getWorkspace().getQueryManager();

        // test if query is defined by requested repository node
        String itemPath = locator.getRepositoryPath();
        if (!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) {
            q = qMgr.createQuery(sInfo.getQuery(), sInfo.getLanguageName());
        } 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
View Full Code Here

        // if the session has locks, we logout the session and drop it,
        // as there is no easy way of finding the temporary locks and
        // unlocking them, we could use the search, however ???
        try {
            QueryManager qm = session.getWorkspace().getQueryManager();
            // FIXME - this search searches for all locks for the user of the session
            //         so if the user has more than one session, locks from other
            //         sessions will be delivered as well.
            Query q = qm.createQuery(
                "/jcr:root//element(*,mix:lockable)[@jcr:lockOwner='"
                    + session.getUserID() + "']", Query.XPATH);
            NodeIterator ni = q.execute().getNodes();
            while (ni.hasNext()) {
                Node node = ni.nextNode();
View Full Code Here

    }
  }

  protected Iterator<Node> _getReferents(Node referenced, String propertyName) throws RepositoryException {
    String path = referenced.getPath();
    QueryManager queryMgr = session.getWorkspace().getQueryManager();
    Query query = queryMgr.createQuery("SELECT * FROM nt:base WHERE " + propertyName + "='" + path + "'", Query.SQL);
    QueryResult result = query.execute();
    @SuppressWarnings("unchecked") Iterator<Node> nodes = result.getNodes();
    return new AbstractFilterIterator<Node, Node>(nodes) {
      private Node current;
      protected Node adapt(Node node) {
View Full Code Here

TOP

Related Classes of javax.jcr.query.QueryManager

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.