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

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

Introduction

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

Prototype

public int size() 

Source Link

Document

Get the number of values in this JSON array

Usage

From source file:org.entcore.feeder.ManualFeeder.java

License:Open Source License

private void createUserInStructure(final Message<JsonObject> message, final JsonObject user, String profile,
        String structureId, JsonArray childrenIds) {
    String related = "";
    JsonObject params = new JsonObject().put("structureId", structureId).put("profile", profile).put("props",
            user);//w  w  w.j av a 2s  .c  o m
    if (childrenIds != null && childrenIds.size() > 0) {
        related = "WITH u " + "MATCH (student:User) " + "WHERE student.id IN {childrenIds} "
                + "CREATE student-[:RELATED]->u "
                + "SET student.relative = coalesce(student.relative, []) + (u.externalId + '$10$1$1$0$0') ";
        params.put("childrenIds", childrenIds);
    }
    String query = "MATCH (s:Structure { id : {structureId}})<-[:DEPENDS]-"
            + "(pg:ProfileGroup)-[:HAS_PROFILE]->(p:Profile { name : {profile}}) "
            + "CREATE UNIQUE pg<-[:IN]-(u:User {props}) " + "SET u.structures = [s.externalId] " + related
            + "RETURN DISTINCT u.id as id";
    neo4j.execute(query, params, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> m) {
            message.reply(m.body());
            if ("ok".equals(m.body().getString("status"))) {
                eventStore.createAndStoreEvent(Feeder.FeederEvent.CREATE_USER.name(), (UserInfos) null,
                        new JsonObject().put("new-user", user.getString("id")));
            }
        }
    });
}

From source file:org.entcore.feeder.ManualFeeder.java

License:Open Source License

private void removeUserFromStructure(final Message<JsonObject> message, String userId, String structureId) {
    try {//ww  w  .j  a  v a 2 s  .  com
        TransactionHelper tx = TransactionManager.getTransaction();
        JsonObject params = new JsonObject().put("structureId", structureId).put("userId", userId);
        final String query = "MATCH (u:User { id : {userId}})-[r:IN|COMMUNIQUE]-(cpg:ProfileGroup)-[:DEPENDS*0..1]->"
                + "(pg:ProfileGroup)-[:DEPENDS]->(s:Structure { id : {structureId}}), "
                + "pg-[:HAS_PROFILE]->(p:Profile), p<-[:HAS_PROFILE]-(dpg:DefaultProfileGroup) "
                + "CREATE UNIQUE dpg<-[:IN]-u "
                + "SET u.structures = FILTER(sId IN u.structures WHERE sId <> s.externalId), "
                + "u.classes = FILTER(cId IN u.classes WHERE NOT(cId =~ (s.externalId + '.*'))) " + "DELETE r "
                + "RETURN DISTINCT u.id as id";
        final String removeFunctions = "MATCH (u:User { id : {userId}})-[r:HAS_FUNCTION]->() "
                + "WHERE {structureId} IN r.scope "
                + "SET r.scope = FILTER(sId IN r.scope WHERE sId <> {structureId}) " + "WITH r "
                + "WHERE LENGTH(r.scope) = 0 " + "DELETE r";
        final String removeFunctionGroups = "MATCH (u:User { id : {userId}})-[r:IN|COMMUNIQUE]-(:Group)-[:DEPENDS]->(s:Structure { id : {structureId}})"
                + "DELETE r";
        tx.add(query, params);
        tx.add(removeFunctions, params);
        tx.add(removeFunctionGroups, params);
        tx.commit(new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> event) {
                final JsonArray results = event.body().getJsonArray("results");
                if ("ok".equals(event.body().getString("status")) && results != null && results.size() > 0) {
                    message.reply(event.body().put("result", results.getJsonArray(0)));
                } else {
                    message.reply(event.body());
                }
            }
        });
    } catch (TransactionException e) {
        logger.error("Error in transaction when remove user from structure", e);
        sendError(message, "transaction.error");
    }
}

From source file:org.entcore.feeder.ManualFeeder.java

License:Open Source License

