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(subscribedUserId);
132 }
133 catch (NoSuchUserException nsue) {
134 if (_log.isInfoEnabled()) {
135 _log.info(
136 "Subscription " + subscription.getSubscriptionId() +
137 " is stale and will be deleted");
138 }
139
140 SubscriptionLocalServiceUtil.deleteSubscription(
141 subscription.getSubscriptionId());
142
143 continue;
144 }
145
146 if (!user.isActive()) {
147 continue;
148 }
149
150 try {
151 InternetAddress from = new InternetAddress(
152 fromAddress, fromName);
153
154 InternetAddress to = new InternetAddress(
155 user.getEmailAddress(), user.getFullName());
156
157 String curSubject = StringUtil.replace(
158 subject,
159 new String[] {
160 "[$TO_ADDRESS$]",
161 "[$TO_NAME$]"
162 },
163 new String[] {
164 user.getFullName(),
165 user.getEmailAddress()
166 });
167
168 String curBody = StringUtil.replace(
169 body,
170 new String[] {
171 "[$TO_ADDRESS$]",
172 "[$TO_NAME$]"
173 },
174 new String[] {
175 user.getFullName(),
176 user.getEmailAddress()
177 });
178
179 InternetAddress replyTo = new InternetAddress(
180 replyToAddress, replyToAddress);
181
182 MailMessage message = new MailMessage(
183 from, to, curSubject, curBody, htmlFormat);
184
185 message.setReplyTo(new InternetAddress[] {replyTo});
186 message.setMessageId(mailId);
187
188 MailServiceUtil.sendEmail(message);
189 }
190 catch (Exception e) {
191 _log.error(e);
192 }
193 }
194 }
195
196 private static Log _log = LogFactoryUtil.getLog(WikiMessageListener.class);
197
198 }