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.kcb.bo.Message;
24 import org.kuali.rice.kcb.bo.MessageDelivery;
25 import org.kuali.rice.kcb.service.EmailService;
26 import org.kuali.rice.ken.util.NotificationConstants;
27 import org.kuali.rice.kew.mail.EmailBody;
28 import org.kuali.rice.kew.mail.EmailFrom;
29 import org.kuali.rice.kew.mail.EmailSubject;
30 import org.kuali.rice.kew.mail.EmailTo;
31 import org.kuali.rice.kew.service.KEWServiceLocator;
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
53
54
55
56 @Required
57 public void setWeburl(String weburl) {
58 this.weburl = weburl;
59 }
60
61
62
63
64
65
66 public void setDefaultSender(String defaultSender) {
67 this.defaultSender = defaultSender;
68 }
69
70
71
72
73
74 public Long sendEmail(MessageDelivery messageDelivery, String recipientEmailAddress, String emailFormat) throws Exception {
75
76
77
78 Message message = messageDelivery.getMessage();
79 String channelName = message.getChannel();
80
81 String producer = message.getProducer();
82 String sender = defaultSender;
83 if (producer != null) {
84 try {
85 InternetAddress[] addresses = InternetAddress.parse(producer, false);
86 if (addresses.length > 0) {
87 sender = addresses[0].toString();
88 }
89 } catch (AddressException ae) {
90
91 }
92 }
93
94 String title = messageDelivery.getMessage().getTitle();
95 String subject = (channelName == null ? "" : channelName + " ") + (!StringUtils.isBlank(title) ? " - " + title : "");
96
97 String format = FORMAT_TEXT_PLAIN;
98 String linebreak = "\n\n";
99
100
101
102
103 String link = weburl +"/"+ DETAILACTION +"?"
104 + NotificationConstants.NOTIFICATION_CONTROLLER_CONSTANTS.MSG_DELIVERY_ID +"="+ messageDelivery.getId();
105
106 if (emailFormat == null || emailFormat.equals("text")) {
107
108 } else {
109 format = FORMAT_TEXT_HTML;
110 link = "<a href='"+ link +"'>Notification Detail</a>";
111 linebreak = "<br /><br />";
112 }
113
114 LOG.debug("link: "+link);
115
116
117 StringBuffer sb = new StringBuffer();
118 sb.append("You have a new notification. Click the link below to review its details.");
119 sb.append(linebreak);
120 sb.append(linebreak);
121 sb.append(link);
122 String content = sb.toString();
123
124 LOG.debug("subject: "+subject);
125 LOG.debug("sender: "+sender);
126 LOG.debug("recipient: "+recipientEmailAddress);
127 LOG.debug("content: "+content);
128
129
130 sendEmail(content, subject, sender, recipientEmailAddress, format);
131
132 return null;
133 }
134
135
136
137
138
139
140
141
142
143 protected void sendEmail(String message, String subject, String from, String sendTo, String format) {
144
145 KEWServiceLocator.getEmailService().sendEmail(
146 new EmailFrom(from),
147 new EmailTo(sendTo),
148 new EmailSubject(subject),
149 new EmailBody(message),
150 !FORMAT_TEXT_PLAIN.equals(format));
151
152 }
153 }