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

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

Introduction

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

Prototype

public JsonObject getJsonObject(int pos) 

Source Link

Document

Get the JsonObject at position pos in the array.

Usage

From source file:org.entcore.common.share.impl.MongoDbShareService.java

License:Open Source License

private void removeShare(String resourceId, final String shareId, List<String> removeActions, boolean isGroup,
        final Handler<Either<String, JsonObject>> handler) {
    final String shareIdAttr = isGroup ? "groupId" : "userId";
    final List<String> actions = findRemoveActions(removeActions);
    QueryBuilder query = QueryBuilder.start("_id").is(resourceId);
    JsonObject keys = new JsonObject().put("shared", 1);
    final JsonObject q = MongoQueryBuilder.build(query);
    mongo.findOne(collection, q, keys, new Handler<Message<JsonObject>>() {
        @Override/* w  w  w. j  av  a 2s .c o  m*/
        public void handle(Message<JsonObject> event) {
            if ("ok".equals(event.body().getString("status")) && event.body().getJsonObject("result") != null) {
                JsonArray actual = event.body().getJsonObject("result").getJsonArray("shared",
                        new fr.wseduc.webutils.collections.JsonArray());
                JsonArray shared = new fr.wseduc.webutils.collections.JsonArray();
                for (int i = 0; i < actual.size(); i++) {
                    JsonObject s = actual.getJsonObject(i);
                    String id = s.getString(shareIdAttr);
                    if (shareId.equals(id)) {
                        if (actions != null) {
                            for (String action : actions) {
                                s.remove(action);
                            }
                            if (s.size() > 1) {
                                shared.add(s);
                            }
                        }
                    } else {
                        shared.add(s);
                    }
                }
                MongoUpdateBuilder updateQuery = new MongoUpdateBuilder().set("shared", shared);
                mongo.update(collection, q, updateQuery.build(), new Handler<Message<JsonObject>>() {
                    @Override
                    public void handle(Message<JsonObject> res) {
                        handler.handle(Utils.validResult(res));
                    }
                });
            } else {
                handler.handle(new Either.Left<String, JsonObject>("Resource not found."));
            }
        }
    });
}

From source file:org.entcore.common.sql.SqlResult.java

License:Open Source License

private static Either<String, JsonObject> validUnique(Either<String, JsonArray> r) {
    if (r.isRight()) {
        JsonArray results = r.right().getValue();
        if (results == null || results.size() == 0) {
            return new Either.Right<>(new JsonObject());
        }//from  ww  w  .j  a va 2  s .co m
        if (results.size() == 1 && (results.getValue(0) instanceof JsonObject)) {
            return new Either.Right<>(results.getJsonObject(0));
        }
        return new Either.Left<>("non.unique.result");
    } else {
        return new Either.Left<>(r.left().getValue());
    }
}

From source file:org.entcore.common.sql.SqlResult.java

License:Open Source License

public static Either<String, JsonArray> validResult(int idx, Message<JsonObject> res) {
    if ("ok".equals(res.body().getString("status"))) {
        JsonArray a = res.body().getJsonArray("results");
        if (a != null && idx < a.size()) {
            return jsonToEither(a.getJsonObject(idx).put("jsonb_fields",
                    res.body().getJsonArray("jsonb_fields", new fr.wseduc.webutils.collections.JsonArray())));
        } else {// w ww.  j  ava 2  s .com
            return new Either.Left<>("missing.result");
        }
    } else {
        return new Either.Left<>(res.body().getString("message", ""));
    }
}

From source file:org.entcore.common.sql.SqlResult.java

License:Open Source License

public static Either<String, JsonObject> validRowsResult(int idx, Message<JsonObject> res) {
    if ("ok".equals(res.body().getString("status"))) {
        JsonArray a = res.body().getJsonArray("results");
        if (a != null && idx < a.size()) {
            return validRows(a.getJsonObject(idx));
        } else {/*from w w  w .  j a va2s.  c om*/
            return new Either.Left<>("missing.result");
        }
    } else {
        return new Either.Left<>(res.body().getString("message", ""));
    }
}

