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

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

Introduction

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

Prototype

public JsonObject put(String key, Object value) 

Source Link

Document

Put an Object into the JSON object with the specified key.

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", "")));
                        }//from w  ww .  j  a  va 2  s.  co 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.blog.services.impl.DefaultPostService.java

License:Open Source License

@Override
public void counter(final String blogId, final UserInfos user,
        final Handler<Either<String, JsonArray>> result) {
    final QueryBuilder query = QueryBuilder.start("blog.$id").is(blogId);
    final QueryBuilder isManagerQuery = getDefautQueryBuilderForList(blogId, user, true);
    final JsonObject projection = new JsonObject();
    projection.put("state", 1);
    projection.put("_id", -1);

    final Handler<Message<JsonObject>> finalHandler = new Handler<Message<JsonObject>>() {
        @Override/*  w w w .  ja va2 s.  c o  m*/
        public void handle(Message<JsonObject> event) {
            result.handle(Utils.validResults(event));
        }
    };

    mongo.count("blogs", MongoQueryBuilder.build(isManagerQuery), new Handler<Message<JsonObject>>() {
        public void handle(Message<JsonObject> event) {
            JsonObject res = event.body();
            if (res == null || !"ok".equals(res.getString("status"))) {
                result.handle(new Either.Left<String, JsonArray>(event.body().encodePrettily()));
                return;
            }
            boolean isManager = 1 == res.getInteger("count", 0);

            query.or(QueryBuilder.start("state").is(StateType.PUBLISHED.name()).get(),
                    QueryBuilder.start()
                            .and(QueryBuilder.start("author.userId").is(user.getUserId()).get(),
                                    QueryBuilder.start("state").is(StateType.DRAFT.name()).get())
                            .get(),
                    isManager ? QueryBuilder.start("state").is(StateType.SUBMITTED.name()).get()
                            : QueryBuilder.start()
                                    .and(QueryBuilder.start("author.userId").is(user.getUserId()).get(),
                                            QueryBuilder.start("state").is(StateType.SUBMITTED.name()).get())
                                    .get());
            mongo.find(POST_COLLECTION, MongoQueryBuilder.build(query), null, projection, finalHandler);
        }
    });
}

From source file:org.entcore.cas.data.EntCoreDataHandler.java

License:Open Source License

@Override
public void persistAuth(AuthCas authCas, final Handler<Boolean> handler) {
    JsonObject query = new JsonObject().put("id", authCas.getId());
    JsonObject doc = serialize(authCas);
    if (doc == null) {
        handler.handle(false);/*  w  w w  .  j  a  va2  s.  c o m*/
        return;
    }
    doc.put("updatedAt", MongoDb.now());
    mongoDb.update(COLLECTION, query, doc, true, false, new io.vertx.core.Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> event) {
            handler.handle("ok".equals(event.body().getString("status")));
        }
    });
}

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

License:Open Source License

@Override
public void getUser(final String userId, final String service, final Handler<User> userHandler) {
    JsonObject jo = new JsonObject();
    jo.put("action", directoryAction).put("userId", userId);
    eb.send("directory", jo, handlerToAsyncHandler(new io.vertx.core.Handler<Message<JsonObject>>() {
        @Override//from w  w  w.j a v  a  2 s  .c  o m
        public void handle(Message<JsonObject> event) {
            JsonObject res = event.body().getJsonObject("result");
            log.debug("res : " + res);
            if ("ok".equals(event.body().getString("status")) && res != null) {
                User user = new User();
                prepareUser(user, userId, service, res);
                userHandler.handle(user);
            } else {
                userHandler.handle(null);
            }
        }
    }));
}

From source file:org.entcore.common.aggregation.indicators.mongo.IndicatorMongoImpl.java

License:Open Source License

private void executeAggregationQuery(final IndicatorGroup group, final Handler<JsonObject> finalHandler) {
    //Filter by trace type + custom filters
    final MongoDBBuilder filteringQuery = (MongoDBBuilder) new MongoDBBuilder().put(TRACE_FIELD_TYPE)
            .is(indicatorKey);/*w w w .j a v  a 2s  . c om*/
    for (IndicatorFilter filter : filters) {
        filter.filter(filteringQuery);
    }

    final JsonObject aggregation = new JsonObject();
    JsonArray pipeline = new fr.wseduc.webutils.collections.JsonArray();
    aggregation.put("aggregate", COLLECTIONS.events.name()).put("allowDiskUse", true).put("pipeline", pipeline);

    pipeline.add(new JsonObject().put("$match", MongoQueryBuilder.build(filteringQuery)));
    addUnwindPipeline(pipeline, group);
    JsonObject groupBy = new JsonObject().put("$group",
            new JsonObject().put("_id", getGroupByObject(new JsonObject(), group)).put("count",
                    new JsonObject().put("$sum", 1)));
    pipeline.add(groupBy);

    //Customize the request if needed
    customizeGroupBy(groupBy);
    customizePipeline(pipeline);

    mongo.command(aggregation.toString(), new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> message) {
            if ("ok".equals(message.body().getString("status"))
                    && message.body().getJsonObject("result", new JsonObject()).getInteger("ok") == 1) {
                JsonArray result = message.body().getJsonObject("result").getJsonArray("result");
                writeStats(result, group, finalHandler);
            } else {
                String groupstr = group == null ? "Global" : group.toString();
                log.error("[Aggregation][Error]{" + writtenIndicatorKey + "} (" + groupstr
                        + ") executeAggregationQuery : " + message.body().toString());
                log.info(aggregation.toString());
                finalHandler.handle(new JsonObject());
            }
        }
    });

    //Recurse
    if (group != null)
        for (IndicatorGroup child : group.getChildren()) {
            executeAggregationQuery(child, finalHandler);
        }
}

