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