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.directory.security.TeacherOfClass.java

License:Open Source License

private void validateQuery(final HttpServerRequest request, final Handler<Boolean> handler, String query,
        JsonObject params) {//from ww  w .  ja va  2s  .  co  m
    request.pause();
    neo.execute(query, params, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> r) {
            request.resume();
            JsonArray res = r.body().getJsonArray("result");
            handler.handle("ok".equals(r.body().getString("status")) && res.size() == 1
                    && ((JsonObject) res.getJsonObject(0)).getBoolean("exists", false));
        }
    });
}

From source file:org.entcore.directory.security.TeacherOfUser.java

License:Open Source License

@Override
public void authorize(final HttpServerRequest resourceRequest, Binding binding, final UserInfos user,
        final Handler<Boolean> handler) {
    RequestUtils.bodyToJson(resourceRequest, new Handler<JsonObject>() {
        @Override/*w  w  w .  ja  v  a  2s .  com*/
        public void handle(JsonObject event) {
            if (event != null) {
                JsonArray userIds = event.getJsonArray("users");
                if (userIds == null || userIds.size() == 0 || userIds.contains(user.getUserId())
                        || (!"Teacher".equals(user.getType()) && !"Personnel".equals(user.getType()))) {
                    handler.handle(false);
                    return;
                }
                String query = "MATCH (t:User { id : {teacherId}})-[:IN]->(g:ProfileGroup)-[:DEPENDS]->(c:Class) "
                        + "WITH c " + "MATCH c<-[:DEPENDS]-(og:ProfileGroup)<-[:IN]-(u:User) "
                        + "WHERE u.id IN {userIds} " + "RETURN count(distinct u) = {size} as exists ";
                JsonObject params = new JsonObject().put("userIds", userIds).put("teacherId", user.getUserId())
                        .put("size", userIds.size());
                validateQuery(resourceRequest, handler, query, params);
            } else {
                handler.handle(false);
            }
        }
    });

}

From source file:org.entcore.directory.security.TeacherOfUser.java

License:Open Source License

private void validateQuery(final HttpServerRequest request, final Handler<Boolean> handler, String query,
        JsonObject params) {/*from  www .j  a va  2  s. co m*/
    request.pause();
    neo.execute(query, params, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> r) {
            request.resume();
            JsonArray res = r.body().getJsonArray("result");
            handler.handle("ok".equals(r.body().getString("status")) && res.size() == 1
                    && (res.getJsonObject(0)).getBoolean("exists", false));
        }
    });
}

From source file:org.entcore.directory.services.impl.DefaultClassService.java

License:Open Source License

@Override
public void findUsers(String classId, JsonArray expectedTypes, Handler<Either<String, JsonArray>> results) {
    String filter;//w w w .j  a va  2s.c  o m
    JsonObject params = new JsonObject().put("classId", classId);
    if (expectedTypes == null || expectedTypes.size() < 1) {
        filter = "";
    } else {
        filter = "WHERE p.name IN {expected} ";
        params.put("expected", expectedTypes);
    }
    String query = "MATCH (c:`Class` { id : {classId}})<-[:DEPENDS]-(cpg:ProfileGroup)"
            + "-[:DEPENDS]->(spg:ProfileGroup)-[:HAS_PROFILE]->(p:Profile), cpg<-[:IN]-(m:User) " + filter
            + "RETURN distinct m.lastName as lastName, m.firstName as firstName, m.id as id, "
            + "CASE WHEN m.loginAlias IS NOT NULL THEN m.loginAlias ELSE m.login END as login, m.activationCode as activationCode, m.birthDate as birthDate, "
            + "p.name as type, m.blocked as blocked, m.source as source " + "ORDER BY type, lastName ";
    neo.execute(query, params, validResultHandler(results));
}

From source file:org.entcore.directory.services.impl.DefaultClassService.java

License:Open Source License

