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.wiki.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.wiki.model.WikiNode;
30  import com.liferay.portlet.wiki.model.WikiPage;
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="WikiMessageListener.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class WikiMessageListener 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 nodeId = message.getLong("nodeId");
58          long pageResourcePrimKey = message.getLong("pageResourcePrimKey");
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  
66          Set<Long> sent = new HashSet<Long>();
67  
68          if (_log.isInfoEnabled()) {
69              _log.info(
70                  "Sending notifications for {mailId=" + mailId +
71                      ", pageResourcePrimKey=" + pageResourcePrimKey +
72                          ", nodeId=" + nodeId + "}");
73          }
74  
75          // Pages
76  
77          List<Subscription> subscriptions =
78              SubscriptionLocalServiceUtil.getSubscriptions(
79                  companyId, WikiPage.class.getName(), pageResourcePrimKey);
80  
81          sendEmail(
82              userId, fromName, fromAddress, subject, body, subscriptions, sent,
83              replyToAddress, mailId);
84  
85          // Nodes
86  
87          subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
88              companyId, WikiNode.class.getName(), nodeId);
89  
90          sendEmail(
91              userId, fromName, fromAddress, subject, body, subscriptions, sent,
92              replyToAddress, mailId);
93  
94          if (_log.isInfoEnabled()) {
95              _log.info("Finished sending notifications");
96          }
97      }
98  
99      protected void sendEmail(
100             long userId, String fromName, String fromAddress, String subject,
101             String body, List<Subscription> subscriptions, Set<Long> sent,
102             String replyToAddress, String mailId)
103         throws Exception {
104 
105         for (Subscription subscription : subscriptions) {
106             long subscribedUserId = subscription.getUserId();
107 
108             if (sent.contains(subscribedUserId)) {
109                 if (_log.isDebugEnabled()) {
110                     _log.debug(
111                         "Do not send a duplicate email to user " +
112                             subscribedUserId);
113                 }
114 
115                 continue;
116             }
117             else {
118                 if (_log.isDebugEnabled()) {
119                     _log.debug(
120                         "Add user " + subscribedUserId +
121                             " to the list of users who have received an email");
122                 }
123 
124                 sent.add(subscribedUserId);
125             }
126 
127             User user = null;
128 
129             try {
130                 user = UserLocalServiceUtil.getUserById(
131                     subscription.getUserId());
132             }
133             catch (NoSuchUserException nsue) {
134                 if (_log.isInfoEnabled()) {
135                     _log.info(
136                         "Subscription " + subscription.getSubscriptionId() +
137                             " is stale and will be deleted");
138                 }
139 
140                 SubscriptionLocalServiceUtil.deleteSubscription(
141                     subscription.getSubscriptionId());
142 
143                 continue;
144             }
145 
146             if (!user.isActive()) {
147                 continue;
148             }
149 
150             try {
151                 InternetAddress from = new InternetAddress(
152                     fromAddress, fromName);
153 
154                 InternetAddress to = new InternetAddress(
155                     user.getEmailAddress(), user.getFullName());
156 
157                 String curSubject = StringUtil.replace(
158                     subject,
159                     new String[] {
160                         "[$TO_ADDRESS$]",
161                         "[$TO_NAME$]"
162                     },
163                     new String[] {
164                         user.getFullName(),
165                         user.getEmailAddress()
166                     });
167 
168                 String curBody = StringUtil.replace(
169                     body,
170                     new String[] {
171                         "[$TO_ADDRESS$]",
172                         "[$TO_NAME$]"
173                     },
174                     new String[] {
175                         user.getFullName(),
176                         user.getEmailAddress()
177                     });
178 
179                 InternetAddress replyTo = new InternetAddress(
180                     replyToAddress, replyToAddress);
181 
182                 MailMessage message = new MailMessage(
183                     from, to, curSubject, curBody, false);
184 
185                 message.setReplyTo(new InternetAddress[] {replyTo});
186                 message.setMessageId(mailId);
187 
188                 MailServiceUtil.sendEmail(message);
189             }
190             catch (Exception e) {
191                 _log.error(e);
192             }
193         }
194     }
195 
196     private static Log _log = LogFactoryUtil.getLog(WikiMessageListener.class);
197 
198 }