1
19
20 package com.liferay.portlet.wiki.messaging;
21
22 import com.liferay.mail.service.MailServiceUtil;
23 import com.liferay.portal.NoSuchUserException;
24 import com.liferay.portal.kernel.log.Log;
25 import com.liferay.portal.kernel.log.LogFactoryUtil;
26 import com.liferay.portal.kernel.mail.MailMessage;
27 import com.liferay.portal.kernel.messaging.Message;
28 import com.liferay.portal.kernel.messaging.MessageListener;
29 import com.liferay.portal.kernel.util.StringUtil;
30 import com.liferay.portal.model.Subscription;
31 import com.liferay.portal.model.User;
32 import com.liferay.portal.service.SubscriptionLocalServiceUtil;
33 import com.liferay.portal.service.UserLocalServiceUtil;
34 import com.liferay.portlet.wiki.model.WikiNode;
35 import com.liferay.portlet.wiki.model.WikiPage;
36
37 import java.util.HashSet;
38 import java.util.List;
39 import java.util.Set;
40
41 import javax.mail.internet.InternetAddress;
42
43
49 public class WikiMessageListener implements MessageListener {
50
51 public void receive(Message message) {
52 try {
53 doReceive(message);
54 }
55 catch (Exception e) {
56 _log.error("Unable to process message " + message, e);
57 }
58 }
59
60 public void doReceive(Message message) throws Exception {
61 long companyId = message.getLong("companyId");
62 long userId = message.getLong("userId");
63 long nodeId = message.getLong("nodeId");
64 long pageResourcePrimKey = message.getLong("pageResourcePrimKey");
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 boolean htmlFormat = message.getBoolean("htmlFormat");
72
73 Set<Long> sent = new HashSet<Long>();
74
75 if (_log.isInfoEnabled()) {
76 _log.info(
77 "Sending notifications for {mailId=" + mailId +
78 ", pageResourcePrimKey=" + pageResourcePrimKey +
79 ", nodeId=" + nodeId + "}");
80 }
81
82
84 List<Subscription> subscriptions =
85 SubscriptionLocalServiceUtil.getSubscriptions(
86 companyId, WikiPage.class.getName(), pageResourcePrimKey);
87
88 sendEmail(
89 userId, fromName, fromAddress, subject, body, subscriptions, sent,
90 replyToAddress, mailId, htmlFormat);
91
92
94 subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
95 companyId, WikiNode.class.getName(), nodeId);
96
97 sendEmail(
98 userId, fromName, fromAddress, subject, body, subscriptions, sent,
99 replyToAddress, mailId, htmlFormat);
100
101 if (_log.isInfoEnabled()) {
102 _log.info("Finished sending notifications");
103 }
104 }
105
106 protected void sendEmail(
107 long userId, String fromName, String fromAddress, String subject,
108 String body, List<Subscription> subscriptions, Set<Long> sent,
109 String replyToAddress, String mailId, boolean htmlFormat)
110 throws Exception {
111
112 for (Subscription subscription : subscriptions) {
113 long subscribedUserId = subscription.getUserId();
114
115 if (sent.contains(subscribedUserId)) {
116 if (_log.isDebugEnabled()) {
117 _log.debug(
118 "Do not send a duplicate email to user " +
119 subscribedUserId);
120 }
121
122 continue;
123 }
124 else {
125 if (_log.isDebugEnabled()) {
126 _log.debug(
127 "Add user " + subscribedUserId +
128 " to the list of users who have received an email");
129 }
130
131 sent.add(subscribedUserId);
132 }
133
134 User user = null;
135
136 try {
137 user = UserLocalServiceUtil.getUserById(
138 subscription.getUserId());
139 }
140 catch (NoSuchUserException nsue) {
141 if (_log.isInfoEnabled()) {
142 _log.info(
143 "Subscription " + subscription.getSubscriptionId() +
144 " is stale and will be deleted");
145 }
146
147 SubscriptionLocalServiceUtil.deleteSubscription(
148 subscription.getSubscriptionId());
149
150 continue;
151 }
152
153 if (!user.isActive()) {
154 continue;
155 }
156
157 try {
158 InternetAddress from = new InternetAddress(
159 fromAddress, fromName);
160
161 InternetAddress to = new InternetAddress(
162 user.getEmailAddress(), user.getFullName());
163
164 String curSubject = StringUtil.replace(
165 subject,
166 new String[] {
167 "[$TO_ADDRESS$]",
168 "[$TO_NAME$]"
169 },
170 new String[] {
171 user.getFullName(),
172 user.getEmailAddress()
173 });
174
175 String curBody = StringUtil.replace(
176 body,
177 new String[] {
178 "[$TO_ADDRESS$]",
179 "[$TO_NAME$]"
180 },
181 new String[] {
182 user.getFullName(),
183 user.getEmailAddress()
184 });
185
186 InternetAddress replyTo = new InternetAddress(
187 replyToAddress, replyToAddress);
188
189 MailMessage message = new MailMessage(
190 from, to, curSubject, curBody, htmlFormat);
191
192 message.setReplyTo(new InternetAddress[] {replyTo});
193 message.setMessageId(mailId);
194
195 MailServiceUtil.sendEmail(message);
196 }
197 catch (Exception e) {
198 _log.error(e);
199 }
200 }
201 }
202
203 private static Log _log = LogFactoryUtil.getLog(WikiMessageListener.class);
204
205 }