@Override
public void addUser(final String classId, final String userId, final UserInfos user,
        final Handler<Either<String, JsonObject>> result) {
    if (validationParamsError(result, classId, userId))
        return;/*from  w ww  .  j  a v a 2s  . co  m*/
    if (user == null) {
        result.handle(new Either.Left<String, JsonObject>("invalid.userinfos"));
        return;
    }
    neo.execute(
            "MATCH (u:`User` {id : {id}})-[:IN]->(pg:ProfileGroup)-[:HAS_PROFILE]->(p:Profile) "
                    + "RETURN distinct p.name as type",
            new JsonObject().put("id", 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() == 1) {
                        final String t = (res.getJsonObject(0)).getString("type");
                        String customReturn = "MATCH (c:`Class` { id : {classId}})<-[:DEPENDS]-(cpg:ProfileGroup)"
                                + "-[:DEPENDS]->(spg:ProfileGroup)-[:HAS_PROFILE]->(p:Profile {name : {profile}}), "
                                + "c-[:BELONGS]->(s:Structure) " + "WHERE visibles.id = {uId} "
                                + "CREATE UNIQUE visibles-[:IN]->cpg "
                                + "RETURN DISTINCT visibles.id as id, s.id as schoolId";
                        JsonObject params = new JsonObject().put("classId", classId).put("uId", userId)
                                .put("profile", t);
                        UserUtils.findVisibleUsers(eb, user.getUserId(), false, customReturn, params,
                                new Handler<JsonArray>() {

                                    @Override
                                    public void handle(final JsonArray users) {
                                        if (users != null && users.size() == 1) {
                                            if ("Student".equals(t)) {
                                                String query = "MATCH (c:`Class` { id : {classId}})<-[:DEPENDS]-(cpg:ProfileGroup)-[:DEPENDS]->(spg:ProfileGroup)"
                                                        + "-[:HAS_PROFILE]->(p:Profile {name : 'Relative'}), "
                                                        + "(u:User {id: {uId}})-[:RELATED]->(relative: User) "
                                                        + "CREATE UNIQUE relative-[:IN]->cpg "
                                                        + "RETURN count(relative) as relativeNb";

                                                JsonObject params = new JsonObject().put("classId", classId)
                                                        .put("uId", userId);

                                                neo.execute(query, params, Neo4jResult.validEmptyHandler(
                                                        new Handler<Either<String, JsonObject>>() {
                                                            public void handle(
                                                                    Either<String, JsonObject> event) {
                                                                if (event.isLeft()) {
                                                                    result.handle(
                                                                            new Either.Left<String, JsonObject>(
                                                                                    "error.while.attaching.relatives"));
                                                                } else {
                                                                    result.handle(
                                                                            new Either.Right<String, JsonObject>(
                                                                                    users.getJsonObject(0)));
                                                                }
                                                            }
                                                        }));
                                            } else {
                                                result.handle(new Either.Right<String, JsonObject>(
                                                        users.getJsonObject(0)));
                                            }
                                        } else {
                                            result.handle(
                                                    new Either.Left<String, JsonObject>("user.not.visible"));
                                        }
                                    }
                                });
                    } else {
                        result.handle(new Either.Left<String, JsonObject>("invalid.user"));
                    }
                }
            });
}

From source file:org.entcore.directory.services.impl.DefaultGroupService.java

License:Open Source License