private void createUserInClass(final Message<JsonObject> message, final JsonObject user, String profile,
        String classId, JsonArray childrenIds) {
    String related = "";
    JsonObject params = new JsonObject().put("classId", classId).put("profile", profile).put("props", user);
    if (childrenIds != null && childrenIds.size() > 0) {
        related = "WITH u " + "MATCH (student:User) " + "WHERE student.id IN {childrenIds} "
                + "CREATE student-[:RELATED]->u "
                + "SET student.relative = coalesce(student.relative, []) + (u.externalId + '$10$1$1$0$0') ";
        params.put("childrenIds", childrenIds);
    }/*w ww .j  ava2s. c o m*/
    String query = "MATCH (s:Class { id : {classId}})<-[:DEPENDS]-(cpg:ProfileGroup)-[:DEPENDS]->"
            + "(pg:ProfileGroup)-[:HAS_PROFILE]->(p:Profile { name : {profile}}), s-[:BELONGS]->(struct:Structure) "
            + "CREATE UNIQUE pg<-[:IN]-(u:User {props}), cpg<-[:IN]-u "
            + "SET u.classes = [s.externalId], u.structures = [struct.externalId] " + related
            + "RETURN DISTINCT u.id as id";
    neo4j.execute(query, params, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> m) {
            message.reply(m.body());
            if ("ok".equals(m.body().getString("status"))) {
                eventStore.createAndStoreEvent(Feeder.FeederEvent.CREATE_USER.name(), (UserInfos) null,
                        new JsonObject().put("new-user", user.getString("id")));
            }
        }
    });

}

From source file:org.entcore.feeder.ManualFeeder.java

License:Open Source License

private void removeUserFromClass(final Message<JsonObject> message, String userId, String classId) {
    try {//from w  w  w .j a  va2s . c o m
        TransactionHelper tx = TransactionManager.getTransaction();

        JsonObject params = new JsonObject().put("classId", classId).put("userId", userId);
        String query = "MATCH (u:User { id : {userId}})-[r:IN|COMMUNIQUE]-(cpg:ProfileGroup)-[:DEPENDS]->"
                + "(c:Class  {id : {classId}}), cpg-[:DEPENDS]->(pg:ProfileGroup)-[:HAS_PROFILE]->(p:Profile), "
                + "p<-[:HAS_PROFILE]-(dpg:DefaultProfileGroup) " + "CREATE UNIQUE dpg<-[:IN]-u "
                + "SET u.classes = FILTER(cId IN u.classes WHERE cId <> c.externalId) , u.headTeacherManual = FILTER(x IN u.headTeacherManual WHERE x <> c.externalId) "
                + "DELETE r " + "RETURN DISTINCT u.id as id";

        tx.add(query, params);

        String query2 = "MATCH (u:User { id : {userId}})-[r:IN|COMMUNIQUE]->(g:Group:HTGroup)-[:DEPENDS]->(c:Class {id : {classId}}) "
                + "DELETE r ";

        tx.add(query2, params);

        String query3 = "MATCH (u:User { id : {userId}})-[r:IN|COMMUNIQUE]->(g:Group:HTGroup)-[:DEPENDS]->(s:Structure)<-[b:BELONGS]-(c:Class {id : {classId}}) "
                + "WHERE length(u.headTeacherManual) = 0 AND (u.headTeacher IS NULL OR length(u.headTeacher) = 0) "
                + "DELETE r " + "RETURN DISTINCT u.id as id";

        tx.add(query3, params);

        tx.commit(new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> event) {
                final JsonArray results = event.body().getJsonArray("results");
                if ("ok".equals(event.body().getString("status")) && results != null && results.size() > 0) {
                    message.reply(event.body().put("result", results.getJsonArray(0)));
                } else {
                    message.reply(event.body());
                }
            }
        });
    } catch (TransactionException e) {
        logger.error("Error in transaction when remove user from structure", e);
        sendError(message, "transaction.error");
    }
}

From source file:org.entcore.feeder.ManualFeeder.java

License:Open Source License

public void updateUser(final Message<JsonObject> message) {
    final JsonObject user = getMandatoryObject("data", message);
    if (user == null)
        return;/*w  w  w .  j a v a  2  s.c om*/
    final String userId = getMandatoryString("userId", message);
    if (userId == null)
        return;
    String q = "MATCH (u:User { id : {userId}})-[:IN]->(pg:ProfileGroup)-[:HAS_PROFILE]->(p:Profile) "
            + "RETURN DISTINCT p.name as profile ";
    neo4j.execute(q, new JsonObject().put("userId", userId), new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> r) {
            JsonArray res = r.body().getJsonArray("result");
            if ("ok".equals(r.body().getString("status")) && res != null && res.size() > 0) {
                for (Object o : res) {
                    if (!(o instanceof JsonObject))
                        continue;
                    String profile = ((JsonObject) o).getString("profile");
                    Validator v = profiles.get(profile);
                    if (v == null) {
                        sendError(message, "Invalid profile : " + profile);
                        return;
                    }
                    final String error = v.modifiableValidate(user);
                    if (error != null) {
                        logger.error(error);
                        sendError(message, error);
                        return;
                    }
                }
                String query = "MATCH (u:User { id : {userId}}) " + "SET "
                        + Neo4jUtils.nodeSetPropertiesFromJson("u", user) + "RETURN DISTINCT u.id as id ";
                JsonObject params = user.put("userId", userId);
                neo4j.execute(query, params, new Handler<Message<JsonObject>>() {
                    @Override
                    public void handle(Message<JsonObject> m) {
                        message.reply(m.body());
                    }
                });
            } else {
                sendError(message, "Invalid profile.");
            }
        }
    });
}

