View Javadoc
1   /**
2    * Copyright 2005-2015 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.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   * 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      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      public EmailMessageDeliverer() {
53          this.emailService = GlobalKCBServiceLocator.getInstance().getEmailService();
54          this.recipientPreferenceService = GlobalKCBServiceLocator.getInstance().getRecipientPreferenceService();
55      }
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              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              //String recipientEmailAddress = GlobalNotificationServiceLocator.getInstance().getKENAPIService().getRecipientPreference(messageDelivery.getMessage().getRecipient(), recipientEmailAddressPrefKey);
71              //String recipientEmailFormat = GlobalNotificationServiceLocator.getInstance().getKENAPIService().getRecipientPreference(messageDelivery.getMessage().getRecipient(), recipientEmailFormatPrefKey);
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       * 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      }
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     }
101 
102     /**
103      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#getDescription()
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      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#getName()
114      */
115     public String getName() {
116         return NAME;
117     }
118 
119     /**
120      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#getTitle()
121      */
122     public String getTitle() {
123         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         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      * @see org.kuali.rice.kcb.deliverer.MessageDeliverer#validatePreferenceValues(java.util.HashMap)
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         // validate format
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 }