001
014
015 package com.liferay.portlet.journal.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.GroupLocalServiceUtil;
028 import com.liferay.portal.service.SubscriptionLocalServiceUtil;
029 import com.liferay.portal.service.UserLocalServiceUtil;
030 import com.liferay.portlet.journal.model.JournalArticle;
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 JournalMessageListener 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 groupId = message.getLong("groupId");
056 String articleId = message.getString("articleId");
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 + ", articleId=" +
070 articleId + "}");
071 }
072
073
074
075 List<Subscription> subscriptions =
076 SubscriptionLocalServiceUtil.getSubscriptions(
077 companyId, JournalArticle.class.getName(), groupId);
078
079 sendEmail(
080 userId, groupId, fromName, fromAddress, subject, body,
081 subscriptions, sent, replyToAddress, mailId, htmlFormat);
082
083 if (_log.isInfoEnabled()) {
084 _log.info("Finished sending notifications");
085 }
086 }
087
088 protected void sendEmail(
089 long userId, long groupId, String fromName, String fromAddress,
090 String subject, String body, List<Subscription> subscriptions,
091 Set<Long> sent, String replyToAddress, String mailId,
092 boolean htmlFormat)
093 throws Exception {
094
095 for (Subscription subscription : subscriptions) {
096 long subscribedUserId = subscription.getUserId();
097
098 if (sent.contains(subscribedUserId)) {
099 if (_log.isDebugEnabled()) {
100 _log.debug(
101 "Do not send a duplicate email to user " +
102 subscribedUserId);
103 }
104
105 continue;
106 }
107 else {
108 if (_log.isDebugEnabled()) {
109 _log.debug(
110 "Add user " + subscribedUserId +
111 " to the list of users who have received an email");
112 }
113
114 sent.add(subscribedUserId);
115 }
116
117 User user = null;
118
119 try {
120 user = UserLocalServiceUtil.getUserById(subscribedUserId);
121 }
122 catch (NoSuchUserException nsue) {
123 if (_log.isInfoEnabled()) {
124 _log.info(
125 "Subscription " + subscription.getSubscriptionId() +
126 " is stale and will be deleted");
127 }
128
129 SubscriptionLocalServiceUtil.deleteSubscription(
130 subscription.getSubscriptionId());
131
132 continue;
133 }
134
135 if (!user.isActive()) {
136 continue;
137 }
138
139 if (!GroupLocalServiceUtil.hasUserGroup(
140 subscribedUserId, groupId)) {
141
142 if (_log.isInfoEnabled()) {
143 _log.info(
144 "Subscription " + subscription.getSubscriptionId() +
145 " is stale and will be deleted");
146 }
147
148 SubscriptionLocalServiceUtil.deleteSubscription(
149 subscription.getSubscriptionId());
150
151 continue;
152 }
153
154 try {
155 InternetAddress from = new InternetAddress(
156 fromAddress, fromName);
157
158 InternetAddress to = new InternetAddress(
159 user.getEmailAddress(), user.getFullName());
160
161 String curSubject = StringUtil.replace(
162 subject,
163 new String[] {
164 "[$TO_ADDRESS$]",
165 "[$TO_NAME$]"
166 },
167 new String[] {
168 user.getFullName(),
169 user.getEmailAddress()
170 });
171
172 String curBody = StringUtil.replace(
173 body,
174 new String[] {
175 "[$TO_ADDRESS$]",
176 "[$TO_NAME$]"
177 },
178 new String[] {
179 user.getFullName(),
180 user.getEmailAddress()
181 });
182
183 InternetAddress replyTo = new InternetAddress(
184 replyToAddress, replyToAddress);
185
186 MailMessage message = new MailMessage(
187 from, to, curSubject, curBody, htmlFormat);
188
189 message.setReplyTo(new InternetAddress[] {replyTo});
190 message.setMessageId(mailId);
191
192 MailServiceUtil.sendEmail(message);
193 }
194 catch (Exception e) {
195 _log.error(e);
196 }
197 }
198 }
199
200 private static Log _log = LogFactoryUtil.getLog(
201 JournalMessageListener.class);
202
203 }