Search in sources :

Example 96 with ValueFactory

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

the class QueryTest method simple.

@SuppressWarnings("deprecation")
@Test
public void simple() throws RepositoryException {
    Session session = getAdminSession();
    Node hello = session.getRootNode().addNode("hello");
    hello.setProperty("id", "1");
    hello.setProperty("text", "hello_world");
    session.save();
    Node hello2 = session.getRootNode().addNode("hello2");
    hello2.setProperty("id", "2");
    hello2.setProperty("text", "hello world");
    session.save();
    ValueFactory vf = session.getValueFactory();
    QueryManager qm = session.getWorkspace().getQueryManager();
    // SQL-2
    Query q = qm.createQuery("select text from [nt:base] where id = $id", Query.JCR_SQL2);
    q.bindValue("id", vf.createValue("1"));
    QueryResult r = q.execute();
    RowIterator it = r.getRows();
    assertTrue(it.hasNext());
    Row row = it.nextRow();
    assertEquals("hello_world", row.getValue("text").getString());
    String[] columns = r.getColumnNames();
    assertEquals(1, columns.length);
    assertEquals("text", columns[0]);
    assertFalse(it.hasNext());
    r = q.execute();
    NodeIterator nodeIt = r.getNodes();
    assertTrue(nodeIt.hasNext());
    Node n = nodeIt.nextNode();
    assertEquals("hello_world", n.getProperty("text").getString());
    assertFalse(it.hasNext());
    // SQL
    q = qm.createQuery("select text from [nt:base] where text like 'hello\\_world' escape '\\'", Query.SQL);
    r = q.execute();
    columns = r.getColumnNames();
    assertEquals(3, columns.length);
    assertEquals("text", columns[0]);
    assertEquals("jcr:path", columns[1]);
    assertEquals("jcr:score", columns[2]);
    nodeIt = r.getNodes();
    assertTrue(nodeIt.hasNext());
    n = nodeIt.nextNode();
    assertEquals("hello_world", n.getProperty("text").getString());
    assertFalse(nodeIt.hasNext());
    // XPath
    q = qm.createQuery("//*[@id=1]", Query.XPATH);
    r = q.execute();
    assertEquals(newHashSet("jcr:path", "jcr:score", "jcr:primaryType"), newHashSet(r.getColumnNames()));
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) ValueFactory(javax.jcr.ValueFactory) Row(javax.jcr.query.Row) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 97 with ValueFactory

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

the class QueryTest method testJoin.

public void testJoin() throws Exception {
    // create a visible node /test/node1 
    // with an invisible child /test/node1/node2
    // with an invisible child /test/node1/node2/node3
    Node n = superuser.getNode(path);
    Node visible = n.addNode(nodeName1, testNodeType);
    allow(visible.getPath(), privilegesFromName(Privilege.JCR_READ));
    Node invisible = visible.addNode(nodeName2, testNodeType);
    Node invisible2 = invisible.addNode(nodeName3, testNodeType);
    deny(invisible.getPath(), privilegesFromName(Privilege.JCR_READ));
    deny(invisible2.getPath(), privilegesFromName(Privilege.JCR_READ));
    superuser.save();
    // test visibility
    testSession.refresh(false);
    testSession.checkPermission(visible.getPath(), Session.ACTION_READ);
    try {
        testSession.checkPermission(invisible.getPath(), Session.ACTION_READ);
        fail();
    } catch (AccessControlException e) {
    // expected
    }
    Node x = testSession.getNode(visible.getPath());
    ValueFactory vf = testSession.getValueFactory();
    Query q;
    QueryResult r;
    NodeIterator ni;
    // verify we can see the visible node
    q = testSession.getWorkspace().getQueryManager().createQuery("select * from [nt:base] where [jcr:path]=$path", Query.JCR_SQL2);
    q.bindValue("path", vf.createValue(visible.getPath()));
    r = q.execute();
    ni = r.getNodes();
    assertTrue(ni.hasNext());
    x = ni.nextNode();
    assertTrue(x.getSession() == testSession);
    // verify we cannot see the invisible node
    q = testSession.getWorkspace().getQueryManager().createQuery("select * from [nt:base] where [jcr:path]=$path", Query.JCR_SQL2);
    q.bindValue("path", vf.createValue(invisible.getPath()));
    r = q.execute();
    assertFalse(r.getNodes().hasNext());
    // the superuser should see both nodes
    q = superuser.getWorkspace().getQueryManager().createQuery("select a.* from [nt:base] as a " + "inner join [nt:base] as b on isdescendantnode(b, a) " + "where a.[jcr:path]=$path", Query.JCR_SQL2);
    q.bindValue("path", vf.createValue(visible.getPath()));
    r = q.execute();
    assertTrue(r.getNodes().hasNext());
    // but the testSession must not:
    // verify we can not deduce existence of the invisible node
    // using a join
    q = testSession.getWorkspace().getQueryManager().createQuery("select a.* from [nt:base] as a " + "inner join [nt:base] as b on isdescendantnode(b, a) " + "where a.[jcr:path]=$path", Query.JCR_SQL2);
    q.bindValue("path", vf.createValue(visible.getPath()));
    r = q.execute();
    assertFalse(r.getNodes().hasNext());
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) AccessControlException(java.security.AccessControlException) ValueFactory(javax.jcr.ValueFactory)

Example 98 with ValueFactory

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

the class NodeStateCopyUtils method copyProps.

