1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.kpme.core.service.notification;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.log4j.Logger;
20 import org.kuali.rice.core.api.config.property.ConfigContext;
21 import org.kuali.rice.core.api.mail.EmailBody;
22 import org.kuali.rice.core.api.mail.EmailFrom;
23 import org.kuali.rice.core.api.mail.EmailSubject;
24 import org.kuali.rice.core.api.mail.EmailTo;
25 import org.kuali.rice.core.api.mail.Mailer;
26 import org.kuali.rice.coreservice.framework.parameter.ParameterService;
27 import org.kuali.rice.kew.api.KewApiConstants;
28 import org.kuali.rice.kim.api.KimConstants;
29 import org.kuali.rice.kim.api.identity.Person;
30 import org.kuali.rice.kim.api.identity.PersonService;
31 import org.kuali.rice.kim.api.identity.entity.EntityDefault;
32 import org.kuali.rice.kim.api.identity.type.EntityTypeContactInfoDefault;
33 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
34 import org.kuali.rice.krad.util.KRADConstants;
35
36 public class KPMENotificationServiceImpl implements KPMENotificationService {
37
38 private static final Logger LOG = Logger.getLogger(KPMENotificationServiceImpl.class);
39
40 private static final String KPME_NOTIFICATIONS_ENABLED = "kpme.notifications.enabled";
41 private static final String DEFAULT_EMAIL_FROM_ADDRESS = "admin@localhost";
42
43 private Mailer mailer;
44 private ParameterService parameterService;
45 private PersonService personService;
46
47 @Override
48 public void sendNotification(String subject, String message, String... principalIds) {
49 if (sendEmailNotification()) {
50 for (String principalId : principalIds) {
51 EntityDefault entityDefault = KimApiServiceLocator.getIdentityService().getEntityDefaultByPrincipalId(principalId);
52 if (entityDefault != null) {
53 EntityTypeContactInfoDefault contact = entityDefault.getEntityType(KimConstants.EntityTypes.PERSON);
54 if (contact != null
55 && contact.getDefaultEmailAddress() != null
56 && contact.getDefaultEmailAddress().getEmailAddressUnmasked() != null) {
57 EmailFrom emailFrom = new EmailFrom(getApplicationEmailAddress());
58 EmailTo emailTo = new EmailTo(contact.getDefaultEmailAddress().getEmailAddressUnmasked());
59 EmailSubject emailSubject = new EmailSubject(subject);
60 EmailBody emailBody = new EmailBody(message);
61
62 try {
63 getMailer().sendEmail(emailFrom, emailTo, emailSubject, emailBody, false);
64 } catch (RuntimeException re) {
65 LOG.error("Email message sending failure:", re);
66 }
67 } else {
68 LOG.warn("Could not send message to principalId " + principalId + " because the person entity does not have an email address");
69 }
70 } else {
71 LOG.warn("Could not send message to principalId " + principalId + " because the person entity does not exist");
72 }
73 }
74 } else {
75 LOG.info("Not sending message due to config parameter " + KPME_NOTIFICATIONS_ENABLED + " indicating emails should not be sent");
76 }
77 }
78
79 private boolean sendEmailNotification() {
80 return ConfigContext.getCurrentContextConfig().getBooleanProperty(KPME_NOTIFICATIONS_ENABLED, false);
81 }
82
83 private String getApplicationEmailAddress() {
84 String fromAddress = getParameterService().getParameterValueAsString(KewApiConstants.KEW_NAMESPACE, KRADConstants.DetailTypes.MAILER_DETAIL_TYPE,
85 KewApiConstants.EMAIL_REMINDER_FROM_ADDRESS);
86 if (StringUtils.isEmpty(fromAddress)) {
87 fromAddress = DEFAULT_EMAIL_FROM_ADDRESS;
88 }
89 return fromAddress;
90 }
91
92 public Mailer getMailer() {
93 return mailer;
94 }
95
96 public void setMailer(Mailer mailer) {
97 this.mailer = mailer;
98 }
99
100 public ParameterService getParameterService() {
101 return parameterService;
102 }
103
104 public void setParameterService(ParameterService parameterService) {
105 this.parameterService = parameterService;
106 }
107
108 }