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