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