1
14
15 package com.liferay.portlet.messageboards.messaging;
16
17 import com.liferay.mail.service.MailServiceUtil;
18 import com.liferay.portal.NoSuchUserException;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.kernel.mail.MailMessage;
22 import com.liferay.portal.kernel.messaging.MessageListener;
23 import com.liferay.portal.kernel.util.GetterUtil;
24 import com.liferay.portal.kernel.util.StringPool;
25 import com.liferay.portal.kernel.util.StringUtil;
26 import com.liferay.portal.model.Subscription;
27 import com.liferay.portal.model.User;
28 import com.liferay.portal.service.SubscriptionLocalServiceUtil;
29 import com.liferay.portal.service.UserLocalServiceUtil;
30 import com.liferay.portlet.messageboards.model.MBCategory;
31 import com.liferay.portlet.messageboards.model.MBThread;
32 import com.liferay.portlet.messageboards.util.BBCodeUtil;
33
34 import java.util.ArrayList;
35 import java.util.HashSet;
36 import java.util.List;
37 import java.util.Set;
38
39 import javax.mail.internet.InternetAddress;
40
41
46 public class MBMessageListener implements MessageListener {
47
48 public void receive(com.liferay.portal.kernel.messaging.Message message) {
49 try {
50 doReceive(message);
51 }
52 catch (Exception e) {
53 _log.error("Unable to process message " + message, e);
54 }
55 }
56
57 protected void doReceive(
58 com.liferay.portal.kernel.messaging.Message message)
59 throws Exception {
60
61 long companyId = message.getLong("companyId");
62 long userId = message.getLong("userId");
63 String categoryIds = message.getString("categoryIds");
64 String threadId = message.getString("threadId");
65 String fromName = message.getString("fromName");
66 String fromAddress = message.getString("fromAddress");
67 String subject = message.getString("subject");
68 String body = message.getString("body");
69 String replyToAddress = message.getString("replyToAddress");
70 String mailId = message.getString("mailId");
71 String inReplyTo = message.getString("inReplyTo");
72 boolean htmlFormat = message.getBoolean("htmlFormat");
73
74 subject = subject + StringPool.SPACE + mailId;
75
76 Set<Long> sent = new HashSet<Long>();
77
78 if (_log.isInfoEnabled()) {
79 _log.info(
80 "Sending notifications for {mailId=" + mailId + ", threadId=" +
81 threadId + ", categoryIds=" + categoryIds + "}");
82 }
83
84
86 List<Subscription> subscriptions =
87 SubscriptionLocalServiceUtil.getSubscriptions(
88 companyId, MBThread.class.getName(),
89 GetterUtil.getLong(threadId));
90
91 sendEmail(
92 userId, fromName, fromAddress, subject, body, subscriptions, sent,
93 replyToAddress, mailId, inReplyTo, htmlFormat);
94
95
97 long[] categoryIdsArray = StringUtil.split(categoryIds, 0L);
98
99 for (long categoryId : categoryIdsArray) {
100 subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
101 companyId, MBCategory.class.getName(), categoryId);
102
103 sendEmail(
104 userId, fromName, fromAddress, subject, body, subscriptions,
105 sent, replyToAddress, mailId, inReplyTo, htmlFormat);
106 }
107
108 if (_log.isInfoEnabled()) {
109 _log.info("Finished sending notifications");
110 }
111 }
112
113 protected void sendEmail(
114 long userId, String fromName, String fromAddress, String subject,
115 String body, List<Subscription> subscriptions, Set<Long> sent,
116 String replyToAddress, String mailId, String inReplyTo,
117 boolean htmlFormat)
118 throws Exception {
119
120 List<InternetAddress> addresses =
121 new ArrayList<InternetAddress>();
122
123 for (Subscription subscription : subscriptions) {
124 long subscribedUserId = subscription.getUserId();
125
126 if (sent.contains(subscribedUserId)) {
127 if (_log.isDebugEnabled()) {
128 _log.debug(
129 "Do not send a duplicate email to user " +
130 subscribedUserId);
131 }
132
133 continue;
134 }
135 else {
136 if (_log.isDebugEnabled()) {
137 _log.debug(
138 "Add user " + subscribedUserId +
139 " to the list of users who have received an email");
140 }
141
142 sent.add(subscribedUserId);
143 }
144
145 User user = null;
146
147 try {
148 user = UserLocalServiceUtil.getUserById(
149 subscription.getUserId());
150 }
151 catch (NoSuchUserException nsue) {
152 if (_log.isInfoEnabled()) {
153 _log.info(
154 "Subscription " + subscription.getSubscriptionId() +
155 " is stale and will be deleted");
156 }
157
158 SubscriptionLocalServiceUtil.deleteSubscription(
159 subscription.getSubscriptionId());
160
161 continue;
162 }
163
164 if (!user.isActive()) {
165 continue;
166 }
167
168 InternetAddress userAddress = new InternetAddress(
169 user.getEmailAddress(), user.getFullName());
170
171 addresses.add(userAddress);
172 }
173
174 try {
175 InternetAddress[] bulkAddresses = addresses.toArray(
176 new InternetAddress[addresses.size()]);
177
178 if (bulkAddresses.length == 0) {
179 return;
180 }
181
182 InternetAddress from = new InternetAddress(fromAddress, fromName);
183
184 InternetAddress to = new InternetAddress(
185 replyToAddress, replyToAddress);
186
187 String curSubject = StringUtil.replace(
188 subject,
189 new String[] {
190 "[$TO_ADDRESS$]",
191 "[$TO_NAME$]"
192 },
193 new String[] {
194 replyToAddress,
195 replyToAddress
196 });
197
198 String curBody = StringUtil.replace(
199 body,
200 new String[] {
201 "[$TO_ADDRESS$]",
202 "[$TO_NAME$]"
203 },
204 new String[] {
205 replyToAddress,
206 replyToAddress
207 });
208
209 InternetAddress replyTo = new InternetAddress(
210 replyToAddress, replyToAddress);
211
212 if (htmlFormat) {
213 try {
214 curBody = BBCodeUtil.getHTML(curBody);
215 }
216 catch (Exception e) {
217 _log.error(
218 "Could not parse message " + mailId + " " +
219 e.getMessage());
220 }
221 }
222
223 MailMessage message = new MailMessage(
224 from, to, curSubject, curBody, htmlFormat);
225
226 message.setBulkAddresses(bulkAddresses);
227 message.setMessageId(mailId);
228 message.setInReplyTo(inReplyTo);
229 message.setReplyTo(new InternetAddress[] {replyTo});
230
231 MailServiceUtil.sendEmail(message);
232 }
233 catch (Exception e) {
234 _log.error(e);
235 }
236 }
237
238 private static Log _log = LogFactoryUtil.getLog(MBMessageListener.class);
239
240 }