Example usage for io.vertx.core.json JsonObject put

List of usage examples for io.vertx.core.json JsonObject put

Introduction

In this page you can find the example usage for io.vertx.core.json JsonObject put.

Prototype

public JsonObject put(String key, Object value) 

Source Link

Document

Put an Object into the JSON object with the specified key.

Usage

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

License:Open Source License

@Override
public void shareInfos(final String userId, String resourceId, final String acceptLanguage, final String search,
        final Handler<Either<String, JsonObject>> handler) {
    if (userId == null || userId.trim().isEmpty()) {
        handler.handle(new Either.Left<String, JsonObject>("Invalid userId."));
        return;/* ww  w.j a  va2 s  .  c o m*/
    }
    if (resourceId == null || resourceId.trim().isEmpty()) {
        handler.handle(new Either.Left<String, JsonObject>("Invalid resourceId."));
        return;
    }
    final JsonArray actions = getResoureActions(securedActions);
    QueryBuilder query = QueryBuilder.start("_id").is(resourceId);
    JsonObject keys = new JsonObject().put("shared", 1);
    mongo.findOne(collection, MongoQueryBuilder.build(query), keys, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> event) {
            if ("ok".equals(event.body().getString("status"))) {
                JsonArray shared = event.body().getJsonObject("result", new JsonObject()).getJsonArray("shared",
                        new fr.wseduc.webutils.collections.JsonArray());
                JsonObject gs = new JsonObject();
                JsonObject us = new JsonObject();
                for (Object o : shared) {
                    if (!(o instanceof JsonObject))
                        continue;
                    JsonObject userShared = (JsonObject) o;
                    JsonArray a = new fr.wseduc.webutils.collections.JsonArray();
                    for (String attrName : userShared.fieldNames()) {
                        if ("userId".equals(attrName) || "groupId".equals(attrName)) {
                            continue;
                        }
                        if (groupedActions != null && groupedActions.containsKey(attrName)) {
                            for (String action : groupedActions.get(attrName)) {
                                a.add(action.replaceAll("\\.", "-"));
                            }
                        } else {
                            a.add(attrName);
                        }
                    }
                    final String g = userShared.getString("groupId");
                    String u;
                    if (g != null) {
                        gs.put(g, a);
                    } else if ((u = userShared.getString("userId")) != null && !u.equals(userId)) {
                        us.put(u, a);
                    }
                }
                getShareInfos(userId, actions, gs, us, acceptLanguage, search, new Handler<JsonObject>() {
                    @Override
                    public void handle(JsonObject event) {
                        if (event != null && event.size() == 3) {
                            handler.handle(new Either.Right<String, JsonObject>(event));
                        } else {
                            handler.handle(
                                    new Either.Left<String, JsonObject>("Error finding shared resource."));
                        }
                    }
                });
            } else {
                handler.handle(new Either.Left<String, JsonObject>(
                        event.body().getString("error", "Error finding shared resource.")));
            }
        }
    });
}

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

License:Open Source License

private void share(String resourceId, final String groupShareId, final List<String> actions, boolean isGroup,
        final Handler<Either<String, JsonObject>> handler) {
    final String shareIdAttr = isGroup ? "groupId" : "userId";
    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/*ww w.  j av a  2  s. co  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());
                boolean exist = false;
                for (int i = 0; i < actual.size(); i++) {
                    JsonObject s = actual.getJsonObject(i);
                    String id = s.getString(shareIdAttr);
                    if (groupShareId.equals(id)) {
                        for (String action : actions) {
                            s.put(action, true);
                        }
                        if (groupedActions != null) {
                            for (Map.Entry<String, List<String>> ga : groupedActions.entrySet()) {
                                if (actions.containsAll(ga.getValue())) {
                                    s.put(ga.getKey(), true);
                                }
                            }
                        }
                        exist = true;
                        break;
                    }
                }
                final AtomicBoolean notifyTimeline = new AtomicBoolean(false);
                if (!exist) {
                    JsonObject t = new JsonObject().put(shareIdAttr, groupShareId);
                    actual.add(t);
                    for (String action : actions) {
                        t.put(action, true);
                    }
                    if (groupedActions != null) {
                        for (Map.Entry<String, List<String>> ga : groupedActions.entrySet()) {
                            if (actions.containsAll(ga.getValue())) {
                                t.put(ga.getKey(), true);
                            }
                        }
                    }
                    notifyTimeline.set(true);
                }
                MongoUpdateBuilder updateQuery = new MongoUpdateBuilder().set("shared", actual);
                mongo.update(collection, q, updateQuery.build(), new Handler<Message<JsonObject>>() {
                    @Override
                    public void handle(Message<JsonObject> res) {
                        if (notifyTimeline.get()) {
                            JsonObject notify = new JsonObject();
                            notify.put(shareIdAttr, groupShareId);
                            res.body().put("notify-timeline", notify);
                        }
                        handler.handle(Utils.validResult(res));
                    }
                });
            } else {
                handler.handle(new Either.Left<String, JsonObject>("Resource not found."));
            }
        }
    });
}

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

License:Open Source License

@Override
protected void prepareSharedArray(String resourceId, String type, JsonArray shared, String attr,
        Set<String> actions) {
    JsonObject el = new JsonObject().put(type, attr);
    for (String action : actions) {
        el.put(action, true);
    }/*  w  w  w . j a  v  a2 s.  co  m*/
    shared.add(el);
}

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

