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