001
014
015 package com.liferay.portlet.social.model;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.json.JSONException;
019 import com.liferay.portal.kernel.json.JSONFactoryUtil;
020 import com.liferay.portal.kernel.json.JSONObject;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
024 import com.liferay.portal.kernel.trash.TrashHandler;
025 import com.liferay.portal.kernel.trash.TrashHandlerRegistryUtil;
026 import com.liferay.portal.kernel.util.HtmlUtil;
027 import com.liferay.portal.kernel.util.HttpUtil;
028 import com.liferay.portal.kernel.util.StringBundler;
029 import com.liferay.portal.kernel.util.StringPool;
030 import com.liferay.portal.kernel.util.StringUtil;
031 import com.liferay.portal.kernel.util.Validator;
032 import com.liferay.portal.model.Group;
033 import com.liferay.portal.model.User;
034 import com.liferay.portal.security.permission.ActionKeys;
035 import com.liferay.portal.security.permission.PermissionChecker;
036 import com.liferay.portal.service.GroupLocalServiceUtil;
037 import com.liferay.portal.service.ServiceContext;
038 import com.liferay.portal.service.UserLocalServiceUtil;
039 import com.liferay.portal.theme.ThemeDisplay;
040 import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
041 import com.liferay.portlet.asset.model.AssetRenderer;
042 import com.liferay.portlet.asset.model.AssetRendererFactory;
043 import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
044 import com.liferay.portlet.social.service.SocialActivitySetLocalServiceUtil;
045 import com.liferay.portlet.social.service.persistence.SocialActivityUtil;
046 import com.liferay.portlet.trash.util.TrashUtil;
047
048 import java.util.List;
049
050 import javax.portlet.PortletURL;
051 import javax.portlet.WindowState;
052
053
057 public abstract class BaseSocialActivityInterpreter
058 implements SocialActivityInterpreter {
059
060 @Override
061 public String getSelector() {
062 return StringPool.BLANK;
063 }
064
065 @Override
066 public boolean hasPermission(
067 PermissionChecker permissionChecker, SocialActivity activity,
068 String actionId, ServiceContext serviceContext)
069 throws Exception {
070
071 return hasPermissions(
072 permissionChecker, activity, actionId, serviceContext);
073 }
074
075 @Override
076 public SocialActivityFeedEntry interpret(
077 SocialActivity activity, ServiceContext serviceContext) {
078
079 try {
080 return doInterpret(activity, serviceContext);
081 }
082 catch (Exception e) {
083 _log.error("Unable to interpret activity", e);
084 }
085
086 return null;
087 }
088
089 @Override
090 public SocialActivityFeedEntry interpret(
091 SocialActivitySet activitySet, ServiceContext serviceContext) {
092
093 try {
094 return doInterpret(activitySet, serviceContext);
095 }
096 catch (Exception e) {
097 _log.error("Unable to interpret activity set", e);
098 }
099
100 return null;
101 }
102
103 @Override
104 public void updateActivitySet(long activityId) throws PortalException {
105 SocialActivity activity = SocialActivityUtil.fetchByPrimaryKey(
106 activityId);
107
108 if ((activity == null) || (activity.getActivitySetId() > 0)) {
109 return;
110 }
111
112 long activitySetId = getActivitySetId(activityId);
113
114 if (activitySetId > 0) {
115 SocialActivitySetLocalServiceUtil.incrementActivityCount(
116 activitySetId, activityId);
117 }
118 else {
119 SocialActivitySetLocalServiceUtil.addActivitySet(activityId);
120 }
121 }
122
123 protected String addNoSuchEntryRedirect(
124 String url, String className, long classPK,
125 ServiceContext serviceContext)
126 throws Exception {
127
128 PortletURL portletURL = getViewEntryPortletURL(
129 className, classPK, serviceContext);
130
131 if (portletURL == null) {
132 return url;
133 }
134
135 return HttpUtil.setParameter(
136 url, "noSuchEntryRedirect", portletURL.toString());
137 }
138
139 protected String buildLink(String link, String text) {
140 StringBundler sb = new StringBundler(5);
141
142 sb.append("<a href=\"");
143 sb.append(link);
144 sb.append("\">");
145 sb.append(text);
146 sb.append("</a>");
147
148 return sb.toString();
149 }
150
151
154 @Deprecated
155 protected String cleanContent(String content) {
156 return StringUtil.shorten(HtmlUtil.extractText(content), 200);
157 }
158
159 protected SocialActivityFeedEntry doInterpret(
160 SocialActivity activity, ServiceContext serviceContext)
161 throws Exception {
162
163 ThemeDisplay themeDisplay = serviceContext.getThemeDisplay();
164
165 SocialActivityFeedEntry socialActivityFeedEntry = doInterpret(
166 activity, themeDisplay);
167
168 if (socialActivityFeedEntry !=
169 _deprecatedMarkerSocialActivityFeedEntry) {
170
171 return socialActivityFeedEntry;
172 }
173
174 PermissionChecker permissionChecker =
175 themeDisplay.getPermissionChecker();
176
177 if (!hasPermissions(
178 permissionChecker, activity, ActionKeys.VIEW, serviceContext)) {
179
180 return null;
181 }
182
183 String link = getLink(activity, serviceContext);
184
185 String title = getTitle(activity, serviceContext);
186
187 if (Validator.isNull(title)) {
188 return null;
189 }
190
191 String body = getBody(activity, serviceContext);
192
193 return new SocialActivityFeedEntry(link, title, body);
194 }
195
196
199 @Deprecated
200 protected SocialActivityFeedEntry doInterpret(
201 SocialActivity activity, ThemeDisplay themeDisplay)
202 throws Exception {
203
204 return _deprecatedMarkerSocialActivityFeedEntry;
205 }
206
207 protected SocialActivityFeedEntry doInterpret(
208 SocialActivitySet activitySet, ServiceContext serviceContext)
209 throws Exception {
210
211 List<SocialActivity> activities =
212 SocialActivityLocalServiceUtil.getActivitySetActivities(
213 activitySet.getActivitySetId(), 0, 1);
214
215 if (!activities.isEmpty()) {
216 SocialActivity activity = activities.get(0);
217
218 return doInterpret(activity, serviceContext);
219 }
220
221 return null;
222 }
223
224 protected long getActivitySetId(long activityId) {
225 return 0;
226 }
227
228 protected String getBody(
229 SocialActivity activity, ServiceContext serviceContext)
230 throws Exception {
231
232 return StringPool.BLANK;
233 }
234
235 protected String getEntryTitle(
236 SocialActivity activity, ServiceContext serviceContext)
237 throws Exception {
238
239 return activity.getExtraDataValue("title", serviceContext.getLocale());
240 }
241
242 protected String getGroupName(long groupId, ServiceContext serviceContext) {
243 try {
244 if (groupId <= 0) {
245 return StringPool.BLANK;
246 }
247
248 Group group = GroupLocalServiceUtil.getGroup(groupId);
249
250 String groupName = group.getDescriptiveName();
251
252 if (group.getGroupId() == serviceContext.getScopeGroupId()) {
253 return HtmlUtil.escape(groupName);
254 }
255
256 String groupDisplayURL = StringPool.BLANK;
257
258 if (group.hasPublicLayouts()) {
259 groupDisplayURL = group.getDisplayURL(
260 serviceContext.getThemeDisplay(), false);
261 }
262 else if (group.hasPrivateLayouts()) {
263 groupDisplayURL = group.getDisplayURL(
264 serviceContext.getThemeDisplay(), true);
265 }
266 else {
267 return HtmlUtil.escape(groupName);
268 }
269
270 groupName =
271 "<a class=\"group\" href=\"" + groupDisplayURL + "\">" +
272 HtmlUtil.escape(groupName) + "</a>";
273
274 return groupName;
275 }
276 catch (Exception e) {
277 return StringPool.BLANK;
278 }
279 }
280
281
285 @Deprecated
286 protected String getGroupName(long groupId, ThemeDisplay themeDisplay) {
287 try {
288 if (groupId <= 0) {
289 return StringPool.BLANK;
290 }
291
292 Group group = GroupLocalServiceUtil.getGroup(groupId);
293
294 String groupName = group.getDescriptiveName();
295
296 if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
297 return HtmlUtil.escape(groupName);
298 }
299
300 String groupDisplayURL = StringPool.BLANK;
301
302 if (group.hasPublicLayouts()) {
303 groupDisplayURL = group.getDisplayURL(themeDisplay, false);
304 }
305 else if (group.hasPrivateLayouts()) {
306 groupDisplayURL = group.getDisplayURL(themeDisplay, true);
307 }
308 else {
309 return HtmlUtil.escape(groupName);
310 }
311
312 groupName =
313 "<a class=\"group\" href=\"" + groupDisplayURL + "\">" +
314 HtmlUtil.escape(groupName) + "</a>";
315
316 return groupName;
317 }
318 catch (Exception e) {
319 return StringPool.BLANK;
320 }
321 }
322
323 protected String getJSONValue(String json, String key) {
324 return getJSONValue(json, key, StringPool.BLANK);
325 }
326
327 protected String getJSONValue(
328 String json, String key, String defaultValue) {
329
330 if (Validator.isNull(json)) {
331 return defaultValue;
332 }
333
334 try {
335 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(
336 json);
337
338 String value = extraDataJSONObject.getString(key);
339
340 if (Validator.isNotNull(value)) {
341 return value;
342 }
343 }
344 catch (JSONException jsone) {
345 _log.error("Unable to create a JSON object from " + json);
346 }
347
348 return defaultValue;
349 }
350
351 protected String getLink(
352 SocialActivity activity, ServiceContext serviceContext)
353 throws Exception {
354
355 String className = activity.getClassName();
356 long classPK = activity.getClassPK();
357
358 String viewEntryInTrashURL = getViewEntryInTrashURL(
359 className, classPK, serviceContext);
360
361 if (viewEntryInTrashURL != null) {
362 return viewEntryInTrashURL;
363 }
364
365 String path = getPath(activity, serviceContext);
366
367 if (Validator.isNull(path)) {
368 return null;
369 }
370
371 path = addNoSuchEntryRedirect(path, className, classPK, serviceContext);
372
373 if (!path.startsWith(StringPool.SLASH)) {
374 return path;
375 }
376
377 return serviceContext.getPortalURL() + serviceContext.getPathMain() +
378 path;
379 }
380
381 protected String getPath(
382 SocialActivity activity, ServiceContext serviceContext)
383 throws Exception {
384
385 return StringPool.BLANK;
386 }
387
388 protected String getTitle(
389 SocialActivity activity, ServiceContext serviceContext)
390 throws Exception {
391
392 String groupName = StringPool.BLANK;
393
394 if (activity.getGroupId() != serviceContext.getScopeGroupId()) {
395 groupName = getGroupName(activity.getGroupId(), serviceContext);
396 }
397
398 String titlePattern = getTitlePattern(groupName, activity);
399
400 if (Validator.isNull(titlePattern)) {
401 return null;
402 }
403
404 String link = getLink(activity, serviceContext);
405
406 String entryTitle = getEntryTitle(activity, serviceContext);
407
408 Object[] titleArguments = getTitleArguments(
409 groupName, activity, link, entryTitle, serviceContext);
410
411 return serviceContext.translate(titlePattern, titleArguments);
412 }
413
414 protected Object[] getTitleArguments(
415 String groupName, SocialActivity activity, String link,
416 String title, ServiceContext serviceContext)
417 throws Exception {
418
419 String userName = getUserName(activity.getUserId(), serviceContext);
420
421 return new Object[] {groupName, userName, wrapLink(link, title)};
422 }
423
424 protected String getTitlePattern(String groupName, SocialActivity activity)
425 throws Exception {
426
427 return StringPool.BLANK;
428 }
429
430 protected String getUserName(long userId, ServiceContext serviceContext) {
431 try {
432 if (userId <= 0) {
433 return StringPool.BLANK;
434 }
435
436 User user = UserLocalServiceUtil.getUserById(userId);
437
438 if (user.getUserId() == serviceContext.getUserId()) {
439 return HtmlUtil.escape(user.getFirstName());
440 }
441
442 String userName = user.getFullName();
443
444 Group group = user.getGroup();
445
446 if (group.getGroupId() == serviceContext.getScopeGroupId()) {
447 return HtmlUtil.escape(userName);
448 }
449
450 String userDisplayURL = user.getDisplayURL(
451 serviceContext.getThemeDisplay());
452
453 userName =
454 "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
455 HtmlUtil.escape(userName) + "</a>";
456
457 return userName;
458 }
459 catch (Exception e) {
460 return StringPool.BLANK;
461 }
462 }
463
464
468 @Deprecated
469 protected String getUserName(long userId, ThemeDisplay themeDisplay) {
470 try {
471 if (userId <= 0) {
472 return StringPool.BLANK;
473 }
474
475 User user = UserLocalServiceUtil.getUserById(userId);
476
477 if (user.getUserId() == themeDisplay.getUserId()) {
478 return HtmlUtil.escape(user.getFirstName());
479 }
480
481 String userName = user.getFullName();
482
483 Group group = user.getGroup();
484
485 if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
486 return HtmlUtil.escape(userName);
487 }
488
489 String userDisplayURL = user.getDisplayURL(themeDisplay);
490
491 userName =
492 "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
493 HtmlUtil.escape(userName) + "</a>";
494
495 return userName;
496 }
497 catch (Exception e) {
498 return StringPool.BLANK;
499 }
500 }
501
502
506 @Deprecated
507 protected String getValue(String json, String key, String defaultValue) {
508 return getJSONValue(json, key, defaultValue);
509 }
510
511 protected String getViewEntryInTrashURL(
512 String className, long classPK, ServiceContext serviceContext)
513 throws Exception {
514
515 TrashHandler trashHandler = TrashHandlerRegistryUtil.getTrashHandler(
516 className);
517
518 if ((trashHandler != null) && trashHandler.isInTrash(classPK)) {
519 PortletURL portletURL = TrashUtil.getViewContentURL(
520 serviceContext.getRequest(), className, classPK);
521
522 if (portletURL == null) {
523 return null;
524 }
525
526 return portletURL.toString();
527 }
528
529 return null;
530 }
531
532 protected PortletURL getViewEntryPortletURL(
533 String className, long classPK, ServiceContext serviceContext)
534 throws Exception {
535
536 AssetRendererFactory<?> assetRendererFactory =
537 AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
538 className);
539
540 if (assetRendererFactory == null) {
541 return null;
542 }
543
544 LiferayPortletResponse liferayPortletResponse =
545 serviceContext.getLiferayPortletResponse();
546
547 if (liferayPortletResponse == null) {
548 return null;
549 }
550
551 if (classPK == 0) {
552 return assetRendererFactory.getURLView(
553 liferayPortletResponse, WindowState.MAXIMIZED);
554 }
555
556 AssetRenderer<?> assetRenderer = assetRendererFactory.getAssetRenderer(
557 classPK);
558
559 if (assetRenderer == null) {
560 return null;
561 }
562
563 return assetRenderer.getURLView(
564 liferayPortletResponse, WindowState.MAXIMIZED);
565 }
566
567 protected boolean hasPermissions(
568 PermissionChecker permissionChecker, SocialActivity activity,
569 String actionId, ServiceContext serviceContext)
570 throws Exception {
571
572 return false;
573 }
574
575 protected String wrapLink(String link, String title) {
576 title = HtmlUtil.escape(title);
577
578 if (link == null) {
579 return title;
580 }
581
582 return buildLink(link, title);
583 }
584
585 protected String wrapLink(
586 String link, String key, ServiceContext serviceContext) {
587
588 String title = serviceContext.translate(HtmlUtil.escape(key));
589
590 if (link == null) {
591 return title;
592 }
593
594 return buildLink(link, title);
595 }
596
597 private static final Log _log = LogFactoryUtil.getLog(
598 BaseSocialActivityInterpreter.class);
599
600 private final SocialActivityFeedEntry
601 _deprecatedMarkerSocialActivityFeedEntry = new SocialActivityFeedEntry(
602 StringPool.BLANK, StringPool.BLANK);
603
604 }