License:Open Source License

@Override
public void shareInfos(final String userId, String resourceId, final String acceptLanguage, final String search,
        final Handler<Either<String, JsonObject>> handler) {
    if (userId == null || userId.trim().isEmpty()) {
        handler.handle(new Either.Left<String, JsonObject>("Invalid userId."));
        return;/*from w w  w .j a v  a2  s  . c  o  m*/
    }
    if (resourceId == null || resourceId.trim().isEmpty()) {
        handler.handle(new Either.Left<String, JsonObject>("Invalid resourceId."));
        return;
    }
    final JsonArray actions = getResoureActions(securedActions);
    String query = "SELECT s.member_id, s.action, m.group_id FROM " + shareTable + " AS s " + "JOIN " + schema
            + "members AS m ON s.member_id = m.id WHERE resource_id = ?";
    sql.prepared(query, new fr.wseduc.webutils.collections.JsonArray().add(Sql.parseId(resourceId)),
            new Handler<Message<JsonObject>>() {
                @Override
                public void handle(Message<JsonObject> message) {
                    if ("ok".equals(message.body().getString("status"))) {
                        JsonArray r = message.body().getJsonArray("results");
                        JsonObject groupCheckedActions = new JsonObject();
                        JsonObject userCheckedActions = new JsonObject();
                        for (Object o : r) {
                            if (!(o instanceof JsonArray))
                                continue;
                            JsonArray row = (JsonArray) o;
                            final String memberId = row.getString(0);
                            if (memberId == null || memberId.equals(userId))
                                continue;
                            final JsonObject checkedActions = (row.getValue(2) != null) ? groupCheckedActions
                                    : userCheckedActions;
                            JsonArray m = checkedActions.getJsonArray(memberId);
                            if (m == null) {
                                m = new fr.wseduc.webutils.collections.JsonArray();
                                checkedActions.put(memberId, m);
                            }
                            m.add(row.getValue(1));
                        }
                        getShareInfos(userId, actions, groupCheckedActions, userCheckedActions, acceptLanguage,
                                search, new Handler<JsonObject>() {
                                    @Override
                                    public void handle(JsonObject event) {
                                        if (event != null && event.size() == 3) {
                                            handler.handle(new Either.Right<String, JsonObject>(event));
                                        } else {
                                            handler.handle(new Either.Left<String, JsonObject>(
                                                    "Error finding shared resource."));
                                        }
                                    }
                                });
                    }
                }
            });
}

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

License:Open Source License

private void share(String resourceId, final String shareId, List<String> actions, final String membersTable,
        final Handler<Either<String, JsonObject>> handler) {
    final SqlStatementsBuilder s = new SqlStatementsBuilder();
    s.raw("LOCK TABLE " + schema + membersTable + " IN SHARE ROW EXCLUSIVE MODE");
    s.raw("LOCK TABLE " + shareTable + " IN SHARE ROW EXCLUSIVE MODE");
    s.raw("INSERT INTO " + schema + membersTable + " (id) SELECT '" + shareId
            + "' WHERE NOT EXISTS (SELECT * FROM " + schema + membersTable + " WHERE id='" + shareId + "');");
    final Object rId = Sql.parseId(resourceId);
    final String query = "INSERT INTO " + shareTable
            + " (member_id, resource_id, action) SELECT ?, ?, ? WHERE NOT EXISTS " + "(SELECT * FROM "
            + shareTable + " WHERE member_id = ? AND resource_id = ? AND action = ?);";
    for (String action : actions) {
        JsonArray ar = new fr.wseduc.webutils.collections.JsonArray().add(shareId).add(rId).add(action)
                .add(shareId).add(rId).add(action);
        s.prepared(query, ar);/*from   w  ww .  j av a 2s  . co m*/
    }
    sql.prepared("SELECT count(*) FROM " + shareTable + " WHERE member_id = ? AND resource_id = ?",
            new fr.wseduc.webutils.collections.JsonArray().add(shareId).add(Sql.parseId(resourceId)),
            new Handler<Message<JsonObject>>() {
                @Override
                public void handle(final Message<JsonObject> message) {
                    final Long nb = SqlResult.countResult(message);
                    sql.transaction(s.build(), new Handler<Message<JsonObject>>() {
                        @Override
                        public void handle(Message<JsonObject> res) {
                            Either<String, JsonObject> r = SqlResult.validUniqueResult(2, res);
                            if (r.isRight() && nb == 0) {
                                JsonObject notify = new JsonObject();
                                notify.put(membersTable.substring(0, membersTable.length() - 1) + "Id",
                                        shareId);
                                r.right().getValue().put("notify-timeline", notify);
                            }
                            handler.handle(r);
                        }
                    });
                }
            });
}

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

