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

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

Introduction

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

Prototype

public Set<String> fieldNames() 

Source Link

Document

Return the set of field names in the JSON objects

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;/*from   w  ww . 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);
    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.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;/*  w w  w. ja  va2  s.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, handler);
}

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;
    }/*from   w  ww  .  j  a v a 2  s .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.PostgresqlApplicationStorage.java

License:Open Source License

private String generateColumns(JsonObject j, JsonArray params) {
    StringBuilder sb = new StringBuilder();
    for (String attr : j.fieldNames()) {
        sb.append(attr).append("= ?, ");
        params.add(j.getValue(attr));//from   w ww.j  a va 2  s.  com
    }
    return sb.toString().substring(0, sb.length() - 2);
}

From source file:org.entcore.communication.services.impl.DefaultCommunicationService.java

License:Open Source License

@Override
public void initDefaultRules(JsonArray structureIds, JsonObject defaultRules,
        final Handler<Either<String, JsonObject>> handler) {
    final StatementsBuilder s1 = new StatementsBuilder();
    final StatementsBuilder s2 = new StatementsBuilder();
    final StatementsBuilder s3 = new StatementsBuilder();
    s3.add("MATCH (s:Structure)<-[:DEPENDS*1..2]-(g:ProfileGroup) " + "WHERE NOT(HAS(g.communiqueWith)) "
            + "SET g.communiqueWith = [] ")
            .add("MATCH (fg:FunctionGroup) " + "WHERE fg.name ENDS WITH 'AdminLocal' "
                    + "SET fg.users = 'BOTH' ")
            .add("MATCH (ag:FunctionalGroup) " + "SET ag.users = 'BOTH' ");
    for (String attr : defaultRules.fieldNames()) {
        initDefaultRules(structureIds, attr, defaultRules.getJsonObject(attr), s1, s2);
    }/*from w  w w . j  a  va  2s  .c o  m*/
    neo4j.executeTransaction(s1.build(), null, false, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> event) {
            if ("ok".equals(event.body().getString("status"))) {
                Integer transactionId = event.body().getInteger("transactionId");
                neo4j.executeTransaction(s2.build(), transactionId, false, new Handler<Message<JsonObject>>() {
                    @Override
                    public void handle(Message<JsonObject> event) {
                        if ("ok".equals(event.body().getString("status"))) {
                            Integer transactionId = event.body().getInteger("transactionId");
                            neo4j.executeTransaction(s3.build(), transactionId, true,
                                    new Handler<Message<JsonObject>>() {
                                        @Override
                                        public void handle(Message<JsonObject> message) {
                                            if ("ok".equals(message.body().getString("status"))) {
                                                handler.handle(
                                                        new Either.Right<String, JsonObject>(new JsonObject()));
                                                log.info("Default communication rules initialized.");
                                            } else {
                                                handler.handle(new Either.Left<String, JsonObject>(
                                                        message.body().getString("message")));
                                                log.error("Error init default com rules : "
                                                        + message.body().getString("message"));
                                            }
                                        }
                                    });
                        } else {
                            handler.handle(
                                    new Either.Left<String, JsonObject>(event.body().getString("message")));
                            log.error("Error init default com rules : " + event.body().getString("message"));
                        }
                    }
                });
            } else {
                handler.handle(new Either.Left<String, JsonObject>(event.body().getString("message")));
                log.error("Error init default com rules : " + event.body().getString("message"));
            }
        }
    });
}

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

License:Open Source License

private String nodeSetPropertiesFromJson(String nodeAlias, JsonObject json) {
    StringBuilder sb = new StringBuilder();
    for (String attr : json.fieldNames()) {
        sb.append(", ").append(nodeAlias).append(".").append(attr).append(" = {").append(attr).append("}");
    }//from w ww. ja  va2s .  c  om
    if (sb.length() > 2) {
        return sb.append(" ").substring(2);
    }
    return " ";
}

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

License:Open Source License

