Search in sources :

Example 6 with JsonObject

use of org.apache.jackrabbit.oak.commons.json.JsonObject in project jackrabbit-oak by apache.

the class JsonIndexCommand method addNode.

private void addNode(Node p, String nodeName, JsonObject json) throws RepositoryException {
    Map<String, String> properties = json.getProperties();
    Map<String, JsonObject> children = json.getChildren();
    String primaryType = properties.get("jcr:primaryType");
    Node n;
    if (primaryType == null) {
        n = p.addNode(nodeName);
    } else {
        n = p.addNode(nodeName, getValueOrVariable(primaryType).toString());
    }
    for (Entry<String, String> e : properties.entrySet()) {
        String propertyName = e.getKey();
        if (!"jcr:primaryType".equals(propertyName)) {
            Object value = getValueOrVariable(properties.get(propertyName));
            setProperty(n, propertyName, value);
        }
    }
    for (Entry<String, JsonObject> e : children.entrySet()) {
        String k = e.getKey();
        JsonObject v = e.getValue();
        addNode(n, k, v);
    }
}
Also used : Node(javax.jcr.Node) JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject)

Example 7 with JsonObject

use of org.apache.jackrabbit.oak.commons.json.JsonObject in project jackrabbit-oak by apache.

the class JsonIndexCommand method execute.

void execute(String command) throws RepositoryException {
    JsopTokenizer t = new JsopTokenizer(command);
    t.read('{');
    JsonObject json = JsonObject.create(t);
    Map<String, String> properties = json.getProperties();
    if (properties.containsKey("if")) {
        Object value = getValueOrVariable(properties.get("if"));
        Object equals = getValueOrVariable(properties.get("="));
        if (value == null) {
            if (equals != null) {
                return;
            }
        } else if (!value.equals(equals)) {
            return;
        }
    }
    for (Entry<String, String> e : properties.entrySet()) {
        String k = e.getKey();
        Object value = getValueOrVariable(e.getValue());
        if ("addNode".equals(k)) {
            String nodePath = value.toString();
            String parent = PathUtils.getParentPath(nodePath);
            if (session.nodeExists(parent)) {
                Node p = session.getNode(parent);
                String nodeName = PathUtils.getName(nodePath);
                if (!p.hasNode(nodeName)) {
                    JsonObject node = json.getChildren().get("node");
                    addNode(p, nodeName, node);
                }
            }
        } else if ("removeNode".equals(k)) {
            String path = value.toString();
            if (session.nodeExists(path)) {
                session.getNode(path).remove();
            }
        } else if ("setProperty".equals(k)) {
            String itemPath = value.toString();
            String nodePath = PathUtils.getParentPath(itemPath);
            if (session.nodeExists(nodePath)) {
                String propertyName = PathUtils.getName(itemPath);
                Object propertyValue = getValueOrVariable(properties.get("value"));
                setProperty(session.getNode(nodePath), propertyName, propertyValue);
            }
        } else if ("session".equals(k)) {
            if ("save".equals(value)) {
                session.save();
            }
        } else if ("xpath".equals(k) || "sql".equals(k)) {
            String language = "xpath".equals(k) ? k : Query.JCR_SQL2;
            String columnName = "xpath".equals(k) ? "jcr:path" : null;
            boolean quiet = properties.containsKey("quiet");
            int depth = properties.containsKey("depth") ? Integer.parseInt(properties.get("depth")) : 0;
            runQuery(value.toString(), language, columnName, quiet, depth);
        } else if ("print".equals(k)) {
            output.println(value);
        } else if ("for".equals(k)) {
            String name = JsopTokenizer.decodeQuoted(properties.get(k));
            Object old = data.get(name);
            String[] commands = (String[]) getValueOrVariable(properties.get("do"));
            for (String x : (String[]) value) {
                data.put(name, x);
                for (String c : commands) {
                    execute(c);
                }
            }
            data.put(name, old);
        } else if ("loop".equals(k)) {
            while (true) {
                for (String c : (String[]) value) {
                    execute(c);
                    if (data.remove("$break") != null) {
                        return;
                    }
                }
            }
        } else if (k.startsWith("$")) {
            setVariable(properties, k, value);
        }
    }
}
Also used : Node(javax.jcr.Node) JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) JsopTokenizer(org.apache.jackrabbit.oak.commons.json.JsopTokenizer)

