001package com.identityworksllc.iiq.common;
002
003import org.apache.commons.logging.Log;
004import org.apache.commons.logging.LogFactory;
005import sailpoint.api.ObjectUtil;
006import sailpoint.api.SailPointContext;
007import sailpoint.object.EmailOptions;
008import sailpoint.object.EmailTemplate;
009import sailpoint.object.Identity;
010import sailpoint.tools.GeneralException;
011import sailpoint.tools.Util;
012
013import java.util.ArrayList;
014import java.util.Arrays;
015import java.util.List;
016import java.util.Map;
017
018/**
019 * This class implements a set of utilities for notifications. These can be somewhat
020 * unwieldy in Sailpoint for straightforward operations like "send an email template
021 * with these arguments to one person".
022 */
023@SuppressWarnings("unused")
024public class Notifications {
025
026    private static final Log log = LogFactory.getLog(Notifications.class);
027
028    /**
029     * Sends an email notification to the given users with the given arguments
030     * @param context A sailpoint context
031     * @param singleRecipient The recipient, which can be a user or a workgroup or an email address
032     * @param emailTemplate The email template to send
033     * @param parameters The parameters to use for the template
034     * @throws GeneralException if a send failure occurs
035     */
036    public static void notify(SailPointContext context, String singleRecipient, String emailTemplate, Map<String, Object> parameters) throws GeneralException {
037        notify(context, singleRecipient, null, emailTemplate, parameters);
038    }
039
040    /**
041     * Sends an email notification to the given users with the given arguments
042     * @param context A sailpoint context
043     * @param singleRecipient The recipient, which can be a user or a workgroup or an email address
044     * @param cc Optional user or email to CC the email to
045     * @param emailTemplate The email template to send
046     * @param parameters The parameters to use for the template
047     * @throws GeneralException if a send failure occurs
048     */
049    public static void notify(SailPointContext context, String singleRecipient, String cc, String emailTemplate, Map<String, Object> parameters) throws GeneralException {
050        notify(context, Arrays.asList(singleRecipient), cc, emailTemplate, parameters);
051    }
052
053    /**
054     * Sends an email notification to the given users with the given arguments
055     * @param context A sailpoint context
056     * @param recipients The recipients, which can be a user or a workgroup
057     * @param cc Optional user or email to CC the email to
058     * @param emailTemplate The email template to send
059     * @param parameters The parameters to use for the template
060     * @throws GeneralException if a send failure occurs
061     */
062    public static void notify(SailPointContext context, List<String> recipients, String cc, String emailTemplate, Map<String, Object> parameters) throws GeneralException {
063        List<String> toAddresses = new ArrayList<>();
064        for(String recipient : Util.safeIterable(recipients)) {
065            Identity recipientObject = context.getObjectByName(Identity.class, recipient);
066            if (recipientObject != null) {
067                List<String> effectiveEmails = ObjectUtil.getEffectiveEmails(context, recipientObject);
068                if (effectiveEmails != null) {
069                    toAddresses.addAll(ObjectUtil.getEffectiveEmails(context, recipientObject));
070                }
071            } else {
072                if (recipient.contains("@")) {
073                    toAddresses.addAll(Arrays.asList(recipient.split("\\s*,\\s*")));
074                }
075            }
076        }
077        if (toAddresses.isEmpty()) {
078            return;
079        }
080        String ccAddress = null;
081        if (cc != null) {
082            Identity recipientObject = context.getObjectByName(Identity.class, cc);
083            if (recipientObject != null) {
084                ccAddress = recipientObject.getEmail();
085            } else {
086                if (cc.contains("@")) {
087                    ccAddress = cc;
088                }
089            }
090        }
091        EmailTemplate template = context.getObjectByName(EmailTemplate.class, emailTemplate);
092        if (template == null) {
093            throw new GeneralException("Unable to locate an email template called [" + emailTemplate + "]");
094        }
095
096        EmailOptions options = new EmailOptions();
097        options.setTo(toAddresses);
098        if (ccAddress != null) {
099            options.setCc(ccAddress);
100        }
101        options.addVariables(parameters);
102        options.setNoRetry(false);
103        options.setSendImmediate(false);
104        context.sendEmailNotification(template, options);
105    }
106
107}