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