From source file:org.entcore.common.storage.impl.PostgresqlApplicationStorage.java

License:Open Source License

protected void getInfoProcess(final String fileId, final Handler<AsyncResult<FileInfos>> handler) {
    sql.prepared(getInfoQuery(), new fr.wseduc.webutils.collections.JsonArray().add(fileId),
            new Handler<Message<JsonObject>>() {
                @Override/*from   ww w .  j a v  a 2s  .c  o m*/
                public void handle(Message<JsonObject> event) {
                    Either<String, JsonArray> r = validResult(event);
                    if (r.isRight()) {
                        final JsonArray result = r.right().getValue();
                        if (result.size() == 1) {
                            final JsonObject res = result.getJsonObject(0);
                            final FileInfos fi = new FileInfos();
                            fi.setApplication(application);
                            fi.setId(fileId);
                            fi.setName(res.getString(mapping.getString("name", "name")));
                            fi.setOwner(res.getString(mapping.getString("owner", "owner")));
                            fi.setSize(res.getInteger(mapping.getString("size", "size")));
                            handler.handle(new DefaultAsyncResult<>(fi));
                        } else {
                            handler.handle(new DefaultAsyncResult<>((FileInfos) null));
                        }
                    } else {
                        handler.handle(
                                new DefaultAsyncResult<FileInfos>(new StorageException(r.left().getValue())));
                    }
                }
            });
}

From source file:org.entcore.communication.filters.CommunicationFilter.java

License:Open Source License

private void check(String query, JsonObject params, final Handler<Boolean> handler) {
    neo4j.execute(query, params, new Handler<Message<JsonObject>>() {
        @Override/*from  w w  w.  j  av  a2  s  .co m*/
        public void handle(Message<JsonObject> event) {
            JsonArray r = event.body().getJsonArray("result");
            handler.handle("ok".equals(event.body().getString("status")) && r != null && r.size() == 1
                    && (r.getJsonObject(0)).getBoolean("exists", false));
        }
    });
}

From source file:org.entcore.conversation.service.impl.DefaultConversationService.java

License:Open Source License

private void addDisplayNames(final JsonObject message, final Handler<JsonObject> handler) {
    String query = "MATCH (v:Visible) " + "WHERE v.id IN {ids} "
            + "RETURN COLLECT(distinct (v.id + '$' + coalesce(v.displayName, ' ') + '$' + "
            + "coalesce(v.name, ' ') + '$' + coalesce(v.groupDisplayName, ' '))) as displayNames ";
    Set<String> ids = new HashSet<>();
    ids.addAll(message.getJsonArray("to", new fr.wseduc.webutils.collections.JsonArray()).getList());
    ids.addAll(message.getJsonArray("cc", new fr.wseduc.webutils.collections.JsonArray()).getList());
    if (message.containsKey("from")) {
        ids.add(message.getString("from"));
    }/* www. j  av a2  s. com*/
    neo.execute(query,
            new JsonObject().put("ids", new fr.wseduc.webutils.collections.JsonArray(new ArrayList<>(ids))),
            new Handler<Message<JsonObject>>() {
                @Override
                public void handle(Message<JsonObject> m) {
                    JsonArray r = m.body().getJsonArray("result");
                    if ("ok".equals(m.body().getString("status")) && r != null && r.size() == 1) {
                        JsonObject j = r.getJsonObject(0);
                        JsonArray d = j.getJsonArray("displayNames");
                        if (d != null && d.size() > 0) {
                            message.put("displayNames", d);
                        }
                    }
                    handler.handle(message);
                }
            });
}

From source file:org.entcore.conversation.service.impl.DefaultConversationService.java

License:Open Source License

