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

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

Introduction

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

Prototype

public Object getValue(String key) 

Source Link

Document

Get the value with the specified key, as an Object with types respecting the limitations of JSON.

Usage

From source file:org.eclipse.hono.util.TenantObject.java

License:Open Source License

/**
 * Adds configuration information for a protocol adapter.
 * /*from   w  w w .j a  v a  2  s .c o m*/
 * @param config The configuration properties to add.
 * @throws NullPointerException if config is {@code null}.
 * @throws IllegalArgumentException if the given configuration does not contain
 *                a <em>type</em> name.
 * @return This tenant for command chaining.
 */
public TenantObject addAdapterConfiguration(final JsonObject config) {

    final Object type = config.getValue(TenantConstants.FIELD_ADAPTERS_TYPE);
    if (String.class.isInstance(type)) {
        if (adapterConfigurations == null) {
            adapterConfigurations = new HashMap<>();
        }
        adapterConfigurations.put((String) type, config);
    } else {
        throw new IllegalArgumentException("adapter configuration must contain type field");
    }
    return this;
}

From source file:org.entcore.auth.services.impl.DefaultConfigurationService.java

License:Open Source License

@Override
public void getWelcomeMessage(String domain, String language,
        final Handler<Either<String, JsonObject>> handler) {
    final JsonObject q = new JsonObject().put("type", WELCOME_MESSAGE_TYPE);
    JsonObject keys = null;/*from   ww w .  j a  v  a2s . c om*/
    if (isNotEmpty(domain) && isNotEmpty(language)) {
        keys = new JsonObject();
        keys.put("_id", 0);
        keys.put(domain.replaceAll("\\.", "_") + "." + language, 1);
        keys.put(domain.replaceAll("\\.", "_") + ".enabled", 1);
    } else if (isNotEmpty(domain)) {
        keys = new JsonObject();
        keys.put("_id", 0);
        keys.put(domain.replaceAll("\\.", "_"), 1);
    }
    mongoDb.findOne(PLATEFORM_COLLECTION, q, keys, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> res) {
            if ("ok".equals(res.body().getString("status"))) {
                JsonObject r = res.body().getJsonObject("result", new JsonObject());
                JsonObject j = new JsonObject();
                for (String attr : r.fieldNames()) {
                    j.put(attr.replaceAll("_", "."), r.getValue(attr));
                }
                handler.handle(new Either.Right<String, JsonObject>(j));
            } else {
                handler.handle(new Either.Left<String, JsonObject>(res.body().getString("message", "")));
            }
        }
    });
}

From source file:org.entcore.blog.services.impl.DefaultBlogService.java

License:Open Source License

@Override
public void update(String blogId, JsonObject blog, final Handler<Either<String, JsonObject>> result) {
    blog.put("modified", MongoDb.now());
    if (blog.getString("comment-type") != null) {
        try {/* w  ww.ja  va2 s . c o  m*/
            CommentType.valueOf(blog.getString("comment-type").toUpperCase());
            blog.put("comment-type", blog.getString("comment-type").toUpperCase());
        } catch (IllegalArgumentException | NullPointerException e) {
            blog.remove("comment-type");
        }
    }
    if (blog.getString("publish-type") != null) {
        try {
            PublishType.valueOf(blog.getString("publish-type").toUpperCase());
            blog.put("publish-type", blog.getString("publish-type").toUpperCase());
        } catch (IllegalArgumentException | NullPointerException e) {
            blog.remove("publish-type");
        }
    }
    JsonObject b = Utils.validAndGet(blog, UPDATABLE_FIELDS, Collections.<String>emptyList());
    if (validationError(result, b))
        return;
    QueryBuilder query = QueryBuilder.start("_id").is(blogId);
    MongoUpdateBuilder modifier = new MongoUpdateBuilder();
    for (String attr : b.fieldNames()) {
        modifier.set(attr, b.getValue(attr));
    }
    mongo.update(BLOG_COLLECTION, MongoQueryBuilder.build(query), modifier.build(),
            new Handler<Message<JsonObject>>() {
                @Override
                public void handle(Message<JsonObject> event) {
                    result.handle(Utils.validResult(event));
                }
            });
}

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", "")));
                        }//from w  ww  .  ja v a2  s  .c o m

                        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 {//ww  w . ja va2s.  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.neo4j.Neo4jResult.java

License:Open Source License

public static Either<String, JsonObject> fullNodeMerge(String nodeAttr, Message<JsonObject> res,
        String... otherNodes) {/*from  w  w w  . j  av a 2  s  .  c  om*/
    Either<String, JsonObject> r = validUniqueResult(res);
    if (r.isRight() && r.right().getValue().size() > 0) {
        JsonObject j = r.right().getValue();
        JsonObject data = j.getJsonObject(nodeAttr, new JsonObject()).getJsonObject("data");
        if (otherNodes != null && otherNodes.length > 0) {
            for (String attr : otherNodes) {
                Object e = j.getValue(attr);
                if (e == null)
                    continue;
                if (e instanceof JsonObject) {
                    data.put(attr, ((JsonObject) e).getJsonObject("data"));
                } else if (e instanceof JsonArray) {
                    JsonArray a = new fr.wseduc.webutils.collections.JsonArray();
                    for (Object o : (JsonArray) e) {
                        if (!(o instanceof JsonObject))
                            continue;
                        JsonObject jo = (JsonObject) o;
                        a.add(jo.getJsonObject("data"));
                    }
                    data.put(attr, a);
                }
                j.remove(attr);
            }
        }
        if (data != null) {
            j.remove(nodeAttr);
            return new Either.Right<>(data.mergeIn(j));
        }
    }
    return r;
}

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);/*from w ww  .jav  a 2  s.  c o 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.MongoDbCrudService.java

License:Open Source License

private void addPlainField(JsonObject data) {
    if (!this.mongoDbConf.getSearchTextFields().isEmpty()) {
        for (final String field : this.mongoDbConf.getSearchTextFields()) {
            final List<String> decomposition = StringUtils.split(field, "\\.");
            final String collection = decomposition.get(0);
            if (this.collection.equals(collection)) {
                if (decomposition.size() == 2) {
                    //not an object or array
                    final String label = decomposition.get(1);
                    if (data.containsKey(label)) {
                        data.put(label + plainSuffixField, StringUtils.stripHtmlTag(data.getString(label)));
                    }//from   w  ww  .  j  a  v  a 2 s.  co m
                } else if (decomposition.size() == 3) {
                    final String label = decomposition.get(1);
                    final String deepLabel = decomposition.get(2);
                    final Object element = data.getValue(label);
                    if (element instanceof JsonArray) {
                        //not processed yet
                        log.error("the plain duplication d'ont support Json Array");
                    } else if (element instanceof JsonObject) {
                        final JsonObject jo = (JsonObject) element;
                        if (jo.containsKey(deepLabel)) {
                            jo.put(deepLabel + plainSuffixField,
                                    StringUtils.stripHtmlTag(jo.getString(deepLabel)));
                        }
                    }
                } else {
                    //object too complex : not treaty
                    log.error(
                            "the plain duplication only works for a string field or top-level field of an object : collection.field | collection.object.field");
                }
            } else {
                break;
            }
        }
    }
}

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));
    }/* w w w .jav  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.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;//from ww  w . j  av  a 2  s .  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);
}