| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kew.mail.service.impl; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.StringUtils; |
| 19 | |
import org.kuali.rice.kew.actionitem.ActionItem; |
| 20 | |
import org.kuali.rice.kew.actionrequest.ActionRequestValue; |
| 21 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
| 22 | |
import org.kuali.rice.kew.dto.ActionRequestDTO; |
| 23 | |
import org.kuali.rice.kew.dto.DTOConverter; |
| 24 | |
import org.kuali.rice.kew.dto.RouteHeaderDTO; |
| 25 | |
import org.kuali.rice.kew.mail.CustomEmailAttribute; |
| 26 | |
import org.kuali.rice.kew.mail.EmailBody; |
| 27 | |
import org.kuali.rice.kew.mail.EmailSubject; |
| 28 | |
import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue; |
| 29 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 30 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 31 | |
import org.kuali.rice.kew.util.Utilities; |
| 32 | |
import org.kuali.rice.kim.bo.Person; |
| 33 | |
import org.kuali.rice.kns.util.KNSConstants; |
| 34 | |
|
| 35 | |
import java.util.Collection; |
| 36 | |
import java.util.HashMap; |
| 37 | |
import java.util.Iterator; |
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | 0 | public class HardCodedActionListEmailServiceImpl extends ActionListEmailServiceImpl { |
| 46 | 0 | private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger |
| 47 | |
.getLogger(HardCodedActionListEmailServiceImpl.class); |
| 48 | |
|
| 49 | |
private static final String DEFAULT_EMAIL_FROM_ADDRESS = "admin@localhost"; |
| 50 | |
|
| 51 | |
private static final String ACTION_LIST_REMINDER = "Action List Reminder"; |
| 52 | |
|
| 53 | |
private String deploymentEnvironment; |
| 54 | |
|
| 55 | |
public String getDocumentTypeEmailAddress(DocumentType documentType) { |
| 56 | 0 | String fromAddress = (documentType == null ? null : documentType |
| 57 | |
.getNotificationFromAddress()); |
| 58 | 0 | if (Utilities.isEmpty(fromAddress)) { |
| 59 | 0 | fromAddress = getApplicationEmailAddress(); |
| 60 | |
} |
| 61 | 0 | return fromAddress; |
| 62 | |
} |
| 63 | |
|
| 64 | |
public String getApplicationEmailAddress() { |
| 65 | |
|
| 66 | 0 | String fromAddress = Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.MAILER_DETAIL_TYPE, KEWConstants.EMAIL_REMINDER_FROM_ADDRESS); |
| 67 | |
|
| 68 | 0 | if (Utilities.isEmpty(fromAddress)) { |
| 69 | 0 | fromAddress = DEFAULT_EMAIL_FROM_ADDRESS; |
| 70 | |
} |
| 71 | 0 | return fromAddress; |
| 72 | |
} |
| 73 | |
|
| 74 | |
public EmailSubject getEmailSubject() { |
| 75 | 0 | return new EmailSubject(ACTION_LIST_REMINDER); |
| 76 | |
} |
| 77 | |
|
| 78 | |
public EmailSubject getEmailSubject(String customSubject) { |
| 79 | 0 | return new EmailSubject(ACTION_LIST_REMINDER + " " + customSubject); |
| 80 | |
} |
| 81 | |
|
| 82 | |
public void sendImmediateReminder(Person user, ActionItem actionItem) { |
| 83 | 0 | if (sendActionListEmailNotification()) { |
| 84 | 0 | DocumentRouteHeaderValue document = KEWServiceLocator |
| 85 | |
.getRouteHeaderService().getRouteHeader( |
| 86 | |
actionItem.getRouteHeaderId()); |
| 87 | 0 | StringBuffer emailBody = new StringBuffer( |
| 88 | |
buildImmediateReminderBody(user, actionItem, document |
| 89 | |
.getDocumentType())); |
| 90 | 0 | StringBuffer emailSubject = new StringBuffer(); |
| 91 | |
try { |
| 92 | 0 | CustomEmailAttribute customEmailAttribute = actionItem |
| 93 | |
.getRouteHeader().getCustomEmailAttribute(); |
| 94 | 0 | if (customEmailAttribute != null) { |
| 95 | 0 | RouteHeaderDTO routeHeaderVO = DTOConverter |
| 96 | |
.convertRouteHeader(actionItem.getRouteHeader(), |
| 97 | |
user.getPrincipalId()); |
| 98 | 0 | ActionRequestValue actionRequest = KEWServiceLocator |
| 99 | |
.getActionRequestService().findByActionRequestId( |
| 100 | |
actionItem.getActionRequestId()); |
| 101 | 0 | ActionRequestDTO actionRequestVO = DTOConverter |
| 102 | |
.convertActionRequest(actionRequest); |
| 103 | 0 | customEmailAttribute.setRouteHeaderVO(routeHeaderVO); |
| 104 | 0 | customEmailAttribute.setActionRequestVO(actionRequestVO); |
| 105 | 0 | String customBody = customEmailAttribute |
| 106 | |
.getCustomEmailBody(); |
| 107 | 0 | if (!Utilities.isEmpty(customBody)) { |
| 108 | 0 | emailBody.append(customBody); |
| 109 | |
} |
| 110 | 0 | String customEmailSubject = customEmailAttribute |
| 111 | |
.getCustomEmailSubject(); |
| 112 | 0 | if (!Utilities.isEmpty(customEmailSubject)) { |
| 113 | 0 | emailSubject.append(customEmailSubject); |
| 114 | |
} |
| 115 | |
} |
| 116 | 0 | } catch (Exception e) { |
| 117 | 0 | LOG |
| 118 | |
.error( |
| 119 | |
"Error when checking for custom email body and subject.", |
| 120 | |
e); |
| 121 | 0 | } |
| 122 | 0 | sendEmail(user, getEmailSubject(emailSubject.toString()), |
| 123 | |
new EmailBody(emailBody.toString()), document |
| 124 | |
.getDocumentType()); |
| 125 | |
} |
| 126 | |
|
| 127 | 0 | } |
| 128 | |
|
| 129 | |
public void sendDailyReminder() { |
| 130 | 0 | if (sendActionListEmailNotification()) { |
| 131 | 0 | Collection<Person> users = getUsersWithEmailSetting(KEWConstants.EMAIL_RMNDR_DAY_VAL); |
| 132 | 0 | for (Iterator<Person> userIter = users.iterator(); userIter.hasNext();) { |
| 133 | 0 | Person user = userIter.next(); |
| 134 | |
try { |
| 135 | 0 | Collection actionItems = getActionListService() |
| 136 | |
.getActionList(user.getPrincipalId(), null); |
| 137 | 0 | if (actionItems != null && actionItems.size() > 0) { |
| 138 | 0 | sendReminder(user, actionItems, |
| 139 | |
KEWConstants.EMAIL_RMNDR_DAY_VAL); |
| 140 | |
} |
| 141 | 0 | } catch (Exception e) { |
| 142 | 0 | LOG.error( |
| 143 | |
"Error sending daily action list reminder to user: " |
| 144 | |
+ user.getEmailAddressUnmasked(), e); |
| 145 | 0 | } |
| 146 | 0 | } |
| 147 | |
} |
| 148 | 0 | LOG.debug("Daily action list emails sent successful"); |
| 149 | 0 | } |
| 150 | |
|
| 151 | |
public void sendWeeklyReminder() { |
| 152 | 0 | if (sendActionListEmailNotification()) { |
| 153 | 0 | Collection<Person> users = getUsersWithEmailSetting(KEWConstants.EMAIL_RMNDR_WEEK_VAL); |
| 154 | 0 | for (Iterator<Person> userIter = users.iterator(); userIter.hasNext();) { |
| 155 | 0 | Person user = userIter.next(); |
| 156 | |
try { |
| 157 | 0 | Collection actionItems = getActionListService() |
| 158 | |
.getActionList(user.getPrincipalId(), null); |
| 159 | 0 | if (actionItems != null && actionItems.size() > 0) { |
| 160 | 0 | sendReminder(user, actionItems, |
| 161 | |
KEWConstants.EMAIL_RMNDR_WEEK_VAL); |
| 162 | |
} |
| 163 | 0 | } catch (Exception e) { |
| 164 | 0 | LOG.error( |
| 165 | |
"Error sending weekly action list reminder to user: " |
| 166 | |
+ user.getEmailAddressUnmasked(), e); |
| 167 | 0 | } |
| 168 | 0 | } |
| 169 | |
} |
| 170 | 0 | LOG.debug("Weekly action list emails sent successful"); |
| 171 | 0 | } |
| 172 | |
|
| 173 | |
private void sendReminder(Person user, Collection actionItems, |
| 174 | |
String emailSetting) { |
| 175 | 0 | String emailBody = null; |
| 176 | 0 | if (KEWConstants.EMAIL_RMNDR_DAY_VAL.equals(emailSetting)) { |
| 177 | 0 | emailBody = buildDailyReminderBody(user, actionItems); |
| 178 | 0 | } else if (KEWConstants.EMAIL_RMNDR_WEEK_VAL.equals(emailSetting)) { |
| 179 | 0 | emailBody = buildWeeklyReminderBody(user, actionItems); |
| 180 | |
} |
| 181 | 0 | sendEmail(user, getEmailSubject(), new EmailBody(emailBody)); |
| 182 | 0 | } |
| 183 | |
|
| 184 | |
public String buildImmediateReminderBody(Person person, |
| 185 | |
ActionItem actionItem, DocumentType documentType) { |
| 186 | 0 | String docHandlerUrl = actionItem.getRouteHeader().getDocumentType() |
| 187 | |
.getDocHandlerUrl(); |
| 188 | 0 | if (StringUtils.isNotBlank(docHandlerUrl)) { |
| 189 | 0 | if (!docHandlerUrl.contains("?")) { |
| 190 | 0 | docHandlerUrl += "?"; |
| 191 | |
} else { |
| 192 | 0 | docHandlerUrl += "&"; |
| 193 | |
} |
| 194 | 0 | docHandlerUrl += KEWConstants.ROUTEHEADER_ID_PARAMETER + "=" |
| 195 | |
+ actionItem.getRouteHeaderId(); |
| 196 | 0 | docHandlerUrl += "&" + KEWConstants.COMMAND_PARAMETER + "=" |
| 197 | |
+ KEWConstants.ACTIONLIST_COMMAND; |
| 198 | |
} |
| 199 | 0 | StringBuffer sf = new StringBuffer(); |
| 200 | |
|
| 201 | 0 | sf.append("Your Action List has an eDoc(electronic document) that needs your attention: \n\n"); |
| 202 | 0 | sf.append("Document ID:\t" + actionItem.getRouteHeaderId() + "\n"); |
| 203 | 0 | sf.append("Initiator:\t\t"); |
| 204 | |
try { |
| 205 | 0 | String name = (person == null ? "" : person.getName()); |
| 206 | 0 | sf.append(name + "\n"); |
| 207 | 0 | } catch (Exception e) { |
| 208 | 0 | LOG.error("Error retrieving initiator for action item " |
| 209 | |
+ actionItem.getRouteHeaderId()); |
| 210 | 0 | sf.append("\n"); |
| 211 | 0 | } |
| 212 | 0 | sf.append("Type:\t\t" + "Add/Modify " |
| 213 | |
+ actionItem.getRouteHeader().getDocumentType().getName() |
| 214 | |
+ "\n"); |
| 215 | 0 | sf.append("Title:\t\t" + actionItem.getDocTitle() + "\n"); |
| 216 | 0 | sf.append("\n\n"); |
| 217 | 0 | if (StringUtils.isNotBlank(docHandlerUrl)) { |
| 218 | 0 | sf.append("To respond to this eDoc: \n"); |
| 219 | 0 | sf.append("\tGo to " + docHandlerUrl + "\n\n"); |
| 220 | 0 | sf.append("\tOr you may access the eDoc from your Action List: \n"); |
| 221 | 0 | sf.append("\tGo to " + getActionListUrl() + ", and then click on the numeric Document ID: " + actionItem.getRouteHeaderId() + " in the first column of the List. \n"); |
| 222 | |
} |
| 223 | |
else { |
| 224 | 0 | sf.append("To respond to this eDoc go to your Action List: \n"); |
| 225 | 0 | sf.append("\tGo to " + getActionListUrl() + ", and then take actions related to Document ID: " + actionItem.getRouteHeaderId() + ". \n"); |
| 226 | |
} |
| 227 | |
|
| 228 | 0 | sf.append("\n\n\n"); |
| 229 | 0 | sf.append("To change how these email notifications are sent(daily, weekly or none): \n"); |
| 230 | 0 | sf.append("\tGo to " + getPreferencesUrl() + "\n"); |
| 231 | 0 | sf.append("\n\n\n"); |
| 232 | 0 | sf.append(getHelpLink(documentType) + "\n\n\n"); |
| 233 | |
|
| 234 | |
|
| 235 | 0 | if (!isProduction()) { |
| 236 | |
try { |
| 237 | 0 | sf.append("Action Item sent to " + actionItem.getPerson().getPrincipalName()); |
| 238 | 0 | if (actionItem.getDelegationType() != null) { |
| 239 | 0 | sf.append(" for delegation type " |
| 240 | |
+ actionItem.getDelegationType()); |
| 241 | |
} |
| 242 | 0 | } catch (Exception e) { |
| 243 | 0 | throw new RuntimeException(e); |
| 244 | 0 | } |
| 245 | |
} |
| 246 | |
|
| 247 | 0 | return sf.toString(); |
| 248 | |
} |
| 249 | |
|
| 250 | |
public String buildDailyReminderBody(Person person, |
| 251 | |
Collection actionItems) { |
| 252 | 0 | StringBuffer sf = new StringBuffer(); |
| 253 | 0 | sf.append(getDailyWeeklyMessageBody(actionItems)); |
| 254 | 0 | sf |
| 255 | |
.append("To change how these email notifications are sent (immediately, weekly or none): \n"); |
| 256 | 0 | sf.append("\tGo to " + getPreferencesUrl() + "\n"); |
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | 0 | sf.append("\n\n\n"); |
| 262 | 0 | sf.append(getHelpLink() + "\n\n\n"); |
| 263 | 0 | return sf.toString(); |
| 264 | |
} |
| 265 | |
|
| 266 | |
public String buildWeeklyReminderBody(Person person, |
| 267 | |
Collection actionItems) { |
| 268 | 0 | StringBuffer sf = new StringBuffer(); |
| 269 | 0 | sf.append(getDailyWeeklyMessageBody(actionItems)); |
| 270 | 0 | sf |
| 271 | |
.append("To change how these email notifications are sent (immediately, daily or none): \n"); |
| 272 | 0 | sf.append("\tGo to " + getPreferencesUrl() + "\n"); |
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | 0 | sf.append("\n\n\n"); |
| 278 | 0 | sf.append(getHelpLink() + "\n\n\n"); |
| 279 | 0 | return sf.toString(); |
| 280 | |
} |
| 281 | |
|
| 282 | |
protected String getDailyWeeklyMessageBody(Collection actionItems) { |
| 283 | 0 | StringBuffer sf = new StringBuffer(); |
| 284 | 0 | HashMap docTypes = getActionListItemsStat(actionItems); |
| 285 | |
|
| 286 | 0 | sf |
| 287 | |
.append("Your Action List has " |
| 288 | |
+ actionItems.size() |
| 289 | |
+ " eDocs(electronic documents) that need your attention: \n\n"); |
| 290 | 0 | Iterator iter = docTypes.keySet().iterator(); |
| 291 | 0 | while (iter.hasNext()) { |
| 292 | 0 | String docTypeName = (String) iter.next(); |
| 293 | 0 | sf.append("\t" + ((Integer) docTypes.get(docTypeName)).toString() |
| 294 | |
+ "\t" + docTypeName + "\n"); |
| 295 | 0 | } |
| 296 | 0 | sf.append("\n\n"); |
| 297 | 0 | sf.append("To respond to each of these eDocs: \n"); |
| 298 | 0 | sf |
| 299 | |
.append("\tGo to " |
| 300 | |
+ getActionListUrl() |
| 301 | |
+ ", and then click on its numeric Document ID in the first column of the List.\n"); |
| 302 | 0 | sf.append("\n\n\n"); |
| 303 | 0 | return sf.toString(); |
| 304 | |
} |
| 305 | |
|
| 306 | |
private HashMap getActionListItemsStat(Collection actionItems) { |
| 307 | 0 | HashMap docTypes = new HashMap(); |
| 308 | 0 | Iterator iter = actionItems.iterator(); |
| 309 | |
|
| 310 | 0 | while (iter.hasNext()) { |
| 311 | 0 | String docTypeName = ((ActionItem) iter.next()).getRouteHeader() |
| 312 | |
.getDocumentType().getName(); |
| 313 | 0 | if (docTypes.containsKey(docTypeName)) { |
| 314 | 0 | docTypes.put(docTypeName, new Integer(((Integer) docTypes |
| 315 | |
.get(docTypeName)).intValue() + 1)); |
| 316 | |
} else { |
| 317 | 0 | docTypes.put(docTypeName, new Integer(1)); |
| 318 | |
} |
| 319 | 0 | } |
| 320 | 0 | return docTypes; |
| 321 | |
} |
| 322 | |
|
| 323 | |
public String getDeploymentEnvironment() { |
| 324 | 0 | return deploymentEnvironment; |
| 325 | |
} |
| 326 | |
|
| 327 | |
public void setDeploymentEnvironment(String deploymentEnvironment) { |
| 328 | 0 | this.deploymentEnvironment = deploymentEnvironment; |
| 329 | 0 | } |
| 330 | |
|
| 331 | |
} |