1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.wiki.messaging;
24  
25  import com.liferay.mail.service.MailServiceUtil;
26  import com.liferay.portal.NoSuchUserException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.mail.MailMessage;
30  import com.liferay.portal.kernel.messaging.Message;
31  import com.liferay.portal.kernel.messaging.MessageListener;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.model.Subscription;
34  import com.liferay.portal.model.User;
35  import com.liferay.portal.service.SubscriptionLocalServiceUtil;
36  import com.liferay.portal.service.UserLocalServiceUtil;
37  import com.liferay.portlet.wiki.model.WikiNode;
38  import com.liferay.portlet.wiki.model.WikiPage;
39  
40  import java.util.HashSet;
41  import java.util.List;
42  import java.util.Set;
43  
44  import javax.mail.internet.InternetAddress;
45  
46  /**
47   * <a href="WikiMessageListener.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   *
51   */
52  public class WikiMessageListener implements MessageListener {
53  
54      public void receive(Message message) {
55          try {
56              doReceive(message);
57          }
58          catch (Exception e) {
59              _log.error("Unable to process message " + message, e);
60          }
61      }
62  
63      public void doReceive(Message message) throws Exception {
64          long companyId = message.getLong("companyId");
65          long userId = message.getLong("userId");
66          long nodeId = message.getLong("nodeId");
67          long pageResourcePrimKey = message.getLong("pageResourcePrimKey");
68          String fromName = message.getString("fromName");
69          String fromAddress = message.getString("fromAddress");
70          String subject = message.getString("subject");
71          String body = message.getString("body");
72          String replyToAddress = message.getString("replyToAddress");
73          String mailId = message.getString("mailId");
74  
75          Set<Long> sent = new HashSet<Long>();
76  
77          if (_log.isInfoEnabled()) {
78              _log.info(
79                  "Sending notifications for {mailId=" + mailId +
80                      ", pageResourcePrimKey=" + pageResourcePrimKey +
81                          ", nodeId=" + nodeId + "}");
82          }
83  
84          // Pages
85  
86          List<Subscription> subscriptions =
87              SubscriptionLocalServiceUtil.getSubscriptions(
88                  companyId, WikiPage.class.getName(), pageResourcePrimKey);
89  
90          sendEmail(
91              userId, fromName, fromAddress, subject, body, subscriptions, sent,
92              replyToAddress, mailId);
93  
94          // Nodes
95  
96          subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
97              companyId, WikiNode.class.getName(), nodeId);
98  
99          sendEmail(
100             userId, fromName, fromAddress, subject, body, subscriptions, sent,
101             replyToAddress, mailId);
102 
103         if (_log.isInfoEnabled()) {
104             _log.info("Finished sending notifications");
105         }
106     }
107 
108     protected void sendEmail(
109             long userId, String fromName, String fromAddress, String subject,
110             String body, List<Subscription> subscriptions, Set<Long> sent,
111             String replyToAddress, String mailId)
112         throws Exception {
113 
114         for (Subscription subscription : subscriptions) {
115             long subscribedUserId = subscription.getUserId();
116 
117             if (sent.contains(subscribedUserId)) {
118                 if (_log.isDebugEnabled()) {
119                     _log.debug(
120                         "Do not send a duplicate email to user " +
121                             subscribedUserId);
122                 }
123 
124                 continue;
125             }
126             else {
127                 if (_log.isDebugEnabled()) {
128                     _log.debug(
129                         "Add user " + subscribedUserId +
130                             " to the list of users who have received an email");
131                 }
132 
133                 sent.add(subscribedUserId);
134             }
135 
136             User user = null;
137 
138             try {
139                 user = UserLocalServiceUtil.getUserById(
140                     subscription.getUserId());
141             }
142             catch (NoSuchUserException nsue) {
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, false);
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(WikiMessageListener.class);
202 
203 }