001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.messaging;
016    
017    import com.liferay.mail.service.MailServiceUtil;
018    import com.liferay.portal.NoSuchUserException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.mail.MailMessage;
022    import com.liferay.portal.kernel.messaging.Message;
023    import com.liferay.portal.kernel.messaging.MessageListener;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.model.Subscription;
026    import com.liferay.portal.model.User;
027    import com.liferay.portal.service.GroupLocalServiceUtil;
028    import com.liferay.portal.service.SubscriptionLocalServiceUtil;
029    import com.liferay.portal.service.UserLocalServiceUtil;
030    import com.liferay.portlet.wiki.model.WikiNode;
031    import com.liferay.portlet.wiki.model.WikiPage;
032    
033    import java.util.HashSet;
034    import java.util.List;
035    import java.util.Set;
036    
037    import javax.mail.internet.InternetAddress;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class WikiMessageListener implements MessageListener {
043    
044            public void receive(Message message) {
045                    try {
046                            doReceive(message);
047                    }
048                    catch (Exception e) {
049                            _log.error("Unable to process message " + message, e);
050                    }
051            }
052    
053            protected void doReceive(Message message) throws Exception {
054                    long companyId = message.getLong("companyId");
055                    long userId = message.getLong("userId");
056                    long groupId = message.getLong("groupId");
057                    long nodeId = message.getLong("nodeId");
058                    long pageResourcePrimKey = message.getLong("pageResourcePrimKey");
059                    String fromName = message.getString("fromName");
060                    String fromAddress = message.getString("fromAddress");
061                    String subject = message.getString("subject");
062                    String body = message.getString("body");
063                    String replyToAddress = message.getString("replyToAddress");
064                    String mailId = message.getString("mailId");
065                    boolean htmlFormat = message.getBoolean("htmlFormat");
066    
067                    Set<Long> sent = new HashSet<Long>();
068    
069                    if (_log.isInfoEnabled()) {
070                            _log.info(
071                                    "Sending notifications for {mailId=" + mailId +
072                                            ", pageResourcePrimKey=" + pageResourcePrimKey +
073                                                    ", nodeId=" + nodeId + "}");
074                    }
075    
076                    // Pages
077    
078                    List<Subscription> subscriptions =
079                            SubscriptionLocalServiceUtil.getSubscriptions(
080                                    companyId, WikiPage.class.getName(), pageResourcePrimKey);
081    
082                    sendEmail(
083                            userId, groupId, fromName, fromAddress, subject, body,
084                            subscriptions, sent, replyToAddress, mailId, htmlFormat);
085    
086                    // Nodes
087    
088                    subscriptions = SubscriptionLocalServiceUtil.getSubscriptions(
089                            companyId, WikiNode.class.getName(), nodeId);
090    
091                    sendEmail(
092                            userId, groupId, fromName, fromAddress, subject, body,
093                            subscriptions, sent, replyToAddress, mailId, htmlFormat);
094    
095                    if (_log.isInfoEnabled()) {
096                            _log.info("Finished sending notifications");
097                    }
098            }
099    
100            protected void sendEmail(
101                            long userId, long groupId, String fromName, String fromAddress,
102                            String subject, String body, List<Subscription> subscriptions,
103                            Set<Long> sent, String replyToAddress, String mailId,
104                            boolean htmlFormat)
105                    throws Exception {
106    
107                    for (Subscription subscription : subscriptions) {
108                            long subscribedUserId = subscription.getUserId();
109    
110                            if (sent.contains(subscribedUserId)) {
111                                    if (_log.isDebugEnabled()) {
112                                            _log.debug(
113                                                    "Do not send a duplicate email to user " +
114                                                            subscribedUserId);
115                                    }
116    
117                                    continue;
118                            }
119                            else {
120                                    if (_log.isDebugEnabled()) {
121                                            _log.debug(
122                                                    "Add user " + subscribedUserId +
123                                                            " to the list of users who have received an email");
124                                    }
125    
126                                    sent.add(subscribedUserId);
127                            }
128    
129                            User user = null;
130    
131                            try {
132                                    user = UserLocalServiceUtil.getUserById(subscribedUserId);
133                            }
134                            catch (NoSuchUserException nsue) {
135                                    if (_log.isInfoEnabled()) {
136                                            _log.info(
137                                                    "Subscription " + subscription.getSubscriptionId() +
138                                                            " is stale and will be deleted");
139                                    }
140    
141                                    SubscriptionLocalServiceUtil.deleteSubscription(
142                                            subscription.getSubscriptionId());
143    
144                                    continue;
145                            }
146    
147                            if (!user.isActive()) {
148                                    continue;
149                            }
150    
151                            if (!GroupLocalServiceUtil.hasUserGroup(
152                                            subscribedUserId, groupId)) {
153    
154                                    if (_log.isInfoEnabled()) {
155                                            _log.info(
156                                                    "Subscription " + subscription.getSubscriptionId() +
157                                                            " is stale and will be deleted");
158                                    }
159    
160                                    SubscriptionLocalServiceUtil.deleteSubscription(
161                                            subscription.getSubscriptionId());
162    
163                                    continue;
164                            }
165    
166                            try {
167                                    InternetAddress from = new InternetAddress(
168                                            fromAddress, fromName);
169    
170                                    InternetAddress to = new InternetAddress(
171                                            user.getEmailAddress(), user.getFullName());
172    
173                                    String curSubject = StringUtil.replace(
174                                            subject,
175                                            new String[] {
176                                                    "[$TO_ADDRESS$]",
177                                                    "[$TO_NAME$]"
178                                            },
179                                            new String[] {
180                                                    user.getFullName(),
181                                                    user.getEmailAddress()
182                                            });
183    
184                                    String curBody = StringUtil.replace(
185                                            body,
186                                            new String[] {
187                                                    "[$TO_ADDRESS$]",
188                                                    "[$TO_NAME$]"
189                                            },
190                                            new String[] {
191                                                    user.getFullName(),
192                                                    user.getEmailAddress()
193                                            });
194    
195                                    InternetAddress replyTo = new InternetAddress(
196                                            replyToAddress, replyToAddress);
197    
198                                    MailMessage message = new MailMessage(
199                                            from, to, curSubject, curBody, htmlFormat);
200    
201                                    message.setReplyTo(new InternetAddress[] {replyTo});
202                                    message.setMessageId(mailId);
203    
204                                    MailServiceUtil.sendEmail(message);
205                            }
206                            catch (Exception e) {
207                                    _log.error(e);
208                            }
209                    }
210            }
211    
212            private static Log _log = LogFactoryUtil.getLog(WikiMessageListener.class);
213    
214    }