001    /**
002     * Copyright (c) 2000-2013 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.flags.messaging;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.language.LanguageUtil;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.messaging.BaseMessageListener;
023    import com.liferay.portal.kernel.messaging.Message;
024    import com.liferay.portal.kernel.util.LocaleUtil;
025    import com.liferay.portal.kernel.util.PropsKeys;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.UniqueList;
028    import com.liferay.portal.model.Company;
029    import com.liferay.portal.model.Group;
030    import com.liferay.portal.model.Layout;
031    import com.liferay.portal.model.Role;
032    import com.liferay.portal.model.RoleConstants;
033    import com.liferay.portal.model.User;
034    import com.liferay.portal.model.UserGroupRole;
035    import com.liferay.portal.security.permission.ResourceActionsUtil;
036    import com.liferay.portal.service.CompanyLocalServiceUtil;
037    import com.liferay.portal.service.GroupLocalServiceUtil;
038    import com.liferay.portal.service.LayoutLocalServiceUtil;
039    import com.liferay.portal.service.RoleLocalServiceUtil;
040    import com.liferay.portal.service.ServiceContext;
041    import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
042    import com.liferay.portal.service.UserLocalServiceUtil;
043    import com.liferay.portal.util.PortletKeys;
044    import com.liferay.portal.util.PrefsPropsUtil;
045    import com.liferay.portal.util.SubscriptionSender;
046    
047    import java.io.IOException;
048    
049    import java.util.ArrayList;
050    import java.util.Date;
051    import java.util.List;
052    import java.util.Locale;
053    
054    /**
055     * @author Julio Camarero
056     * @author Michael C. Han
057     * @author Brian Wing Shun Chan
058     */
059    public class FlagsRequestMessageListener extends BaseMessageListener {
060    
061            @Override
062            protected void doReceive(Message message) throws Exception {
063                    FlagsRequest flagsRequest = (FlagsRequest)message.getPayload();
064    
065                    // Service context
066    
067                    ServiceContext serviceContext = flagsRequest.getServiceContext();
068    
069                    // Company
070    
071                    long companyId = serviceContext.getCompanyId();
072    
073                    Company company = CompanyLocalServiceUtil.getCompany(
074                            serviceContext.getCompanyId());
075    
076                    // Group
077    
078                    Layout layout = LayoutLocalServiceUtil.getLayout(
079                            serviceContext.getPlid());
080    
081                    Group group = layout.getGroup();
082    
083                    String groupName = group.getDescriptiveName();
084    
085                    // Reporter user
086    
087                    String reporterUserName = null;
088                    String reporterEmailAddress = null;
089    
090                    User reporterUser = UserLocalServiceUtil.getUserById(
091                            serviceContext.getUserId());
092    
093                    Locale locale = LocaleUtil.getDefault();
094    
095                    if (reporterUser.isDefaultUser()) {
096                            reporterUserName = LanguageUtil.get(locale, "anonymous");
097                    }
098                    else {
099                            reporterUserName = reporterUser.getFullName();
100                            reporterEmailAddress = reporterUser.getEmailAddress();
101                    }
102    
103                    // Reported user
104    
105                    String reportedUserName = StringPool.BLANK;
106                    String reportedEmailAddress = StringPool.BLANK;
107                    String reportedURL = StringPool.BLANK;
108    
109                    User reportedUser = UserLocalServiceUtil.getUserById(
110                            flagsRequest.getReportedUserId());
111    
112                    if (reportedUser.isDefaultUser()) {
113                            reportedUserName = group.getDescriptiveName();
114                    }
115                    else {
116                            reportedUserName = reportedUser.getFullName();
117                            reportedEmailAddress = reportedUser.getEmailAddress();
118                            reportedURL = reportedUser.getDisplayURL(
119                                    serviceContext.getPortalURL(), serviceContext.getPathMain());
120                    }
121    
122                    // Content
123    
124                    String contentType = ResourceActionsUtil.getModelResource(
125                            locale, flagsRequest.getClassName());
126    
127                    // Reason
128    
129                    String reason = LanguageUtil.get(locale, flagsRequest.getReason());
130    
131                    // Email
132    
133                    String fromName = PrefsPropsUtil.getStringFromNames(
134                            companyId, PropsKeys.FLAGS_EMAIL_FROM_NAME,
135                            PropsKeys.ADMIN_EMAIL_FROM_NAME);
136                    String fromAddress = PrefsPropsUtil.getStringFromNames(
137                            companyId, PropsKeys.FLAGS_EMAIL_FROM_ADDRESS,
138                            PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
139    
140                    String subject = PrefsPropsUtil.getContent(
141                            companyId, PropsKeys.FLAGS_EMAIL_SUBJECT);
142                    String body = PrefsPropsUtil.getContent(
143                            companyId, PropsKeys.FLAGS_EMAIL_BODY);
144    
145                    // Recipients
146    
147                    List<User> recipients = getRecipients(
148                            companyId, serviceContext.getScopeGroupId());
149    
150                    for (User recipient : recipients) {
151                            try {
152                                    notify(
153                                            company, groupName, reporterEmailAddress, reporterUserName,
154                                            reportedEmailAddress, reportedUserName, reportedURL,
155                                            flagsRequest.getClassPK(), flagsRequest.getContentTitle(),
156                                            contentType, flagsRequest.getContentURL(), reason, fromName,
157                                            fromAddress, recipient.getFullName(),
158                                            recipient.getEmailAddress(), subject, body, serviceContext);
159                            }
160                            catch (IOException ioe) {
161                                    if (_log.isWarnEnabled()) {
162                                            _log.warn(ioe);
163                                    }
164                            }
165                    }
166            }
167    
168            protected List<User> getRecipients(long companyId, long groupId)
169                    throws PortalException, SystemException {
170    
171                    List<User> recipients = new UniqueList<User>();
172    
173                    List<String> roleNames = new ArrayList<String>();
174    
175                    Group group = GroupLocalServiceUtil.getGroup(groupId);
176    
177                    if (group.isSite()) {
178                            roleNames.add(RoleConstants.SITE_ADMINISTRATOR);
179                            roleNames.add(RoleConstants.SITE_OWNER);
180                    }
181    
182                    if (group.isCompany()) {
183                            roleNames.add(RoleConstants.ADMINISTRATOR);
184                    }
185                    else if (group.isOrganization()) {
186                            roleNames.add(RoleConstants.ORGANIZATION_ADMINISTRATOR);
187                            roleNames.add(RoleConstants.ORGANIZATION_OWNER);
188                    }
189    
190                    for (String roleName : roleNames) {
191                            Role role = RoleLocalServiceUtil.getRole(companyId, roleName);
192    
193                            List<UserGroupRole> userGroupRoles =
194                                    UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(
195                                            groupId, role.getRoleId());
196    
197                            for (UserGroupRole userGroupRole : userGroupRoles) {
198                                    recipients.add(userGroupRole.getUser());
199                            }
200                    }
201    
202                    if (recipients.isEmpty()) {
203                            Role role = RoleLocalServiceUtil.getRole(
204                                    companyId, RoleConstants.ADMINISTRATOR);
205    
206                            recipients.addAll(
207                                    UserLocalServiceUtil.getRoleUsers(role.getRoleId()));
208                    }
209    
210                    return recipients;
211            }
212    
213            protected void notify(
214                            Company company, String groupName, String reporterEmailAddress,
215                            String reporterUserName, String reportedEmailAddress,
216                            String reportedUserName, String reportedUserURL, long contentId,
217                            String contentTitle, String contentType, String contentURL,
218                            String reason, String fromName, String fromAddress, String toName,
219                            String toAddress, String subject, String body,
220                            ServiceContext serviceContext)
221                    throws Exception {
222    
223                    Date now = new Date();
224    
225                    SubscriptionSender subscriptionSender = new SubscriptionSender();
226    
227                    subscriptionSender.setBody(body);
228                    subscriptionSender.setCompanyId(company.getCompanyId());
229                    subscriptionSender.setContextAttributes(
230                            "[$CONTENT_ID$]", contentId, "[$CONTENT_TYPE$]", contentType,
231                            "[$DATE$]", now.toString(), "[$REASON$]", reason,
232                            "[$REPORTED_USER_ADDRESS$]", reportedEmailAddress,
233                            "[$REPORTED_USER_NAME$]", reportedUserName, "[$REPORTED_USER_URL$]",
234                            reportedUserURL, "[$REPORTER_USER_ADDRESS$]", reporterEmailAddress,
235                            "[$REPORTER_USER_NAME$]", reporterUserName, "[$SITE_NAME$]",
236                            groupName);
237                    subscriptionSender.setContextAttribute(
238                            "[$CONTENT_TITLE$]", contentTitle, false);
239                    subscriptionSender.setContextAttribute(
240                            "[$CONTENT_URL$]", contentURL, false);
241                    subscriptionSender.setFrom(fromAddress, fromName);
242                    subscriptionSender.setHtmlFormat(true);
243                    subscriptionSender.setMailId("flags_request", contentId);
244                    subscriptionSender.setPortletId(PortletKeys.FLAGS);
245                    subscriptionSender.setServiceContext(serviceContext);
246                    subscriptionSender.setSubject(subject);
247    
248                    subscriptionSender.addRuntimeSubscribers(toAddress, toName);
249    
250                    subscriptionSender.flushNotificationsAsync();
251            }
252    
253            private static Log _log = LogFactoryUtil.getLog(
254                    FlagsRequestMessageListener.class);
255    
256    }