1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kcb.service.impl;
17
18 import javax.mail.internet.AddressException;
19 import javax.mail.internet.InternetAddress;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.apache.log4j.Logger;
23 import org.kuali.rice.core.api.mail.EmailBody;
24 import org.kuali.rice.core.api.mail.EmailFrom;
25 import org.kuali.rice.core.api.mail.EmailSubject;
26 import org.kuali.rice.core.api.mail.EmailTo;
27 import org.kuali.rice.core.api.mail.Mailer;
28 import org.kuali.rice.kcb.bo.Message;
29 import org.kuali.rice.kcb.bo.MessageDelivery;
30 import org.kuali.rice.kcb.service.EmailService;
31 import org.kuali.rice.ken.util.NotificationConstants;
32 import org.springframework.beans.factory.annotation.Required;
33
34
35
36
37
38
39 public class EmailServiceImpl implements EmailService {
40
41 private static Logger LOG = Logger.getLogger(EmailServiceImpl.class);
42
43 private static final String FORMAT_TEXT_HTML = "text/html";
44 private static final String FORMAT_TEXT_PLAIN = "text/plain";
45
46
47 private String weburl;
48 private String defaultSender = "kcb@localhost";
49
50 private final String DETAILACTION = "DetailView.form";
51
52 private Mailer mailer;
53
54 public void setMailer(Mailer mailer){
55 this.mailer = mailer;
56 }
57
58
59
60
61
62 @Required
63 public void setWeburl(String weburl) {
64 this.weburl = weburl;
65 }
66
67
68
69
70
71
72 public void setDefaultSender(String defaultSender) {
73 this.defaultSender = defaultSender;
74 }
75
76
77
78
79
80 public Long sendEmail(MessageDelivery messageDelivery, String recipientEmailAddress, String emailFormat) throws Exception {
81
82
83
84 Message message = messageDelivery.getMessage();
85 String channelName = message.getChannel();
86
87 String producer = message.getProducer();
88 String sender = defaultSender;
89 if (producer != null) {
90 try {
91 InternetAddress[] addresses = InternetAddress.parse(producer, false);
92 if (addresses.length > 0) {
93 sender = addresses[0].toString();
94 }
95 } catch (AddressException ae) {
96
97 }
98 }
99
100 String title = messageDelivery.getMessage().getTitle();
101 String subject = (channelName == null ? "" : channelName + " ") + (!StringUtils.isBlank(title) ? " - " + title : "");
102
103 String format = FORMAT_TEXT_PLAIN;
104 String linebreak = "\n\n";
105
106
107
108
109 String link = weburl +"/"+ DETAILACTION +"?"
110 + NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID +"="+ messageDelivery.getId();
111
112 if (emailFormat == null || emailFormat.equals("text")) {
113
114 } else {
115 format = FORMAT_TEXT_HTML;
116 link = "<a href='"+ link +"'>Notification Detail</a>";
117 linebreak = "<br /><br />";
118 }
119
120 LOG.debug("link: "+link);
121
122
123 StringBuffer sb = new StringBuffer();
124 sb.append("You have a new notification. Click the link below to review its details.");
125 sb.append(linebreak);
126 sb.append(linebreak);
127 sb.append(link);
128 String content = sb.toString();
129
130 LOG.debug("subject: "+subject);
131 LOG.debug("sender: "+sender);
132 LOG.debug("recipient: "+recipientEmailAddress);
133 LOG.debug("content: "+content);
134
135
136 mailer.sendEmail(new EmailFrom(sender), new EmailTo(recipientEmailAddress), new EmailSubject(subject), new EmailBody(content), !FORMAT_TEXT_PLAIN.equals(format));
137
138 return null;
139 }
140 }