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
72 Set<Long> sent = new HashSet<Long>();
73
74 if (_log.isInfoEnabled()) {
75 _log.info(
76 "Sending notifications for {mailId=" + mailId +
77 ", pageResourcePrimKey=" + pageResourcePrimKey +
78 ", nodeId=" + nodeId + "}");
79 }
80
81
83 List<Subscription> subscriptions =
84 SubscriptionLocalServiceUtil.getSubscriptions(
85 companyId, WikiPage.class.getName(), pageResourcePrimKey);
86
87 sendEmail(
88 userId, fromName, fromAddress, subject, body, subscriptions, sent,
89 replyToAddress, mailId);
90
91
93 subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
94 companyId, WikiNode.class.getName(), nodeId);
95
96 sendEmail(
97 userId, fromName, fromAddress, subject, body, subscriptions, sent,
98 replyToAddress, mailId);
99
100 if (_log.isInfoEnabled()) {
101 _log.info("Finished sending notifications");
102 }
103 }
104
105 protected void sendEmail(
106 long userId, String fromName, String fromAddress, String subject,
107 String body, List<Subscription> subscriptions, Set<Long> sent,
108 String replyToAddress, String mailId)
109 throws Exception {
110
111 for (Subscription subscription : subscriptions) {
112 long subscribedUserId = subscription.getUserId();
113
114 if (sent.contains(subscribedUserId)) {
115 if (_log.isDebugEnabled()) {
116 _log.debug(
117 "Do not send a duplicate email to user " +
118 subscribedUserId);
119 }
120
121 continue;
122 }
123 else {
124 if (_log.isDebugEnabled()) {
125 _log.debug(
126 "Add user " + subscribedUserId +
127 " to the list of users who have received an email");
128 }
129
130 sent.add(subscribedUserId);
131 }
132
133 User user = null;
134
135 try {
136 user = UserLocalServiceUtil.getUserById(
137 subscription.getUserId());
138 }
139 catch (NoSuchUserException nsue) {
140 if (_log.isInfoEnabled()) {
141 _log.info(
142 "Subscription " + subscription.getSubscriptionId() +
143 " is stale and will be deleted");
144 }
145
146 SubscriptionLocalServiceUtil.deleteSubscription(
147 subscription.getSubscriptionId());
148
149 continue;
150 }
151
152 if (!user.isActive()) {
153 continue;
154 }
155
156 try {
157 InternetAddress from = new InternetAddress(
158 fromAddress, fromName);
159
160 InternetAddress to = new InternetAddress(
161 user.getEmailAddress(), user.getFullName());
162
163 String curSubject = StringUtil.replace(
164 subject,
165 new String[] {
166 "[$TO_ADDRESS$]",
167 "[$TO_NAME$]"
168 },
169 new String[] {
170 user.getFullName(),
171 user.getEmailAddress()
172 });
173
174 String curBody = StringUtil.replace(
175 body,
176 new String[] {
177 "[$TO_ADDRESS$]",
178 "[$TO_NAME$]"
179 },
180 new String[] {
181 user.getFullName(),
182 user.getEmailAddress()
183 });
184
185 InternetAddress replyTo = new InternetAddress(
186 replyToAddress, replyToAddress);
187
188 MailMessage message = new MailMessage(
189 from, to, curSubject, curBody, false);
190
191 message.setReplyTo(new InternetAddress[] {replyTo});
192 message.setMessageId(mailId);
193
194 MailServiceUtil.sendEmail(message);
195 }
196 catch (Exception e) {
197 _log.error(e);
198 }
199 }
200 }
201
202 private static Log _log = LogFactoryUtil.getLog(WikiMessageListener.class);
203
204 }