@Override
public void listAdmin(String structureId, UserInfos userInfos, JsonArray expectedTypes,
        Handler<Either<String, JsonArray>> results) {
    if (userInfos == null) {
        results.handle(new Either.Left<String, JsonArray>("invalid.user"));
        return;//w w  w . j  a  v  a2  s .  co m
    }
    String condition;
    if (expectedTypes != null && expectedTypes.size() > 0) {
        condition = "WHERE (g:" + Joiner.on(" OR g:").join(expectedTypes) + ") ";
    } else {
        condition = "WHERE g:Group ";
    }

    JsonObject params = new JsonObject();
    if (!userInfos.getFunctions().containsKey(SUPER_ADMIN)
            && !userInfos.getFunctions().containsKey(ADMIN_LOCAL)) {
        results.handle(new Either.Left<String, JsonArray>("forbidden"));
        return;
    } else if (userInfos.getFunctions().containsKey(ADMIN_LOCAL)) {
        UserInfos.Function f = userInfos.getFunctions().get(ADMIN_LOCAL);
        List<String> scope = f.getScope();
        if (scope != null && !scope.isEmpty()) {
            condition += "AND s.id IN {structures} ";
            params.put("structures", new fr.wseduc.webutils.collections.JsonArray(scope));
        }
    }

    if (structureId != null && !structureId.trim().isEmpty()) {
        condition += " AND s.id = {structure} ";
        params.put("structure", structureId);
    }
    String query = "MATCH (s:Structure)<-[:BELONGS*0..1]-()<-[:DEPENDS]-(g) " + condition
            + "OPTIONAL MATCH (s)<-[:BELONGS]-(c: Class)<-[:DEPENDS]-(g) "
            + "WITH g, collect({name: c.name, id: c.id}) as classes, "
            + "HEAD(filter(x IN labels(g) WHERE x <> 'Visible' AND x <> 'Group')) as type "
            + "RETURN DISTINCT g.id as id, g.name as name, g.displayName as displayName, type, "
            + "CASE WHEN any(x in classes where x <> {name: null, id: null}) THEN classes END as classes,"
            + "CASE WHEN (g: ProfileGroup)-[:DEPENDS]-(:Structure) THEN 'StructureGroup' END as subType";
    neo.execute(query, params, validResultHandler(results));
}

From source file:org.entcore.directory.services.impl.DefaultSchoolService.java

License:Open Source License

@Override
public void list(JsonArray fields, Handler<Either<String, JsonArray>> results) {
    if (fields == null || fields.size() == 0) {
        fields = new fr.wseduc.webutils.collections.JsonArray().add("id").add("externalId").add("name")
                .add("UAI");
    }/*  w w w .  j  a v a  2  s .  c  o  m*/
    StringBuilder query = new StringBuilder();
    query.append("MATCH (s:Structure) RETURN ");
    for (Object field : fields) {
        query.append(" s.").append(field).append(" as ").append(field).append(",");
    }
    query.deleteCharAt(query.length() - 1);
    neo.execute(query.toString(), (JsonObject) null, validResultHandler(results));
}

From source file:org.entcore.directory.services.impl.DefaultShareBookmarkService.java

License:Open Source License

@Override
public void list(String userId, Handler<Either<String, JsonArray>> handler) {
    final String query = "MATCH (:User {id:{userId}})-[:HAS_SB]->(sb:ShareBookmark) return sb";
    JsonObject params = new JsonObject();
    params.put("userId", userId);
    neo4j.execute(query, params, fullNodeMergeHandler("sb", node -> {
        if (node.isRight()) {
            final JsonObject j = node.right().getValue();
            final JsonArray result = new JsonArray();
            for (String id : j.fieldNames()) {
                final JsonArray value = j.getJsonArray(id);
                if (value == null || value.size() < 2) {
                    delete(userId, id, dres -> {
                        if (dres.isLeft()) {
                            log.error("Error deleting sharebookmark " + id + " : " + dres.left().getValue());
                        }/*from  w w w.ja  va 2  s . c o m*/
                    });
                    continue;
                }
                final JsonObject r = new fr.wseduc.webutils.collections.JsonObject();
                r.put("id", id);
                r.put("name", value.remove(0));
                //r.put("membersIds", value);
                result.add(r);
            }
            handler.handle(new Either.Right<>(result));
        } else {
            handler.handle(new Either.Left<>(node.left().getValue()));
        }
    }));
}

From source file:org.entcore.directory.services.impl.DefaultUserBookService.java

License:Open Source License