From source file:org.entcore.common.appregistry.ApplicationUtils.java

License:Open Source License

private static void applicationInfos(EventBus eb, String action, String application, JsonArray users,
        JsonArray groups, final Handler<JsonArray> handler) {
    JsonObject json = new JsonObject();
    json.put("action", action).put("application", application);
    if (users != null) {
        json.put("users", users);
    }/*from  w ww. j a v  a2s  .  co  m*/
    if (groups != null) {
        json.put("groups", groups);
    }
    eb.send(APP_REGISTRY_ADDRESS, json, new Handler<AsyncResult<Message<JsonArray>>>() {
        @Override
        public void handle(AsyncResult<Message<JsonArray>> event) {
            if (event.succeeded()) {
                handler.handle(event.result().body());
            } else {
                handler.handle(null);
            }
        }
    });
}

From source file:org.entcore.common.appregistry.ApplicationUtils.java

License:Open Source License

public static void setDefaultClassRoles(EventBus eb, String classId,
        Handler<AsyncResult<Message<JsonObject>>> handler) {
    JsonObject json = new JsonObject();
    json.put("action", "setDefaultClassRoles").put("classId", classId);
    eb.send(APP_REGISTRY_BUS_ADDRESS, json, handler);
}

From source file:org.entcore.common.bus.WorkspaceHelper.java

License:Open Source License

public void addDocument(JsonObject uploaded, UserInfos userInfos, String name, String application,
        boolean protectedContent, JsonArray thumbs, Handler<AsyncResult<Message<JsonObject>>> handler) {
    if (userInfos == null) {
        if (handler != null) {
            handleAsyncError("invalid.user", handler);
        }/*ww w.  j  av  a2  s .c o  m*/
        return;
    }
    JsonObject doc = new JsonObject();
    String now = MongoDb.formatDate(new Date());
    doc.put("created", now);
    doc.put("modified", now);
    doc.put("owner", userInfos.getUserId());
    doc.put("ownerName", userInfos.getUsername());
    if (application != null && !application.trim().isEmpty() && protectedContent) {
        doc.put("protected", true);
    }
    JsonObject m = new JsonObject().put("action", "addDocument").put("document", doc).put("uploaded", uploaded)
            .put("name", name).put("application", application).put("thumbs", thumbs);
    eb.send(WORKSPACE_ADDRESS, m, handler);
}

From source file:org.entcore.common.controller.ControllerHelper.java

License:Open Source License

@Override
public void sendNotify(final HttpServerRequest request, final String resource, final UserInfos user,
        final List<String> recipients, final String notificationName, JsonObject p,
        final String resourceNameAttribute) {
    if (p == null) {
        p = new JsonObject().put("uri", "/userbook/annuaire#" + user.getUserId() + "#" + user.getType())
                .put("username", user.getUsername()).put("resourceUri", pathPrefix + "/" + resource);
    }//from  w w  w . j  ava  2 s . com
    final JsonObject params = p;
    crudService.retrieve(resource, new Handler<Either<String, JsonObject>>() {
        @Override
        public void handle(Either<String, JsonObject> r) {
            if (r.isRight()) {
                String attr = (resourceNameAttribute != null && !resourceNameAttribute.trim().isEmpty())
                        ? resourceNameAttribute
                        : "name";
                params.put("resourceName", r.right().getValue().getString(attr, ""));
                notification.notifyTimeline(request, notificationName, user, recipients, params);
            } else {
                log.error("Unable to send timeline notification : missing name on resource " + resource);
            }
        }
    });
}

From source file:org.entcore.common.elasticsearch.BulkRequest.java

License:Open Source License

public void index(JsonObject element, JsonObject metadata) {
    if (element == null)
        return;/*  w ww . ja v  a  2s.com*/
    if (metadata == null) {
        metadata = new JsonObject();
        final Object id = element.remove("_id");
        if (id != null) {
            metadata.put("_id", id);
        }
    }
    request.write(new JsonObject().put("index", metadata).encode() + "\n" + element.encode() + "\n");
}