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.blog.services.impl.DefaultPostService.java

License:Open Source License

@Override
public void update(String postId, final JsonObject post, final UserInfos user,
        final Handler<Either<String, JsonObject>> result) {

    final JsonObject jQuery = MongoQueryBuilder.build(QueryBuilder.start("_id").is(postId));
    mongo.findOne(POST_COLLECTION, jQuery,
            MongoDbResult.validActionResultHandler(new Handler<Either<String, JsonObject>>() {
                public void handle(Either<String, JsonObject> event) {
                    if (event.isLeft()) {
                        result.handle(event);
                        return;
                    } else {
                        final JsonObject postFromDb = event.right().getValue().getJsonObject("result",
                                new JsonObject());
                        final JsonObject now = MongoDb.now();
                        post.put("modified", now);
                        final JsonObject b = Utils.validAndGet(post, UPDATABLE_FIELDS,
                                Collections.<String>emptyList());

                        if (validationError(result, b))
                            return;
                        if (b.containsKey("content")) {
                            b.put("contentPlain", StringUtils.stripHtmlTag(b.getString("content", "")));
                        }//www .  j a v a2 s . c om

                        if (postFromDb.getJsonObject("firstPublishDate") != null) {
                            b.put("sorted", postFromDb.getJsonObject("firstPublishDate"));
                        } else {
                            b.put("sorted", now);
                        }

                        //if user is author, draft state
                        if (user.getUserId().equals(
                                postFromDb.getJsonObject("author", new JsonObject()).getString("userId"))) {
                            b.put("state", StateType.DRAFT.name());
                        }

                        MongoUpdateBuilder modifier = new MongoUpdateBuilder();
                        for (String attr : b.fieldNames()) {
                            modifier.set(attr, b.getValue(attr));
                        }
                        mongo.update(POST_COLLECTION, jQuery, modifier.build(),
                                new Handler<Message<JsonObject>>() {
                                    @Override
                                    public void handle(Message<JsonObject> event) {
                                        if ("ok".equals(event.body().getString("status"))) {
                                            final JsonObject r = new JsonObject().put("state",
                                                    b.getString("state", postFromDb.getString("state")));
                                            result.handle(new Either.Right<String, JsonObject>(r));
                                        } else {
                                            result.handle(new Either.Left<String, JsonObject>(
                                                    event.body().getString("message", "")));
                                        }
                                    }
                                });
                    }
                }
            }));

}

From source file:org.entcore.cas.services.DefaultRegisteredService.java

License:Open Source License

protected void prepareUser(final User user, final String userId, String service, final JsonObject data) {
    if (principalAttributeName != null) {
        user.setUser(data.getString(principalAttributeName));
        data.remove(principalAttributeName);
    } else {/*from w  w  w.ja va2  s  .  c  o m*/
        user.setUser(userId);
    }
    data.remove("password");

    Map<String, String> attributes = new HashMap<>();
    for (String attr : data.fieldNames()) {
        attributes.put(attr, data.getValue(attr).toString());
    }
    user.setAttributes(attributes);
}

From source file:org.entcore.common.http.request.JsonHttpServerRequest.java

License:Open Source License

@Override
public MultiMap headers() {
    MultiMap m = new CaseInsensitiveHeaders();
    JsonObject h = object.getJsonObject("headers");
    if (h != null) {
        for (String attr : h.fieldNames()) {
            m.add(attr, h.getString(attr));
        }//from  w  w  w .  jav  a2  s.c om
    }
    return m;
}

From source file:org.entcore.common.http.request.JsonHttpServerRequest.java

License:Open Source License

@Override
public MultiMap params() {
    MultiMap m = new CaseInsensitiveHeaders();
    JsonObject p = object.getJsonObject("params");
    if (p != null) {
        for (String attr : p.fieldNames()) {
            m.add(attr, p.getString(attr));
        }/* w ww  .j ava2 s.c o  m*/
    }
    return m;
}

From source file:org.entcore.common.neo4j.Neo.java

License:Open Source License

@Deprecated
public static JsonArray resultToJsonArray(JsonObject j) {
    JsonArray r = new fr.wseduc.webutils.collections.JsonArray();
    if (j != null) {
        for (String idx : j.fieldNames()) {
            r.add(j.getJsonObject(idx));
        }/*  w ww  .  j  a v a 2  s  .  c o m*/
    }
    return r;
}

From source file:org.entcore.common.neo4j.Neo4jUtils.java

License:Open Source License

