001
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.HtmlUtil;
025 import com.liferay.portal.kernel.util.LocaleUtil;
026 import com.liferay.portal.kernel.util.PropsKeys;
027 import com.liferay.portal.kernel.util.StringPool;
028 import com.liferay.portal.kernel.util.UniqueList;
029 import com.liferay.portal.model.Company;
030 import com.liferay.portal.model.Group;
031 import com.liferay.portal.model.Layout;
032 import com.liferay.portal.model.Role;
033 import com.liferay.portal.model.RoleConstants;
034 import com.liferay.portal.model.User;
035 import com.liferay.portal.model.UserGroupRole;
036 import com.liferay.portal.security.permission.ResourceActionsUtil;
037 import com.liferay.portal.service.CompanyLocalServiceUtil;
038 import com.liferay.portal.service.GroupLocalServiceUtil;
039 import com.liferay.portal.service.LayoutLocalServiceUtil;
040 import com.liferay.portal.service.RoleLocalServiceUtil;
041 import com.liferay.portal.service.ServiceContext;
042 import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
043 import com.liferay.portal.service.UserLocalServiceUtil;
044 import com.liferay.portal.util.PortletKeys;
045 import com.liferay.portal.util.PrefsPropsUtil;
046 import com.liferay.portal.util.SubscriptionSender;
047
048 import java.io.IOException;
049
050 import java.util.ArrayList;
051 import java.util.Date;
052 import java.util.List;
053 import java.util.Locale;
054
055
060 public class FlagsRequestMessageListener extends BaseMessageListener {
061
062 @Override
063 protected void doReceive(Message message) throws Exception {
064 FlagsRequest flagsRequest = (FlagsRequest)message.getPayload();
065
066
067
068 ServiceContext serviceContext = flagsRequest.getServiceContext();
069
070
071
072 long companyId = serviceContext.getCompanyId();
073
074 Company company = CompanyLocalServiceUtil.getCompany(
075 serviceContext.getCompanyId());
076
077
078
079 Layout layout = LayoutLocalServiceUtil.getLayout(
080 serviceContext.getPlid());
081
082 Group group = layout.getGroup();
083
084 String groupName = HtmlUtil.escape(group.getDescriptiveName());
085
086
087
088 String reporterUserName = null;
089 String reporterEmailAddress = null;
090
091 User reporterUser = UserLocalServiceUtil.getUserById(
092 serviceContext.getUserId());
093
094 Locale locale = LocaleUtil.getDefault();
095
096 if (reporterUser.isDefaultUser()) {
097 reporterUserName = LanguageUtil.get(locale, "anonymous");
098 }
099 else {
100 reporterUserName = reporterUser.getFullName();
101 reporterEmailAddress = reporterUser.getEmailAddress();
102 }
103
104
105
106 String reportedUserName = StringPool.BLANK;
107 String reportedEmailAddress = StringPool.BLANK;
108 String reportedURL = StringPool.BLANK;
109
110 User reportedUser = UserLocalServiceUtil.getUserById(
111 flagsRequest.getReportedUserId());
112
113 if (reportedUser.isDefaultUser()) {
114 reportedUserName = HtmlUtil.escape(group.getDescriptiveName());
115 }
116 else {
117 reportedUserName = HtmlUtil.escape(reportedUser.getFullName());
118 reportedEmailAddress = reportedUser.getEmailAddress();
119 reportedURL = reportedUser.getDisplayURL(
120 serviceContext.getPortalURL(), serviceContext.getPathMain());
121 }
122
123
124
125 String contentType = ResourceActionsUtil.getModelResource(
126 locale, flagsRequest.getClassName());
127
128
129
130 String reason = LanguageUtil.get(locale, flagsRequest.getReason());
131
132
133
134 String fromName = PrefsPropsUtil.getStringFromNames(
135 companyId, PropsKeys.FLAGS_EMAIL_FROM_NAME,
136 PropsKeys.ADMIN_EMAIL_FROM_NAME);
137 String fromAddress = PrefsPropsUtil.getStringFromNames(
138 companyId, PropsKeys.FLAGS_EMAIL_FROM_ADDRESS,
139 PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
140
141 String subject = PrefsPropsUtil.getContent(
142 companyId, PropsKeys.FLAGS_EMAIL_SUBJECT);
143 String body = PrefsPropsUtil.getContent(
144 companyId, PropsKeys.FLAGS_EMAIL_BODY);
145
146
147
148 List<User> recipients = getRecipients(
149 companyId, serviceContext.getScopeGroupId());
150
151 for (User recipient : recipients) {
152 try {
153 notify(
154 company, groupName, 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 List<User> getRecipients(long companyId, long groupId)
170 throws PortalException, SystemException {
171
172 List<User> recipients = new UniqueList<User>();
173
174 List<String> roleNames = new ArrayList<String>();
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 Company company, String groupName, String reporterEmailAddress,
216 String reporterUserName, String reportedEmailAddress,
217 String reportedUserName, String reportedUserURL, long contentId,
218 String contentTitle, String contentType, String contentURL,
219 String reason, String fromName, String fromAddress, String toName,
220 String toAddress, String subject, String body,
221 ServiceContext serviceContext)
222 throws Exception {
223
224 Date now = new Date();
225
226 SubscriptionSender subscriptionSender = new SubscriptionSender();
227
228 subscriptionSender.setBody(body);
229 subscriptionSender.setCompanyId(company.getCompanyId());
230 subscriptionSender.setContextAttributes(
231 "[$CONTENT_ID$]", contentId, "[$CONTENT_TITLE$]", contentTitle,
232 "[$CONTENT_TYPE$]", contentType, "[$CONTENT_URL$]", contentURL,
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.setFrom(fromAddress, fromName);
240 subscriptionSender.setHtmlFormat(true);
241 subscriptionSender.setMailId("flags_request", contentId);
242 subscriptionSender.setPortletId(PortletKeys.FLAGS);
243 subscriptionSender.setServiceContext(serviceContext);
244 subscriptionSender.setSubject(subject);
245
246 subscriptionSender.addRuntimeSubscribers(toAddress, toName);
247
248 subscriptionSender.flushNotificationsAsync();
249 }
250
251 private static Log _log = LogFactoryUtil.getLog(
252 FlagsRequestMessageListener.class);
253
254 }