License:Open Source License

public void insert(String table, JsonArray fields, JsonArray values, String returning,
        Handler<Message<JsonObject>> handler) {
    JsonObject j = new JsonObject().put("action", "insert").put("table", table).put("fields", fields)
            .put("values", values);
    if (returning != null && !returning.trim().isEmpty()) {
        j.put("returning", returning);
    }/*from  w w  w  .  ja  v a  2  s .  co  m*/
    eb.send(address, j, handlerToAsyncHandler(handler));
}

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

License:Open Source License

private static JsonArray transform(JsonObject body) {
    JsonArray f = body.getJsonArray("fields");
    JsonArray r = body.getJsonArray("results");
    JsonArray result = new fr.wseduc.webutils.collections.JsonArray();
    if (f != null && r != null) {
        JsonArray jsonbAttributes = body.getJsonArray("jsonb_fields");
        List ja = (jsonbAttributes != null) ? jsonbAttributes.getList() : new ArrayList<>();
        for (Object o : r) {
            if (!(o instanceof JsonArray))
                continue;
            JsonArray a = (JsonArray) o;
            JsonObject j = new fr.wseduc.webutils.collections.JsonObject();
            for (int i = 0; i < f.size(); i++) {
                Object item = a.getValue(i);
                if (item instanceof Boolean) {
                    j.put(f.getString(i), (Boolean) item);
                } else if (item instanceof Number) {
                    j.put(f.getString(i), (Number) item);
                } else if (item instanceof JsonArray) {
                    j.put(f.getString(i), (JsonArray) item);
                } else if (item != null && ja.contains(f.getValue(i))) {
                    String stringRepresentation = item.toString().trim();
                    if (stringRepresentation.startsWith("[")) {
                        j.put(f.getString(i), new fr.wseduc.webutils.collections.JsonArray(item.toString()));
                    } else {
                        j.put(f.getString(i), new fr.wseduc.webutils.collections.JsonObject(item.toString()));
                    }/* w w  w  .ja  v  a2  s  .com*/
                } else if (item != null) {
                    j.put(f.getString(i), item.toString());
                } else {
                    j.put(f.getString(i), (String) null);
                }
            }
            result.add(j);
        }
    }
    return result;
}

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

License:Open Source License

private static Either<String, JsonObject> validRows(JsonObject body) {
    if ("ok".equals(body.getString("status"))) {
        long rows = body.getLong("rows", 0l);
        JsonObject result = new JsonObject();
        if (rows > 0) {
            result.put("rows", rows);
        }/* w  w w. j  av  a  2s  .c om*/
        return new Either.Right<>(result);
    } else {
        return new Either.Left<>(body.getString("message", ""));
    }
}

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

License:Open Source License

private static void parseShared(JsonObject j) {
    Map<String, JsonObject> shared = new HashMap<>();
    JsonArray a = new fr.wseduc.webutils.collections.JsonArray();
    JsonArray s = new fr.wseduc.webutils.collections.JsonArray(j.getString("shared"));
    JsonArray m = new fr.wseduc.webutils.collections.JsonArray(j.getString("groups"));
    for (Object o : s) {
        if (o == null || !(o instanceof JsonObject))
            continue;
        JsonObject json = (JsonObject) o;
        String member = json.getString("member_id");
        String action = json.getString("action");
        if (member != null && action != null) {
            if (shared.containsKey(member)) {
                shared.get(member).put(action, true);
            } else {
                JsonObject sj = new JsonObject().put(action, true);
                if (m.contains(member)) {
                    sj.put("groupId", member);
                } else {
                    sj.put("userId", member);
                }// ww  w  . j av a  2s  .  c  om
                shared.put(member, sj);
                a.add(sj);
            }
        }
    }
    j.remove("groups");
    j.put("shared", a);
}