private void sendWithoutAttachments(final String parentMessageId, String messageId, UserInfos user,
        final Handler<Either<String, JsonObject>> result) {
    String usersQuery;//from  w ww . ja va2s.  co  m
    JsonObject params = new JsonObject().put("userId", user.getUserId()).put("messageId", messageId)
            .put("draft", State.DRAFT.name()).put("outbox", "OUTBOX").put("inbox", "INBOX")
            .put("sent", State.SENT.name()).put("true", true);
    if (parentMessageId != null && !parentMessageId.trim().isEmpty()) { // reply
        usersQuery = "MATCH (m:ConversationMessage { id : {parentMessageId}}) "
                + "WITH (COLLECT(visibles.id) + coalesce(m.to, '') + coalesce(m.cc, '') + coalesce(m.from, '')) as vis "
                + "MATCH (v:Visible) " + "WHERE v.id IN vis " + "WITH DISTINCT v ";
        params.put("parentMessageId", parentMessageId);
    } else {
        usersQuery = "WITH visibles as v ";
    }
    String query = usersQuery + "MATCH v<-[:IN*0..1]-(u:User), (message:ConversationMessage) "
            + "WHERE (v: User or v:Group) "
            + "AND message.id = {messageId} AND message.state = {draft} AND message.from = {userId} AND "
            + "(v.id IN message.to OR v.id IN message.cc) "
            + "WITH DISTINCT u, message, (v.id + '$' + coalesce(v.displayName, ' ') + '$' + "
            + "coalesce(v.name, ' ') + '$' + coalesce(v.groupDisplayName, ' ')) as dNames "
            + "MATCH u-[:HAS_CONVERSATION]->(c:Conversation {active:{true}})"
            + "-[:HAS_CONVERSATION_FOLDER]->(f:ConversationSystemFolder {name:{inbox}}) "
            + "CREATE UNIQUE f-[:HAS_CONVERSATION_MESSAGE { unread: {true} }]->message "
            + "WITH COLLECT(c.userId) as sentIds, COLLECT(u) as users, message, "
            + "COLLECT(distinct dNames) as displayNames "
            + "MATCH (s:User {id : {userId}})-[:HAS_CONVERSATION]->(:Conversation)"
            + "-[:HAS_CONVERSATION_FOLDER]->(fOut:ConversationSystemFolder {name : {outbox}}), "
            + "message<-[r:HAS_CONVERSATION_MESSAGE]-(fDraft:ConversationSystemFolder {name : {draft}}) "
            + "SET message.state = {sent}, "
            + "message.displayNames = displayNames + (s.id + '$' + coalesce(s.displayName, ' ') + '$ $ ') "
            + "CREATE UNIQUE fOut-[:HAS_CONVERSATION_MESSAGE { insideFolder: r.insideFolder }]->message "
            + "DELETE r "
            + "RETURN EXTRACT(u IN FIlTER(x IN users WHERE NOT(x.id IN sentIds)) | u.displayName) as undelivered,  "
            + "EXTRACT(u IN FIlTER(x IN users WHERE x.id IN sentIds AND NOT(x.activationCode IS NULL)) "
            + "| u.displayName) as inactive, LENGTH(sentIds) as sent, "
            + "sentIds, message.id as id, message.subject as subject";
    findVisibles(eb, user.getUserId(), query, params, true, true, false, new Handler<JsonArray>() {
        @Override
        public void handle(JsonArray event) {
            if (event != null && event.size() == 1 && (event.getValue(0) instanceof JsonObject)) {
                result.handle(new Either.Right<String, JsonObject>(event.getJsonObject(0)));
            } else {
                result.handle(new Either.Left<String, JsonObject>("conversation.send.error"));
            }
        }
    });
}

From source file:org.entcore.conversation.service.impl.DefaultConversationService.java

License:Open Source License

