1
14
15 package com.liferay.portlet.journal.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.journal.model.JournalArticle;
30
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Set;
34
35 import javax.mail.internet.InternetAddress;
36
37
42 public class JournalMessageListener implements MessageListener {
43
44 public void receive(Message message) {
45 try {
46 doReceive(message);
47 }
48 catch (Exception e) {
49 _log.error("Unable to process message " + message, e);
50 }
51 }
52
53 protected void doReceive(Message message) throws Exception {
54 long companyId = message.getLong("companyId");
55 long userId = message.getLong("userId");
56 long groupId = message.getLong("groupId");
57 String articleId = message.getString("articleId");
58 String fromName = message.getString("fromName");
59 String fromAddress = message.getString("fromAddress");
60 String subject = message.getString("subject");
61 String body = message.getString("body");
62 String replyToAddress = message.getString("replyToAddress");
63 String mailId = message.getString("mailId");
64 boolean htmlFormat = message.getBoolean("htmlFormat");
65
66 Set<Long> sent = new HashSet<Long>();
67
68 if (_log.isInfoEnabled()) {
69 _log.info(
70 "Sending notifications for {mailId=" + mailId + ", articleId=" +
71 articleId + "}");
72 }
73
74
76 List<Subscription> subscriptions =
77 SubscriptionLocalServiceUtil.getSubscriptions(
78 companyId, JournalArticle.class.getName(), groupId);
79
80 sendEmail(
81 userId, fromName, fromAddress, subject, body, subscriptions, sent,
82 replyToAddress, mailId, htmlFormat);
83
84 if (_log.isInfoEnabled()) {
85 _log.info("Finished sending notifications");
86 }
87 }
88
89 protected void sendEmail(
90 long userId, String fromName, String fromAddress, String subject,
91 String body, List<Subscription> subscriptions, Set<Long> sent,
92 String replyToAddress, String mailId, boolean htmlFormat)
93 throws Exception {
94
95 for (Subscription subscription : subscriptions) {
96 long subscribedUserId = subscription.getUserId();
97
98 if (sent.contains(subscribedUserId)) {
99 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 try {
140 InternetAddress from = new InternetAddress(
141 fromAddress, fromName);
142
143 InternetAddress to = new InternetAddress(
144 user.getEmailAddress(), user.getFullName());
145
146 String curSubject = StringUtil.replace(
147 subject,
148 new String[] {
149 "[$TO_ADDRESS$]",
150 "[$TO_NAME$]"
151 },
152 new String[] {
153 user.getFullName(),
154 user.getEmailAddress()
155 });
156
157 String curBody = StringUtil.replace(
158 body,
159 new String[] {
160 "[$TO_ADDRESS$]",
161 "[$TO_NAME$]"
162 },
163 new String[] {
164 user.getFullName(),
165 user.getEmailAddress()
166 });
167
168 InternetAddress replyTo = new InternetAddress(
169 replyToAddress, replyToAddress);
170
171 MailMessage message = new MailMessage(
172 from, to, curSubject, curBody, htmlFormat);
173
174 message.setReplyTo(new InternetAddress[] {replyTo});
175 message.setMessageId(mailId);
176
177 MailServiceUtil.sendEmail(message);
178 }
179 catch (Exception e) {
180 _log.error(e);
181 }
182 }
183 }
184
185 private static Log _log = LogFactoryUtil.getLog(
186 JournalMessageListener.class);
187
188 }