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