public static String nodeSetPropertiesFromJson(String nodeAlias, JsonObject json, String... ignore) {
    StringBuilder sb = new StringBuilder();
    List<String> i;// w w  w  .  j  a v a 2 s.  c  o  m
    if (ignore != null) {
        i = Arrays.asList(ignore);
    } else {
        i = Collections.emptyList();
    }
    for (String a : json.fieldNames()) {
        String attr = a.replaceAll("\\W+", "");
        if (i.contains(attr))
            continue;
        sb.append(", ").append(nodeAlias).append(".").append(attr).append(" = {").append(attr).append("}");
    }
    if (sb.length() > 2) {
        return sb.append(" ").substring(2);
    }
    return " ";
}

From source file:org.entcore.common.service.impl.MongoDbCrudService.java

License:Open Source License

@Override
public void update(String id, JsonObject data, UserInfos user, Handler<Either<String, JsonObject>> handler) {
    QueryBuilder query = QueryBuilder.start("_id").is(id);
    addPlainField(data);// w  w  w.j  av a  2  s .  co  m
    MongoUpdateBuilder modifier = new MongoUpdateBuilder();
    for (String attr : data.fieldNames()) {
        modifier.set(attr, data.getValue(attr));
    }
    modifier.set("modified", MongoDb.now());
    mongo.update(collection, MongoQueryBuilder.build(query), modifier.build(),
            validActionResultHandler(handler));
}

From source file:org.entcore.common.service.impl.SqlCrudService.java

License:Open Source License

@Override
public void update(String id, JsonObject data, UserInfos user, Handler<Either<String, JsonObject>> handler) {
    StringBuilder sb = new StringBuilder();
    JsonArray values = new fr.wseduc.webutils.collections.JsonArray();
    for (String attr : data.fieldNames()) {
        sb.append(attr).append(" = ?, ");
        values.add(data.getValue(attr));
    }//from www  . ja  v a 2 s  .c  o  m
    String query = "UPDATE " + resourceTable + " SET " + sb.toString() + "modified = NOW() " + "WHERE id = ? ";
    sql.prepared(query, values.add(parseId(id)), validRowsResultHandler(handler));
}

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

License:Open Source License

protected void getShareInfos(final String userId, final JsonArray actions, final JsonObject groupCheckedActions,
        final JsonObject userCheckedActions, final String acceptLanguage, String search,
        final Handler<JsonObject> handler) {
    final JsonObject params = new JsonObject().put("groupIds",
            new fr.wseduc.webutils.collections.JsonArray(new ArrayList<>(groupCheckedActions.fieldNames())));
    final JsonObject params2 = new JsonObject().put("userIds",
            new fr.wseduc.webutils.collections.JsonArray(new ArrayList<>(userCheckedActions.fieldNames())));
    if (search != null && search.trim().isEmpty()) {
        final Neo4j neo4j = Neo4j.getInstance();
        neo4j.execute(GROUP_SHARED, params, validResultHandler(new Handler<Either<String, JsonArray>>() {
            @Override/*w w  w. ja va 2s . c o m*/
            public void handle(Either<String, JsonArray> sg) {
                JsonArray visibleGroups;
                if (sg.isRight()) {
                    visibleGroups = sg.right().getValue();
                } else {
                    visibleGroups = new fr.wseduc.webutils.collections.JsonArray();
                }
                final JsonObject groups = new JsonObject();
                groups.put("visibles", visibleGroups);
                groups.put("checked", groupCheckedActions);
                for (Object u : visibleGroups) {
                    if (!(u instanceof JsonObject))
                        continue;
                    JsonObject group = (JsonObject) u;
                    UserUtils.groupDisplayName(group, acceptLanguage);
                }
                neo4j.execute(USER_SHARED, params2,
                        validResultHandler(new Handler<Either<String, JsonArray>>() {
                            @Override
                            public void handle(Either<String, JsonArray> event) {
                                JsonArray visibleUsers;
                                if (event.isRight()) {
                                    visibleUsers = event.right().getValue();
                                } else {
                                    visibleUsers = new fr.wseduc.webutils.collections.JsonArray();
                                }
                                JsonObject users = new JsonObject();
                                users.put("visibles", visibleUsers);
                                users.put("checked", userCheckedActions);
                                JsonObject share = new JsonObject().put("actions", actions)
                                        .put("groups", groups).put("users", users);
                                handler.handle(share);
                            }
                        }));
            }
        }));
    } else {
        final String preFilter;
        if (search != null) {
            preFilter = "AND m.displayNameSearchField CONTAINS {search} ";
            String sanitizedSearch = StringValidation.sanitize(search);
            params.put("search", sanitizedSearch);
            params2.put("search", sanitizedSearch);
        } else {
            preFilter = null;
        }

        final String q = "RETURN distinct profileGroup.id as id, profileGroup.name as name, "
                + "profileGroup.groupDisplayName as groupDisplayName, profileGroup.structureName as structureName "
                + "ORDER BY name " + "UNION " + GROUP_SHARED;

        final String q2 = "RETURN distinct visibles.id as id, visibles.login as login, visibles.displayName as username, "
                + "visibles.lastName as lastName, visibles.firstName as firstName, visibles.profiles[0] as profile "
                + "ORDER BY username " + "UNION " + USER_SHARED;

        UserUtils.findVisibleProfilsGroups(eb, userId, q, params, new Handler<JsonArray>() {
            @Override
            public void handle(JsonArray visibleGroups) {
                final JsonObject groups = new JsonObject();
                groups.put("visibles", visibleGroups);
                groups.put("checked", groupCheckedActions);
                for (Object u : visibleGroups) {
                    if (!(u instanceof JsonObject))
                        continue;
                    JsonObject group = (JsonObject) u;
                    UserUtils.groupDisplayName(group, acceptLanguage);
                }
                findVisibleUsers(eb, userId, false, preFilter, q2, params2, new Handler<JsonArray>() {
                    @Override
                    public void handle(JsonArray visibleUsers) {
                        JsonObject users = new JsonObject();
                        users.put("visibles", visibleUsers);
                        users.put("checked", userCheckedActions);
                        JsonObject share = new JsonObject().put("actions", actions).put("groups", groups)
                                .put("users", users);
                        handler.handle(share);
                    }
                });
            }
        });
    }
}

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

