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

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

Introduction

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

Prototype

public Map<String, Object> getMap() 

Source Link

Document

Get the underlying Map as is.

Usage

From source file:org.entcore.timeline.services.impl.DefaultTimelineMailerService.java

License:Open Source License

public void sendWeeklyMails(int dayDelta, final Handler<Either<String, JsonObject>> handler) {

    final HttpServerRequest request = new JsonHttpServerRequest(new JsonObject());
    final AtomicInteger userPagination = new AtomicInteger(0);
    final AtomicInteger endPage = new AtomicInteger(0);
    final Calendar weekDate = Calendar.getInstance();
    weekDate.add(Calendar.DAY_OF_MONTH, dayDelta - 6);
    weekDate.set(Calendar.HOUR_OF_DAY, 0);
    weekDate.set(Calendar.MINUTE, 0);
    weekDate.set(Calendar.SECOND, 0);
    weekDate.set(Calendar.MILLISECOND, 0);

    final JsonObject results = new JsonObject().put("mails.sent", 0).put("users.ko", 0);
    final JsonObject notificationsDefaults = new JsonObject();
    final List<String> notifiedUsers = new ArrayList<>();

    final Handler<Boolean> userContinuationHandler = new Handler<Boolean>() {

        private final Handler<Boolean> continuation = this;
        private final Handler<JsonArray> usersHandler = new Handler<JsonArray>() {
            public void handle(final JsonArray users) {
                final int nbUsers = users.size();
                if (nbUsers == 0) {
                    log.info("[WeeklyMails] Page0 : " + userPagination.get() + "/" + endPage.get());
                    continuation.handle(userPagination.get() != endPage.get());
                    return;
                }/*w w  w .  j ava 2s.  c  om*/
                final AtomicInteger usersCountdown = new AtomicInteger(nbUsers);

                final Handler<Void> usersEndHandler = new Handler<Void>() {
                    public void handle(Void v) {
                        if (usersCountdown.decrementAndGet() <= 0) {
                            log.info("[WeeklyMails] Page : " + userPagination.get() + "/" + endPage.get());
                            continuation.handle(userPagination.get() != endPage.get());
                        }
                    }
                };

                final JsonArray userIds = new fr.wseduc.webutils.collections.JsonArray();
                for (Object userObj : users)
                    userIds.add(((JsonObject) userObj).getString("id", ""));
                NotificationUtils.getUsersPreferences(eb, userIds, "language: uac.language",
                        new Handler<JsonArray>() {
                            public void handle(JsonArray preferences) {
                                for (Object userObj : preferences) {
                                    final JsonObject userPrefs = (JsonObject) userObj;
                                    final String userDomain = userPrefs.getString("lastDomain",
                                            I18n.DEFAULT_DOMAIN);
                                    final String userScheme = userPrefs.getString("lastScheme", "http");
                                    String mutableUserLanguage = "fr";
                                    try {
                                        mutableUserLanguage = getOrElse(new JsonObject(
                                                getOrElse(userPrefs.getString("language"), "{}", false))
                                                        .getString("default-domain"),
                                                "fr", false);
                                    } catch (Exception e) {
                                        log.error("UserId [" + userPrefs.getString("userId", "")
                                                + "] - Bad language preferences format");
                                    }
                                    final String userLanguage = mutableUserLanguage;

                                    getAggregatedUserNotifications(userPrefs.getString("userId", ""),
                                            weekDate.getTime(), new Handler<JsonArray>() {
                                                public void handle(JsonArray notifications) {
                                                    if (notifications.size() == 0) {
                                                        usersEndHandler.handle(null);
                                                        return;
                                                    }

                                                    final JsonArray weeklyNotifications = new fr.wseduc.webutils.collections.JsonArray();

                                                    for (Object notificationObj : notifications) {
                                                        JsonObject notification = (JsonObject) notificationObj;
                                                        final String notificationName = notification
                                                                .getString("type", "").toLowerCase() + "."
                                                                + notification.getString("event-type", "")
                                                                        .toLowerCase();
                                                        if (notificationsDefaults
                                                                .getJsonObject(notificationName) == null)
                                                            continue;

                                                        JsonObject notificationPreference = userPrefs
                                                                .getJsonObject("preferences", new JsonObject())
                                                                .getJsonObject("config", new JsonObject())
                                                                .getJsonObject(notificationName,
                                                                        new JsonObject());
                                                        if (TimelineNotificationsLoader.Frequencies.WEEKLY
                                                                .name()
                                                                .equals(notificationPrefsMixin(
                                                                        "defaultFrequency",
                                                                        notificationPreference,
                                                                        notificationsDefaults.getJsonObject(
                                                                                notificationName)))
                                                                && !TimelineNotificationsLoader.Restrictions.INTERNAL
                                                                        .name()
                                                                        .equals(notificationPrefsMixin(
                                                                                "restriction",
                                                                                notificationPreference,
                                                                                notificationsDefaults
                                                                                        .getJsonObject(
                                                                                                notificationName)))
                                                                && !TimelineNotificationsLoader.Restrictions.HIDDEN
                                                                        .name()
                                                                        .equals(notificationPrefsMixin(
                                                                                "restriction",
                                                                                notificationPreference,
                                                                                notificationsDefaults
                                                                                        .getJsonObject(
                                                                                                notificationName)))) {
                                                            notification.put("notificationName",
                                                                    notificationName);
                                                            weeklyNotifications.add(notification);
                                                        }
                                                    }

                                                    final JsonObject weeklyNotificationsObj = new JsonObject();
                                                    final JsonArray weeklyNotificationsGroupedArray = new fr.wseduc.webutils.collections.JsonArray();
                                                    for (Object notif : weeklyNotifications) {
                                                        JsonObject notification = (JsonObject) notif;
                                                        if (!weeklyNotificationsObj.containsKey(
                                                                notification.getString("type").toLowerCase()))
                                                            weeklyNotificationsObj.put(
                                                                    notification.getString("type")
                                                                            .toLowerCase(),
                                                                    new JsonObject().put("link",
                                                                            notificationsDefaults.getJsonObject(
                                                                                    notification.getString(
                                                                                            "notificationName"))
                                                                                    .getString("app-address",
                                                                                            ""))
                                                                            .put("event-types",
                                                                                    new fr.wseduc.webutils.collections.JsonArray()));
                                                        weeklyNotificationsObj
                                                                .getJsonObject(notification.getString("type")
                                                                        .toLowerCase())
                                                                .getJsonArray(("event-types"),
                                                                        new fr.wseduc.webutils.collections.JsonArray())
                                                                .add(notification);
                                                    }

                                                    for (String key : weeklyNotificationsObj.getMap()
                                                            .keySet()) {
                                                        weeklyNotificationsGroupedArray.add(new JsonObject()
                                                                .put("type", key)
                                                                .put("link",
                                                                        weeklyNotificationsObj
                                                                                .getJsonObject(key)
                                                                                .getString("link", ""))
                                                                .put("event-types",
                                                                        weeklyNotificationsObj
                                                                                .getJsonObject(key)
                                                                                .getJsonArray("event-types")));
                                                    }

                                                    if (weeklyNotifications.size() > 0) {
                                                        JsonObject templateParams = new JsonObject().put(
                                                                "notifications",
                                                                weeklyNotificationsGroupedArray);
                                                        processTimelineTemplate(templateParams, "",
                                                                "notifications/weekly-mail.html", userDomain,
                                                                userScheme, userLanguage, false,
                                                                new Handler<String>() {
                                                                    public void handle(
                                                                            final String processedTemplate) {
                                                                        //On completion : log
                                                                        final Handler<AsyncResult<Message<JsonObject>>> completionHandler = event -> {
                                                                            if (event.failed()
                                                                                    || "error".equals(event
                                                                                            .result().body()
                                                                                            .getString("status",
                                                                                                    "error"))) {
                                                                                log.error(
                                                                                        "[Timeline weekly emails] Error while sending mail : ",
                                                                                        event.cause());
                                                                                results.put("users.ko",
                                                                                        results.getInteger(
                                                                                                "users.ko")
                                                                                                + 1);
                                                                            } else {
                                                                                results.put("mails.sent",
                                                                                        results.getInteger(
                                                                                                "mails.sent")
                                                                                                + 1);
                                                                            }
                                                                            usersEndHandler.handle(null);
                                                                        };

                                                                        //Translate mail title
                                                                        JsonArray keys = new fr.wseduc.webutils.collections.JsonArray()
                                                                                .add("timeline.weekly.mail.subject.header");
                                                                        translateTimeline(keys, userDomain,
                                                                                userLanguage,
                                                                                new Handler<JsonArray>() {
                                                                                    public void handle(
                                                                                            JsonArray translations) {
                                                                                        //Send mail containing the "weekly" notifications
                                                                                        emailSender.sendEmail(
                                                                                                request,
                                                                                                userPrefs
                                                                                                        .getString(
                                                                                                                "userMail",
                                                                                                                ""),
                                                                                                null, null,
                                                                                                translations
                                                                                                        .getString(
                                                                                                                0),
                                                                                                processedTemplate,
                                                                                                null, false,
                                                                                                completionHandler);
                                                                                    }
                                                                                });
                                                                    }
                                                                });
                                                    } else {
                                                        usersEndHandler.handle(null);
                                                    }

                                                }
                                            });
                                }
                            }
                        });
            }
        };

        public void handle(Boolean continuation) {
            if (continuation) {
                getImpactedUsers(notifiedUsers, userPagination.getAndIncrement(),
                        new Handler<Either<String, JsonArray>>() {
                            public void handle(Either<String, JsonArray> event) {
                                if (event.isLeft()) {
                                    log.error("[sendWeeklyMails] Error while retrieving impacted users : "
                                            + event.left().getValue());
                                    handler.handle(
                                            new Either.Left<String, JsonObject>(event.left().getValue()));
                                } else {
                                    JsonArray users = event.right().getValue();
                                    usersHandler.handle(users);
                                }
                            }
                        });
            } else {
                handler.handle(new Either.Right<String, JsonObject>(results));
            }
        }
    };
    getRecipientsUsers(weekDate.getTime(), new Handler<JsonArray>() {
        @Override
        public void handle(JsonArray event) {
            if (event != null && event.size() > 0) {
                notifiedUsers.addAll(event.getList());
                endPage.set((event.size() / USERS_LIMIT) + (event.size() % USERS_LIMIT != 0 ? 1 : 0));
            } else {
                handler.handle(new Either.Right<String, JsonObject>(results));
                return;
            }
            getNotificationsDefaults(new Handler<JsonArray>() {
                public void handle(final JsonArray notifications) {
                    if (notifications == null) {
                        log.error("[sendWeeklyMails] Error while retrieving notifications defaults.");
                    } else {
                        for (Object notifObj : notifications) {
                            final JsonObject notif = (JsonObject) notifObj;
                            notificationsDefaults.put(notif.getString("key", ""), notif);
                        }
                        userContinuationHandler.handle(true);
                    }
                }
            });

        }
    });
}

From source file:org.etourdot.vertx.marklogic.model.options.TransformOptions.java

License:Open Source License

public TransformOptions(JsonObject jsonObject) {
    this();//from   w  w w.j  a va2  s. c o  m
    requireNonNull(jsonObject);

    name(jsonObject.getString(NAME));
    Map<String, Object> params = jsonObject.getMap();
    params.remove(NAME);
    parameters(new JsonObject(params));
}