Example usage for io.vertx.core.json JsonArray getJsonArray

List of usage examples for io.vertx.core.json JsonArray getJsonArray

Introduction

In this page you can find the example usage for io.vertx.core.json JsonArray getJsonArray.

Prototype

public JsonArray getJsonArray(int pos) 

Source Link

Document

Get the JsonArray at position pos in the array.

Usage

From source file:org.entcore.registry.services.impl.DefaultAppRegistryService.java

License:Open Source License

@Override
public void createApplication(String structureId, JsonObject application, JsonArray actions,
        final Handler<Either<String, JsonObject>> handler) {
    if (defaultValidationParamsNull(handler, application, application.getString("name")))
        return;//  www. j a va2 s. c  om
    final String applicationName = application.getString("name");
    final String id = UUID.randomUUID().toString();
    final String address = application.getString("address");
    application.put("scope", new fr.wseduc.webutils.collections.JsonArray(
            "[\"" + application.getString("scope", "").replaceAll("\\s", "\",\"") + "\"]"));
    application.put("id", id);
    final String createApplicationQuery = "MATCH (n:Application) " + "WHERE n.name = {applicationName} "
            + "WITH count(*) AS exists " + "WHERE exists=0 " + "CREATE (m:Application {props}) "
            + "RETURN m.id as id";
    final JsonObject params = new JsonObject().put("applicationName", applicationName);
    if (structureId != null && !structureId.trim().isEmpty()) {
        application.put("structureId", structureId);
    }
    final StatementsBuilder b = new StatementsBuilder().add(createApplicationQuery,
            params.copy().put("props", application));
    if (actions != null && actions.size() > 0) {
        for (Object o : actions) {
            JsonObject json = (JsonObject) o;
            String type;
            List<String> removeLabels = new ArrayList<>();
            removeLabels.add("ResourceAction");
            removeLabels.add("AuthenticatedAction");
            removeLabels.add("WorkflowAction");
            switch (json.getString("type", "WORKFLOW")) {
            case "RESOURCE":
                type = "Resource";
                break;
            case "AUTHENTICATED":
                type = "Authenticated";
                break;
            default:
                type = "Workflow";
                break;
            }
            removeLabels.remove(type + "Action");
            String createAction = "MERGE (a:Action {name:{name}}) " + "REMOVE a:"
                    + Joiner.on(":").join(removeLabels) + " "
                    + "SET a.displayName = {displayName}, a.type = {type}, a:" + type + "Action " + "WITH a "
                    + "MATCH (n:Application) " + "WHERE n.name = {applicationName} "
                    + "CREATE UNIQUE n-[r:PROVIDE]->a " + "RETURN a.name as name";
            b.add(createAction, json.put("applicationName", applicationName).put("type",
                    "SECURED_ACTION_" + json.getString("type", "WORKFLOW")));
        }
        final String removeNotWorkflowInRole = "MATCH (:Role)-[r:AUTHORIZE]->(a:Action) "
                + "WHERE a:ResourceAction OR a:AuthenticatedAction " + "DELETE r";
        b.add(removeNotWorkflowInRole);
    } else if (address != null && !address.trim().isEmpty()) {
        String query2 = "MATCH (n:Application) " + "WHERE n.id = {id} "
                + "CREATE UNIQUE n-[r:PROVIDE]->(a:Action:WorkflowAction {type: {type}, "
                + "name:{name}, displayName:{displayName}}) " + "RETURN a.name as name";
        b.add(query2, new JsonObject().put("id", id).put("type", "SECURED_ACTION_WORKFLOW")
                .put("name", applicationName + "|address").put("displayName", applicationName + ".address"));
    }
    neo.executeTransaction(b.build(), null, true, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> m) {
            JsonArray results = m.body().getJsonArray("results");
            if ("ok".equals(m.body().getString("status")) && results != null) {
                JsonArray r = results.getJsonArray(0);
                JsonObject j;
                if (r.size() > 0) {
                    j = r.getJsonObject(0);
                } else {
                    j = new JsonObject();
                }
                handler.handle(new Either.Right<String, JsonObject>(j));
            } else {
                handler.handle(new Either.Left<String, JsonObject>(m.body().getString("message")));
            }
        }
    });
}

From source file:org.entcore.registry.services.impl.DefaultExternalApplicationService.java

License:Open Source License

