1
22
23 package com.liferay.portlet.flags.messaging;
24
25 import com.liferay.mail.service.MailServiceUtil;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.kernel.language.LanguageUtil;
29 import com.liferay.portal.kernel.log.Log;
30 import com.liferay.portal.kernel.log.LogFactoryUtil;
31 import com.liferay.portal.kernel.mail.MailMessage;
32 import com.liferay.portal.kernel.messaging.Message;
33 import com.liferay.portal.kernel.messaging.MessageListener;
34 import com.liferay.portal.kernel.util.LocaleUtil;
35 import com.liferay.portal.kernel.util.PropsKeys;
36 import com.liferay.portal.kernel.util.StringPool;
37 import com.liferay.portal.kernel.util.StringUtil;
38 import com.liferay.portal.model.Company;
39 import com.liferay.portal.model.Group;
40 import com.liferay.portal.model.Layout;
41 import com.liferay.portal.model.Role;
42 import com.liferay.portal.model.RoleConstants;
43 import com.liferay.portal.model.User;
44 import com.liferay.portal.model.UserGroupRole;
45 import com.liferay.portal.service.CompanyLocalServiceUtil;
46 import com.liferay.portal.service.GroupLocalServiceUtil;
47 import com.liferay.portal.service.LayoutLocalServiceUtil;
48 import com.liferay.portal.service.RoleLocalServiceUtil;
49 import com.liferay.portal.service.ServiceContext;
50 import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
51 import com.liferay.portal.service.UserLocalServiceUtil;
52 import com.liferay.portal.util.PrefsPropsUtil;
53 import com.liferay.util.UniqueList;
54
55 import java.io.IOException;
56
57 import java.util.ArrayList;
58 import java.util.Date;
59 import java.util.List;
60 import java.util.Locale;
61
62 import javax.mail.internet.InternetAddress;
63
64
71 public class FlagsRequestMessageListener implements MessageListener {
72
73 public void receive(Message message) {
74 try {
75 doReceive(message);
76 }
77 catch (Exception e) {
78 _log.error("Unable to process message " + message, e);
79 }
80 }
81
82 protected void doReceive(Message message) throws Exception {
83 FlagsRequest flagsRequest = (FlagsRequest)message.getPayload();
84
85
87 ServiceContext serviceContext = flagsRequest.getServiceContext();
88
89
91 long companyId = serviceContext.getCompanyId();
92
93 Company company = CompanyLocalServiceUtil.getCompany(
94 serviceContext.getCompanyId());
95
96
98 Layout layout = LayoutLocalServiceUtil.getLayout(
99 serviceContext.getPlid());
100
101 Group group = layout.getGroup();
102
103 String groupName = group.getDescriptiveName();
104
105
107 String reporterUserName = null;
108 String reporterEmailAddress = null;
109
110 User reporterUser = UserLocalServiceUtil.getUserById(
111 serviceContext.getUserId());
112
113 Locale locale = LocaleUtil.getDefault();
114
115 if (reporterUser.isDefaultUser()) {
116 reporterUserName = LanguageUtil.get(locale, "anonymous");
117 }
118 else {
119 reporterUserName = reporterUser.getFullName();
120 reporterEmailAddress = reporterUser.getEmailAddress();
121 }
122
123
125 String reportedUserName = StringPool.BLANK;
126 String reportedEmailAddress = StringPool.BLANK;
127 String reportedURL = StringPool.BLANK;
128
129 User reportedUser = UserLocalServiceUtil.getUserById(
130 flagsRequest.getReportedUserId());
131
132 if (reportedUser.isDefaultUser()) {
133 reportedUserName = group.getDescriptiveName();
134 }
135 else {
136 reportedUserName = reportedUser.getFullName();
137 reportedEmailAddress = reportedUser.getEmailAddress();
138 reportedURL = reportedUser.getDisplayURL(
139 serviceContext.getPortalURL(), serviceContext.getPathMain());
140 }
141
142
144 String contentType = LanguageUtil.get(
145 locale, "model.resource." + flagsRequest.getClassName());
146
147
149 String reason = LanguageUtil.get(locale, flagsRequest.getReason());
150
151
153 String fromName = PrefsPropsUtil.getString(
154 companyId, PropsKeys.FLAGS_EMAIL_FROM_NAME);
155 String fromAddress = PrefsPropsUtil.getString(
156 companyId, PropsKeys.FLAGS_EMAIL_FROM_ADDRESS);
157 String subject = PrefsPropsUtil.getContent(
158 companyId, PropsKeys.FLAGS_EMAIL_SUBJECT);
159 String body = PrefsPropsUtil.getContent(
160 companyId, PropsKeys.FLAGS_EMAIL_BODY);
161
162
164 List<User> recipients = getRecipients(
165 companyId, serviceContext.getScopeGroupId());
166
167 for (User recipient : recipients) {
168 try {
169 notify(
170 company, groupName, reporterEmailAddress, reporterUserName,
171 reportedEmailAddress, reportedUserName, reportedURL,
172 flagsRequest.getClassPK(), flagsRequest.getContentTitle(),
173 contentType, flagsRequest.getContentURL(), reason,
174 fromName, fromAddress, recipient.getFullName(),
175 recipient.getEmailAddress(), subject, body, serviceContext);
176 }
177 catch (IOException ioe) {
178 if (_log.isWarnEnabled()) {
179 _log.warn(ioe);
180 }
181 }
182 }
183 }
184
185 protected List<User> getRecipients(long companyId, long groupId)
186 throws PortalException, SystemException {
187
188 List<User> recipients = new UniqueList<User>();
189
190 List<String> roleNames = new ArrayList<String>();
191
192 Group group = GroupLocalServiceUtil.getGroup(groupId);
193
194 if (group.isCommunity()) {
195 roleNames.add(RoleConstants.COMMUNITY_ADMINISTRATOR);
196 roleNames.add(RoleConstants.COMMUNITY_OWNER);
197 }
198 else if (group.isOrganization()) {
199 roleNames.add(RoleConstants.ORGANIZATION_ADMINISTRATOR);
200 roleNames.add(RoleConstants.ORGANIZATION_OWNER);
201 }
202
203 for (String roleName : roleNames) {
204 Role role = RoleLocalServiceUtil.getRole(companyId, roleName);
205
206 List<UserGroupRole> userGroupRoles =
207 UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(
208 groupId, role.getRoleId());
209
210 for (UserGroupRole userGroupRole : userGroupRoles) {
211 recipients.add(userGroupRole.getUser());
212 }
213 }
214
215 if (recipients.isEmpty()) {
216 Role role = RoleLocalServiceUtil.getRole(
217 companyId, RoleConstants.ADMINISTRATOR);
218
219 recipients.addAll(
220 UserLocalServiceUtil.getRoleUsers(role.getRoleId()));
221 }
222
223 return recipients;
224 }
225
226 protected void notify(
227 Company company, String groupName, String reporterEmailAddress,
228 String reporterUserName, String reportedEmailAddress,
229 String reportedUserName, String reportedUserURL, long contentId,
230 String contentTitle, String contentType, String contentURL,
231 String reason, String fromName, String fromAddress, String toName,
232 String toAddress, String subject, String body,
233 ServiceContext serviceContext)
234 throws IOException {
235
236 Date now = new Date();
237
238 subject = StringUtil.replace(
239 subject,
240 new String[] {
241 "[$COMMUNITY_NAME$]",
242 "[$COMPANY_ID$]",
243 "[$COMPANY_MX$]",
244 "[$COMPANY_NAME$]",
245 "[$CONTENT_ID$]",
246 "[$CONTENT_TITLE$]",
247 "[$CONTENT_TYPE$]",
248 "[$CONTENT_URL$]",
249 "[$DATE$]",
250 "[$FROM_ADDRESS$]",
251 "[$FROM_NAME$]",
252 "[$PORTAL_URL$]",
253 "[$REASON$]",
254 "[$REPORTED_USER_ADDRESS$]",
255 "[$REPORTED_USER_NAME$]",
256 "[$REPORTED_USER_URL$]",
257 "[$REPORTER_USER_ADDRESS$]",
258 "[$REPORTER_USER_NAME$]",
259 "[$TO_ADDRESS$]",
260 "[$TO_NAME$]"
261 },
262 new String[] {
263 groupName,
264 String.valueOf(company.getCompanyId()),
265 company.getMx(),
266 company.getName(),
267 String.valueOf(contentId),
268 contentTitle,
269 contentType,
270 contentURL,
271 now.toString(),
272 fromAddress,
273 fromName,
274 serviceContext.getPortalURL(),
275 reason,
276 reportedEmailAddress,
277 reportedUserName,
278 reportedUserURL,
279 reporterEmailAddress,
280 reporterUserName,
281 toAddress,
282 toName
283 });
284
285 body = StringUtil.replace(
286 body,
287 new String[] {
288 "[$COMMUNITY_NAME$]",
289 "[$COMPANY_ID$]",
290 "[$COMPANY_MX$]",
291 "[$COMPANY_NAME$]",
292 "[$CONTENT_ID$]",
293 "[$CONTENT_TITLE$]",
294 "[$CONTENT_TYPE$]",
295 "[$CONTENT_URL$]",
296 "[$DATE$]",
297 "[$FROM_ADDRESS$]",
298 "[$FROM_NAME$]",
299 "[$PORTAL_URL$]",
300 "[$REASON$]",
301 "[$REPORTED_USER_ADDRESS$]",
302 "[$REPORTED_USER_NAME$]",
303 "[$REPORTED_USER_URL$]",
304 "[$REPORTER_USER_ADDRESS$]",
305 "[$REPORTER_USER_NAME$]",
306 "[$TO_ADDRESS$]",
307 "[$TO_NAME$]"
308 },
309 new String[] {
310 groupName,
311 String.valueOf(company.getCompanyId()),
312 company.getMx(),
313 company.getName(),
314 String.valueOf(contentId),
315 contentTitle,
316 contentType,
317 contentURL,
318 now.toString(),
319 fromAddress,
320 fromName,
321 serviceContext.getPortalURL(),
322 reason,
323 reportedEmailAddress,
324 reportedUserName,
325 reportedUserURL,
326 reporterEmailAddress,
327 reporterUserName,
328 toAddress,
329 toName
330 });
331
332 InternetAddress from = new InternetAddress(fromAddress, fromName);
333
334 InternetAddress to = new InternetAddress(toAddress, toName);
335
336 MailMessage message = new MailMessage(from, to, subject, body, true);
337
338 MailServiceUtil.sendEmail(message);
339 }
340
341 private static Log _log =
342 LogFactoryUtil.getLog(FlagsRequestMessageListener.class);
343
344 }