001
014
015 package com.liferay.portlet.flags.messaging;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.language.LanguageUtil;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.messaging.BaseMessageListener;
022 import com.liferay.portal.kernel.messaging.Message;
023 import com.liferay.portal.kernel.util.LocaleUtil;
024 import com.liferay.portal.kernel.util.PropsKeys;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.model.Company;
027 import com.liferay.portal.model.Group;
028 import com.liferay.portal.model.Layout;
029 import com.liferay.portal.model.Role;
030 import com.liferay.portal.model.RoleConstants;
031 import com.liferay.portal.model.User;
032 import com.liferay.portal.model.UserGroupRole;
033 import com.liferay.portal.security.permission.ResourceActionsUtil;
034 import com.liferay.portal.service.CompanyLocalServiceUtil;
035 import com.liferay.portal.service.GroupLocalServiceUtil;
036 import com.liferay.portal.service.LayoutLocalServiceUtil;
037 import com.liferay.portal.service.RoleLocalServiceUtil;
038 import com.liferay.portal.service.ServiceContext;
039 import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
040 import com.liferay.portal.service.UserLocalServiceUtil;
041 import com.liferay.portal.util.PortletKeys;
042 import com.liferay.portal.util.PrefsPropsUtil;
043 import com.liferay.portal.util.SubscriptionSender;
044
045 import java.io.IOException;
046
047 import java.util.ArrayList;
048 import java.util.Date;
049 import java.util.LinkedHashSet;
050 import java.util.List;
051 import java.util.Locale;
052 import java.util.Set;
053
054
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
066
067 ServiceContext serviceContext = flagsRequest.getServiceContext();
068
069
070
071 long companyId = serviceContext.getCompanyId();
072
073 Company company = CompanyLocalServiceUtil.getCompany(
074 serviceContext.getCompanyId());
075
076
077
078 Layout layout = LayoutLocalServiceUtil.getLayout(
079 serviceContext.getPlid());
080
081 Group group = layout.getGroup();
082
083 String groupName = group.getDescriptiveName();
084
085
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
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.getThemeDisplay());
120 }
121
122
123
124 String contentType = ResourceActionsUtil.getModelResource(
125 locale, flagsRequest.getClassName());
126
127
128
129 String reason = LanguageUtil.get(locale, flagsRequest.getReason());
130
131
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
146
147 Set<User> recipients = getRecipients(
148 companyId, serviceContext.getScopeGroupId());
149
150 for (User recipient : recipients) {
151 try {
152 notify(
153 reporterUser.getUserId(), company, groupName,
154 reporterEmailAddress, reporterUserName,
155 reportedEmailAddress, reportedUserName, reportedURL,
156 flagsRequest.getClassPK(), flagsRequest.getContentTitle(),
157 contentType, flagsRequest.getContentURL(), reason, fromName,
158 fromAddress, recipient.getFullName(),
159 recipient.getEmailAddress(), subject, body, serviceContext);
160 }
161 catch (IOException ioe) {
162 if (_log.isWarnEnabled()) {
163 _log.warn(ioe);
164 }
165 }
166 }
167 }
168
169 protected Set<User> getRecipients(long companyId, long groupId)
170 throws PortalException {
171
172 Set<User> recipients = new LinkedHashSet<>();
173
174 List<String> roleNames = new ArrayList<>();
175
176 Group group = GroupLocalServiceUtil.getGroup(groupId);
177
178 if (group.isSite()) {
179 roleNames.add(RoleConstants.SITE_ADMINISTRATOR);
180 roleNames.add(RoleConstants.SITE_OWNER);
181 }
182
183 if (group.isCompany()) {
184 roleNames.add(RoleConstants.ADMINISTRATOR);
185 }
186 else if (group.isOrganization()) {
187 roleNames.add(RoleConstants.ORGANIZATION_ADMINISTRATOR);
188 roleNames.add(RoleConstants.ORGANIZATION_OWNER);
189 }
190
191 for (String roleName : roleNames) {
192 Role role = RoleLocalServiceUtil.getRole(companyId, roleName);
193
194 List<UserGroupRole> userGroupRoles =
195 UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(
196 groupId, role.getRoleId());
197
198 for (UserGroupRole userGroupRole : userGroupRoles) {
199 recipients.add(userGroupRole.getUser());
200 }
201 }
202
203 if (recipients.isEmpty()) {
204 Role role = RoleLocalServiceUtil.getRole(
205 companyId, RoleConstants.ADMINISTRATOR);
206
207 recipients.addAll(
208 UserLocalServiceUtil.getRoleUsers(role.getRoleId()));
209 }
210
211 return recipients;
212 }
213
214 protected void notify(
215 long reporterUserId, Company company, String groupName,
216 String reporterEmailAddress, String reporterUserName,
217 String reportedEmailAddress, String reportedUserName,
218 String reportedUserURL, long contentId, String contentTitle,
219 String contentType, String contentURL, String reason,
220 String fromName, String fromAddress, String toName,
221 String toAddress, String subject, String body,
222 ServiceContext serviceContext)
223 throws Exception {
224
225 Date now = new Date();
226
227 SubscriptionSender subscriptionSender = new SubscriptionSender();
228
229 subscriptionSender.setBody(body);
230 subscriptionSender.setCompanyId(company.getCompanyId());
231 subscriptionSender.setContextAttributes(
232 "[$CONTENT_ID$]", contentId, "[$CONTENT_TYPE$]", contentType,
233 "[$DATE$]", now.toString(), "[$REASON$]", reason,
234 "[$REPORTED_USER_ADDRESS$]", reportedEmailAddress,
235 "[$REPORTED_USER_NAME$]", reportedUserName, "[$REPORTED_USER_URL$]",
236 reportedUserURL, "[$REPORTER_USER_ADDRESS$]", reporterEmailAddress,
237 "[$REPORTER_USER_NAME$]", reporterUserName, "[$SITE_NAME$]",
238 groupName);
239 subscriptionSender.setContextAttribute(
240 "[$CONTENT_TITLE$]", contentTitle, false);
241 subscriptionSender.setContextAttribute(
242 "[$CONTENT_URL$]", contentURL, false);
243 subscriptionSender.setCreatorUserId(reporterUserId);
244 subscriptionSender.setFrom(fromAddress, fromName);
245 subscriptionSender.setHtmlFormat(true);
246 subscriptionSender.setMailId("flags_request", contentId);
247 subscriptionSender.setPortletId(PortletKeys.FLAGS);
248 subscriptionSender.setServiceContext(serviceContext);
249 subscriptionSender.setSubject(subject);
250
251 subscriptionSender.addRuntimeSubscribers(toAddress, toName);
252
253 subscriptionSender.flushNotificationsAsync();
254 }
255
256 private static final Log _log = LogFactoryUtil.getLog(
257 FlagsRequestMessageListener.class);
258
259 }