@Override
public void createExternalApplication(String structureId, final JsonObject application,
        final Handler<Either<String, JsonObject>> handler) {
    if (defaultValidationParamsNull(handler, application, application.getString("name"), structureId,
            application.getString("address")))
        return;//from   w ww .j  a  v  a2 s. co m

    final String applicationName = application.getString("name");
    final String id = UUID.randomUUID().toString();
    application.put("scope", new fr.wseduc.webutils.collections.JsonArray(
            "[\"" + application.getString("scope", "").replaceAll("\\s", "\",\"") + "\"]"));
    application.put("id", id);
    application.put("structureId", structureId);

    /* App creation query */
    final String createApplicationQuery = "MATCH (n:Application) " + "WHERE n.name = {applicationName} "
            + "WITH count(*) AS exists " + "WHERE exists=0 " + "CREATE (m:Application:External {props}) "
            + "RETURN m.id as id";

    final StatementsBuilder b = new StatementsBuilder().add(createApplicationQuery,
            new JsonObject().put("applicationName", applicationName).put("props", application));

    /* Underlying action & role creation query */
    String createActionsAndRolesQuery = "MATCH (n:Application) " + "WHERE n.id = {id} "
            + "CREATE UNIQUE n-[r:PROVIDE]->(a:Action:WorkflowAction {type: {type}, "
            + "name:{name}, displayName:{displayName}}) " + "WITH a "
            + "CREATE UNIQUE (r:Role {id: {roleId}, name: {roleName}, structureId: {structureId}})-[:AUTHORIZE]->(a) "
            + "RETURN r.id as roleId";
    b.add(createActionsAndRolesQuery,
            new JsonObject().put("id", id).put("roleId", UUID.randomUUID().toString())
                    .put("type", "SECURED_ACTION_WORKFLOW").put("name", applicationName + "|address")
                    .put("roleName", applicationName + "- ACCESS ").put("structureId", structureId)
                    .put("displayName", applicationName + ".address"));

    neo.executeTransaction(b.build(), null, true, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> m) {
            JsonArray results = m.body().getJsonArray("results");
            if ("ok".equals(m.body().getString("status")) && results != null) {
                JsonArray appRes = results.getJsonArray(0);
                JsonArray roleRes = results.getJsonArray(1);
                JsonObject j = new JsonObject()
                        .mergeIn(appRes.size() > 0 ? appRes.getJsonObject(0) : new JsonObject())
                        .mergeIn(roleRes.size() > 0 ? roleRes.getJsonObject(0) : new JsonObject());
                handler.handle(new Either.Right<String, JsonObject>(j));
            } else {
                handler.handle(new Either.Left<String, JsonObject>(m.body().getString("message")));
            }
        }
    });
}

From source file:org.entcore.session.AuthManager.java

License:Open Source License