Example 8 with JsonObject

use of org.apache.jackrabbit.oak.commons.json.JsonObject in project jackrabbit-oak by apache.

the class ClusterViewTest method testOneActiveSeveralInactive.

@Test
public void testOneActiveSeveralInactive() throws Exception {
    String clusterId = UUID.randomUUID().toString();
    ClusterViewBuilder builder = new ClusterViewBuilder(10, 2);
    ClusterView view = builder.active(2).inactive(3, 4, 5, 6).asView(clusterId);
    // {"seq":10,"id":"35f60ed3-508d-4a81-b812-89f07f57db20","me":2,"active":[2],"deactivating":[],"inactive":[3]}
    JsonObject o = asJsonObject(view);
    Map<String, String> props = o.getProperties();
    assertEquals("10", props.get("seq"));
    assertEquals("true", props.get("final"));
    assertEquals(clusterId, unwrapString(props.get("id")));
    assertEquals("2", props.get("me"));
    assertEquals(asJsonArray(2), props.get("active"));
    assertEquals(asJsonArray(), props.get("deactivating"));
    assertEquals(asJsonArray(3, 4, 5, 6), props.get("inactive"));
}
Also used : JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) Test(org.junit.Test)

Example 9 with JsonObject

use of org.apache.jackrabbit.oak.commons.json.JsonObject in project jackrabbit-oak by apache.

the class ClusterViewTest method testWithRecoveringOnly.

@Test
public void testWithRecoveringOnly() throws Exception {
    String clusterId = UUID.randomUUID().toString();
    ClusterViewBuilder builder = new ClusterViewBuilder(10, 2);
    ClusterView view = builder.active(2, 3).recovering(4).inactive(5, 6).asView(clusterId);
    JsonObject o = asJsonObject(view);
    Map<String, String> props = o.getProperties();
    assertEquals("10", props.get("seq"));
    assertEquals("true", props.get("final"));
    assertEquals(clusterId, unwrapString(props.get("id")));
    assertEquals("2", props.get("me"));
    assertEquals(asJsonArray(2, 3), props.get("active"));
    assertEquals(asJsonArray(4), props.get("deactivating"));
    assertEquals(asJsonArray(5, 6), props.get("inactive"));
}
Also used : JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) Test(org.junit.Test)

Example 10 with JsonObject

use of org.apache.jackrabbit.oak.commons.json.JsonObject in project jackrabbit-oak by apache.

the class ClusterViewTest method testWithRecoveringAndBacklog.

@Test
public void testWithRecoveringAndBacklog() throws Exception {
    String clusterId = UUID.randomUUID().toString();
    ClusterViewBuilder builder = new ClusterViewBuilder(10, 2);
    ClusterView view = builder.active(2, 3).recovering(4).inactive(5, 6).backlogs(5).asView(clusterId);
    JsonObject o = asJsonObject(view);
    Map<String, String> props = o.getProperties();
    assertEquals("10", props.get("seq"));
    assertEquals(clusterId, unwrapString(props.get("id")));
    assertEquals("2", props.get("me"));
    assertEquals("false", props.get("final"));
    assertEquals(asJsonArray(2, 3), props.get("active"));
    assertEquals(asJsonArray(4, 5), props.get("deactivating"));
    assertEquals(asJsonArray(6), props.get("inactive"));
}
Also used : JsonObject(org.apache.jackrabbit.oak.commons.json.JsonObject) Test(org.junit.Test)

Aggregations

JsonObject (org.apache.jackrabbit.oak.commons.json.JsonObject)12 Test (org.junit.Test)6 JsopTokenizer (org.apache.jackrabbit.oak.commons.json.JsopTokenizer)3 Node (javax.jcr.Node)2 QueryManager (javax.jcr.query.QueryManager)1 QueryResult (javax.jcr.query.QueryResult)1 Row (javax.jcr.query.Row)1 JsopBuilder (org.apache.jackrabbit.oak.commons.json.JsopBuilder)1