Search in sources :

Example 1 with RecipientsProvider

use of org.apache.syncope.core.provisioning.api.notification.RecipientsProvider in project syncope by apache.

the class NotificationManagerImpl method getNotificationTask.

/**
 * Create a notification task.
 *
 * @param notification notification to take as model
 * @param any the any object this task is about
 * @param jexlVars JEXL variables
 * @return notification task, fully populated
 */
private NotificationTask getNotificationTask(final Notification notification, final Any<?> any, final Map<String, Object> jexlVars) {
    if (any != null) {
        virAttrHander.getValues(any);
    }
    List<User> recipients = new ArrayList<>();
    if (notification.getRecipientsFIQL() != null) {
        recipients.addAll(searchDAO.<User>search(SearchCondConverter.convert(notification.getRecipientsFIQL()), Collections.<OrderByClause>emptyList(), AnyTypeKind.USER));
    }
    if (notification.isSelfAsRecipient() && any instanceof User) {
        recipients.add((User) any);
    }
    Set<String> recipientEmails = new HashSet<>();
    List<UserTO> recipientTOs = new ArrayList<>(recipients.size());
    recipients.forEach(recipient -> {
        virAttrHander.getValues(recipient);
        String email = getRecipientEmail(notification.getRecipientAttrName(), recipient);
        if (email == null) {
            LOG.warn("{} cannot be notified: {} not found", recipient, notification.getRecipientAttrName());
        } else {
            recipientEmails.add(email);
            recipientTOs.add(userDataBinder.getUserTO(recipient, true));
        }
    });
    if (notification.getStaticRecipients() != null) {
        recipientEmails.addAll(notification.getStaticRecipients());
    }
    if (notification.getRecipientsProvider() != null) {
        try {
            RecipientsProvider recipientsProvider = ImplementationManager.build(notification.getRecipientsProvider());
            recipientEmails.addAll(recipientsProvider.provideRecipients(notification));
        } catch (Exception e) {
            LOG.error("While building {}", notification.getRecipientsProvider(), e);
        }
    }
    jexlVars.put("recipients", recipientTOs);
    jexlVars.put("syncopeConf", this.findAllSyncopeConfs());
    jexlVars.put("events", notification.getEvents());
    NotificationTask task = entityFactory.newEntity(NotificationTask.class);
    task.setNotification(notification);
    if (any != null) {
        task.setEntityKey(any.getKey());
        task.setAnyTypeKind(any.getType().getKind());
    }
    task.setTraceLevel(notification.getTraceLevel());
    task.getRecipients().addAll(recipientEmails);
    task.setSender(notification.getSender());
    task.setSubject(notification.getSubject());
    if (StringUtils.isNotBlank(notification.getTemplate().getTextTemplate())) {
        task.setTextBody(evaluate(notification.getTemplate().getTextTemplate(), jexlVars));
    }
    if (StringUtils.isNotBlank(notification.getTemplate().getHTMLTemplate())) {
        task.setHtmlBody(evaluate(notification.getTemplate().getHTMLTemplate(), jexlVars));
    }
    return task;
}
Also used : RecipientsProvider(org.apache.syncope.core.provisioning.api.notification.RecipientsProvider) NotificationTask(org.apache.syncope.core.persistence.api.entity.task.NotificationTask) User(org.apache.syncope.core.persistence.api.entity.user.User) OrderByClause(org.apache.syncope.core.persistence.api.dao.search.OrderByClause) UserTO(org.apache.syncope.common.lib.to.UserTO) ArrayList(java.util.ArrayList) ParseException(java.text.ParseException) HashSet(java.util.HashSet)

Aggregations

ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 UserTO (org.apache.syncope.common.lib.to.UserTO)1 OrderByClause (org.apache.syncope.core.persistence.api.dao.search.OrderByClause)1 NotificationTask (org.apache.syncope.core.persistence.api.entity.task.NotificationTask)1 User (org.apache.syncope.core.persistence.api.entity.user.User)1 RecipientsProvider (org.apache.syncope.core.provisioning.api.notification.RecipientsProvider)1