1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.blogs.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.blogs.model.BlogsEntry;
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  /**
38   * <a href="BlogsMessageListener.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Thiago Moreira
41   */
42  public class BlogsMessageListener 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          long entryId = message.getLong("entryId");
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 + ", entryId=" +
71                      entryId + "}");
72          }
73  
74          // Entries
75  
76          List<Subscription> subscriptions =
77              SubscriptionLocalServiceUtil.getSubscriptions(
78                  companyId, BlogsEntry.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(
121                     subscription.getUserId());
122             }
123             catch (NoSuchUserException nsue) {
124                 if (_log.isInfoEnabled()) {
125                     _log.info(
126                         "Subscription " + subscription.getSubscriptionId() +
127                             " is stale and will be deleted");
128                 }
129 
130                 SubscriptionLocalServiceUtil.deleteSubscription(
131                     subscription.getSubscriptionId());
132 
133                 continue;
134             }
135 
136             if (!user.isActive()) {
137                 continue;
138             }
139 
140             try {
141                 InternetAddress from = new InternetAddress(
142                     fromAddress, fromName);
143 
144                 InternetAddress to = new InternetAddress(
145                     user.getEmailAddress(), user.getFullName());
146 
147                 String curSubject = StringUtil.replace(
148                     subject,
149                     new String[] {
150                         "[$TO_ADDRESS$]",
151                         "[$TO_NAME$]"
152                     },
153                     new String[] {
154                         user.getFullName(),
155                         user.getEmailAddress()
156                     });
157 
158                 String curBody = StringUtil.replace(
159                     body,
160                     new String[] {
161                         "[$TO_ADDRESS$]",
162                         "[$TO_NAME$]"
163                     },
164                     new String[] {
165                         user.getFullName(),
166                         user.getEmailAddress()
167                     });
168 
169                 InternetAddress replyTo = new InternetAddress(
170                     replyToAddress, replyToAddress);
171 
172                 MailMessage message = new MailMessage(
173                     from, to, curSubject, curBody, htmlFormat);
174 
175                 message.setReplyTo(new InternetAddress[] {replyTo});
176                 message.setMessageId(mailId);
177 
178                 MailServiceUtil.sendEmail(message);
179             }
180             catch (Exception e) {
181                 _log.error(e);
182             }
183         }
184     }
185 
186     private static Log _log = LogFactoryUtil.getLog(BlogsMessageListener.class);
187 
188 }