1
14
15 package com.liferay.portlet.flags.messaging;
16
17 import com.liferay.mail.service.MailServiceUtil;
18 import com.liferay.portal.kernel.exception.PortalException;
19 import com.liferay.portal.kernel.exception.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
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
79 ServiceContext serviceContext = flagsRequest.getServiceContext();
80
81
83 long companyId = serviceContext.getCompanyId();
84
85 Company company = CompanyLocalServiceUtil.getCompany(
86 serviceContext.getCompanyId());
87
88
90 Layout layout = LayoutLocalServiceUtil.getLayout(
91 serviceContext.getPlid());
92
93 Group group = layout.getGroup();
94
95 String groupName = group.getDescriptiveName();
96
97
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
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
136 String contentType = LanguageUtil.get(
137 locale, "model.resource." + flagsRequest.getClassName());
138
139
141 String reason = LanguageUtil.get(locale, flagsRequest.getReason());
142
143
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
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.isCompany()) {
191 roleNames.add(RoleConstants.ADMINISTRATOR);
192 }
193 else if (group.isOrganization()) {
194 roleNames.add(RoleConstants.ORGANIZATION_ADMINISTRATOR);
195 roleNames.add(RoleConstants.ORGANIZATION_OWNER);
196 }
197
198 for (String roleName : roleNames) {
199 Role role = RoleLocalServiceUtil.getRole(companyId, roleName);
200
201 List<UserGroupRole> userGroupRoles =
202 UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(
203 groupId, role.getRoleId());
204
205 for (UserGroupRole userGroupRole : userGroupRoles) {
206 recipients.add(userGroupRole.getUser());
207 }
208 }
209
210 if (recipients.isEmpty()) {
211 Role role = RoleLocalServiceUtil.getRole(
212 companyId, RoleConstants.ADMINISTRATOR);
213
214 recipients.addAll(
215 UserLocalServiceUtil.getRoleUsers(role.getRoleId()));
216 }
217
218 return recipients;
219 }
220
221 protected void notify(
222 Company company, String groupName, String reporterEmailAddress,
223 String reporterUserName, String reportedEmailAddress,
224 String reportedUserName, String reportedUserURL, long contentId,
225 String contentTitle, String contentType, String contentURL,
226 String reason, String fromName, String fromAddress, String toName,
227 String toAddress, String subject, String body,
228 ServiceContext serviceContext)
229 throws IOException {
230
231 Date now = new Date();
232
233 subject = StringUtil.replace(
234 subject,
235 new String[] {
236 "[$COMMUNITY_NAME$]",
237 "[$COMPANY_ID$]",
238 "[$COMPANY_MX$]",
239 "[$COMPANY_NAME$]",
240 "[$CONTENT_ID$]",
241 "[$CONTENT_TITLE$]",
242 "[$CONTENT_TYPE$]",
243 "[$CONTENT_URL$]",
244 "[$DATE$]",
245 "[$FROM_ADDRESS$]",
246 "[$FROM_NAME$]",
247 "[$PORTAL_URL$]",
248 "[$REASON$]",
249 "[$REPORTED_USER_ADDRESS$]",
250 "[$REPORTED_USER_NAME$]",
251 "[$REPORTED_USER_URL$]",
252 "[$REPORTER_USER_ADDRESS$]",
253 "[$REPORTER_USER_NAME$]",
254 "[$TO_ADDRESS$]",
255 "[$TO_NAME$]"
256 },
257 new String[] {
258 groupName,
259 String.valueOf(company.getCompanyId()),
260 company.getMx(),
261 company.getName(),
262 String.valueOf(contentId),
263 contentTitle,
264 contentType,
265 contentURL,
266 now.toString(),
267 fromAddress,
268 fromName,
269 serviceContext.getPortalURL(),
270 reason,
271 reportedEmailAddress,
272 reportedUserName,
273 reportedUserURL,
274 reporterEmailAddress,
275 reporterUserName,
276 toAddress,
277 toName
278 });
279
280 body = StringUtil.replace(
281 body,
282 new String[] {
283 "[$COMMUNITY_NAME$]",
284 "[$COMPANY_ID$]",
285 "[$COMPANY_MX$]",
286 "[$COMPANY_NAME$]",
287 "[$CONTENT_ID$]",
288 "[$CONTENT_TITLE$]",
289 "[$CONTENT_TYPE$]",
290 "[$CONTENT_URL$]",
291 "[$DATE$]",
292 "[$FROM_ADDRESS$]",
293 "[$FROM_NAME$]",
294 "[$PORTAL_URL$]",
295 "[$REASON$]",
296 "[$REPORTED_USER_ADDRESS$]",
297 "[$REPORTED_USER_NAME$]",
298 "[$REPORTED_USER_URL$]",
299 "[$REPORTER_USER_ADDRESS$]",
300 "[$REPORTER_USER_NAME$]",
301 "[$TO_ADDRESS$]",
302 "[$TO_NAME$]"
303 },
304 new String[] {
305 groupName,
306 String.valueOf(company.getCompanyId()),
307 company.getMx(),
308 company.getName(),
309 String.valueOf(contentId),
310 contentTitle,
311 contentType,
312 contentURL,
313 now.toString(),
314 fromAddress,
315 fromName,
316 serviceContext.getPortalURL(),
317 reason,
318 reportedEmailAddress,
319 reportedUserName,
320 reportedUserURL,
321 reporterEmailAddress,
322 reporterUserName,
323 toAddress,
324 toName
325 });
326
327 InternetAddress from = new InternetAddress(fromAddress, fromName);
328
329 InternetAddress to = new InternetAddress(toAddress, toName);
330
331 MailMessage message = new MailMessage(from, to, subject, body, true);
332
333 MailServiceUtil.sendEmail(message);
334 }
335
336 private static Log _log = LogFactoryUtil.getLog(
337 FlagsRequestMessageListener.class);
338
339 }