private static void copyProps(NodeState state, Node node) throws RepositoryException {
    ValueFactory vf = node.getSession().getValueFactory();
    for (PropertyState ps : state.getProperties()) {
        String name = ps.getName();
        if (name.equals(JcrConstants.JCR_PRIMARYTYPE) || name.equals(OAK_CHILD_ORDER)) {
            continue;
        }
        if (name.equals(JcrConstants.JCR_MIXINTYPES)) {
            for (String n : ps.getValue(NAMES)) {
                node.addMixin(n);
            }
            continue;
        }
        if (ps.isArray()) {
            Value[] values = new Value[ps.count()];
            for (int i = 0; i < ps.count(); i++) {
                values[i] = createValue(vf, ps, i);
            }
            node.setProperty(name, values, ps.getType().tag());
        } else {
            node.setProperty(name, createValue(vf, ps, -1), ps.getType().tag());
        }
    }
}
Also used : Value(javax.jcr.Value) ValueFactory(javax.jcr.ValueFactory) PropertyState(org.apache.jackrabbit.oak.api.PropertyState)

Example 99 with ValueFactory

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

the class JsonIndexCommand method runQuery.

private void runQuery(String query, String language, String columnName, boolean quiet, int depth) throws RepositoryException {
    ArrayList<String> list = new ArrayList<String>();
    columnName = query.startsWith("explain") ? "plan" : columnName;
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery(query, language);
    for (String b : q.getBindVariableNames()) {
        ValueFactory vf = session.getValueFactory();
        q.bindValue(b, vf.createValue(data.get("$" + b).toString()));
    }
    QueryResult result = q.execute();
    if (depth != 0) {
        NodeIterator ni = result.getNodes();
        JsopBuilder builder = new JsopBuilder().array();
        while (ni.hasNext()) {
            Node n = ni.nextNode();
            builder.key(n.getPath());
            appendNode(builder, n, depth - 1);
        }
        output.println(JsopBuilder.prettyPrint(builder.endArray().toString()));
        return;
    }
    RowIterator ri = result.getRows();
    while (ri.hasNext()) {
        Row r = ri.nextRow();
        if (columnName != null) {
            String x = r.getValue(columnName).getString();
            list.add(x);
            if (!quiet) {
                output.println(x);
            }
        } else {
            String[] columnNames = result.getColumnNames();
            for (String cn : columnNames) {
                Value v = r.getValue(cn);
                String x = v == null ? null : v.getString();
                if (columnNames.length == 1) {
                    list.add(x);
                    if (!quiet) {
                        output.println(x);
                    }
                } else {
                    list.add(x);
                    if (!quiet) {
                        output.println(cn + ": " + x);
                    }
                }
            }
        }
    }
    data.put("$resultSize", (long) list.size());
    data.put("$result", list.toArray(new String[0]));
}
Also used : NodeIterator(javax.jcr.NodeIterator) Query(javax.jcr.query.Query) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) ValueFactory(javax.jcr.ValueFactory) QueryResult(javax.jcr.query.QueryResult) JsopBuilder(org.apache.jackrabbit.oak.commons.json.JsopBuilder) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Value(javax.jcr.Value) Row(javax.jcr.query.Row)

Example 100 with ValueFactory

use of javax.jcr.ValueFactory in project sling by apache.

the class JcrResourceUtil method createValue.

/**
     * Creates a {@link javax.jcr.Value JCR Value} for the given object with
     * the given Session.
     * Selects the the {@link javax.jcr.PropertyType PropertyType} according
     * the instance of the object's Class
     *
     * @param value object
     * @param session to create value for
     * @return the value or null if not convertible to a valid PropertyType
     * @throws RepositoryException in case of error, accessing the Repository
     */
public static Value createValue(final Object value, final Session session) throws RepositoryException {
    Value val;
    ValueFactory fac = session.getValueFactory();
    if (value instanceof Calendar) {
        val = fac.createValue((Calendar) value);
    } else if (value instanceof InputStream) {
        val = fac.createValue(fac.createBinary((InputStream) value));
    } else if (value instanceof Node) {
        val = fac.createValue((Node) value);
    } else if (value instanceof BigDecimal) {
        val = fac.createValue((BigDecimal) value);
    } else if (value instanceof Long) {
        val = fac.createValue((Long) value);
    } else if (value instanceof Short) {
        val = fac.createValue((Short) value);
    } else if (value instanceof Integer) {
        val = fac.createValue((Integer) value);
    } else if (value instanceof Number) {
        val = fac.createValue(((Number) value).doubleValue());
    } else if (value instanceof Boolean) {
        val = fac.createValue((Boolean) value);
    } else if (value instanceof String) {
        val = fac.createValue((String) value);
    } else {
        val = null;
    }
    return val;
}
Also used : InputStream(java.io.InputStream) Calendar(java.util.Calendar) Node(javax.jcr.Node) Value(javax.jcr.Value) ValueFactory(javax.jcr.ValueFactory) BigDecimal(java.math.BigDecimal)

Aggregations

ValueFactory (javax.jcr.ValueFactory)105 Value (javax.jcr.Value)51 Node (javax.jcr.Node)50 Session (javax.jcr.Session)40 Test (org.junit.Test)17 RepositoryException (javax.jcr.RepositoryException)16 InputStream (java.io.InputStream)13 AccessControlManager (javax.jcr.security.AccessControlManager)13 HashMap (java.util.HashMap)12 Privilege (javax.jcr.security.Privilege)12 Property (javax.jcr.Property)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 Query (javax.jcr.query.Query)8 Calendar (java.util.Calendar)7 QueryManager (javax.jcr.query.QueryManager)7 RowIterator (javax.jcr.query.RowIterator)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 NodeIterator (javax.jcr.NodeIterator)6 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)6 IOException (java.io.IOException)5