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