Example usage for io.vertx.core.json JsonArray getString

List of usage examples for io.vertx.core.json JsonArray getString

Introduction

In this page you can find the example usage for io.vertx.core.json JsonArray getString.

Prototype

public String getString(int pos) 

Source Link

Document

Get the String at position pos in the array,

Usage

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

License:Open Source License

@Override
public void sendDailyMails(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 dayDate = Calendar.getInstance();
    dayDate.add(Calendar.DAY_OF_MONTH, dayDelta);
    dayDate.set(Calendar.HOUR_OF_DAY, 0);
    dayDate.set(Calendar.MINUTE, 0);
    dayDate.set(Calendar.SECOND, 0);
    dayDate.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("[DailyMails] Page0 : " + userPagination.get() + "/" + endPage.get());
                    continuation.handle(userPagination.get() != endPage.get());
                    return;
                }//w ww .j  av  a2  s. com
                final AtomicInteger usersCountdown = new AtomicInteger(nbUsers);

                final Handler<Void> usersEndHandler = new Handler<Void>() {
                    public void handle(Void v) {
                        if (usersCountdown.decrementAndGet() <= 0) {
                            log.info("[DailyMails] 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;

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

                                                    SimpleDateFormat formatter = new SimpleDateFormat(
                                                            "EEE, d MMM yyyy HH:mm:ss",
                                                            Locale.forLanguageTag(userLanguage));
                                                    final JsonArray dates = new fr.wseduc.webutils.collections.JsonArray();
                                                    final JsonArray templates = 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.DAILY.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)))) {
                                                            templates.add(new JsonObject()
                                                                    .put("template", notificationsDefaults
                                                                            .getJsonObject(notificationName,
                                                                                    new JsonObject())
                                                                            .getString("template", ""))
                                                                    .put("params", notification.getJsonObject(
                                                                            "params", new JsonObject())));
                                                            dates.add(formatter.format(MongoDb.parseIsoDate(
                                                                    notification.getJsonObject("date"))));
                                                        }
                                                    }
                                                    if (templates.size() > 0) {
                                                        JsonObject templateParams = new JsonObject()
                                                                .put("nestedTemplatesArray", templates)
                                                                .put("notificationDates", dates);
                                                        processTimelineTemplate(templateParams, "",
                                                                "notifications/daily-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 daily 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.daily.mail.subject.header");
                                                                        translateTimeline(keys, userDomain,
                                                                                userLanguage,
                                                                                new Handler<JsonArray>() {
                                                                                    public void handle(
                                                                                            JsonArray translations) {
                                                                                        //Send mail containing the "daily" 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("[sendDailyMails] 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(dayDate.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("[sendDailyMails] 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.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;
                }//from www .  jav  a2  s  .  c o  m
                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.entcore.workspace.service.WorkspaceService.java

License:Open Source License

private void addDocument(final Message<JsonObject> message) {
    JsonObject uploaded = message.body().getJsonObject("uploaded");
    JsonObject doc = message.body().getJsonObject("document");
    if (doc == null || uploaded == null) {
        message.reply(new JsonObject().put("status", "error").put("message", "missing.attribute"));
        return;// w  ww  .  j  ava  2  s  .c o m
    }
    String name = message.body().getString("name");
    String application = message.body().getString("application");
    JsonArray t = message.body().getJsonArray("thumbs", new fr.wseduc.webutils.collections.JsonArray());
    List<String> thumbs = new ArrayList<>();
    for (int i = 0; i < t.size(); i++) {
        thumbs.add(t.getString(i));
    }
    addAfterUpload(uploaded, doc, name, application, thumbs, DocumentDao.DOCUMENTS_COLLECTION,
            new Handler<Message<JsonObject>>() {
                @Override
                public void handle(Message<JsonObject> m) {
                    if (m != null) {
                        message.reply(m.body());
                    }
                }
            });
}

From source file:org.entcore.workspace.service.WorkspaceService.java

License:Open Source License

private void updateDocument(final Message<JsonObject> message) {
    JsonObject uploaded = message.body().getJsonObject("uploaded");
    String id = message.body().getString("id");
    if (uploaded == null || id == null || id.trim().isEmpty()) {
        message.reply(new JsonObject().put("status", "error").put("message", "missing.attribute"));
        return;/*from   w  w w  .j  a v  a 2s .c o m*/
    }
    String name = message.body().getString("name");
    JsonArray t = message.body().getJsonArray("thumbs", new fr.wseduc.webutils.collections.JsonArray());
    List<String> thumbs = new ArrayList<>();
    for (int i = 0; i < t.size(); i++) {
        thumbs.add(t.getString(i));
    }
    updateAfterUpload(id, name, uploaded, thumbs, null, new Handler<Message<JsonObject>>() {
        @Override
        public void handle(Message<JsonObject> m) {
            if (m != null) {
                message.reply(m.body());
            }
        }
    });
}

From source file:org.pac4j.vertx.core.DefaultJsonConverter.java

License:Apache License

@Override
public Object decodeObject(Object value) {
    if (value == null) {
        return null;
    } else if (isPrimitiveType(value)) {
        return value;
    } else if (value instanceof JsonArray) {
        JsonArray src = (JsonArray) value;
        List<Object> list = new ArrayList<>(src.size());
        for (Object object : src) {
            list.add(decodeObject(object));
        }/*ww w  .j a v a  2 s . co m*/
        return list.toArray();
    } else if (value instanceof JsonObject) {
        JsonObject src = (JsonObject) value;
        try {
            return decode(src.getJsonObject("value").encode(), Class.forName(src.getString("class")));
        } catch (Exception e) {
            throw new TechnicalException("Error while decoding object", e);
        }
    }
    return null;
}

From source file:org.pac4j.vertx.VertxWebContext.java

License:Apache License

@Override
public String getRequestParameter(String name) {
    JsonArray values = parameters.getJsonArray(name);
    if (values != null && values.size() > 0) {
        return values.getString(0);
    }/*w  ww. ja v a 2 s. com*/
    return null;
}