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