From source file:org.entcore.feeder.ManualFeeder.java

License:Open Source License

public void deleteUser(final Message<JsonObject> message) {
    final JsonArray users = message.body().getJsonArray("users");
    if (users == null || users.size() == 0) {
        sendError(message, "Missing users.");
        return;/*  w w  w.  j a  va 2  s. c o  m*/
    }
    String query = "MATCH (u:User)"
            + "WHERE u.id IN {users} AND (u.source IN ['MANUAL', 'CSV', 'CLASS_PARAM', 'BE1D'] OR HAS(u.disappearanceDate)) "
            + "return count(*) as count ";
    neo4j.execute(query, new JsonObject().put("users", users), new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> event) {
            JsonArray res = event.body().getJsonArray("result");
            if ("ok".equals(event.body().getString("status")) && res != null && res.size() == 1) {
                JsonObject j = res.getJsonObject(0);
                if (users.size() == j.getInteger("count", 0)) {
                    executeTransaction(message, new VoidFunction<TransactionHelper>() {
                        @Override
                        public void apply(TransactionHelper tx) {
                            for (Object o : users) {
                                User.backupRelationship(o.toString(), tx);
                                User.preDelete(o.toString(), tx);
                            }
                        }
                    });
                } else {
                    sendError(message, "unauthorized.user");
                }
            } else {
                message.reply(event.body());
            }
        }
    });
}

From source file:org.entcore.feeder.ManualFeeder.java

License:Open Source License

public void restoreUser(final Message<JsonObject> message) {
    final JsonArray users = message.body().getJsonArray("users");
    if (users == null || users.size() == 0) {
        sendError(message, "Missing users.");
        return;/* w  w w  . j ava 2 s .  com*/
    }
    executeTransaction(message, new VoidFunction<TransactionHelper>() {
        @Override
        public void apply(TransactionHelper tx) {
            for (Object o : users) {
                User.restorePreDeleted(o.toString(), tx);
            }
        }
    });
}

From source file:org.entcore.feeder.ManualFeeder.java

License:Open Source License

public void createFunctionGroup(Message<JsonObject> message) {
    final JsonArray functions = message.body().getJsonArray("functions");
    if (functions == null || functions.size() == 0) {
        sendError(message, "missing.functions");
        return;/*w  w  w  .  j a  va  2  s. c o m*/
    }
    final String name = message.body().getString("name");
    if (name == null || name.trim().isEmpty()) {
        sendError(message, "missing.name");
        return;
    }
    final String externalId = message.body().getString("externalId");
    if (externalId == null || externalId.trim().isEmpty()) {
        sendError(message, "missing.externalId");
        return;
    }
    executeTransaction(message, new VoidFunction<TransactionHelper>() {
        @Override
        public void apply(TransactionHelper tx) {
            Profile.createFunctionGroup(functions, name, externalId, tx);
        }
    });
}

From source file:org.entcore.feeder.ManualFeeder.java

License:Open Source License

public void addUserFunction(final Message<JsonObject> message) {
    final String userId = getMandatoryString("userId", message);
    final String function = message.body().getString("function");
    if (userId == null || function == null)
        return;/*from ww w  .  ja  v a 2  s . co m*/
    final JsonArray scope = message.body().getJsonArray("scope");
    String inherit = message.body().getString("inherit", "");
    if (scope != null && ("s".equals(inherit) || "sc".equals(inherit))) {
        String query;
        if ("sc".equals(inherit)) {
            query = "MATCH (s:Structure)<-[:HAS_ATTACHMENT*0..]-(:Structure)<-[:BELONGS*0..1]-(scope) "
                    + "WHERE s.id IN {scope} " + "RETURN COLLECT(scope.id) as ids ";
        } else {
            query = "MATCH (s:Structure)<-[:HAS_ATTACHMENT*0..]-(scope:Structure) " + "WHERE s.id IN {scope} "
                    + "RETURN COLLECT(scope.id) as ids ";
        }
        neo4j.execute(query, new JsonObject().put("scope", scope), new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> event) {
                JsonArray result = event.body().getJsonArray("result");
                if ("ok".equals(event.body().getString("status")) && result != null && result.size() == 1) {
                    final JsonArray s = result.getJsonObject(0).getJsonArray("ids");
                    executeTransaction(message, new VoidFunction<TransactionHelper>() {
                        @Override
                        public void apply(TransactionHelper tx) {
                            User.addFunction(userId, function, s, tx);
                        }
                    });
                } else {
                    sendError(message, "invalid.scope");
                }
            }
        });
    } else {
        executeTransaction(message, new VoidFunction<TransactionHelper>() {
            @Override
            public void apply(TransactionHelper tx) {
                User.addFunction(userId, function, scope, tx);
            }
        });
    }
}