private void update(String messageId, JsonObject message, UserInfos user,
        Handler<Either<String, JsonObject>> result) {
    message.put("date", System.currentTimeMillis());
    JsonObject m = Utils.validAndGet(message, UPDATE_DRAFT_FIELDS, UPDATE_DRAFT_REQUIRED_FIELDS);
    if (validationError(user, m, result, messageId))
        return;/*from w  ww .  j  a v  a  2s .  c  o m*/

    StringBuilder sb = new StringBuilder();
    JsonArray values = new fr.wseduc.webutils.collections.JsonArray();

    for (String attr : message.fieldNames()) {
        if ("to".equals(attr) || "cc".equals(attr) || "displayNames".equals(attr)) {
            sb.append("\"" + attr + "\"").append(" = CAST(? AS JSONB),");
        } else {
            sb.append("\"" + attr + "\"").append(" = ?,");
        }
        values.add(message.getValue(attr));
    }
    if (sb.length() > 0)
        sb.deleteCharAt(sb.length() - 1);

    String query = "UPDATE " + messageTable + " SET " + sb.toString() + " " + "WHERE id = ? AND state = ?";
    values.add(messageId).add("DRAFT");

    sql.prepared(query, values, SqlResult.validUniqueResultHandler(result));
}

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

License:Open Source License

@Override
public void blockProfiles(JsonObject profiles, Handler<Either<String, JsonObject>> handler) {
    final String query = "MATCH (p:Profile {name : {name}}) set p.blocked = {blocked}";
    final StatementsBuilder sb = new StatementsBuilder();
    for (String profile : profiles.fieldNames()) {
        sb.add(query, new JsonObject().put("name", profile).put("blocked",
                (profiles.getBoolean(profile, false) ? true : null)));
    }//from   ww w  .ja  v  a 2s .  c o m
    neo4j.executeTransaction(sb.build(), null, true, validEmptyHandler(handler));
}

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());
                        }/*w  w  w  .  j av a  2s  . c  om*/
                    });
                    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

private Future<Boolean> cacheAvatarFromUserBook(String userId, Optional<String> pictureId, Boolean remove) {
    // clean avatar when changing or when removing
    Future<Boolean> futureClean = (pictureId.isPresent() || remove) ? cleanAvatarCache(userId)
            : Future.succeededFuture();
    return futureClean.compose(res -> {
        if (!pictureId.isPresent()) {
            return Future.succeededFuture();
        }/*from   w  ww.j av a  2 s .c  om*/
        Future<Boolean> futureCopy = Future.future();
        this.wsHelper.getDocument(pictureId.get(), resDoc -> {
            if (resDoc.succeeded() && "ok".equals(resDoc.result().body().getString("status"))) {
                JsonObject document = resDoc.result().body().getJsonObject("result");
                String fileId = document.getString("file");
                // Extensions are not used by storage
                String defaultFilename = avatarFileNameFromUserId(userId, Optional.empty());
                //
                JsonObject thumbnails = document.getJsonObject("thumbnails", new JsonObject());
                Map<String, String> filenamesByIds = new HashMap<>();
                filenamesByIds.put(fileId, defaultFilename);

                for (String size : thumbnails.fieldNames()) {
                    filenamesByIds.put(thumbnails.getString(size),
                            avatarFileNameFromUserId(userId, Optional.of(size)));
                }
                // TODO avoid buffer to improve performances and avoid cache every time
                List<Future> futures = new ArrayList<>();
                for (Entry<String, String> entry : filenamesByIds.entrySet()) {
                    String cFileId = entry.getKey();
                    String cFilename = entry.getValue();
                    Future<JsonObject> future = Future.future();
                    futures.add(future);
                    this.wsHelper.readFile(cFileId, buffer -> {
                        if (buffer != null) {
                            this.avatarStorage.writeBuffer(FileUtils.stripExtension(cFilename), buffer, "",
                                    cFilename, wRes -> {
                                        future.complete(wRes);
                                    });
                        } else {
                            future.fail("Cannot read file from workspace storage. ID =: " + cFileId);
                        }
                    });
                }
                //
                CompositeFuture.all(futures)
                        .setHandler(finishRes -> futureCopy.complete(finishRes.succeeded()));
            }
        });
        return futureCopy;
    });

}