Coverage Report - org.kuali.rice.kew.mail.service.impl.ActionListEmailServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionListEmailServiceImpl
0%
0/232
0%
0/82
2.531
 
 1  
 /*
 2  
  * Copyright 2005-2007 The Kuali Foundation
 3  
  *
 4  
  *
 5  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 6  
  * you may not use this file except in compliance with the License.
 7  
  * You may obtain a copy of the License at
 8  
  *
 9  
  * http://www.opensource.org/licenses/ecl2.php
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.kuali.rice.kew.mail.service.impl;
 18  
 
 19  
 import org.apache.commons.lang.StringUtils;
 20  
 import org.kuali.rice.core.config.ConfigContext;
 21  
 import org.kuali.rice.kew.actionitem.ActionItem;
 22  
 import org.kuali.rice.kew.actionlist.service.ActionListService;
 23  
 import org.kuali.rice.kew.actionrequest.ActionRequestValue;
 24  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 25  
 import org.kuali.rice.kew.dto.ActionRequestDTO;
 26  
 import org.kuali.rice.kew.dto.DTOConverter;
 27  
 import org.kuali.rice.kew.dto.RouteHeaderDTO;
 28  
 import org.kuali.rice.kew.mail.*;
 29  
 import org.kuali.rice.kew.mail.service.ActionListEmailService;
 30  
 import org.kuali.rice.kew.preferences.Preferences;
 31  
 import org.kuali.rice.kew.routeheader.DocumentRouteHeaderValue;
 32  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 33  
 import org.kuali.rice.kew.useroptions.UserOptions;
 34  
 import org.kuali.rice.kew.useroptions.UserOptionsService;
 35  
 import org.kuali.rice.kew.util.KEWConstants;
 36  
 import org.kuali.rice.kew.util.Utilities;
 37  
 import org.kuali.rice.kim.bo.Person;
 38  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 39  
 import org.kuali.rice.kns.util.KNSConstants;
 40  
 import org.kuali.rice.ksb.service.KSBServiceLocator;
 41  
 import org.quartz.*;
 42  
 
 43  
 import java.text.FieldPosition;
 44  
 import java.text.MessageFormat;
 45  
 import java.util.*;
 46  
 
 47  
 
 48  
 /**
 49  
  * ActionListeEmailService which generates messages whose body and subject can be customized via
 50  
  * KEW configuration parameters, 'immediate.reminder.email.message' and 'immediate.reminder.email.subject'.
 51  
  * The immediate reminder email message key should specify a MessageFormat string.  See code for the parameters
 52  
  * to this MessageFormat.
 53  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 54  
  */
 55  0
 public class ActionListEmailServiceImpl implements ActionListEmailService {
 56  0
         private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger
 57  
                         .getLogger(ActionListEmailServiceImpl.class);
 58  
 
 59  
         private static final String DEFAULT_EMAIL_FROM_ADDRESS = "admin@localhost";
 60  
 
 61  
         private static final String ACTION_LIST_REMINDER = "Action List Reminder";
 62  
 
 63  
     private static final String IMMEDIATE_REMINDER_EMAIL_MESSAGE_KEY = "immediate.reminder.email.message";
 64  
 
 65  
     private static final String IMMEDIATE_REMINDER_EMAIL_SUBJECT_KEY = "immediate.reminder.email.subject";
 66  
 
 67  
     private static final String DAILY_TRIGGER_NAME = "Daily Email Trigger";
 68  
     private static final String DAILY_JOB_NAME = "Daily Email";
 69  
     private static final String WEEKLY_TRIGGER_NAME = "Weekly Email Trigger";
 70  
     private static final String WEEKLY_JOB_NAME = "Weekly Email";
 71  
 
 72  
         private String deploymentEnvironment;
 73  
 
 74  
         public String getDocumentTypeEmailAddress(DocumentType documentType) {
 75  0
                 String fromAddress = (documentType == null ? null : documentType
 76  
                                 .getNotificationFromAddress());
 77  0
                 if (Utilities.isEmpty(fromAddress)) {
 78  0
                         fromAddress = getApplicationEmailAddress();
 79  
                 }
 80  0
                 return fromAddress;
 81  
         }
 82  
 
 83  
         public String getApplicationEmailAddress() {
 84  
                 // first check the configured value
 85  0
                 String fromAddress = Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.MAILER_DETAIL_TYPE, KEWConstants.EMAIL_REMINDER_FROM_ADDRESS);
 86  
                 // if there's no value configured, use the default
 87  0
                 if (Utilities.isEmpty(fromAddress)) {
 88  0
                         fromAddress = DEFAULT_EMAIL_FROM_ADDRESS;
 89  
                 }
 90  0
                 return fromAddress;
 91  
         }
 92  
 
 93  
         protected String getHelpLink() {
 94  0
                 return getHelpLink(null);
 95  
         }
 96  
 
 97  
         protected String getHelpLink(DocumentType documentType) {
 98  0
                 return "For additional help, email " + "<mailto:"
 99  
                                 + getDocumentTypeEmailAddress(documentType) + ">";
 100  
         }
 101  
 
 102  
         public EmailSubject getEmailSubject() {
 103  0
         String subject = ConfigContext.getCurrentContextConfig().getProperty(IMMEDIATE_REMINDER_EMAIL_SUBJECT_KEY);
 104  0
         if (subject == null) {
 105  0
             subject = ACTION_LIST_REMINDER;
 106  
         }
 107  0
                 return new EmailSubject(subject);
 108  
         }
 109  
 
 110  
         public EmailSubject getEmailSubject(String customSubject) {
 111  0
         String subject = ConfigContext.getCurrentContextConfig().getProperty(IMMEDIATE_REMINDER_EMAIL_SUBJECT_KEY);
 112  0
         if (subject == null) {
 113  0
             subject = ACTION_LIST_REMINDER;
 114  
         }
 115  0
                 return new EmailSubject(subject + " " + customSubject);
 116  
         }
 117  
 
 118  
         protected EmailFrom getEmailFrom(DocumentType documentType) {
 119  0
                 return new EmailFrom(getDocumentTypeEmailAddress(documentType));
 120  
         }
 121  
 
 122  
         protected void sendEmail(Person user, EmailSubject subject,
 123  
                         EmailBody body) {
 124  0
                 sendEmail(user, subject, body, null);
 125  0
         }
 126  
 
 127  
         protected void sendEmail(Person user, EmailSubject subject,
 128  
                         EmailBody body, DocumentType documentType) {
 129  
                 try {
 130  0
                         if (isProduction()) {
 131  0
                                 KEWServiceLocator.getEmailService().sendEmail(
 132  
                                                 getEmailFrom(documentType),
 133  
                                                 new EmailTo(user.getEmailAddressUnmasked()), subject, body,
 134  
                                                 false);
 135  
                         } else {
 136  0
                                 KEWServiceLocator
 137  
                                                 .getEmailService()
 138  
                                                 .sendEmail(
 139  
                                                                 getEmailFrom(documentType),
 140  
                                                                 new EmailTo(Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.ACTION_LIST_DETAIL_TYPE, KEWConstants.ACTIONLIST_EMAIL_TEST_ADDRESS)),
 141  
                                                                 subject, body, false);
 142  
                         }
 143  0
                 } catch (Exception e) {
 144  0
                         LOG.error("Error sending Action List email.", e);
 145  0
                 }
 146  0
         }
 147  
 
 148  
         public void sendImmediateReminder(Person user, ActionItem actionItem) {
 149  0
         boolean shouldSendActionListEmailNotification = sendActionListEmailNotification();
 150  0
                 if (shouldSendActionListEmailNotification) {
 151  0
             LOG.debug("sending immediate reminder");
 152  0
                         DocumentRouteHeaderValue document = KEWServiceLocator
 153  
                                         .getRouteHeaderService().getRouteHeader(
 154  
                                                         actionItem.getRouteHeaderId());
 155  0
                         StringBuffer emailBody = new StringBuffer(
 156  
                                         buildImmediateReminderBody(user, actionItem, document
 157  
                                                         .getDocumentType()));
 158  0
                         StringBuffer emailSubject = new StringBuffer();
 159  
                         try {
 160  0
                                 CustomEmailAttribute customEmailAttribute = actionItem
 161  
                                                 .getRouteHeader().getCustomEmailAttribute();
 162  0
                                 if (customEmailAttribute != null) {
 163  0
                                         RouteHeaderDTO routeHeaderVO = DTOConverter
 164  
                                                         .convertRouteHeader(actionItem.getRouteHeader(),
 165  
                                                                         user.getPrincipalId());
 166  0
                                         ActionRequestValue actionRequest = KEWServiceLocator
 167  
                                                         .getActionRequestService().findByActionRequestId(
 168  
                                                                         actionItem.getActionRequestId());
 169  0
                                         ActionRequestDTO actionRequestVO = DTOConverter
 170  
                                                         .convertActionRequest(actionRequest);
 171  0
                                         customEmailAttribute.setRouteHeaderVO(routeHeaderVO);
 172  0
                                         customEmailAttribute.setActionRequestVO(actionRequestVO);
 173  0
                                         String customBody = customEmailAttribute
 174  
                                                         .getCustomEmailBody();
 175  0
                                         if (!Utilities.isEmpty(customBody)) {
 176  0
                                                 emailBody.append(customBody);
 177  
                                         }
 178  0
                                         String customEmailSubject = customEmailAttribute
 179  
                                                         .getCustomEmailSubject();
 180  0
                                         if (!Utilities.isEmpty(customEmailSubject)) {
 181  0
                                                 emailSubject.append(customEmailSubject);
 182  
                                         }
 183  
                                 }
 184  0
                         } catch (Exception e) {
 185  0
                                 LOG
 186  
                                                 .error(
 187  
                                                                 "Error when checking for custom email body and subject.",
 188  
                                                                 e);
 189  0
                         }
 190  0
             LOG.debug("Sending email to " + user);
 191  0
                         sendEmail(user, getEmailSubject(emailSubject.toString()),
 192  
                                         new EmailBody(emailBody.toString()), document
 193  
                                                         .getDocumentType());
 194  
                 }
 195  
 
 196  0
         }
 197  
 
 198  
         protected boolean isProduction() {
 199  0
                 return ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.PROD_DEPLOYMENT_CODE).equalsIgnoreCase(getDeploymentEnvironment());
 200  
         }
 201  
 
 202  
         public void sendDailyReminder() {
 203  0
                 if (sendActionListEmailNotification()) {
 204  0
                         Collection<Person> users = getUsersWithEmailSetting(KEWConstants.EMAIL_RMNDR_DAY_VAL);
 205  0
                         for (Iterator<Person> userIter = users.iterator(); userIter.hasNext();) {
 206  0
                                 Person user = userIter.next();
 207  
                                 try {
 208  0
                                         Collection actionItems = getActionListService().getActionList(user.getPrincipalId(), null);
 209  0
                                         if (actionItems != null && actionItems.size() > 0) {
 210  0
                                             sendPeriodicReminder(user, actionItems,
 211  
                                                                 KEWConstants.EMAIL_RMNDR_DAY_VAL);
 212  
                                         }
 213  0
                                 } catch (Exception e) {
 214  0
                                         LOG.error(
 215  
                                                         "Error sending daily action list reminder to user: "
 216  
                                                                         + user.getEmailAddressUnmasked(), e);
 217  0
                                 }
 218  0
                         }
 219  
                 }
 220  0
                 LOG.debug("Daily action list emails sent successful");
 221  0
         }
 222  
 
 223  
         public void sendWeeklyReminder() {
 224  0
                 if (sendActionListEmailNotification()) {
 225  0
                         List<Person> users = getUsersWithEmailSetting(KEWConstants.EMAIL_RMNDR_WEEK_VAL);
 226  0
                         for (Iterator<Person> userIter = users.iterator(); userIter.hasNext();) {
 227  0
                                 Person user = userIter.next();
 228  
                                 try {
 229  0
                                         Collection actionItems = getActionListService()
 230  
                                                         .getActionList(user.getPrincipalId(), null);
 231  0
                                         if (actionItems != null && actionItems.size() > 0) {
 232  0
                                             sendPeriodicReminder(user, actionItems,
 233  
                                                                 KEWConstants.EMAIL_RMNDR_WEEK_VAL);
 234  
                                         }
 235  0
                                 } catch (Exception e) {
 236  0
                                         LOG.error(
 237  
                                                         "Error sending weekly action list reminder to user: "
 238  
                                                                         + user.getEmailAddressUnmasked(), e);
 239  0
                                 }
 240  0
                         }
 241  
                 }
 242  0
                 LOG.debug("Weekly action list emails sent successful");
 243  0
         }
 244  
 
 245  
         protected void sendPeriodicReminder(Person user, Collection<ActionItem> actionItems, String emailSetting) {
 246  0
                 String emailBody = null;
 247  0
                 actionItems = filterActionItemsToNotify(user.getPrincipalId(), actionItems);
 248  
                 // if there are no action items after being filtered, there's no
 249  
                 // reason to send the email
 250  0
                 if (actionItems.isEmpty()) {
 251  0
                         return;
 252  
                 }
 253  0
                 if (KEWConstants.EMAIL_RMNDR_DAY_VAL.equals(emailSetting)) {
 254  0
                         emailBody = buildDailyReminderBody(user, actionItems);
 255  0
                 } else if (KEWConstants.EMAIL_RMNDR_WEEK_VAL.equals(emailSetting)) {
 256  0
                         emailBody = buildWeeklyReminderBody(user, actionItems);
 257  
                 }
 258  0
                 sendEmail(user, getEmailSubject(), new EmailBody(emailBody));
 259  0
         }
 260  
 
 261  
         /**
 262  
          * Returns a filtered Collection of {@link ActionItem}s which are
 263  
          * filtered according to the user's preferences.  If they have opted
 264  
          * not to recieve secondary or primary delegation emails then they
 265  
          * will not be included.
 266  
          */
 267  
         protected Collection filterActionItemsToNotify(String principalId, Collection actionItems) {
 268  0
                 List filteredItems = new ArrayList();
 269  0
                 Preferences preferences = KEWServiceLocator.getPreferencesService().getPreferences(principalId);
 270  0
                 for (Iterator iterator = actionItems.iterator(); iterator.hasNext();) {
 271  0
                         ActionItem actionItem = (ActionItem) iterator.next();
 272  0
                         if (!actionItem.getPrincipalId().equals(principalId)) {
 273  0
                                 LOG.warn("Encountered an ActionItem with an incorrect workflow ID.  Was " + actionItem.getPrincipalId() +
 274  
                                                 " but expected " + principalId);
 275  0
                                 continue;
 276  
                         }
 277  0
                         boolean includeItem = true;
 278  0
                         if (KEWConstants.DELEGATION_PRIMARY.equals(actionItem.getDelegationType())) {
 279  0
                                 includeItem = KEWConstants.PREFERENCES_YES_VAL.equals(preferences.getNotifyPrimaryDelegation());
 280  0
                         } else if (KEWConstants.DELEGATION_SECONDARY.equals(actionItem.getDelegationType())) {
 281  0
                                 includeItem = KEWConstants.PREFERENCES_YES_VAL.equals(preferences.getNotifySecondaryDelegation());
 282  
                         }
 283  0
                         if (includeItem) {
 284  0
                                 filteredItems.add(actionItem);
 285  
                         }
 286  0
                 }
 287  0
                 return filteredItems;
 288  
         }
 289  
 
 290  
         protected List<Person> getUsersWithEmailSetting(String setting) {
 291  0
                 List users = new ArrayList();
 292  0
                 Collection userOptions = getUserOptionsService().findByOptionValue(
 293  
                                 KEWConstants.EMAIL_RMNDR_KEY, setting);
 294  0
                 for (Iterator iter = userOptions.iterator(); iter.hasNext();) {
 295  0
                         String workflowId = ((UserOptions) iter.next()).getWorkflowId();
 296  
                         try {
 297  
 
 298  0
                                 users.add(KIMServiceLocator.getPersonService().getPerson(workflowId));
 299  0
                         } catch (Exception e) {
 300  0
                                 LOG.error("error retrieving workflow user with ID: "
 301  
                                                 + workflowId);
 302  0
                         }
 303  0
                 }
 304  0
                 return users;
 305  
         }
 306  
 
 307  
     /**
 308  
      * 0 = actionItem.getRouteHeaderId()
 309  
      * 1 = actionItem.getRouteHeader().getInitiatorUser().getDisplayName()
 310  
      * 2 = actionItem.getRouteHeader().getDocumentType().getName()
 311  
      * 3 = actionItem.getDocTitle()
 312  
      * 4 = docHandlerUrl
 313  
      * 5 = getActionListUrl()
 314  
      * 6 = getPreferencesUrl()
 315  
      * 7 = getHelpLink(documentType)
 316  
      */
 317  0
     private static final MessageFormat DEFAULT_IMMEDIATE_REMINDER = new MessageFormat(
 318  
         "Your Action List has an eDoc(electronic document) that needs your attention: \n\n" +
 319  
         "Document ID:\t{0,number,#}\n" +
 320  
         "Initiator:\t\t{1}\n" +
 321  
         "Type:\t\tAdd/Modify {2}\n" +
 322  
         "Title:\t\t{3}\n" +
 323  
         "\n\n" +
 324  
         "To respond to this eDoc: \n" +
 325  
         "\tGo to {4}\n\n" +
 326  
         "\tOr you may access the eDoc from your Action List: \n" +
 327  
         "\tGo to {5}, and then click on the numeric Document ID: {0,number,#} in the first column of the List. \n" +
 328  
         "\n\n\n" +
 329  
         "To change how these email notifications are sent(daily, weekly or none): \n" +
 330  
         "\tGo to {6}\n" +
 331  
         "\n\n\n" +
 332  
         "{7}\n\n\n"
 333  
     );
 334  
 
 335  
     /**
 336  
      * 0 = actionItem.getRouteHeaderId()
 337  
      * 1 = actionItem.getRouteHeader().getInitiatorUser().getDisplayName()
 338  
      * 2 = actionItem.getRouteHeader().getDocumentType().getName()
 339  
      * 3 = actionItem.getDocTitle()
 340  
      * 4 = getActionListUrl()
 341  
      * 5 = getPreferencesUrl()
 342  
      * 6 = getHelpLink(documentType)
 343  
      */
 344  0
     private static final MessageFormat DEFAULT_IMMEDIATE_REMINDER_NO_DOC_HANDLER = new MessageFormat(
 345  
         "Your Action List has an eDoc(electronic document) that needs your attention: \n\n" +
 346  
         "Document ID:\t{0,number,#}\n" +
 347  
         "Initiator:\t\t{1}\n" +
 348  
         "Type:\t\tAdd/Modify {2}\n" +
 349  
         "Title:\t\t{3}\n" +
 350  
         "\n\n" +
 351  
         "To respond to this eDoc you may use your Action List: \n" +
 352  
         "\tGo to {4}, and then take actions related to Document ID: {0,number,#}. \n" +
 353  
         "\n\n\n" +
 354  
         "To change how these email notifications are sent(daily, weekly or none): \n" +
 355  
         "\tGo to {5}\n" +
 356  
         "\n\n\n" +
 357  
         "{6}\n\n\n"
 358  
     );
 359  
 
 360  
         public String buildImmediateReminderBody(Person person,
 361  
                         ActionItem actionItem, DocumentType documentType) {
 362  0
                 String docHandlerUrl = actionItem.getRouteHeader().getDocumentType()
 363  
                                 .getDocHandlerUrl();
 364  0
                 if (StringUtils.isNotBlank(docHandlerUrl)) {
 365  0
                     if (!docHandlerUrl.contains("?")) {
 366  0
                             docHandlerUrl += "?";
 367  
                     } else {
 368  0
                             docHandlerUrl += "&";
 369  
                     }
 370  0
                     docHandlerUrl += KEWConstants.ROUTEHEADER_ID_PARAMETER + "="
 371  
                                     + actionItem.getRouteHeaderId();
 372  0
                     docHandlerUrl += "&" + KEWConstants.COMMAND_PARAMETER + "="
 373  
                                     + KEWConstants.ACTIONLIST_COMMAND;
 374  
                 }
 375  0
                 StringBuffer sf = new StringBuffer();
 376  
 
 377  
                 /*sf
 378  
                                 .append("Your Action List has an eDoc(electronic document) that needs your attention: \n\n");
 379  
                 sf.append("Document ID:\t" + actionItem.getRouteHeaderId() + "\n");
 380  
                 sf.append("Initiator:\t\t");
 381  
                 try {
 382  
                         sf.append(actionItem.getRouteHeader().getInitiatorUser()
 383  
                                         .getDisplayName()
 384  
                                         + "\n");
 385  
                 } catch (Exception e) {
 386  
                         LOG.error("Error retrieving initiator for action item "
 387  
                                         + actionItem.getRouteHeaderId());
 388  
                         sf.append("\n");
 389  
                 }
 390  
                 sf.append("Type:\t\t" + "Add/Modify "
 391  
                                 + actionItem.getRouteHeader().getDocumentType().getName()
 392  
                                 + "\n");
 393  
                 sf.append("Title:\t\t" + actionItem.getDocTitle() + "\n");
 394  
                 sf.append("\n\n");
 395  
                 sf.append("To respond to this eDoc: \n");
 396  
                 sf.append("\tGo to " + docHandlerUrl + "\n\n");
 397  
                 sf.append("\tOr you may access the eDoc from your Action List: \n");
 398  
                 sf.append("\tGo to " + getActionListUrl()
 399  
                                 + ", and then click on the numeric Document ID: "
 400  
                                 + actionItem.getRouteHeaderId()
 401  
                                 + " in the first column of the List. \n");
 402  
                 sf.append("\n\n\n");
 403  
                 sf
 404  
                                 .append("To change how these email notifications are sent(daily, weekly or none): \n");
 405  
                 sf.append("\tGo to " + getPreferencesUrl() + "\n");
 406  
                 sf.append("\n\n\n");
 407  
                 sf.append(getHelpLink(documentType) + "\n\n\n");*/
 408  
 
 409  0
         MessageFormat messageFormat = null;
 410  0
         String stringMessageFormat = ConfigContext.getCurrentContextConfig().getProperty(IMMEDIATE_REMINDER_EMAIL_MESSAGE_KEY);
 411  0
         LOG.debug("Immediate reminder email message from configuration (" + IMMEDIATE_REMINDER_EMAIL_MESSAGE_KEY + "): " + stringMessageFormat);
 412  0
         if (stringMessageFormat == null) {
 413  0
             messageFormat = DEFAULT_IMMEDIATE_REMINDER;
 414  
         } else {
 415  0
             messageFormat = new MessageFormat(stringMessageFormat);
 416  
         }
 417  0
         String initiatorUser = (person == null ? "" : person.getName());
 418  
 
 419  0
         if (StringUtils.isNotBlank(docHandlerUrl)) {
 420  0
             Object[] args = { actionItem.getRouteHeaderId(), 
 421  
                     initiatorUser,
 422  
                     actionItem.getRouteHeader().getDocumentType().getName(),
 423  
                     actionItem.getDocTitle(), 
 424  
                     docHandlerUrl,
 425  
                     getActionListUrl(), 
 426  
                     getPreferencesUrl(),
 427  
                     getHelpLink(documentType) 
 428  
                     };
 429  
 
 430  0
             messageFormat.format(args, sf, new FieldPosition(0));
 431  
 
 432  0
             LOG.debug("default immediate reminder: " + DEFAULT_IMMEDIATE_REMINDER.format(args));
 433  0
         } else {
 434  0
             Object[] args = { actionItem.getRouteHeaderId(), 
 435  
                     initiatorUser,
 436  
                     actionItem.getRouteHeader().getDocumentType().getName(),
 437  
                     actionItem.getDocTitle(), 
 438  
                     getActionListUrl(), 
 439  
                     getPreferencesUrl(),
 440  
                     getHelpLink(documentType) 
 441  
                     };
 442  
 
 443  0
             messageFormat.format(args, sf, new FieldPosition(0));
 444  
 
 445  0
             LOG.debug("default immediate reminder: " + DEFAULT_IMMEDIATE_REMINDER_NO_DOC_HANDLER.format(args));
 446  
         }
 447  0
         LOG.debug("immediate reminder: " + sf);
 448  
 
 449  
                 // for debugging purposes on the immediate reminder only
 450  0
                 if (!isProduction()) {
 451  
                         try {
 452  0
                                 sf.append("Action Item sent to " + actionItem.getPerson().getPrincipalName());
 453  0
                                 if (actionItem.getDelegationType() != null) {
 454  0
                                         sf.append(" for delegation type "
 455  
                                                         + actionItem.getDelegationType());
 456  
                                 }
 457  0
                         } catch (Exception e) {
 458  0
                                 throw new RuntimeException(e);
 459  0
                         }
 460  
                 }
 461  
 
 462  0
                 return sf.toString();
 463  
         }
 464  
 
 465  
         public String buildDailyReminderBody(Person user,
 466  
                         Collection actionItems) {
 467  0
                 StringBuffer sf = new StringBuffer();
 468  0
                 sf.append(getDailyWeeklyMessageBody(actionItems));
 469  0
                 sf
 470  
                                 .append("To change how these email notifications are sent (immediately, weekly or none): \n");
 471  0
                 sf.append("\tGo to " + getPreferencesUrl() + "\n");
 472  
                 // sf.append("\tSend as soon as you get an eDoc\n\t" +
 473  
                 // getPreferencesUrl() + "\n\n");
 474  
                 // sf.append("\tSend weekly\n\t" + getPreferencesUrl() + "\n\n");
 475  
                 // sf.append("\tDo not send\n\t" + getPreferencesUrl() + "\n");
 476  0
                 sf.append("\n\n\n");
 477  0
                 sf.append(getHelpLink() + "\n\n\n");
 478  0
                 return sf.toString();
 479  
         }
 480  
 
 481  
         public String buildWeeklyReminderBody(Person user,
 482  
                         Collection actionItems) {
 483  0
                 StringBuffer sf = new StringBuffer();
 484  0
                 sf.append(getDailyWeeklyMessageBody(actionItems));
 485  0
                 sf
 486  
                                 .append("To change how these email notifications are sent (immediately, daily or none): \n");
 487  0
                 sf.append("\tGo to " + getPreferencesUrl() + "\n");
 488  
                 // sf.append("\tSend as soon as you get an eDoc\n\t" +
 489  
                 // getPreferencesUrl() + "\n\n");
 490  
                 // sf.append("\tSend daily\n\t" + getPreferencesUrl() + "\n\n");
 491  
                 // sf.append("\tDo not send\n\t" + getPreferencesUrl() + "\n");
 492  0
                 sf.append("\n\n\n");
 493  0
                 sf.append(getHelpLink() + "\n\n\n");
 494  0
                 return sf.toString();
 495  
         }
 496  
 
 497  
         String getDailyWeeklyMessageBody(Collection actionItems) {
 498  0
                 StringBuffer sf = new StringBuffer();
 499  0
                 HashMap docTypes = getActionListItemsStat(actionItems);
 500  
 
 501  0
                 sf
 502  
                                 .append("Your Action List has "
 503  
                                                 + actionItems.size()
 504  
                                                 + " eDocs(electronic documents) that need your attention: \n\n");
 505  0
                 Iterator iter = docTypes.keySet().iterator();
 506  0
                 while (iter.hasNext()) {
 507  0
                         String docTypeName = (String) iter.next();
 508  0
                         sf.append("\t" + ((Integer) docTypes.get(docTypeName)).toString()
 509  
                                         + "\t" + docTypeName + "\n");
 510  0
                 }
 511  0
                 sf.append("\n\n");
 512  0
                 sf.append("To respond to each of these eDocs: \n");
 513  0
                 sf
 514  
                                 .append("\tGo to "
 515  
                                                 + getActionListUrl()
 516  
                                                 + ", and then click on its numeric Document ID in the first column of the List.\n");
 517  0
                 sf.append("\n\n\n");
 518  0
                 return sf.toString();
 519  
         }
 520  
 
 521  
         private HashMap getActionListItemsStat(Collection actionItems) {
 522  0
                 HashMap docTypes = new LinkedHashMap();
 523  0
                 Iterator iter = actionItems.iterator();
 524  
 
 525  0
                 while (iter.hasNext()) {
 526  0
                         String docTypeName = ((ActionItem) iter.next()).getRouteHeader()
 527  
                                         .getDocumentType().getName();
 528  0
                         if (docTypes.containsKey(docTypeName)) {
 529  0
                                 docTypes.put(docTypeName, new Integer(((Integer) docTypes
 530  
                                                 .get(docTypeName)).intValue() + 1));
 531  
                         } else {
 532  0
                                 docTypes.put(docTypeName, new Integer(1));
 533  
                         }
 534  0
                 }
 535  0
                 return docTypes;
 536  
         }
 537  
 
 538  
         protected boolean sendActionListEmailNotification() {
 539  0
         LOG.debug("actionlistsendconstant: " + Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.ACTION_LIST_DETAIL_TYPE, KEWConstants.ACTION_LIST_SEND_EMAIL_NOTIFICATION_IND));
 540  
 
 541  0
         return KEWConstants.ACTION_LIST_SEND_EMAIL_NOTIFICATION_VALUE
 542  
                         .equals(Utilities.getKNSParameterValue(KEWConstants.KEW_NAMESPACE, KNSConstants.DetailTypes.ACTION_LIST_DETAIL_TYPE, KEWConstants.ACTION_LIST_SEND_EMAIL_NOTIFICATION_IND));
 543  
         }
 544  
 
 545  
         public void scheduleBatchEmailReminders() throws Exception {
 546  0
             String emailBatchGroup = "Email Batch";
 547  0
             String dailyCron = ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.DAILY_EMAIL_CRON_EXPRESSION);
 548  0
             if (!StringUtils.isBlank(dailyCron)) {
 549  0
                 LOG.info("Scheduling Daily Email batch with cron expression: " + dailyCron);
 550  0
                 CronTrigger dailyTrigger = new CronTrigger(DAILY_TRIGGER_NAME, emailBatchGroup, dailyCron);
 551  0
                 JobDetail dailyJobDetail = new JobDetail(DAILY_JOB_NAME, emailBatchGroup, DailyEmailJob.class);
 552  0
                 dailyTrigger.setJobName(dailyJobDetail.getName());
 553  0
                 dailyTrigger.setJobGroup(dailyJobDetail.getGroup());
 554  0
                 addJobToScheduler(dailyJobDetail);
 555  0
                 addTriggerToScheduler(dailyTrigger);
 556  0
             } else {
 557  0
                 LOG.warn("No " + KEWConstants.DAILY_EMAIL_CRON_EXPRESSION + " parameter was configured.  Daily Email batch was not scheduled!");
 558  
             }
 559  
 
 560  0
             String weeklyCron = ConfigContext.getCurrentContextConfig().getProperty(KEWConstants.WEEKLY_EMAIL_CRON_EXPRESSION);
 561  0
             if (!StringUtils.isBlank(weeklyCron)) {
 562  0
                 LOG.info("Scheduling Weekly Email batch with cron expression: " + weeklyCron);
 563  0
                 CronTrigger weeklyTrigger = new CronTrigger(WEEKLY_TRIGGER_NAME, emailBatchGroup, weeklyCron);
 564  0
                 JobDetail weeklyJobDetail = new JobDetail(WEEKLY_JOB_NAME, emailBatchGroup, WeeklyEmailJob.class);
 565  0
                 weeklyTrigger.setJobName(weeklyJobDetail.getName());
 566  0
                 weeklyTrigger.setJobGroup(weeklyJobDetail.getGroup());
 567  0
                 addJobToScheduler(weeklyJobDetail);
 568  0
                 addTriggerToScheduler(weeklyTrigger);
 569  0
             } else {
 570  0
                 LOG.warn("No " + KEWConstants.WEEKLY_EMAIL_CRON_EXPRESSION + " parameter was configured.  Weekly Email batch was not scheduled!");
 571  
             }
 572  0
         }
 573  
 
 574  
         private void addJobToScheduler(JobDetail jobDetail) throws SchedulerException {
 575  0
                 getScheduler().addJob(jobDetail, true);
 576  0
         }
 577  
 
 578  
         private void addTriggerToScheduler(Trigger trigger) throws SchedulerException {
 579  0
                 boolean triggerExists = (getScheduler().getTrigger(trigger.getName(), trigger.getGroup()) != null);
 580  0
                 if (!triggerExists) {
 581  
                         try {
 582  0
                                 getScheduler().scheduleJob(trigger);
 583  0
                         } catch (ObjectAlreadyExistsException ex) {
 584  0
                                 getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
 585  0
                         }
 586  
                 } else {
 587  0
                     getScheduler().rescheduleJob(trigger.getName(), trigger.getGroup(), trigger);
 588  
                 }
 589  0
         }
 590  
 
 591  
         private Scheduler getScheduler() {
 592  0
                 return KSBServiceLocator.getScheduler();
 593  
         }
 594  
 
 595  
         private UserOptionsService getUserOptionsService() {
 596  0
                 return (UserOptionsService) KEWServiceLocator
 597  
                                 .getUserOptionsService();
 598  
         }
 599  
 
 600  
         protected ActionListService getActionListService() {
 601  0
                 return (ActionListService) KEWServiceLocator.getActionListService();
 602  
         }
 603  
 
 604  
         public String getDeploymentEnvironment() {
 605  0
                 return deploymentEnvironment;
 606  
         }
 607  
 
 608  
         public void setDeploymentEnvironment(String deploymentEnvironment) {
 609  0
                 this.deploymentEnvironment = deploymentEnvironment;
 610  0
         }
 611  
 
 612  
         protected String getActionListUrl() {
 613  0
                 return ConfigContext.getCurrentContextConfig().getProperty(KNSConstants.WORKFLOW_URL_KEY)
 614  
                                 + "/" + "ActionList.do";
 615  
         }
 616  
 
 617  
         protected String getPreferencesUrl() {
 618  0
                 return ConfigContext.getCurrentContextConfig().getProperty(KNSConstants.WORKFLOW_URL_KEY)
 619  
                                 + "/" + "Preferences.do";
 620  
         }
 621  
 }