List of usage examples for io.vertx.core.json JsonObject containsKey
public boolean containsKey(String key)
From source file:org.entcore.feeder.dictionary.users.AbstractUser.java
License:Open Source License
public static void checkUpdateEmail(JsonObject object, TransactionHelper transactionHelper) { if (object.containsKey("email")) { final String queryUpdateEmail = "MATCH (u:User {externalId: {externalId}}) " + "WHERE NOT(HAS(u.email)) OR (HAS(u.activationCode) AND u.email <> {email}) " + "SET u.email = {email}"; transactionHelper.add(queryUpdateEmail, object); }//w w w. j a v a2s . co m }
From source file:org.entcore.feeder.timetable.edt.EDTImporter.java
License:Open Source License
void addCourse(JsonObject currentEntity) { final List<Long> weeks = new ArrayList<>(); final List<JsonObject> items = new ArrayList<>(); for (String attr : currentEntity.fieldNames()) { if (!ignoreAttributes.contains(attr) && currentEntity.getValue(attr) instanceof JsonArray) { for (Object o : currentEntity.getJsonArray(attr)) { if (!(o instanceof JsonObject)) continue; final JsonObject j = (JsonObject) o; j.put("itemType", attr); final String week = j.getString("Semaines"); if (week != null) { weeks.add(Long.valueOf(week)); items.add(j);//from w ww.ja v a 2 s.co m } } } } if (log.isDebugEnabled() && currentEntity.containsKey("SemainesAnnulation")) { log.debug(currentEntity.encode()); } final Long cancelWeek = (currentEntity.getString("SemainesAnnulation") != null) ? Long.valueOf(currentEntity.getString("SemainesAnnulation")) : null; BitSet lastWeek = new BitSet(weeks.size()); int startCourseWeek = 0; for (int i = 1; i < 53; i++) { final BitSet currentWeek = new BitSet(weeks.size()); boolean enabledCurrentWeek = false; for (int j = 0; j < weeks.size(); j++) { if (cancelWeek != null && ((1L << i) & cancelWeek) != 0) { currentWeek.set(j, false); } else { final Long week = weeks.get(j); currentWeek.set(j, ((1L << i) & week) != 0); } enabledCurrentWeek = enabledCurrentWeek | currentWeek.get(j); } if (!currentWeek.equals(lastWeek)) { if (startCourseWeek > 0) { persistCourse(generateCourse(startCourseWeek, i - 1, lastWeek, items, currentEntity)); } startCourseWeek = enabledCurrentWeek ? i : 0; lastWeek = currentWeek; } } }
From source file:org.entcore.feeder.utils.Validator.java
License:Open Source License
private void generate(JsonObject object) { for (String attr : generate.fieldNames()) { if (object.containsKey(attr)) continue; JsonObject j = generate.getJsonObject(attr); switch (j.getString("generator", "")) { case "uuid4": uuid4(attr, object);//from w w w. ja va 2 s . co m break; case "login": loginGenerator(attr, object, getParameters(object, j)); break; case "displayName": displayNameGenerator(attr, object, getParameters(object, j)); break; case "activationCode": activationCodeGenerator(attr, object, getParameter(object, j)); break; case "nowDate": nowDate(attr, object); break; case "sanitize": sanitizeGenerator(attr, object, getParameter(object, j)); break; default: } } }
From source file:org.entcore.timeline.controllers.helper.NotificationHelper.java
License:Open Source License
public void sendImmediateNotifications(final HttpServerRequest request, final JsonObject json) { //Get notification properties (mixin : admin console configuration which overrides default properties) final String notificationName = json.getString("notificationName"); final JsonObject notification = json.getJsonObject("notification"); configService.getNotificationProperties(notificationName, new Handler<Either<String, JsonObject>>() { public void handle(final Either<String, JsonObject> properties) { if (properties.isLeft() || properties.right().getValue() == null) { log.error("[NotificationHelper] Issue while retrieving notification (" + notificationName + ") properties."); return; }// www. j a v a 2 s . c o m final JsonObject notificationProperties = properties.right().getValue(); //Get users preferences (overrides notification properties) NotificationUtils.getUsersPreferences(eb, json.getJsonArray("recipientsIds"), "language: uac.language, tokens: uac.fcmTokens ", new Handler<JsonArray>() { public void handle(final JsonArray userList) { if (userList == null) { log.error("[NotificationHelper] Issue while retrieving users preferences."); return; } mailerService.sendImmediateMails(request, notificationName, notification, json.getJsonObject("params"), userList, notificationProperties); if (pushNotifService != null && json.containsKey("pushNotif") && notificationProperties.getBoolean("push-notif") && !TimelineNotificationsLoader.Restrictions.INTERNAL.name() .equals(notificationProperties.getString("restriction")) && !TimelineNotificationsLoader.Restrictions.HIDDEN.name() .equals(notificationProperties.getString("restriction"))) pushNotifService.sendImmediateNotifs(notificationName, json, userList, notificationProperties); } }); } }); }
From source file:org.entcore.timeline.controllers.TimelineController.java
License:Open Source License
private void getExternalNotifications(final Handler<Either<String, JsonObject>> handler) { configService.list(new Handler<Either<String, JsonArray>>() { public void handle(Either<String, JsonArray> event) { if (event.isLeft()) { handler.handle(new Either.Left<String, JsonObject>(event.left().getValue())); return; }// w ww. j av a 2s .co m final JsonObject restricted = new JsonObject(); for (String key : registeredNotifications.keySet()) { JsonObject notif = new JsonObject(registeredNotifications.get(key)); String restriction = notif.getString("restriction", TimelineNotificationsLoader.Restrictions.NONE.name()); for (Object notifConfigObj : event.right().getValue()) { JsonObject notifConfig = (JsonObject) notifConfigObj; if (notifConfig.getString("key", "").equals(key)) { restriction = notifConfig.getString("restriction", restriction); break; } } if (restriction.equals(TimelineNotificationsLoader.Restrictions.EXTERNAL.name()) || restriction.equals(TimelineNotificationsLoader.Restrictions.HIDDEN.name())) { String notifType = notif.getString("type"); if (!restricted.containsKey(notifType)) { restricted.put(notifType, new fr.wseduc.webutils.collections.JsonArray()); } restricted.getJsonArray(notifType).add(notif.getString("event-type")); } } handler.handle(new Either.Right<String, JsonObject>(restricted)); } }); }
From source file:org.entcore.timeline.events.DefaultTimelineEventStore.java
License:Open Source License
@Override public void add(JsonObject event, final Handler<JsonObject> result) { JsonObject doc = validAndGet(event); if (doc != null) { if (!doc.containsKey("date")) { doc.put("date", MongoDb.now()); }/* w w w . j a v a 2s . c o m*/ doc.put("created", doc.getJsonObject("date")); mongo.save(TIMELINE_COLLECTION, doc, resultHandler(result)); } else { result.handle(invalidArguments()); } }
From source file:org.entcore.timeline.services.impl.DefaultPushNotifService.java
License:Open Source License
public void processMessage(final JsonObject notification, String language, final boolean typeNotification, final boolean typeData, final Handler<JsonObject> handler) { final JsonObject message = new JsonObject(); translateMessage(language, new Handler<JsonObject>() { @Override/*from w w w . j a v a2 s . co m*/ public void handle(JsonObject keys) { final JsonObject notif = new JsonObject(); final JsonObject data = new JsonObject(); final JsonObject pushNotif = notification.getJsonObject("pushNotif"); String body = pushNotif.getString("body", ""); body = body.length() < MAX_BODY_LENGTH ? body : body.substring(0, MAX_BODY_LENGTH) + "..."; notif.put("title", keys.getString(pushNotif.getString("title"), pushNotif.getString("title", ""))); notif.put("body", body); if (typeData) { if (notification.containsKey("params")) data.put("params", notification.getJsonObject("params").toString()); if (notification.containsKey("resource")) data.put("resource", notification.getString("resource")); if (notification.containsKey("sub-resource")) data.put("sub-resource", notification.getString("sub-resource")); if (!typeNotification) data.put("notification", notif); message.put("data", data); } if (typeNotification) message.put("notification", notif); handler.handle(message); } }); }
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 a va 2 s .co 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
@Put("/share/resource/:id") @SecuredAction(value = "workspace.manager", type = ActionType.RESOURCE) public void shareResource(final HttpServerRequest request) { final String id = request.params().get("id"); request.pause();//from ww w.jav a 2s . co m getUserInfos(eb, request, user -> { if (user != null) { mongo.findOne(DocumentDao.DOCUMENTS_COLLECTION, new JsonObject().put("_id", id), new Handler<Message<JsonObject>>() { @Override public void handle(Message<JsonObject> event) { if ("ok".equals(event.body().getString("status")) && event.body().getJsonObject("result") != null) { request.resume(); final JsonObject res = event.body().getJsonObject("result"); final boolean isFolder = !res.containsKey("file"); if (isFolder) shareFolderAction(request, id, user, res); else shareFileAction(request, id, user, res); } else { unauthorized(request); } } }); } else { badRequest(request, "invalid.user"); } }); }
From source file:org.entcore.workspace.service.WorkspaceService.java
License:Open Source License
private void updateStorage(JsonArray addeds, JsonArray removeds, final Handler<Either<String, JsonObject>> handler) { Map<String, Long> sizes = new HashMap<>(); if (addeds != null) { for (Object o : addeds) { if (!(o instanceof JsonObject)) continue; JsonObject added = (JsonObject) o; Long size = added.getJsonObject("metadata", new JsonObject()).getLong("size", 0l); String userId = (added.containsKey("to")) ? added.getString("to") : added.getString("owner"); if (userId == null) { log.info("UserId is null when update storage size"); log.info(added.encode()); continue; }//from w ww. j a va 2 s . co m Long old = sizes.get(userId); if (old != null) { size += old; } sizes.put(userId, size); } } if (removeds != null) { for (Object o : removeds) { if (!(o instanceof JsonObject)) continue; JsonObject removed = (JsonObject) o; Long size = removed.getJsonObject("metadata", new JsonObject()).getLong("size", 0l); String userId = (removed.containsKey("to")) ? removed.getString("to") : removed.getString("owner"); if (userId == null) { log.info("UserId is null when update storage size"); log.info(removed.encode()); continue; } Long old = sizes.get(userId); if (old != null) { old -= size; } else { old = -1l * size; } sizes.put(userId, old); } } for (final Map.Entry<String, Long> e : sizes.entrySet()) { quotaService.incrementStorage(e.getKey(), e.getValue(), threshold, new Handler<Either<String, JsonObject>>() { @Override public void handle(Either<String, JsonObject> r) { if (r.isRight()) { JsonObject j = r.right().getValue(); UserUtils.addSessionAttribute(eb, e.getKey(), "storage", j.getLong("storage"), null); if (j.getBoolean("notify", false)) { notifyEmptySpaceIsSmall(e.getKey()); } } else { log.error(r.left().getValue()); } if (handler != null) { handler.handle(r); } } }); } }