private void sendWithAttachments(final String parentMessageId, final String messageId, JsonArray attachments,
        final UserInfos user, final Handler<Either<String, JsonObject>> result) {
    long totalAttachmentsSize = 0l;
    for (Object o : attachments) {
        totalAttachmentsSize = totalAttachmentsSize + ((JsonObject) o).getLong("size", 0l);
    }/* w ww .j a  v  a2 s  .c  om*/

    final String usersQuery;
    JsonObject params = new JsonObject().put("userId", user.getUserId()).put("messageId", messageId)
            .put("draft", State.DRAFT.name()).put("outbox", "OUTBOX").put("inbox", "INBOX")
            .put("sent", State.SENT.name()).put("attachmentsSize", totalAttachmentsSize).put("true", true);
    if (parentMessageId != null && !parentMessageId.trim().isEmpty()) { // reply
        usersQuery = "MATCH (m:ConversationMessage { id : {parentMessageId}}) "
                + "WITH (COLLECT(visibles.id) + coalesce(m.to, '') + coalesce(m.cc, '') + coalesce(m.from, '')) as vis "
                + "MATCH (v:Visible) " + "WHERE v.id IN vis " + "WITH DISTINCT v ";
        params.put("parentMessageId", parentMessageId);
    } else {
        usersQuery = "WITH visibles as v ";
    }

    String query = usersQuery
            + "MATCH v<-[:IN*0..1]-(u:User), (message:ConversationMessage)-[:HAS_ATTACHMENT]->(attachment: MessageAttachment) "
            + "WHERE (v: User or v:Group) "
            + "AND message.id = {messageId} AND message.state = {draft} AND message.from = {userId} AND "
            + "(v.id IN message.to OR v.id IN message.cc) "
            + "WITH DISTINCT u, message, (v.id + '$' + coalesce(v.displayName, ' ') + '$' + "
            + "coalesce(v.name, ' ') + '$' + coalesce(v.groupDisplayName, ' ')) as dNames, COLLECT(distinct attachment.id) as attachments "
            + "MATCH (ub: UserBook)<-[:USERBOOK]-(u)-[:HAS_CONVERSATION]->(c:Conversation {active:{true}})"
            + "-[:HAS_CONVERSATION_FOLDER]->(f:ConversationSystemFolder {name:{inbox}}) "
            + "WHERE (ub.quota - ub.storage) >= {attachmentsSize} "
            + "CREATE UNIQUE f-[:HAS_CONVERSATION_MESSAGE { unread: {true}, attachments: attachments }]->message "
            + "SET ub.storage = ub.storage + {attachmentsSize} "
            + "WITH message, COLLECT(c.userId) as sentIds, COLLECT(distinct u) as users, "
            + "COLLECT(distinct dNames) as displayNames "
            + "MATCH (s:User {id : {userId}})-[:HAS_CONVERSATION]->(:Conversation)"
            + "-[:HAS_CONVERSATION_FOLDER]->(fOut:ConversationSystemFolder {name : {outbox}}), "
            + "message<-[r:HAS_CONVERSATION_MESSAGE]-(fDraft:ConversationSystemFolder {name : {draft}}) "
            + "SET message.state = {sent}, "
            + "message.displayNames = displayNames + (s.id + '$' + coalesce(s.displayName, ' ') + '$ $ ') "
            + "CREATE UNIQUE fOut-[:HAS_CONVERSATION_MESSAGE { insideFolder: r.insideFolder, attachments: r.attachments }]->message "
            + "DELETE r " + "RETURN sentIds, message.id as id";

    findVisibles(eb, user.getUserId(), query, params, true, true, false, new Handler<JsonArray>() {
        @Override
        public void handle(JsonArray event) {
            if (event != null && event.size() == 1 && (event.getValue(0) instanceof JsonObject)) {

                JsonObject resultObj = event.getJsonObject(0);
                JsonArray sentIds = resultObj.getJsonArray("sentIds");
                String messageId = resultObj.getString("id");

                String query = usersQuery + "MATCH v<-[:IN*0..1]-(u:User), (message:ConversationMessage) "
                        + "WHERE (v: User or v:Group) "
                        + "AND message.id = {messageId} AND message.from = {userId} AND "
                        + "(v.id IN message.to OR v.id IN message.cc) "
                        + "RETURN EXTRACT(user IN FIlTER(x IN COLLECT(u) WHERE NOT(x.id IN {sentIds}))|user.displayName) as undelivered, {sentIds} as sentIds, [] as inactive, "
                        + "{sentIdsLength} as sent, message.id as id, message.subject as subject";

                JsonObject params = new JsonObject().put("userId", user.getUserId()).put("messageId", messageId)
                        .put("sentIds", sentIds).put("sentIdsLength", sentIds.size());
                if (parentMessageId != null && !parentMessageId.trim().isEmpty()) {
                    params.put("parentMessageId", parentMessageId);
                }

                findVisibles(eb, user.getUserId(), query, params, true, true, false, new Handler<JsonArray>() {
                    @Override
                    public void handle(JsonArray event) {
                        if (event != null && event.size() == 1 && (event.getValue(0) instanceof JsonObject)) {
                            result.handle(new Either.Right<String, JsonObject>(event.getJsonObject(0)));
                        } else {
                            result.handle(new Either.Left<String, JsonObject>("conversation.send.error"));
                        }
                    }
                });
            } else {
                result.handle(new Either.Left<String, JsonObject>("conversation.send.error"));
            }
        }
    });
}

