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