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

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

Introduction

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

Prototype

public JsonArray add(Object value) 

Source Link

Document

Add an Object to the JSON array.

Usage

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 va2 s .c om
    }
    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 removeShare(String resourceId, String userId, List<String> actions,
        Handler<Either<String, JsonObject>> handler) {
    String actionFilter;// w  w w.j  a  va2s.c  o  m
    JsonArray values;
    if (actions != null && actions.size() > 0) {
        Object[] a = actions.toArray();
        actionFilter = "action IN " + Sql.listPrepared(a) + " AND ";
        values = new fr.wseduc.webutils.collections.JsonArray(actions);
    } else {
        actionFilter = "";
        values = new fr.wseduc.webutils.collections.JsonArray();
    }
    String query = "DELETE FROM " + shareTable + " WHERE " + actionFilter + "resource_id = ? AND member_id = ?";
    values.add(Sql.parseId(resourceId)).add(userId);
    sql.prepared(query, values, SqlResult.validUniqueResultHandler(handler));
}

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

License:Open Source License

@Override
protected void prepareSharedArray(String resourceId, String type, JsonArray shared, String attr,
        Set<String> actions) {
    for (String action : actions) {
        shared.add(new JsonArray().add(attr).add(resourceId).add(action));
    }//w  ww . j  a  v a2s.  co m

}

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

License:Open Source License

public void insert(String table, JsonObject params, String returning, Handler<Message<JsonObject>> handler) {
    if (params == null) {
        handler.handle(new ErrorMessage("invalid.parameters"));
        return;//  www .  ja va 2s  .  c om
    }
    JsonArray fields = new fr.wseduc.webutils.collections.JsonArray();
    JsonArray values = new fr.wseduc.webutils.collections.JsonArray();
    for (String attr : params.fieldNames()) {
        fields.add(attr);
        values.add(params.getValue(attr));
    }
    insert(table, fields, new fr.wseduc.webutils.collections.JsonArray().add(values), returning, handler);
}

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

License:Open Source License

public static Either<String, JsonArray> validResults(Message<JsonObject> res) {
    if ("ok".equals(res.body().getString("status"))) {
        JsonArray a = res.body().getJsonArray("results");
        JsonArray r = new fr.wseduc.webutils.collections.JsonArray();
        for (Object o : a) {
            if (!(o instanceof JsonObject))
                continue;
            r.add(transform((JsonObject) o));
        }/*from  w w  w.  j ava  2  s . c o  m*/
        return new Either.Right<>(r);
    } else {
        return new Either.Left<>(res.body().getString("message", ""));
    }
}

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()));
                    }//from w  ww .  jav a2s  .co  m
                } 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 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);
                }//from  www .java  2  s . co  m
                shared.put(member, sj);
                a.add(sj);
            }
        }
    }
    j.remove("groups");
    j.put("shared", a);
}

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

License:Open Source License

public static Handler<Message<JsonObject>> parseShared(final Handler<Either<String, JsonArray>> handler) {
    return new Handler<Message<JsonObject>>() {
        @Override/* w  ww .  ja  va2 s . c  o m*/
        public void handle(Message<JsonObject> message) {
            Either<String, JsonArray> res = validResult(message);
            if (res.isRight()) {
                JsonArray out = new fr.wseduc.webutils.collections.JsonArray();
                for (Object row : res.right().getValue()) {
                    if (!(row instanceof JsonObject))
                        continue;
                    JsonObject j = (JsonObject) row;
                    parseShared(j);
                    out.add(j);
                }
                handler.handle(new Either.Right<String, JsonArray>(out));
            } else {
                handler.handle(res);
            }
        }
    };

}

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

License:Open Source License

public SqlStatementsBuilder insert(String table, JsonObject params, String returning) {
    if (params == null) {
        return this;
    }//ww  w . j  a  va2s.  c  o m
    JsonArray fields = new fr.wseduc.webutils.collections.JsonArray();
    JsonArray values = new fr.wseduc.webutils.collections.JsonArray();
    for (String attr : params.fieldNames()) {
        fields.add(attr);
        values.add(params.getValue(attr));
    }
    insert(table, fields, new fr.wseduc.webutils.collections.JsonArray().add(values), returning);
    return this;
}

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

License:Open Source License

@Override
public void removeFiles(JsonArray ids, final Handler<JsonObject> handler) {
    final JsonObject res = new JsonObject();
    final AtomicInteger count = new AtomicInteger(ids.size());
    final JsonArray errors = new fr.wseduc.webutils.collections.JsonArray();
    for (final Object o : ids) {
        if (o == null) {
            decrementRemove(count, errors, handler, res);
            continue;
        }//from w  w  w  . ja  v  a  2  s . co m
        try {
            final String path = getPath(o.toString());
            fs.delete(path, new Handler<AsyncResult<Void>>() {
                @Override
                public void handle(AsyncResult<Void> event) {
                    if (event.failed()) {
                        errors.add(new JsonObject().put("id", o.toString()).put("message",
                                event.cause().getMessage()));
                    }
                    decrementRemove(count, errors, handler, res);
                }
            });
        } catch (FileNotFoundException e) {
            errors.add(new JsonObject().put("id", o.toString()).put("message", "invalid.path"));
            decrementRemove(count, errors, handler, res);
            log.warn(e.getMessage(), e);
        }
    }
}