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