1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.GroupLocalServiceUtil;
28  import com.liferay.portal.service.SubscriptionLocalServiceUtil;
29  import com.liferay.portal.service.UserLocalServiceUtil;
30  import com.liferay.portlet.blogs.model.BlogsEntry;
31  
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Set;
35  
36  import javax.mail.internet.InternetAddress;
37  
38  /**
39   * <a href="BlogsMessageListener.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Thiago Moreira
42   */
43  public class BlogsMessageListener implements MessageListener {
44  
45      public void receive(Message message) {
46          try {
47              doReceive(message);
48          }
49          catch (Exception e) {
50              _log.error("Unable to process message " + message, e);
51          }
52      }
53  
54      protected void doReceive(Message message) throws Exception {
55          long companyId = message.getLong("companyId");
56          long userId = message.getLong("userId");
57          long groupId = message.getLong("groupId");
58          long entryId = message.getLong("entryId");
59          String fromName = message.getString("fromName");
60          String fromAddress = message.getString("fromAddress");
61          String subject = message.getString("subject");
62          String body = message.getString("body");
63          String replyToAddress = message.getString("replyToAddress");
64          String mailId = message.getString("mailId");
65          boolean htmlFormat = message.getBoolean("htmlFormat");
66  
67          Set<Long> sent = new HashSet<Long>();
68  
69          if (_log.isInfoEnabled()) {
70              _log.info(
71                  "Sending notifications for {mailId=" + mailId + ", entryId=" +
72                      entryId + "}");
73          }
74  
75          // Entries
76  
77          List<Subscription> subscriptions =
78              SubscriptionLocalServiceUtil.getSubscriptions(
79                  companyId, BlogsEntry.class.getName(), groupId);
80  
81          sendEmail(
82              userId, groupId, fromName, fromAddress, subject, body,
83              subscriptions, sent, replyToAddress, mailId, htmlFormat);
84  
85          if (_log.isInfoEnabled()) {
86              _log.info("Finished sending notifications");
87          }
88      }
89  
90      protected void sendEmail(
91              long userId, long groupId, String fromName, String fromAddress,
92              String subject, String body, List<Subscription> subscriptions,
93              Set<Long> sent, String replyToAddress, String mailId,
94              boolean htmlFormat)
95          throws Exception {
96  
97          for (Subscription subscription : subscriptions) {
98              long subscribedUserId = subscription.getUserId();
99  
100             if (sent.contains(subscribedUserId)) {
101                 if (_log.isDebugEnabled()) {
102                     _log.debug(
103                         "Do not send a duplicate email to user " +
104                             subscribedUserId);
105                 }
106 
107                 continue;
108             }
109             else {
110                 if (_log.isDebugEnabled()) {
111                     _log.debug(
112                         "Add user " + subscribedUserId +
113                             " to the list of users who have received an email");
114                 }
115 
116                 sent.add(subscribedUserId);
117             }
118 
119             User user = null;
120 
121             try {
122                 user = UserLocalServiceUtil.getUserById(
123                     subscription.getUserId());
124             }
125             catch (NoSuchUserException nsue) {
126                 if (_log.isInfoEnabled()) {
127                     _log.info(
128                         "Subscription " + subscription.getSubscriptionId() +
129                             " is stale and will be deleted");
130                 }
131 
132                 SubscriptionLocalServiceUtil.deleteSubscription(
133                     subscription.getSubscriptionId());
134 
135                 continue;
136             }
137 
138             if (!user.isActive()) {
139                 continue;
140             }
141 
142             if (!GroupLocalServiceUtil.hasUserGroup(userId, groupId)) {
143                 if (_log.isInfoEnabled()) {
144                     _log.info(
145                         "Subscription " + subscription.getSubscriptionId() +
146                             " is stale and will be deleted");
147                 }
148 
149                 SubscriptionLocalServiceUtil.deleteSubscription(
150                     subscription.getSubscriptionId());
151 
152                 continue;
153             }
154 
155             try {
156                 InternetAddress from = new InternetAddress(
157                     fromAddress, fromName);
158 
159                 InternetAddress to = new InternetAddress(
160                     user.getEmailAddress(), user.getFullName());
161 
162                 String curSubject = StringUtil.replace(
163                     subject,
164                     new String[] {
165                         "[$TO_ADDRESS$]",
166                         "[$TO_NAME$]"
167                     },
168                     new String[] {
169                         user.getFullName(),
170                         user.getEmailAddress()
171                     });
172 
173                 String curBody = StringUtil.replace(
174                     body,
175                     new String[] {
176                         "[$TO_ADDRESS$]",
177                         "[$TO_NAME$]"
178                     },
179                     new String[] {
180                         user.getFullName(),
181                         user.getEmailAddress()
182                     });
183 
184                 InternetAddress replyTo = new InternetAddress(
185                     replyToAddress, replyToAddress);
186 
187                 MailMessage message = new MailMessage(
188                     from, to, curSubject, curBody, htmlFormat);
189 
190                 message.setReplyTo(new InternetAddress[] {replyTo});
191                 message.setMessageId(mailId);
192 
193                 MailServiceUtil.sendEmail(message);
194             }
195             catch (Exception e) {
196                 _log.error(e);
197             }
198         }
199     }
200 
201     private static Log _log = LogFactoryUtil.getLog(BlogsMessageListener.class);
202 
203 }