License:Open Source License

protected void shareValidation(String resourceId, String userId, JsonObject share,
        Handler<Either<String, JsonObject>> handler) {
    final JsonObject groups = share.getJsonObject("groups");
    final JsonObject users = share.getJsonObject("users");
    final JsonObject shareBookmark = share.getJsonObject("bookmarks");
    final HashMap<String, Set<String>> membersActions = new HashMap<>();

    if (groups != null && groups.size() > 0) {
        for (String attr : groups.fieldNames()) {
            JsonArray actions = groups.getJsonArray(attr);
            if (actionsExists(actions.getList())) {
                membersActions.put(attr, new HashSet<>(actions.getList()));
            }//from   w  ww .j a va 2s  .  c om
        }
    }
    if (users != null && users.size() > 0) {
        for (String attr : users.fieldNames()) {
            JsonArray actions = users.getJsonArray(attr);
            if (actionsExists(actions.getList())) {
                membersActions.put(attr, new HashSet<>(actions.getList()));
            }
        }
    }
    if (shareBookmark != null && shareBookmark.size() > 0) {
        final JsonObject p = new JsonObject().put("userId", userId);
        StatementsBuilder statements = new StatementsBuilder();
        for (String sbId : shareBookmark.fieldNames()) {
            final String csbId = cleanId(sbId);
            final String query = "MATCH (:User {id:{userId}})-[:HAS_SB]->(sb:ShareBookmark) "
                    + "RETURN DISTINCT '" + csbId + "' as id, TAIL(sb." + csbId + ") as members ";
            statements.add(query, p);
        }
        Neo4j.getInstance().executeTransaction(statements.build(), null, true,
                Neo4jResult.validResultsHandler(sbRes -> {
                    if (sbRes.isRight()) {
                        JsonArray a = sbRes.right().getValue();
                        for (Object o : a) {
                            JsonObject r = ((JsonArray) o).getJsonObject(0);
                            JsonArray actions = shareBookmark.getJsonArray(r.getString("id"));
                            JsonArray mIds = r.getJsonArray("members");
                            if (actions != null && mIds != null && mIds.size() > 0
                                    && actionsExists(actions.getList())) {
                                for (Object mId : mIds) {
                                    Set<String> actionsShare = membersActions.get(mId.toString());
                                    if (actionsShare == null) {
                                        actionsShare = new HashSet<>(new HashSet<>(actions.getList()));
                                        membersActions.put(mId.toString(), actionsShare);
                                        //                        } else {
                                        //                           actionsShare.addAll(new HashSet<>(actions.getList()));
                                    }
                                }
                            }
                        }
                        shareValidationVisible(userId, resourceId, handler, membersActions,
                                shareBookmark.fieldNames());
                    } else {
                        handler.handle(new Either.Left<>(sbRes.left().getValue()));
                    }
                }));
    } else {
        shareValidationVisible(userId, resourceId, handler, membersActions, null);
    }
}