1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kcb.deliverer.impl;
17
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.LinkedHashMap;
21 import java.util.Set;
22
23 import org.apache.commons.validator.EmailValidator;
24 import org.apache.log4j.Logger;
25 import org.kuali.rice.kcb.bo.MessageDelivery;
26 import org.kuali.rice.kcb.deliverer.MessageDeliverer;
27 import org.kuali.rice.kcb.exception.ErrorList;
28 import org.kuali.rice.kcb.api.exception.MessageDeliveryException;
29 import org.kuali.rice.kcb.api.exception.MessageDismissalException;
30 import org.kuali.rice.kcb.service.EmailService;
31 import org.kuali.rice.kcb.service.GlobalKCBServiceLocator;
32 import org.kuali.rice.kcb.service.RecipientPreferenceService;
33
34
35
36
37
38
39 public class EmailMessageDeliverer implements MessageDeliverer {
40 private static Logger LOG = Logger.getLogger(EmailMessageDeliverer.class);
41
42 private EmailService emailService;
43 private RecipientPreferenceService recipientPreferenceService;
44
45 public static final String NAME = "Email";
46 public static final String EMAIL_ADDR_PREF_KEY = "email_address";
47 public static final String EMAIL_DELIV_FRMT_PREF_KEY = "email_delivery_format";
48
49
50
51
52 public EmailMessageDeliverer() {
53 this.emailService = GlobalKCBServiceLocator.getInstance().getEmailService();
54 this.recipientPreferenceService = GlobalKCBServiceLocator.getInstance().getRecipientPreferenceService();
55 }
56
57
58
59
60
61 public void deliver(MessageDelivery messageDelivery) throws MessageDeliveryException {
62 try {
63
64 String recipientEmailAddressPrefKey = getName()+"."+EMAIL_ADDR_PREF_KEY;
65 String recipientEmailFormatPrefKey = getName()+"."+EMAIL_DELIV_FRMT_PREF_KEY;
66
67 String recipientEmailAddress = recipientPreferenceService.getRecipientPreference(messageDelivery.getMessage().getRecipient(), recipientEmailAddressPrefKey).getValue();
68 String recipientEmailFormat = recipientPreferenceService.getRecipientPreference(messageDelivery.getMessage().getRecipient(), recipientEmailFormatPrefKey).getValue();
69
70
71
72
73 Long emailMessageId = emailService.sendEmail(messageDelivery, recipientEmailAddress, recipientEmailFormat);
74
75 String deliverySystemId = null;
76 if (emailMessageId != null) {
77 deliverySystemId = emailMessageId.toString();
78 }
79 messageDelivery.setDelivererSystemId(deliverySystemId);
80 } catch (Exception we) {
81 LOG.error("Error delivering email notification", we);
82 throw new MessageDeliveryException("Error delivering email notification", we);
83 }
84 }
85
86
87
88
89
90
91 public void autoRemoveMessageDelivery(MessageDelivery messageDelivery)
92
93 }
94
95
96
97
98 public void dismiss(MessageDelivery messageDelivery, String user, String cause) throws MessageDismissalException {
99
100 }
101
102
103
104
105 public String getDescription() {
106 return "Enter an Email Address and Email Delivery Format below and select the channels for which you would like email delivery " +
107 "notifications. Select \"None\" in the channel list to remove a delivery type for all channels. " +
108 "Only one Email Address and Email Delivery Format may be specified. Any data entered and " +
109 "saved will override prior Delivery Type selections.";
110 }
111
112
113
114
115 public String getName() {
116 return NAME;
117 }
118
119
120
121
122 public String getTitle() {
123 return "Email Message Delivery";
124 }
125
126
127
128
129
130 public LinkedHashMap<String, String> getPreferenceKeys() {
131 LinkedHashMap<String, String> prefKeys = new LinkedHashMap<String, String>();
132 prefKeys.put(EMAIL_ADDR_PREF_KEY, "Email Address (\"abc@def.edu\")");
133 prefKeys.put(EMAIL_DELIV_FRMT_PREF_KEY, "Email Delivery Format (text or html)");
134 return prefKeys;
135 }
136
137
138
139
140 public void validatePreferenceValues(HashMap<String, String> prefs) throws ErrorList {
141 boolean error = false;
142 ErrorList errorList = new ErrorList();
143 String[] validformats = {"text","html"};
144
145 if (!prefs.containsKey(getName()+"."+EMAIL_ADDR_PREF_KEY)) {
146 errorList.addError("Email Address is a required field.");
147 error = true;
148 } else {
149 String addressValue = (String) prefs.get(getName()+"."+EMAIL_ADDR_PREF_KEY);
150 EmailValidator validator = EmailValidator.getInstance();
151 if (!validator.isValid(addressValue)) {
152 errorList.addError("Email Address is required and must be properly formatted - \"abc@def.edu\".");
153 error = true;
154 }
155 }
156
157
158 if (!prefs.containsKey(getName()+"."+EMAIL_DELIV_FRMT_PREF_KEY)) {
159 errorList.addError("Email Delivery Format is required.");
160 error = true;
161 } else {
162 String formatValue = (String) prefs.get(getName()+"."+EMAIL_DELIV_FRMT_PREF_KEY);
163 Set<String> formats = new HashSet<String>();
164 for (int i=0; i < validformats.length ; i++) {
165 formats.add(validformats[i]);
166 }
167
168 if (!formats.contains(formatValue)) {
169 errorList.addError("Email Delivery Format is required and must be entered as \"text\" or \"html\".");
170 error = true;
171 }
172 }
173
174 if (error) throw errorList;
175 }
176 }