1
22
23 package com.liferay.portlet.flags.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.language.LanguageUtil;
28 import com.liferay.portal.kernel.log.Log;
29 import com.liferay.portal.kernel.log.LogFactoryUtil;
30 import com.liferay.portal.kernel.mail.MailMessage;
31 import com.liferay.portal.kernel.util.LocaleUtil;
32 import com.liferay.portal.kernel.util.PropsKeys;
33 import com.liferay.portal.kernel.util.StringPool;
34 import com.liferay.portal.kernel.util.StringUtil;
35 import com.liferay.portal.model.Company;
36 import com.liferay.portal.model.Group;
37 import com.liferay.portal.model.Layout;
38 import com.liferay.portal.model.Role;
39 import com.liferay.portal.model.RoleConstants;
40 import com.liferay.portal.model.User;
41 import com.liferay.portal.model.UserGroupRole;
42 import com.liferay.portal.service.ServiceContext;
43 import com.liferay.portal.util.PrefsPropsUtil;
44 import com.liferay.portlet.flags.service.base.FlagsEntryServiceBaseImpl;
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
61 public class FlagsEntryServiceImpl extends FlagsEntryServiceBaseImpl {
62
63 public void addEntry(
64 String className, long classPK, String reporterEmailAddress,
65 long reportedUserId, String contentTitle, String contentURL,
66 String reason, ServiceContext serviceContext)
67 throws PortalException, SystemException {
68
69
71 long companyId = serviceContext.getCompanyId();
72
73 Company company = companyPersistence.findByPrimaryKey(
74 serviceContext.getCompanyId());
75
76
78 Layout layout = layoutPersistence.findByPrimaryKey(
79 serviceContext.getPlid());
80
81 Group group = layout.getGroup();
82
83 String groupName = group.getDescriptiveName();
84
85
87 String reporterUserName = null;
88
89 User reporterUser = getUser();
90
91 Locale locale = LocaleUtil.getDefault();
92
93 if (reporterUser.isDefaultUser()) {
94 reporterUserName = LanguageUtil.get(locale, "anonymous");
95 }
96 else {
97 reporterUserName = reporterUser.getFullName();
98 reporterEmailAddress = reporterUser.getEmailAddress();
99 }
100
101
103 String reportedUserName = StringPool.BLANK;
104 String reportedEmailAddress = StringPool.BLANK;
105 String reportedURL = StringPool.BLANK;
106
107 User reportedUser = userPersistence.findByPrimaryKey(reportedUserId);
108
109 if (reportedUser.isDefaultUser()) {
110 reportedUserName = group.getDescriptiveName();
111 }
112 else {
113 reportedUserName = reportedUser.getFullName();
114 reportedEmailAddress = reportedUser.getEmailAddress();
115 reportedURL = reportedUser.getDisplayURL(
116 serviceContext.getPortalURL(), serviceContext.getPathMain());
117 }
118
119
121 String contentType = LanguageUtil.get(
122 locale, "model.resource." + className);
123
124
126 reason = LanguageUtil.get(locale, reason);
127
128
130 String fromName = PrefsPropsUtil.getString(
131 companyId, PropsKeys.FLAGS_EMAIL_FROM_NAME);
132 String fromAddress = PrefsPropsUtil.getString(
133 companyId, PropsKeys.FLAGS_EMAIL_FROM_ADDRESS);
134 String subject = PrefsPropsUtil.getContent(
135 companyId, PropsKeys.FLAGS_EMAIL_SUBJECT);
136 String body = PrefsPropsUtil.getContent(
137 companyId, PropsKeys.FLAGS_EMAIL_BODY);
138
139
141 List<User> recipients = getRecipients(
142 companyId, serviceContext.getScopeGroupId());
143
144 for (User recipient : recipients) {
145 try {
146 notify(
147 company, groupName, reporterEmailAddress, reporterUserName,
148 reportedEmailAddress, reportedUserName, reportedURL,
149 classPK, contentTitle, contentType, contentURL, reason,
150 fromName, fromAddress, recipient.getFullName(),
151 recipient.getEmailAddress(), subject, body, serviceContext);
152 }
153 catch (IOException ioe) {
154 if (_log.isWarnEnabled()) {
155 _log.warn(ioe);
156 }
157 }
158 }
159 }
160
161 protected List<User> getRecipients(long companyId, long groupId)
162 throws PortalException, SystemException {
163
164 List<User> recipients = new UniqueList<User>();
165
166 List<String> roleNames = new ArrayList<String>();
167
168 Group group = groupLocalService.getGroup(groupId);
169
170 if (group.isCommunity()) {
171 roleNames.add(RoleConstants.COMMUNITY_ADMINISTRATOR);
172 roleNames.add(RoleConstants.COMMUNITY_OWNER);
173 }
174 else if (group.isOrganization()) {
175 roleNames.add(RoleConstants.ORGANIZATION_ADMINISTRATOR);
176 roleNames.add(RoleConstants.ORGANIZATION_OWNER);
177 }
178
179 for (String roleName : roleNames) {
180 Role role = roleLocalService.getRole(companyId, roleName);
181
182 List<UserGroupRole> userGroupRoles =
183 userGroupRoleLocalService.getUserGroupRolesByGroupAndRole(
184 groupId, role.getRoleId());
185
186 for (UserGroupRole userGroupRole : userGroupRoles) {
187 recipients.add(userGroupRole.getUser());
188 }
189 }
190
191 if (recipients.isEmpty()) {
192 Role role = roleLocalService.getRole(
193 companyId, RoleConstants.ADMINISTRATOR);
194
195 recipients.addAll(userLocalService.getRoleUsers(role.getRoleId()));
196 }
197
198 return recipients;
199 }
200
201 protected void notify(
202 Company company, String groupName, String reporterEmailAddress,
203 String reporterUserName, String reportedEmailAddress,
204 String reportedUserName, String reportedUserURL, long contentId,
205 String contentTitle, String contentType, String contentURL,
206 String reason, String fromName, String fromAddress, String toName,
207 String toAddress, String subject, String body,
208 ServiceContext serviceContext)
209 throws IOException {
210
211 Date now = new Date();
212
213 subject = StringUtil.replace(
214 subject,
215 new String[] {
216 "[$COMMUNITY_NAME$]",
217 "[$COMPANY_ID$]",
218 "[$COMPANY_MX$]",
219 "[$COMPANY_NAME$]",
220 "[$CONTENT_ID$]",
221 "[$CONTENT_TITLE$]",
222 "[$CONTENT_TYPE$]",
223 "[$CONTENT_URL$]",
224 "[$DATE$]",
225 "[$FROM_ADDRESS$]",
226 "[$FROM_NAME$]",
227 "[$PORTAL_URL$]",
228 "[$REASON$]",
229 "[$REPORTED_USER_ADDRESS$]",
230 "[$REPORTED_USER_NAME$]",
231 "[$REPORTED_USER_URL$]",
232 "[$REPORTER_USER_ADDRESS$]",
233 "[$REPORTER_USER_NAME$]",
234 "[$TO_ADDRESS$]",
235 "[$TO_NAME$]"
236 },
237 new String[] {
238 groupName,
239 String.valueOf(company.getCompanyId()),
240 company.getMx(),
241 company.getName(),
242 String.valueOf(contentId),
243 contentTitle,
244 contentType,
245 contentURL,
246 now.toString(),
247 fromAddress,
248 fromName,
249 serviceContext.getPortalURL(),
250 reason,
251 reportedEmailAddress,
252 reportedUserName,
253 reportedUserURL,
254 reporterEmailAddress,
255 reporterUserName,
256 toAddress,
257 toName
258 });
259
260 body = StringUtil.replace(
261 body,
262 new String[] {
263 "[$COMMUNITY_NAME$]",
264 "[$COMPANY_ID$]",
265 "[$COMPANY_MX$]",
266 "[$COMPANY_NAME$]",
267 "[$CONTENT_ID$]",
268 "[$CONTENT_TITLE$]",
269 "[$CONTENT_TYPE$]",
270 "[$CONTENT_URL$]",
271 "[$DATE$]",
272 "[$FROM_ADDRESS$]",
273 "[$FROM_NAME$]",
274 "[$PORTAL_URL$]",
275 "[$REASON$]",
276 "[$REPORTED_USER_ADDRESS$]",
277 "[$REPORTED_USER_NAME$]",
278 "[$REPORTED_USER_URL$]",
279 "[$REPORTER_USER_ADDRESS$]",
280 "[$REPORTER_USER_NAME$]",
281 "[$TO_ADDRESS$]",
282 "[$TO_NAME$]"
283 },
284 new String[] {
285 groupName,
286 String.valueOf(company.getCompanyId()),
287 company.getMx(),
288 company.getName(),
289 String.valueOf(contentId),
290 contentTitle,
291 contentType,
292 contentURL,
293 now.toString(),
294 fromAddress,
295 fromName,
296 serviceContext.getPortalURL(),
297 reason,
298 reportedEmailAddress,
299 reportedUserName,
300 reportedUserURL,
301 reporterEmailAddress,
302 reporterUserName,
303 toAddress,
304 toName
305 });
306
307 InternetAddress from = new InternetAddress(fromAddress, fromName);
308
309 InternetAddress to = new InternetAddress(toAddress, toName);
310
311 MailMessage message = new MailMessage(from, to, subject, body, true);
312
313 mailService.sendEmail(message);
314 }
315
316 private static Log _log =
317 LogFactoryUtil.getLog(FlagsEntryServiceImpl.class);
318
319 }