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.exception.MessageDeliveryException; |
29 | |
import org.kuali.rice.kcb.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 | 0 | 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 | 0 | public EmailMessageDeliverer() { |
53 | 0 | this.emailService = GlobalKCBServiceLocator.getInstance().getEmailService(); |
54 | 0 | this.recipientPreferenceService = GlobalKCBServiceLocator.getInstance().getRecipientPreferenceService(); |
55 | 0 | } |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
public void deliver(MessageDelivery messageDelivery) throws MessageDeliveryException { |
62 | |
try { |
63 | |
|
64 | 0 | String recipientEmailAddressPrefKey = getName()+"."+EMAIL_ADDR_PREF_KEY; |
65 | 0 | String recipientEmailFormatPrefKey = getName()+"."+EMAIL_DELIV_FRMT_PREF_KEY; |
66 | |
|
67 | 0 | String recipientEmailAddress = recipientPreferenceService.getRecipientPreference(messageDelivery.getMessage().getRecipient(), recipientEmailAddressPrefKey).getValue(); |
68 | 0 | String recipientEmailFormat = recipientPreferenceService.getRecipientPreference(messageDelivery.getMessage().getRecipient(), recipientEmailFormatPrefKey).getValue(); |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | 0 | Long emailMessageId = emailService.sendEmail(messageDelivery, recipientEmailAddress, recipientEmailFormat); |
74 | |
|
75 | 0 | String deliverySystemId = null; |
76 | 0 | if (emailMessageId != null) { |
77 | 0 | deliverySystemId = emailMessageId.toString(); |
78 | |
} |
79 | 0 | messageDelivery.setDelivererSystemId(deliverySystemId); |
80 | 0 | } catch (Exception we) { |
81 | 0 | LOG.error("Error delivering email notification", we); |
82 | 0 | throw new MessageDeliveryException("Error delivering email notification", we); |
83 | 0 | } |
84 | 0 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
public void autoRemoveMessageDelivery(MessageDelivery messageDelivery) { |
92 | |
|
93 | 0 | } |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public void dismiss(MessageDelivery messageDelivery, String user, String cause) throws MessageDismissalException { |
99 | |
|
100 | 0 | } |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
public String getDescription() { |
106 | 0 | 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 | 0 | return NAME; |
117 | |
} |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public String getTitle() { |
123 | 0 | return "Email Message Delivery"; |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public LinkedHashMap<String, String> getPreferenceKeys() { |
131 | 0 | LinkedHashMap<String, String> prefKeys = new LinkedHashMap<String, String>(); |
132 | 0 | prefKeys.put(EMAIL_ADDR_PREF_KEY, "Email Address (\"abc@def.edu\")"); |
133 | 0 | prefKeys.put(EMAIL_DELIV_FRMT_PREF_KEY, "Email Delivery Format (text or html)"); |
134 | 0 | return prefKeys; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public void validatePreferenceValues(HashMap<String, String> prefs) throws ErrorList { |
141 | 0 | boolean error = false; |
142 | 0 | ErrorList errorList = new ErrorList(); |
143 | 0 | String[] validformats = {"text","html"}; |
144 | |
|
145 | 0 | if (!prefs.containsKey(getName()+"."+EMAIL_ADDR_PREF_KEY)) { |
146 | 0 | errorList.addError("Email Address is a required field."); |
147 | 0 | error = true; |
148 | |
} else { |
149 | 0 | String addressValue = (String) prefs.get(getName()+"."+EMAIL_ADDR_PREF_KEY); |
150 | 0 | EmailValidator validator = EmailValidator.getInstance(); |
151 | 0 | if (!validator.isValid(addressValue)) { |
152 | 0 | errorList.addError("Email Address is required and must be properly formatted - \"abc@def.edu\"."); |
153 | 0 | error = true; |
154 | |
} |
155 | |
} |
156 | |
|
157 | |
|
158 | 0 | if (!prefs.containsKey(getName()+"."+EMAIL_DELIV_FRMT_PREF_KEY)) { |
159 | 0 | errorList.addError("Email Delivery Format is required."); |
160 | 0 | error = true; |
161 | |
} else { |
162 | 0 | String formatValue = (String) prefs.get(getName()+"."+EMAIL_DELIV_FRMT_PREF_KEY); |
163 | 0 | Set<String> formats = new HashSet<String>(); |
164 | 0 | for (int i=0; i < validformats.length ; i++) { |
165 | 0 | formats.add(validformats[i]); |
166 | |
} |
167 | |
|
168 | 0 | if (!formats.contains(formatValue)) { |
169 | 0 | errorList.addError("Email Delivery Format is required and must be entered as \"text\" or \"html\"."); |
170 | 0 | error = true; |
171 | |
} |
172 | |
} |
173 | |
|
174 | 0 | if (error) throw errorList; |
175 | 0 | } |
176 | |
} |