@Override
public void update(String userId, JsonObject userBook, final Handler<Either<String, JsonObject>> result) {
    JsonObject u = Utils.validAndGet(userBook, UPDATE_USERBOOK_FIELDS, Collections.<String>emptyList());
    if (Utils.defaultValidationError(u, result, userId))
        return;//from   w w  w . ja va 2 s . co  m
    // OVERRIDE AVATAR URL
    Optional<String> pictureId = getPictureIdForUserbook(userBook);
    if (pictureId.isPresent()) {
        String fileId = avatarFileNameFromUserId(userId, Optional.empty());
        u.put("picture", "/userbook/avatar/" + fileId);
    }

    StatementsBuilder b = new StatementsBuilder();
    String query = "MATCH (u:`User` { id : {id}})-[:USERBOOK]->(ub:UserBook) WITH ub.picture as oldpic,ub,u SET "
            + nodeSetPropertiesFromJson("ub", u);
    query += " RETURN oldpic,ub.picture as picture";
    boolean updateUserBook = u.size() > 0;
    if (updateUserBook) {
        b.add(query, u.put("id", userId));
    }
    String q2 = "MATCH (u:`User` { id : {id}})-[:USERBOOK]->(ub:UserBook)"
            + "-[:PUBLIC|PRIVE]->(h:`Hobby` { category : {category}}) " + "SET h.values = {values} ";
    JsonArray hobbies = userBook.getJsonArray("hobbies");
    if (hobbies != null) {
        for (Object o : hobbies) {
            if (!(o instanceof JsonObject))
                continue;
            JsonObject j = (JsonObject) o;
            b.add(q2, j.put("id", userId));
        }
    }
    neo.executeTransaction(b.build(), null, true, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> r) {
            if ("ok".equals(r.body().getString("status"))) {
                if (updateUserBook) {
                    JsonArray results = r.body().getJsonArray("results", new JsonArray());
                    JsonArray firstStatement = results.getJsonArray(0);
                    if (firstStatement != null && firstStatement.size() > 0) {
                        JsonObject object = firstStatement.getJsonObject(0);
                        String picture = object.getString("picture", "");
                        cacheAvatarFromUserBook(userId, pictureId, StringUtils.isEmpty(picture))
                                .setHandler(e -> {
                                    if (e.succeeded()) {
                                        result.handle(new Either.Right<String, JsonObject>(new JsonObject()));
                                    } else {
                                        result.handle(new Either.Left<String, JsonObject>(
                                                r.body().getString("message", "update.error")));
                                    }
                                });
                    }
                } else {
                    result.handle(new Either.Right<String, JsonObject>(new JsonObject()));
                }
            } else {
                result.handle(
                        new Either.Left<String, JsonObject>(r.body().getString("message", "update.error")));
            }
        }
    });
}

From source file:org.entcore.directory.services.impl.DefaultUserService.java

License:Open Source License

@Override
public void list(String structureId, String classId, JsonArray expectedProfiles,
        Handler<Either<String, JsonArray>> results) {
    JsonObject params = new JsonObject();
    String filterProfile = "";
    String filterStructure = "";
    String filterClass = "";
    if (expectedProfiles != null && expectedProfiles.size() > 0) {
        filterProfile = "WHERE p.name IN {expectedProfiles} ";
        params.put("expectedProfiles", expectedProfiles);
    }//from  www .j a  v  a 2s . c o m
    if (classId != null && !classId.trim().isEmpty()) {
        filterClass = "(g:ProfileGroup)-[:DEPENDS]->(n:Class {id : {classId}}), ";
        params.put("classId", classId);
    } else if (structureId != null && !structureId.trim().isEmpty()) {
        filterStructure = "(pg:ProfileGroup)-[:DEPENDS]->(n:Structure {id : {structureId}}), ";
        params.put("structureId", structureId);
    }
    String query = "MATCH " + filterClass + filterStructure
            + "(u:User)-[:IN]->g-[:DEPENDS*0..1]->pg-[:HAS_PROFILE]->(p:Profile) " + filterProfile
            + "RETURN DISTINCT u.id as id, p.name as type, u.externalId as externalId, u.IDPN as IDPN, "
            + "u.activationCode as code, u.login as login, u.firstName as firstName, "
            + "u.lastName as lastName, u.displayName as displayName " + "ORDER BY type DESC, displayName ASC ";
    neo.execute(query, params, validResultHandler(results));
}