From source file:org.entcore.feeder.timetable.AbstractTimetableImporter.java

License:Open Source License

protected void init(final Handler<AsyncResult<Void>> handler) throws TransactionException {
    importTimestamp = System.currentTimeMillis();
    final String externalIdFromUAI = "MATCH (s:Structure {UAI : {UAI}}) "
            + "return s.externalId as externalId, s.id as id, s.timetable as timetable ";
    final String tma = getTeacherMappingAttribute();
    final String getUsersByProfile = "MATCH (:Structure {UAI : {UAI}})<-[:DEPENDS]-(:ProfileGroup)<-[:IN]-(u:User) "
            + "WHERE head(u.profiles) = {profile} AND NOT(u." + tma + " IS NULL) "
            + "RETURN DISTINCT u.id as id, u." + tma
            + " as tma, head(u.profiles) as profile, u.source as source, "
            + "u.lastName as lastName, u.firstName as firstName";
    final String classesMappingQuery = "MATCH (s:Structure {UAI : {UAI}})<-[:MAPPING]-(cm:ClassesMapping) "
            + "return cm.mapping as mapping ";
    final String subjectsMappingQuery = "MATCH (s:Structure {UAI : {UAI}})<-[:SUBJECT]-(sub:Subject) return sub.code as code, sub.id as id";
    final TransactionHelper tx = TransactionManager.getTransaction();
    tx.add(getUsersByProfile, new JsonObject().put("UAI", UAI).put("profile", "Teacher"));
    tx.add(externalIdFromUAI, new JsonObject().put("UAI", UAI));
    tx.add(classesMappingQuery, new JsonObject().put("UAI", UAI));
    tx.add(subjectsMappingQuery, new JsonObject().put("UAI", UAI));
    tx.commit(new Handler<Message<JsonObject>>() {
        @Override//from   w ww. j av a2s  . co m
        public void handle(Message<JsonObject> event) {
            final JsonArray res = event.body().getJsonArray("results");
            if ("ok".equals(event.body().getString("status")) && res != null && res.size() == 4) {
                try {
                    for (Object o : res.getJsonArray(0)) {
                        if (o instanceof JsonObject) {
                            final JsonObject j = (JsonObject) o;
                            teachersMapping.put(j.getString("tma"),
                                    new String[] { j.getString("id"), j.getString("source") });
                            teachersCleanNameMapping.put(
                                    Validator.sanitize(j.getString("firstName") + j.getString("lastName")),
                                    new String[] { j.getString("id"), j.getString("source") });
                        }
                    }
                    JsonArray a = res.getJsonArray(1);
                    if (a != null && a.size() == 1) {
                        structureExternalId = a.getJsonObject(0).getString("externalId");
                        structure.add(structureExternalId);
                        structureId = a.getJsonObject(0).getString("id");
                        if (!getSource().equals(a.getJsonObject(0).getString("timetable"))) {
                            handler.handle(new DefaultAsyncResult<Void>(
                                    new TransactionException("different.timetable.type")));
                            return;
                        }
                    } else {
                        handler.handle(new DefaultAsyncResult<Void>(new ValidationException("invalid.uai")));
                        return;
                    }
                    JsonArray cm = res.getJsonArray(2);
                    if (cm != null && cm.size() == 1) {
                        try {
                            final JsonObject cmn = cm.getJsonObject(0);
                            log.info(cmn.encode());
                            if (isNotEmpty(cmn.getString("mapping"))) {
                                classesMapping = new JsonObject(cmn.getString("mapping"));
                                log.info("classMapping : " + classesMapping.encodePrettily());
                            } else {
                                classesMapping = new JsonObject();
                            }
                        } catch (Exception ecm) {
                            classesMapping = new JsonObject();
                            log.error(ecm.getMessage(), ecm);
                        }
                    }
                    JsonArray subjects = res.getJsonArray(3);
                    if (subjects != null && subjects.size() > 0) {
                        for (Object o : subjects) {
                            if (o instanceof JsonObject) {
                                final JsonObject s = (JsonObject) o;
                                subjectsMapping.put(s.getString("code"), s.getString("id"));
                            }
                        }
                    }
                    txXDT = TransactionManager.getTransaction();
                    persEducNat = new PersEducNat(txXDT, report, getSource());
                    persEducNat.setMapping(
                            "dictionary/mapping/" + getSource().toLowerCase() + "/PersEducNat.json");
                    handler.handle(new DefaultAsyncResult<>((Void) null));
                } catch (Exception e) {
                    handler.handle(new DefaultAsyncResult<Void>(e));
                }
            } else {
                handler.handle(new DefaultAsyncResult<Void>(
                        new TransactionException(event.body().getString("message"))));
            }
        }
    });
}