Search in sources :

Example 1 with ManagementUINotifierConfiguration

use of io.gravitee.am.management.service.impl.notifications.ManagementUINotifierConfiguration in project gravitee-access-management by gravitee-io.

the class UserNotificationServiceImpl method send.

@Override
public CompletableFuture<Void> send(Notification notification, Map<String, Object> param) {
    var future = new CompletableFuture<Void>();
    final UserProperties audience = (UserProperties) param.get(NotificationDefinitionUtils.NOTIFIER_DATA_USER);
    final DomainProperties domain = (DomainProperties) param.get(NotificationDefinitionUtils.NOTIFIER_DATA_DOMAIN);
    if (audience == null || domain == null) {
        logger.warn("Receive notification to store in database without user or domain, ignore it.");
        future.complete(null);
    } else {
        try {
            ManagementUINotifierConfiguration notifierConfiguration = mapper.readValue(notification.getConfiguration(), ManagementUINotifierConfiguration.class);
            String content = uiTemplateProvider.getNotificationContent(notifierConfiguration.getTemplate(), param);
            final Date now = new Date();
            final UserNotification userNotif = new UserNotification();
            userNotif.setMessage(content);
            userNotif.setCreatedAt(now);
            userNotif.setUpdatedAt(now);
            userNotif.setStatus(UserNotificationStatus.UNREAD);
            userNotif.setReferenceId(domain.getId());
            userNotif.setReferenceType(ReferenceType.DOMAIN);
            userNotif.setAudienceId(audience.getId());
            logger.debug("Receive notification to store in database for user '{}'", audience.getId());
            notificationRepository.create(userNotif).observeOn(Schedulers.io()).subscribe(createdNotif -> {
                logger.debug("Notification stored: {}", createdNotif);
                // CompletableStage use the Void type. So it requires null to be mapped properly in the NotificationTrigger
                future.complete(null);
            }, future::completeExceptionally);
        } catch (Exception e) {
            logger.warn("Unable to deserialize ManagementUI Notifier configuration : {}", e.getMessage());
            future.completeExceptionally(e);
        }
    }
    return future;
}
Also used : DomainProperties(io.gravitee.am.model.safe.DomainProperties) ManagementUINotifierConfiguration(io.gravitee.am.management.service.impl.notifications.ManagementUINotifierConfiguration) CompletableFuture(java.util.concurrent.CompletableFuture) UserProperties(io.gravitee.am.model.safe.UserProperties) UserNotification(io.gravitee.am.model.notification.UserNotification) Date(java.util.Date)

Aggregations

ManagementUINotifierConfiguration (io.gravitee.am.management.service.impl.notifications.ManagementUINotifierConfiguration)1 UserNotification (io.gravitee.am.model.notification.UserNotification)1 DomainProperties (io.gravitee.am.model.safe.DomainProperties)1 UserProperties (io.gravitee.am.model.safe.UserProperties)1 Date (java.util.Date)1 CompletableFuture (java.util.concurrent.CompletableFuture)1