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 try {
153 InternetAddress from = new InternetAddress(
154 fromAddress, fromName);
155
156 InternetAddress to = new InternetAddress(
157 user.getEmailAddress(), user.getFullName());
158
159 String curSubject = StringUtil.replace(
160 subject,
161 new String[] {
162 "[$TO_ADDRESS$]",
163 "[$TO_NAME$]"
164 },
165 new String[] {
166 user.getFullName(),
167 user.getEmailAddress()
168 });
169
170 String curBody = StringUtil.replace(
171 body,
172 new String[] {
173 "[$TO_ADDRESS$]",
174 "[$TO_NAME$]"
175 },
176 new String[] {
177 user.getFullName(),
178 user.getEmailAddress()
179 });
180
181 InternetAddress replyTo = new InternetAddress(
182 replyToAddress, replyToAddress);
183
184 MailMessage message = new MailMessage(
185 from, to, curSubject, curBody, false);
186
187 message.setReplyTo(new InternetAddress[] {replyTo});
188 message.setMessageId(mailId);
189
190 MailServiceUtil.sendEmail(message);
191 }
192 catch (Exception e) {
193 _log.error(e);
194 }
195 }
196 }
197
198 private static Log _log = LogFactoryUtil.getLog(WikiMessageListener.class);
199
200 }