1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.flags.messaging;
16  
17  import com.liferay.mail.service.MailServiceUtil;
18  import com.liferay.portal.PortalException;
19  import com.liferay.portal.SystemException;
20  import com.liferay.portal.kernel.language.LanguageUtil;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.kernel.mail.MailMessage;
24  import com.liferay.portal.kernel.messaging.Message;
25  import com.liferay.portal.kernel.messaging.MessageListener;
26  import com.liferay.portal.kernel.util.LocaleUtil;
27  import com.liferay.portal.kernel.util.PropsKeys;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.model.Company;
31  import com.liferay.portal.model.Group;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.model.Role;
34  import com.liferay.portal.model.RoleConstants;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.model.UserGroupRole;
37  import com.liferay.portal.service.CompanyLocalServiceUtil;
38  import com.liferay.portal.service.GroupLocalServiceUtil;
39  import com.liferay.portal.service.LayoutLocalServiceUtil;
40  import com.liferay.portal.service.RoleLocalServiceUtil;
41  import com.liferay.portal.service.ServiceContext;
42  import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
43  import com.liferay.portal.service.UserLocalServiceUtil;
44  import com.liferay.portal.util.PrefsPropsUtil;
45  import com.liferay.util.UniqueList;
46  
47  import java.io.IOException;
48  
49  import java.util.ArrayList;
50  import java.util.Date;
51  import java.util.List;
52  import java.util.Locale;
53  
54  import javax.mail.internet.InternetAddress;
55  
56  /**
57   * <a href="FlagsRequestMessageListener.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Julio Camarero
60   * @author Michael C. Han
61   * @author Brian Wing Shun Chan
62   */
63  public class FlagsRequestMessageListener implements MessageListener {
64  
65      public void receive(Message message) {
66          try {
67              doReceive(message);
68          }
69          catch (Exception e) {
70              _log.error("Unable to process message " + message, e);
71          }
72      }
73  
74      protected void doReceive(Message message) throws Exception {
75          FlagsRequest flagsRequest = (FlagsRequest)message.getPayload();
76  
77          // Service context
78  
79          ServiceContext serviceContext = flagsRequest.getServiceContext();
80  
81          // Company
82  
83          long companyId = serviceContext.getCompanyId();
84  
85          Company company = CompanyLocalServiceUtil.getCompany(
86              serviceContext.getCompanyId());
87  
88          // Group
89  
90          Layout layout = LayoutLocalServiceUtil.getLayout(
91              serviceContext.getPlid());
92  
93          Group group = layout.getGroup();
94  
95          String groupName = group.getDescriptiveName();
96  
97          // Reporter user
98  
99          String reporterUserName = null;
100         String reporterEmailAddress = null;
101 
102         User reporterUser = UserLocalServiceUtil.getUserById(
103             serviceContext.getUserId());
104 
105         Locale locale = LocaleUtil.getDefault();
106 
107         if (reporterUser.isDefaultUser()) {
108             reporterUserName = LanguageUtil.get(locale, "anonymous");
109         }
110         else {
111             reporterUserName = reporterUser.getFullName();
112             reporterEmailAddress = reporterUser.getEmailAddress();
113         }
114 
115         // Reported user
116 
117         String reportedUserName = StringPool.BLANK;
118         String reportedEmailAddress = StringPool.BLANK;
119         String reportedURL = StringPool.BLANK;
120 
121         User reportedUser = UserLocalServiceUtil.getUserById(
122             flagsRequest.getReportedUserId());
123 
124         if (reportedUser.isDefaultUser()) {
125             reportedUserName = group.getDescriptiveName();
126         }
127         else {
128             reportedUserName = reportedUser.getFullName();
129             reportedEmailAddress = reportedUser.getEmailAddress();
130             reportedURL = reportedUser.getDisplayURL(
131                 serviceContext.getPortalURL(), serviceContext.getPathMain());
132         }
133 
134         // Content
135 
136         String contentType = LanguageUtil.get(
137             locale, "model.resource." + flagsRequest.getClassName());
138 
139         // Reason
140 
141         String reason = LanguageUtil.get(locale, flagsRequest.getReason());
142 
143         // Email
144 
145         String fromName = PrefsPropsUtil.getString(
146             companyId, PropsKeys.FLAGS_EMAIL_FROM_NAME);
147         String fromAddress = PrefsPropsUtil.getString(
148             companyId, PropsKeys.FLAGS_EMAIL_FROM_ADDRESS);
149         String subject = PrefsPropsUtil.getContent(
150             companyId, PropsKeys.FLAGS_EMAIL_SUBJECT);
151         String body = PrefsPropsUtil.getContent(
152             companyId, PropsKeys.FLAGS_EMAIL_BODY);
153 
154         // Recipients
155 
156         List<User> recipients = getRecipients(
157             companyId, serviceContext.getScopeGroupId());
158 
159         for (User recipient : recipients) {
160             try {
161                 notify(
162                     company, groupName, reporterEmailAddress, reporterUserName,
163                     reportedEmailAddress, reportedUserName, reportedURL,
164                     flagsRequest.getClassPK(), flagsRequest.getContentTitle(),
165                     contentType, flagsRequest.getContentURL(), reason,
166                     fromName, fromAddress, recipient.getFullName(),
167                     recipient.getEmailAddress(), subject, body, serviceContext);
168             }
169             catch (IOException ioe) {
170                 if (_log.isWarnEnabled()) {
171                     _log.warn(ioe);
172                 }
173             }
174         }
175     }
176 
177     protected List<User> getRecipients(long companyId, long groupId)
178         throws PortalException, SystemException {
179 
180         List<User> recipients = new UniqueList<User>();
181 
182         List<String> roleNames = new ArrayList<String>();
183 
184         Group group = GroupLocalServiceUtil.getGroup(groupId);
185 
186         if (group.isCommunity()) {
187             roleNames.add(RoleConstants.COMMUNITY_ADMINISTRATOR);
188             roleNames.add(RoleConstants.COMMUNITY_OWNER);
189         }
190         else if (group.isOrganization()) {
191             roleNames.add(RoleConstants.ORGANIZATION_ADMINISTRATOR);
192             roleNames.add(RoleConstants.ORGANIZATION_OWNER);
193         }
194 
195         for (String roleName : roleNames) {
196             Role role = RoleLocalServiceUtil.getRole(companyId, roleName);
197 
198             List<UserGroupRole> userGroupRoles =
199                 UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(
200                     groupId, role.getRoleId());
201 
202             for (UserGroupRole userGroupRole : userGroupRoles) {
203                 recipients.add(userGroupRole.getUser());
204             }
205         }
206 
207         if (recipients.isEmpty()) {
208             Role role = RoleLocalServiceUtil.getRole(
209                 companyId, RoleConstants.ADMINISTRATOR);
210 
211             recipients.addAll(
212                 UserLocalServiceUtil.getRoleUsers(role.getRoleId()));
213         }
214 
215         return recipients;
216     }
217 
218     protected void notify(
219             Company company, String groupName, String reporterEmailAddress,
220             String reporterUserName, String reportedEmailAddress,
221             String reportedUserName, String reportedUserURL, long contentId,
222             String contentTitle, String contentType, String contentURL,
223             String reason, String fromName, String fromAddress, String toName,
224             String toAddress, String subject, String body,
225             ServiceContext serviceContext)
226         throws IOException {
227 
228         Date now = new Date();
229 
230         subject = StringUtil.replace(
231             subject,
232             new String[] {
233                 "[$COMMUNITY_NAME$]",
234                 "[$COMPANY_ID$]",
235                 "[$COMPANY_MX$]",
236                 "[$COMPANY_NAME$]",
237                 "[$CONTENT_ID$]",
238                 "[$CONTENT_TITLE$]",
239                 "[$CONTENT_TYPE$]",
240                 "[$CONTENT_URL$]",
241                 "[$DATE$]",
242                 "[$FROM_ADDRESS$]",
243                 "[$FROM_NAME$]",
244                 "[$PORTAL_URL$]",
245                 "[$REASON$]",
246                 "[$REPORTED_USER_ADDRESS$]",
247                 "[$REPORTED_USER_NAME$]",
248                 "[$REPORTED_USER_URL$]",
249                 "[$REPORTER_USER_ADDRESS$]",
250                 "[$REPORTER_USER_NAME$]",
251                 "[$TO_ADDRESS$]",
252                 "[$TO_NAME$]"
253             },
254             new String[] {
255                 groupName,
256                 String.valueOf(company.getCompanyId()),
257                 company.getMx(),
258                 company.getName(),
259                 String.valueOf(contentId),
260                 contentTitle,
261                 contentType,
262                 contentURL,
263                 now.toString(),
264                 fromAddress,
265                 fromName,
266                 serviceContext.getPortalURL(),
267                 reason,
268                 reportedEmailAddress,
269                 reportedUserName,
270                 reportedUserURL,
271                 reporterEmailAddress,
272                 reporterUserName,
273                 toAddress,
274                 toName
275             });
276 
277         body = StringUtil.replace(
278             body,
279             new String[] {
280                 "[$COMMUNITY_NAME$]",
281                 "[$COMPANY_ID$]",
282                 "[$COMPANY_MX$]",
283                 "[$COMPANY_NAME$]",
284                 "[$CONTENT_ID$]",
285                 "[$CONTENT_TITLE$]",
286                 "[$CONTENT_TYPE$]",
287                 "[$CONTENT_URL$]",
288                 "[$DATE$]",
289                 "[$FROM_ADDRESS$]",
290                 "[$FROM_NAME$]",
291                 "[$PORTAL_URL$]",
292                 "[$REASON$]",
293                 "[$REPORTED_USER_ADDRESS$]",
294                 "[$REPORTED_USER_NAME$]",
295                 "[$REPORTED_USER_URL$]",
296                 "[$REPORTER_USER_ADDRESS$]",
297                 "[$REPORTER_USER_NAME$]",
298                 "[$TO_ADDRESS$]",
299                 "[$TO_NAME$]"
300             },
301             new String[] {
302                 groupName,
303                 String.valueOf(company.getCompanyId()),
304                 company.getMx(),
305                 company.getName(),
306                 String.valueOf(contentId),
307                 contentTitle,
308                 contentType,
309                 contentURL,
310                 now.toString(),
311                 fromAddress,
312                 fromName,
313                 serviceContext.getPortalURL(),
314                 reason,
315                 reportedEmailAddress,
316                 reportedUserName,
317                 reportedUserURL,
318                 reporterEmailAddress,
319                 reporterUserName,
320                 toAddress,
321                 toName
322             });
323 
324         InternetAddress from = new InternetAddress(fromAddress, fromName);
325 
326         InternetAddress to = new InternetAddress(toAddress, toName);
327 
328         MailMessage message = new MailMessage(from, to, subject, body, true);
329 
330         MailServiceUtil.sendEmail(message);
331     }
332 
333     private static Log _log = LogFactoryUtil.getLog(
334         FlagsRequestMessageListener.class);
335 
336 }