From source file:org.entcore.conversation.service.impl.DefaultConversationService.java

License:Open Source License

@Override
public void removeAttachment(String messageId, String attachmentId, UserInfos user,
        final Handler<Either<String, JsonObject>> result) {
    if (validationParamsError(user, result, messageId, attachmentId))
        return;/*from  ww  w  . j a  v a 2 s .  co m*/

    String get = "MATCH (attachment: MessageAttachment)<-[aLink: HAS_ATTACHMENT]-(m: ConversationMessage)<-[r: HAS_CONVERSATION_MESSAGE]-(f: ConversationSystemFolder)"
            + "<-[:HAS_CONVERSATION_FOLDER]-(c:Conversation) "
            + "WHERE m.id = {messageId} AND c.userId = {userId} AND c.active = {true} AND attachment.id = {attachmentId} AND {attachmentId} IN r.attachments ";

    String query = get
            + "SET r.attachments = filter(attachmentId IN r.attachments WHERE attachmentId <> {attachmentId}) "
            + "RETURN attachment.id as fileId, attachment.size as fileSize";

    String q3 = "MATCH (attachment:MessageAttachment)<-[attachmentLink: HAS_ATTACHMENT]-(:ConversationMessage)<-[messageLinks: HAS_CONVERSATION_MESSAGE]-(:ConversationSystemFolder) "
            + "WHERE attachment.id = {attachmentId} "
            + "WITH attachmentLink, attachment, none(item IN collect(messageLinks.attachments) WHERE attachment.id IN item) as deletionCheck "
            + "WHERE deletionCheck = true " + "DELETE attachmentLink " + "WITH attachment "
            + "WHERE NOT(attachment-[:HAS_ATTACHMENT]-()) " + "DELETE attachment "
            + "RETURN true as deletionCheck";

    JsonObject params = new JsonObject().put("userId", user.getUserId()).put("messageId", messageId)
            .put("attachmentId", attachmentId).put("true", true);

    StatementsBuilder b = new StatementsBuilder();
    b.add(query, params);
    b.add(q3, params);

    neo.executeTransaction(b.build(), null, true, validResultsHandler(new Handler<Either<String, JsonArray>>() {
        @Override
        public void handle(Either<String, JsonArray> event) {
            if (event.isLeft()) {
                result.handle(new Either.Left<String, JsonObject>(event.left().getValue()));
                return;
            }

            JsonArray result1 = event.right().getValue().getJsonArray(0);
            JsonArray result3 = event.right().getValue().getJsonArray(1);

            JsonObject jsonResult = result1.size() > 0 ? result1.getJsonObject(0) : new JsonObject();
            jsonResult.put("deletionCheck",
                    result3.size() > 0 ? result3.getJsonObject(0).getBoolean("deletionCheck", false) : false);

            result.handle(new Either.Right<String, JsonObject>(jsonResult));
        }
    }));
}