private void generateSessionInfos(final String userId, final Handler<JsonObject> handler) {
    final String query = "MATCH (n:User {id : {id}}) " + "WHERE HAS(n.login) "
            + "OPTIONAL MATCH n-[:IN]->(gp:Group) " + "OPTIONAL MATCH gp-[:DEPENDS]->(s:Structure) "
            + "OPTIONAL MATCH gp-[:DEPENDS]->(c:Class) "
            + "OPTIONAL MATCH n-[rf:HAS_FUNCTION]->fg-[:CONTAINS_FUNCTION*0..1]->(f:Function) "
            + "OPTIONAL MATCH n<-[:RELATED]-(child:User) " + "RETURN distinct "
            + "n.classes as classNames, n.level as level, n.login as login, COLLECT(distinct [c.id, c.name]) as classes, "
            + "n.lastName as lastName, n.firstName as firstName, n.externalId as externalId, n.federated as federated, "
            + "n.birthDate as birthDate, "
            + "n.displayName as username, HEAD(n.profiles) as type, COLLECT(distinct [child.id, child.lastName, child.firstName]) as childrenInfo, "
            + "COLLECT(distinct [s.id, s.name]) as structures, COLLECT(distinct [f.externalId, rf.scope]) as functions, "
            + "COLLECT(distinct s.UAI) as uai, "
            + "COLLECT(distinct gp.id) as groupsIds, n.federatedIDP as federatedIDP, n.functions as aafFunctions";
    final String query2 = "MATCH (n:User {id : {id}})-[:IN]->()-[:AUTHORIZED]->(:Role)-[:AUTHORIZE]->(a:Action)"
            + "<-[:PROVIDE]-(app:Application) " + "WHERE HAS(n.login) "
            + "RETURN DISTINCT COLLECT(distinct [a.name,a.displayName,a.type]) as authorizedActions, "
            + "COLLECT(distinct [app.name,app.address,app.icon,app.target,app.displayName,app.display,app.prefix]) as apps";
    final String query3 = "MATCH (u:User {id: {id}})-[:IN]->(g:Group)-[auth:AUTHORIZED]->(w:Widget) "
            + "WHERE HAS(u.login) "
            + "AND ( NOT(w<-[:HAS_WIDGET]-(:Application)-[:PROVIDE]->(:WorkflowAction)) "
            + "XOR w<-[:HAS_WIDGET]-(:Application)-[:PROVIDE]->(:WorkflowAction)<-[:AUTHORIZE]-(:Role)<-[:AUTHORIZED]-g )  "
            + "OPTIONAL MATCH (w)<-[:HAS_WIDGET]-(app:Application) "
            + "WITH w, app, collect(auth) as authorizations " + "RETURN DISTINCT COLLECT({"
            + "id: w.id, name: w.name, " + "path: coalesce(app.address, '') + w.path, "
            + "js: coalesce(app.address, '') + w.js, " + "i18n: coalesce(app.address, '') + w.i18n, "
            + "application: app.name, "
            + "mandatory: ANY(a IN authorizations WHERE HAS(a.mandatory) AND a.mandatory = true)"
            + "}) as widgets";
    final String query4 = "MATCH (s:Structure) return s.id as id, s.externalId as externalId";
    final String query5 = "MATCH (u:User {id: {id}})-[:PREFERS]->(uac:UserAppConf) RETURN uac AS preferences";
    JsonObject params = new JsonObject();
    params.put("id", userId);
    JsonArray statements = new fr.wseduc.webutils.collections.JsonArray()
            .add(new JsonObject().put("statement", query).put("parameters", params))
            .add(new JsonObject().put("statement", query2).put("parameters", params))
            .add(new JsonObject().put("statement", query3).put("parameters", params))
            .add(new JsonObject().put("statement", query4))
            .add(new JsonObject().put("statement", query5).put("parameters", params));
    neo4j.executeTransaction(statements, null, true, new Handler<Message<JsonObject>>() {

        @Override/*from   w  ww.j a va2  s.c o m*/
        public void handle(Message<JsonObject> message) {
            JsonArray results = message.body().getJsonArray("results");
            if ("ok".equals(message.body().getString("status")) && results != null && results.size() == 5
                    && results.getJsonArray(0).size() > 0 && results.getJsonArray(1).size() > 0) {
                JsonObject j = results.getJsonArray(0).getJsonObject(0);
                JsonObject j2 = results.getJsonArray(1).getJsonObject(0);
                JsonObject j3 = results.getJsonArray(2).getJsonObject(0);
                JsonObject structureMapping = new JsonObject();
                for (Object o : results.getJsonArray(3)) {
                    if (!(o instanceof JsonObject))
                        continue;
                    JsonObject jsonObject = (JsonObject) o;
                    structureMapping.put(jsonObject.getString("externalId"), jsonObject.getString("id"));
                }
                j.put("userId", userId);
                JsonObject functions = new JsonObject();
                JsonArray actions = new fr.wseduc.webutils.collections.JsonArray();
                JsonArray apps = new fr.wseduc.webutils.collections.JsonArray();
                for (Object o : getOrElse(j2.getJsonArray("authorizedActions"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    JsonArray a = (JsonArray) o;
                    actions.add(new JsonObject().put("name", a.getString(0)).put("displayName", a.getString(1))
                            .put("type", a.getString(2)));
                }
                for (Object o : getOrElse(j2.getJsonArray("apps"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    JsonArray a = (JsonArray) o;
                    apps.add(new JsonObject().put("name", (String) a.getString(0))
                            .put("address", (String) a.getString(1)).put("icon", (String) a.getString(2))
                            .put("target", (String) a.getString(3)).put("displayName", (String) a.getString(4))
                            .put("display", ((a.getValue(5) == null) || a.getBoolean(5)))
                            .put("prefix", (String) a.getString(6)));
                }
                for (Object o : getOrElse(j.getJsonArray("aafFunctions"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (o == null)
                        continue;
                    String[] sf = o.toString().split("\\$");
                    if (sf.length == 5) {
                        JsonObject jo = functions.getJsonObject(sf[1]);
                        if (jo == null) {
                            jo = new JsonObject().put("code", sf[1]).put("functionName", sf[2])
                                    .put("scope", new fr.wseduc.webutils.collections.JsonArray())
                                    .put("structureExternalIds", new fr.wseduc.webutils.collections.JsonArray())
                                    .put("subjects", new JsonObject());
                            functions.put(sf[1], jo);
                        }
                        JsonObject subject = jo.getJsonObject("subjects").getJsonObject(sf[3]);
                        if (subject == null) {
                            subject = new JsonObject().put("subjectCode", sf[3]).put("subjectName", sf[4])
                                    .put("scope", new fr.wseduc.webutils.collections.JsonArray())
                                    .put("structureExternalIds",
                                            new fr.wseduc.webutils.collections.JsonArray());
                            jo.getJsonObject("subjects").put(sf[3], subject);
                        }
                        jo.getJsonArray("structureExternalIds").add(sf[0]);
                        subject.getJsonArray("structureExternalIds").add(sf[0]);
                        String sid = structureMapping.getString(sf[0]);
                        if (sid != null) {
                            jo.getJsonArray("scope").add(sid);
                            subject.getJsonArray("scope").add(sid);
                        }
                    }
                }
                j.remove("aafFunctions");
                for (Object o : getOrElse(j.getJsonArray("functions"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    JsonArray a = (JsonArray) o;
                    String code = a.getString(0);
                    if (code != null) {
                        functions.put(code, new JsonObject().put("code", code).put("scope", a.getJsonArray(1)));
                    }
                }
                final JsonObject children = new JsonObject();
                final List<String> childrenIds = new ArrayList<String>();
                for (Object o : getOrElse(j.getJsonArray("childrenInfo"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    final JsonArray a = (JsonArray) o;
                    final String childId = a.getString(0);
                    if (childId != null) {
                        childrenIds.add(childId);
                        JsonObject jo = children.getJsonObject(childId);
                        if (jo == null) {
                            jo = new JsonObject().put("lastName", a.getString(1)).put("firstName",
                                    a.getString(2));
                            children.put(childId, jo);
                        }
                    }
                }
                j.remove("childrenInfo");
                final List<String> classesIds = new ArrayList<String>();
                final List<String> classesNames = new ArrayList<String>();
                for (Object o : getOrElse(j.getJsonArray("classes"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    final JsonArray c = (JsonArray) o;
                    if (c.getString(0) != null) {
                        classesIds.add(c.getString(0));
                        classesNames.add(c.getString(1));
                    }
                }
                j.remove("classes");
                final List<String> structureIds = new ArrayList<String>();
                final List<String> structureNames = new ArrayList<String>();
                for (Object o : getOrElse(j.getJsonArray("structures"),
                        new fr.wseduc.webutils.collections.JsonArray())) {
                    if (!(o instanceof JsonArray))
                        continue;
                    final JsonArray s = (JsonArray) o;
                    if (s.getString(0) != null) {
                        structureIds.add(s.getString(0));
                        structureNames.add(StringUtils.trimToBlank(s.getString(1)));
                    }
                }
                j.remove("structures");
                j.put("structures", new fr.wseduc.webutils.collections.JsonArray(structureIds));
                j.put("structureNames", new fr.wseduc.webutils.collections.JsonArray(structureNames));
                j.put("classes", new fr.wseduc.webutils.collections.JsonArray(classesIds));
                j.put("realClassesNames", new fr.wseduc.webutils.collections.JsonArray(classesNames));
                j.put("functions", functions);
                j.put("authorizedActions", actions);
                j.put("apps", apps);
                j.put("childrenIds", new fr.wseduc.webutils.collections.JsonArray(childrenIds));
                j.put("children", children);
                final JsonObject cache = (results.getJsonArray(4) != null && results.getJsonArray(4).size() > 0
                        && results.getJsonArray(4).getJsonObject(0) != null)
                                ? results.getJsonArray(4).getJsonObject(0)
                                : new JsonObject();
                j.put("cache", cache);
                j.put("widgets",
                        getOrElse(j3.getJsonArray("widgets"), new fr.wseduc.webutils.collections.JsonArray()));
                handler.handle(j);
            } else {
                handler.handle(null);
            }
        }
    });
}