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