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