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.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/*from  w  ww  .  ja va  2 s  .  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());
                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

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//from   w w  w.java  2s.  com
        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.share.impl.SqlShareService.java

License:Open Source License

@Override
public void share(String userId, String resourceId, JsonObject share,
        Handler<Either<String, JsonObject>> handler) {
    shareValidation(resourceId, userId, share, res -> {
        if (res.isRight()) {
            final SqlStatementsBuilder s = new SqlStatementsBuilder();
            s.prepared("SELECT member_id FROM " + shareTable + " WHERE resource_id = ?",
                    new JsonArray().add(resourceId));
            s.prepared("DELETE FROM " + shareTable + " WHERE resource_id = ?", new JsonArray().add(resourceId));
            final JsonArray users = res.right().getValue().getJsonArray("users");
            if (users != null && users.size() > 0) {
                s.raw("LOCK TABLE " + schema + "users IN SHARE ROW EXCLUSIVE MODE");
                for (Object u : users) {
                    s.raw("INSERT INTO " + schema + "users (id) SELECT '" + u.toString()
                            + "' WHERE NOT EXISTS (SELECT * FROM " + schema + "users WHERE id='" + u.toString()
                            + "');");
                }// w  w  w. j a v  a2s  .co  m
            }
            final JsonArray groups = res.right().getValue().getJsonArray("groups");
            if (groups != null && groups.size() > 0) {
                s.raw("LOCK TABLE " + schema + "groups IN SHARE ROW EXCLUSIVE MODE");
                for (Object g : groups) {
                    s.raw("INSERT INTO " + schema + "groups (id) SELECT '" + g.toString()
                            + "' WHERE NOT EXISTS (SELECT * FROM " + schema + "groups WHERE id='" + g.toString()
                            + "');");
                }
            }
            s.insert(shareTable, new JsonArray().add("member_id").add("resource_id").add("action"),
                    res.right().getValue().getJsonArray("shared"));
            sql.transaction(s.build(), SqlResult.validResultHandler(0, old -> {
                if (old.isRight()) {
                    JsonArray oldMembers = old.right().getValue();
                    JsonArray members = res.right().getValue().getJsonArray("notify-members");
                    getNotifyMembers(handler, oldMembers, members,
                            (m -> ((JsonObject) m).getString("member_id")));
                } else {
                    handler.handle(new Either.Left<>(old.left().getValue()));
                }
            }));
        } else {
            handler.handle(res);
        }
    });
}

From source file:org.entcore.common.share.Shareable.java

License:Open Source License

default void notifyShare(final HttpServerRequest request, final EventBus eb, final String resource,
        final UserInfos user, JsonArray sharedArray, final String notificationName, final JsonObject params,
        final String resourceNameAttribute) {
    final List<String> recipients = new ArrayList<>();
    final AtomicInteger remaining = new AtomicInteger(sharedArray.size());
    for (Object j : sharedArray) {
        JsonObject json = (JsonObject) j;
        String userId = json.getString("userId");
        if (userId != null) {
            recipients.add(userId);//from  ww w. j av  a 2 s. c  om
            remaining.getAndDecrement();
        } else {
            String groupId = json.getString("groupId");
            if (groupId != null) {
                UserUtils.findUsersInProfilsGroups(groupId, eb, user.getUserId(), false,
                        new Handler<JsonArray>() {
                            @Override
                            public void handle(JsonArray event) {
                                if (event != null) {
                                    for (Object o : event) {
                                        if (!(o instanceof JsonObject))
                                            continue;
                                        JsonObject j = (JsonObject) o;
                                        String id = j.getString("id");
                                        recipients.add(id);
                                    }
                                }
                                if (remaining.decrementAndGet() < 1) {
                                    sendNotify(request, resource, user, recipients, notificationName, params,
                                            resourceNameAttribute);
                                }
                            }
                        });
            }
        }
    }
    if (remaining.get() < 1) {
        sendNotify(request, resource, user, recipients, notificationName, params, resourceNameAttribute);
    }
}

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

License:Open Source License

public static Long countResult(Message<JsonObject> res) {
    if ("ok".equals(res.body().getString("status"))) {
        JsonArray values = res.body().getJsonArray("results");
        if (values != null && values.size() == 1) {
            JsonArray row = values.getJsonArray(0);
            if (row != null && row.size() == 1) {
                return row.getLong(0);
            }/*from w ww  .j a v  a 2 s . c  om*/
        }
    }
    return null;
}

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   w  ww.  j a  va  2s . com
        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 w  w.  j a va  2 s  . c o m*/
            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

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 w  w .ja  va2  s  .c  o 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

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 {/*  w  w w .  j  av a  2s  .c  o m*/
            return new Either.Left<>("missing.result");
        }
    } else {
        return new Either.Left<>(res.body().getString("message", ""));
    }
}

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  ww  w.  ja va  2 s.c  om*/
        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);
        }
    }
}