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