001
014
015 package com.liferay.portlet.flags.messaging;
016
017 import com.liferay.mail.service.MailServiceUtil;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.language.LanguageUtil;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.mail.MailMessage;
024 import com.liferay.portal.kernel.messaging.Message;
025 import com.liferay.portal.kernel.messaging.MessageListener;
026 import com.liferay.portal.kernel.util.HtmlUtil;
027 import com.liferay.portal.kernel.util.LocaleUtil;
028 import com.liferay.portal.kernel.util.PropsKeys;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.model.Company;
032 import com.liferay.portal.model.Group;
033 import com.liferay.portal.model.Layout;
034 import com.liferay.portal.model.Role;
035 import com.liferay.portal.model.RoleConstants;
036 import com.liferay.portal.model.User;
037 import com.liferay.portal.model.UserGroupRole;
038 import com.liferay.portal.service.CompanyLocalServiceUtil;
039 import com.liferay.portal.service.GroupLocalServiceUtil;
040 import com.liferay.portal.service.LayoutLocalServiceUtil;
041 import com.liferay.portal.service.RoleLocalServiceUtil;
042 import com.liferay.portal.service.ServiceContext;
043 import com.liferay.portal.service.UserGroupRoleLocalServiceUtil;
044 import com.liferay.portal.service.UserLocalServiceUtil;
045 import com.liferay.portal.util.PrefsPropsUtil;
046 import com.liferay.util.UniqueList;
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 import javax.mail.internet.InternetAddress;
056
057
062 public class FlagsRequestMessageListener implements MessageListener {
063
064 public void receive(Message message) {
065 try {
066 doReceive(message);
067 }
068 catch (Exception e) {
069 _log.error("Unable to process message " + message, e);
070 }
071 }
072
073 protected void doReceive(Message message) throws Exception {
074 FlagsRequest flagsRequest = (FlagsRequest)message.getPayload();
075
076
077
078 ServiceContext serviceContext = flagsRequest.getServiceContext();
079
080
081
082 long companyId = serviceContext.getCompanyId();
083
084 Company company = CompanyLocalServiceUtil.getCompany(
085 serviceContext.getCompanyId());
086
087
088
089 Layout layout = LayoutLocalServiceUtil.getLayout(
090 serviceContext.getPlid());
091
092 Group group = layout.getGroup();
093
094 String groupName = HtmlUtil.escape(group.getDescriptiveName());
095
096
097
098 String reporterUserName = null;
099 String reporterEmailAddress = null;
100
101 User reporterUser = UserLocalServiceUtil.getUserById(
102 serviceContext.getUserId());
103
104 Locale locale = LocaleUtil.getDefault();
105
106 if (reporterUser.isDefaultUser()) {
107 reporterUserName = LanguageUtil.get(locale, "anonymous");
108 }
109 else {
110 reporterUserName = reporterUser.getFullName();
111 reporterEmailAddress = reporterUser.getEmailAddress();
112 }
113
114
115
116 String reportedUserName = StringPool.BLANK;
117 String reportedEmailAddress = StringPool.BLANK;
118 String reportedURL = StringPool.BLANK;
119
120 User reportedUser = UserLocalServiceUtil.getUserById(
121 flagsRequest.getReportedUserId());
122
123 if (reportedUser.isDefaultUser()) {
124 reportedUserName = HtmlUtil.escape(group.getDescriptiveName());
125 }
126 else {
127 reportedUserName = HtmlUtil.escape(reportedUser.getFullName());
128 reportedEmailAddress = reportedUser.getEmailAddress();
129 reportedURL = reportedUser.getDisplayURL(
130 serviceContext.getPortalURL(), serviceContext.getPathMain());
131 }
132
133
134
135 String contentType = LanguageUtil.get(
136 locale, "model.resource." + flagsRequest.getClassName());
137
138
139
140 String reason = LanguageUtil.get(locale, flagsRequest.getReason());
141
142
143
144 String fromName = PrefsPropsUtil.getString(
145 companyId, PropsKeys.FLAGS_EMAIL_FROM_NAME);
146 String fromAddress = PrefsPropsUtil.getString(
147 companyId, PropsKeys.FLAGS_EMAIL_FROM_ADDRESS);
148 String subject = PrefsPropsUtil.getContent(
149 companyId, PropsKeys.FLAGS_EMAIL_SUBJECT);
150 String body = PrefsPropsUtil.getContent(
151 companyId, PropsKeys.FLAGS_EMAIL_BODY);
152
153
154
155 List<User> recipients = getRecipients(
156 companyId, serviceContext.getScopeGroupId());
157
158 for (User recipient : recipients) {
159 try {
160 notify(
161 company, groupName, reporterEmailAddress, reporterUserName,
162 reportedEmailAddress, reportedUserName, reportedURL,
163 flagsRequest.getClassPK(), flagsRequest.getContentTitle(),
164 contentType, flagsRequest.getContentURL(), reason,
165 fromName, fromAddress, recipient.getFullName(),
166 recipient.getEmailAddress(), subject, body, serviceContext);
167 }
168 catch (IOException ioe) {
169 if (_log.isWarnEnabled()) {
170 _log.warn(ioe);
171 }
172 }
173 }
174 }
175
176 protected List<User> getRecipients(long companyId, long groupId)
177 throws PortalException, SystemException {
178
179 List<User> recipients = new UniqueList<User>();
180
181 List<String> roleNames = new ArrayList<String>();
182
183 Group group = GroupLocalServiceUtil.getGroup(groupId);
184
185 if (group.isCommunity()) {
186 roleNames.add(RoleConstants.COMMUNITY_ADMINISTRATOR);
187 roleNames.add(RoleConstants.COMMUNITY_OWNER);
188 }
189 else if (group.isCompany()) {
190 roleNames.add(RoleConstants.ADMINISTRATOR);
191 }
192 else if (group.isOrganization()) {
193 roleNames.add(RoleConstants.ORGANIZATION_ADMINISTRATOR);
194 roleNames.add(RoleConstants.ORGANIZATION_OWNER);
195 }
196
197 for (String roleName : roleNames) {
198 Role role = RoleLocalServiceUtil.getRole(companyId, roleName);
199
200 List<UserGroupRole> userGroupRoles =
201 UserGroupRoleLocalServiceUtil.getUserGroupRolesByGroupAndRole(
202 groupId, role.getRoleId());
203
204 for (UserGroupRole userGroupRole : userGroupRoles) {
205 recipients.add(userGroupRole.getUser());
206 }
207 }
208
209 if (recipients.isEmpty()) {
210 Role role = RoleLocalServiceUtil.getRole(
211 companyId, RoleConstants.ADMINISTRATOR);
212
213 recipients.addAll(
214 UserLocalServiceUtil.getRoleUsers(role.getRoleId()));
215 }
216
217 return recipients;
218 }
219
220 protected void notify(
221 Company company, String groupName, String reporterEmailAddress,
222 String reporterUserName, String reportedEmailAddress,
223 String reportedUserName, String reportedUserURL, long contentId,
224 String contentTitle, String contentType, String contentURL,
225 String reason, String fromName, String fromAddress, String toName,
226 String toAddress, String subject, String body,
227 ServiceContext serviceContext)
228 throws Exception {
229
230 Date now = new Date();
231
232 subject = StringUtil.replace(
233 subject,
234 new String[] {
235 "[$COMMUNITY_NAME$]",
236 "[$COMPANY_ID$]",
237 "[$COMPANY_MX$]",
238 "[$COMPANY_NAME$]",
239 "[$CONTENT_ID$]",
240 "[$CONTENT_TITLE$]",
241 "[$CONTENT_TYPE$]",
242 "[$CONTENT_URL$]",
243 "[$DATE$]",
244 "[$FROM_ADDRESS$]",
245 "[$FROM_NAME$]",
246 "[$PORTAL_URL$]",
247 "[$REASON$]",
248 "[$REPORTED_USER_ADDRESS$]",
249 "[$REPORTED_USER_NAME$]",
250 "[$REPORTED_USER_URL$]",
251 "[$REPORTER_USER_ADDRESS$]",
252 "[$REPORTER_USER_NAME$]",
253 "[$TO_ADDRESS$]",
254 "[$TO_NAME$]"
255 },
256 new String[] {
257 groupName,
258 String.valueOf(company.getCompanyId()),
259 company.getMx(),
260 company.getName(),
261 String.valueOf(contentId),
262 contentTitle,
263 contentType,
264 contentURL,
265 now.toString(),
266 fromAddress,
267 fromName,
268 serviceContext.getPortalURL(),
269 reason,
270 reportedEmailAddress,
271 reportedUserName,
272 reportedUserURL,
273 reporterEmailAddress,
274 reporterUserName,
275 toAddress,
276 toName
277 });
278
279 body = StringUtil.replace(
280 body,
281 new String[] {
282 "[$COMMUNITY_NAME$]",
283 "[$COMPANY_ID$]",
284 "[$COMPANY_MX$]",
285 "[$COMPANY_NAME$]",
286 "[$CONTENT_ID$]",
287 "[$CONTENT_TITLE$]",
288 "[$CONTENT_TYPE$]",
289 "[$CONTENT_URL$]",
290 "[$DATE$]",
291 "[$FROM_ADDRESS$]",
292 "[$FROM_NAME$]",
293 "[$PORTAL_URL$]",
294 "[$REASON$]",
295 "[$REPORTED_USER_ADDRESS$]",
296 "[$REPORTED_USER_NAME$]",
297 "[$REPORTED_USER_URL$]",
298 "[$REPORTER_USER_ADDRESS$]",
299 "[$REPORTER_USER_NAME$]",
300 "[$TO_ADDRESS$]",
301 "[$TO_NAME$]"
302 },
303 new String[] {
304 groupName,
305 String.valueOf(company.getCompanyId()),
306 company.getMx(),
307 company.getName(),
308 String.valueOf(contentId),
309 contentTitle,
310 contentType,
311 contentURL,
312 now.toString(),
313 fromAddress,
314 fromName,
315 serviceContext.getPortalURL(),
316 reason,
317 reportedEmailAddress,
318 reportedUserName,
319 reportedUserURL,
320 reporterEmailAddress,
321 reporterUserName,
322 toAddress,
323 toName
324 });
325
326 InternetAddress from = new InternetAddress(fromAddress, fromName);
327
328 InternetAddress to = new InternetAddress(toAddress, toName);
329
330 MailMessage message = new MailMessage(from, to, subject, body, true);
331
332 MailServiceUtil.sendEmail(message);
333 }
334
335 private static Log _log = LogFactoryUtil.getLog(
336 FlagsRequestMessageListener.class);
337
338 }