001
014
015 package com.liferay.portlet.wiki.messaging;
016
017 import com.liferay.mail.service.MailServiceUtil;
018 import com.liferay.portal.NoSuchUserException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.mail.MailMessage;
022 import com.liferay.portal.kernel.messaging.Message;
023 import com.liferay.portal.kernel.messaging.MessageListener;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.model.Subscription;
026 import com.liferay.portal.model.User;
027 import com.liferay.portal.service.SubscriptionLocalServiceUtil;
028 import com.liferay.portal.service.UserLocalServiceUtil;
029 import com.liferay.portlet.wiki.model.WikiNode;
030 import com.liferay.portlet.wiki.model.WikiPage;
031
032 import java.util.HashSet;
033 import java.util.List;
034 import java.util.Set;
035
036 import javax.mail.internet.InternetAddress;
037
038
041 public class WikiMessageListener implements MessageListener {
042
043 public void receive(Message message) {
044 try {
045 doReceive(message);
046 }
047 catch (Exception e) {
048 _log.error("Unable to process message " + message, e);
049 }
050 }
051
052 protected void doReceive(Message message) throws Exception {
053 long companyId = message.getLong("companyId");
054 long userId = message.getLong("userId");
055 long nodeId = message.getLong("nodeId");
056 long pageResourcePrimKey = message.getLong("pageResourcePrimKey");
057 String fromName = message.getString("fromName");
058 String fromAddress = message.getString("fromAddress");
059 String subject = message.getString("subject");
060 String body = message.getString("body");
061 String replyToAddress = message.getString("replyToAddress");
062 String mailId = message.getString("mailId");
063 boolean htmlFormat = message.getBoolean("htmlFormat");
064
065 Set<Long> sent = new HashSet<Long>();
066
067 if (_log.isInfoEnabled()) {
068 _log.info(
069 "Sending notifications for {mailId=" + mailId +
070 ", pageResourcePrimKey=" + pageResourcePrimKey +
071 ", nodeId=" + nodeId + "}");
072 }
073
074
075
076 List<Subscription> subscriptions =
077 SubscriptionLocalServiceUtil.getSubscriptions(
078 companyId, WikiPage.class.getName(), pageResourcePrimKey);
079
080 sendEmail(
081 userId, fromName, fromAddress, subject, body, subscriptions, sent,
082 replyToAddress, mailId, htmlFormat);
083
084
085
086 subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
087 companyId, WikiNode.class.getName(), nodeId);
088
089 sendEmail(
090 userId, fromName, fromAddress, subject, body, subscriptions, sent,
091 replyToAddress, mailId, htmlFormat);
092
093 if (_log.isInfoEnabled()) {
094 _log.info("Finished sending notifications");
095 }
096 }
097
098 protected void sendEmail(
099 long userId, String fromName, String fromAddress, String subject,
100 String body, List<Subscription> subscriptions, Set<Long> sent,
101 String replyToAddress, String mailId, boolean htmlFormat)
102 throws Exception {
103
104 for (Subscription subscription : subscriptions) {
105 long subscribedUserId = subscription.getUserId();
106
107 if (sent.contains(subscribedUserId)) {
108 if (_log.isDebugEnabled()) {
109 _log.debug(
110 "Do not send a duplicate email to user " +
111 subscribedUserId);
112 }
113
114 continue;
115 }
116 else {
117 if (_log.isDebugEnabled()) {
118 _log.debug(
119 "Add user " + subscribedUserId +
120 " to the list of users who have received an email");
121 }
122
123 sent.add(subscribedUserId);
124 }
125
126 User user = null;
127
128 try {
129 user = UserLocalServiceUtil.getUserById(subscribedUserId);
130 }
131 catch (NoSuchUserException nsue) {
132 if (_log.isInfoEnabled()) {
133 _log.info(
134 "Subscription " + subscription.getSubscriptionId() +
135 " is stale and will be deleted");
136 }
137
138 SubscriptionLocalServiceUtil.deleteSubscription(
139 subscription.getSubscriptionId());
140
141 continue;
142 }
143
144 if (!user.isActive()) {
145 continue;
146 }
147
148 try {
149 InternetAddress from = new InternetAddress(
150 fromAddress, fromName);
151
152 InternetAddress to = new InternetAddress(
153 user.getEmailAddress(), user.getFullName());
154
155 String curSubject = StringUtil.replace(
156 subject,
157 new String[] {
158 "[$TO_ADDRESS$]",
159 "[$TO_NAME$]"
160 },
161 new String[] {
162 user.getFullName(),
163 user.getEmailAddress()
164 });
165
166 String curBody = StringUtil.replace(
167 body,
168 new String[] {
169 "[$TO_ADDRESS$]",
170 "[$TO_NAME$]"
171 },
172 new String[] {
173 user.getFullName(),
174 user.getEmailAddress()
175 });
176
177 InternetAddress replyTo = new InternetAddress(
178 replyToAddress, replyToAddress);
179
180 MailMessage message = new MailMessage(
181 from, to, curSubject, curBody, htmlFormat);
182
183 message.setReplyTo(new InternetAddress[] {replyTo});
184 message.setMessageId(mailId);
185
186 MailServiceUtil.sendEmail(message);
187 }
188 catch (Exception e) {
189 _log.error(e);
190 }
191 }
192 }
193
194 private static Log _log = LogFactoryUtil.getLog(WikiMessageListener.class);
195
196 }