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.mail.MailMessage;
28 import com.liferay.portal.kernel.messaging.Message;
29 import com.liferay.portal.kernel.messaging.MessageListener;
30 import com.liferay.portal.kernel.util.StringUtil;
31 import com.liferay.portal.model.Subscription;
32 import com.liferay.portal.model.User;
33 import com.liferay.portal.service.SubscriptionLocalServiceUtil;
34 import com.liferay.portal.service.UserLocalServiceUtil;
35 import com.liferay.portlet.wiki.model.WikiNode;
36 import com.liferay.portlet.wiki.model.WikiPage;
37
38 import java.util.HashSet;
39 import java.util.List;
40 import java.util.Set;
41
42 import javax.mail.internet.InternetAddress;
43
44 import org.apache.commons.logging.Log;
45 import org.apache.commons.logging.LogFactory;
46
47
53 public class WikiMessageListener implements MessageListener {
54
55 public void receive(Message message) {
56 try {
57 doReceive(message);
58 }
59 catch (Exception e) {
60 _log.error("Unable to process message " + message, e);
61 }
62 }
63
64 public void doReceive(Message message) throws Exception {
65 long companyId = message.getLong("companyId");
66 long userId = message.getLong("userId");
67 long nodeId = message.getLong("nodeId");
68 long pageResourcePrimKey = message.getLong("pageResourcePrimKey");
69 String fromName = message.getString("fromName");
70 String fromAddress = message.getString("fromAddress");
71 String subject = message.getString("subject");
72 String body = message.getString("body");
73 String replyToAddress = message.getString("replyToAddress");
74 String mailId = message.getString("mailId");
75
76 Set<Long> sent = new HashSet<Long>();
77
78 if (_log.isInfoEnabled()) {
79 _log.info(
80 "Sending notifications for {mailId=" + mailId +
81 ", pageResourcePrimKey=" + pageResourcePrimKey +
82 ", nodeId=" + nodeId + "}");
83 }
84
85
87 List<Subscription> subscriptions =
88 SubscriptionLocalServiceUtil.getSubscriptions(
89 companyId, WikiPage.class.getName(), pageResourcePrimKey);
90
91 sendEmail(
92 userId, fromName, fromAddress, subject, body, subscriptions, sent,
93 replyToAddress, mailId);
94
95
97 subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
98 companyId, WikiNode.class.getName(), nodeId);
99
100 sendEmail(
101 userId, fromName, fromAddress, subject, body, subscriptions, sent,
102 replyToAddress, mailId);
103
104 if (_log.isInfoEnabled()) {
105 _log.info("Finished sending notifications");
106 }
107 }
108
109 protected void sendEmail(
110 long userId, String fromName, String fromAddress, String subject,
111 String body, List<Subscription> subscriptions, Set<Long> sent,
112 String replyToAddress, String mailId)
113 throws Exception {
114
115 for (Subscription subscription : subscriptions) {
116 long subscribedUserId = subscription.getUserId();
117
118 if (sent.contains(subscribedUserId)) {
119 if (_log.isDebugEnabled()) {
120 _log.debug(
121 "Do not send a duplicate email to user " +
122 subscribedUserId);
123 }
124
125 continue;
126 }
127 else {
128 if (_log.isDebugEnabled()) {
129 _log.debug(
130 "Add user " + subscribedUserId +
131 " to the list of users who have received an email");
132 }
133
134 sent.add(subscribedUserId);
135 }
136
137 User user = null;
138
139 try {
140 user = UserLocalServiceUtil.getUserById(
141 subscription.getUserId());
142 }
143 catch (NoSuchUserException nsue) {
144 if (_log.isInfoEnabled()) {
145 _log.info(
146 "Subscription " + subscription.getSubscriptionId() +
147 " is stale and will be deleted");
148 }
149
150 SubscriptionLocalServiceUtil.deleteSubscription(
151 subscription.getSubscriptionId());
152
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 = LogFactory.getLog(WikiMessageListener.class);
203
204 }