Coverage Report - org.kuali.rice.kcb.deliverer.impl.EmailMessageDeliverer
 
Classes in this File Line Coverage Branch Coverage Complexity
EmailMessageDeliverer
0%
0/51
0%
0/14
2.111
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  * 
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  * 
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  * 
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 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  
  * This class is responsible for describing the email delivery mechanism for
 36  
  * the system.
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 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  
      * Constructs a EmailMessageDeliverer.java.
 51  
      */
 52  0
     public EmailMessageDeliverer() {
 53  0
         this.emailService = GlobalKCBServiceLocator.getInstance().getEmailService();
 54  0
         this.recipientPreferenceService = GlobalKCBServiceLocator.getInstance().getRecipientPreferenceService();
 55  0
     }
 56  
 
 57  
     /**
 58  
      * This implementation uses the email service to deliver a notification.
 59  
      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#deliver(MessageDelivery)
 60  
      */
 61  
     public void deliver(MessageDelivery messageDelivery) throws MessageDeliveryException {
 62  
         try {
 63  
             // figure out the proper recipient email address
 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  
             //String recipientEmailAddress = GlobalNotificationServiceLocator.getInstance().getKENAPIService().getRecipientPreference(messageDelivery.getMessage().getRecipient(), recipientEmailAddressPrefKey);
 71  
             //String recipientEmailFormat = GlobalNotificationServiceLocator.getInstance().getKENAPIService().getRecipientPreference(messageDelivery.getMessage().getRecipient(), recipientEmailFormatPrefKey);
 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  
      * This implementation does an auto-remove by "canceling" the workflow email with the message delivery record. 
 88  
      * In the case of email, it's a noop 
 89  
      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#autoRemoveMessageDelivery(org.kuali.rice.ken.bo.NotificationMessageDelivery)
 90  
      */
 91  
     public void autoRemoveMessageDelivery(MessageDelivery messageDelivery) /*throws NotificationAutoRemoveException*/ {
 92  
         // we can't remove an email message once it has been sent
 93  0
     }
 94  
 
 95  
     /**
 96  
      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#dismiss(org.kuali.rice.kcb.bo.MessageDelivery, java.lang.String, java.lang.String)
 97  
      */
 98  
     public void dismiss(MessageDelivery messageDelivery, String user, String cause) throws MessageDismissalException {
 99  
         // we can't remove an email message once it has been sent
 100  0
     }
 101  
 
 102  
     /**
 103  
      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#getDescription()
 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  
      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#getName()
 114  
      */
 115  
     public String getName() {
 116  0
         return NAME;
 117  
     }
 118  
 
 119  
     /**
 120  
      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#getTitle()
 121  
      */
 122  
     public String getTitle() {
 123  0
         return "Email Message Delivery";
 124  
     }
 125  
 
 126  
     /**
 127  
      * This implementation returns an address field.
 128  
      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#getPreferenceKeys()
 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  
      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#validatePreferenceValues(java.util.HashMap)
 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  
         // validate format
 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  
 }