001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
055     * @author Brian Wing Shun Chan
056     * @author Ryan Park
057     */
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            /**
155             * @deprecated As of 6.2.0
156             */
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            /**
199             * @deprecated As of 6.2.0
200             */
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            /**
283             * @deprecated As of 6.2.0, replaced by {@link #getGroupName(long,
284             *             ServiceContext)}
285             */
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) &&
364                            (trashHandler.isInTrash(classPK) ||
365                             trashHandler.isInTrashContainer(classPK))) {
366    
367                            PortletURL portletURL = TrashUtil.getViewContentURL(
368                                    serviceContext.getRequest(), className, classPK);
369    
370                            if (portletURL == null) {
371                                    return null;
372                            }
373    
374                            return portletURL.toString();
375                    }
376    
377                    String path = getPath(activity, serviceContext);
378    
379                    if (Validator.isNull(path)) {
380                            return null;
381                    }
382    
383                    path = addNoSuchEntryRedirect(path, className, classPK, serviceContext);
384    
385                    if (!path.startsWith(StringPool.SLASH)) {
386                            return path;
387                    }
388    
389                    return serviceContext.getPortalURL() + serviceContext.getPathMain() +
390                            path;
391            }
392    
393            protected String getPath(
394                            SocialActivity activity, ServiceContext serviceContext)
395                    throws Exception {
396    
397                    return StringPool.BLANK;
398            }
399    
400            protected String getTitle(
401                            SocialActivity activity, ServiceContext serviceContext)
402                    throws Exception {
403    
404                    String groupName = StringPool.BLANK;
405    
406                    if (activity.getGroupId() != serviceContext.getScopeGroupId()) {
407                            groupName = getGroupName(activity.getGroupId(), serviceContext);
408                    }
409    
410                    String titlePattern = getTitlePattern(groupName, activity);
411    
412                    if (Validator.isNull(titlePattern)) {
413                            return null;
414                    }
415    
416                    String link = getLink(activity, serviceContext);
417    
418                    String entryTitle = getEntryTitle(activity, serviceContext);
419    
420                    Object[] titleArguments = getTitleArguments(
421                            groupName, activity, link, entryTitle, serviceContext);
422    
423                    return serviceContext.translate(titlePattern, titleArguments);
424            }
425    
426            protected Object[] getTitleArguments(
427                            String groupName, SocialActivity activity, String link,
428                            String title, ServiceContext serviceContext)
429                    throws Exception {
430    
431                    String userName = getUserName(activity.getUserId(), serviceContext);
432    
433                    return new Object[] {groupName, userName, wrapLink(link, title)};
434            }
435    
436            protected String getTitlePattern(String groupName, SocialActivity activity)
437                    throws Exception {
438    
439                    return StringPool.BLANK;
440            }
441    
442            protected String getUserName(long userId, ServiceContext serviceContext) {
443                    try {
444                            if (userId <= 0) {
445                                    return StringPool.BLANK;
446                            }
447    
448                            User user = UserLocalServiceUtil.getUserById(userId);
449    
450                            if (user.getUserId() == serviceContext.getUserId()) {
451                                    return HtmlUtil.escape(user.getFirstName());
452                            }
453    
454                            String userName = user.getFullName();
455    
456                            Group group = user.getGroup();
457    
458                            if (group.getGroupId() == serviceContext.getScopeGroupId()) {
459                                    return HtmlUtil.escape(userName);
460                            }
461    
462                            String userDisplayURL = user.getDisplayURL(
463                                    serviceContext.getThemeDisplay());
464    
465                            userName =
466                                    "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
467                                            HtmlUtil.escape(userName) + "</a>";
468    
469                            return userName;
470                    }
471                    catch (Exception e) {
472                            return StringPool.BLANK;
473                    }
474            }
475    
476            /**
477             * @deprecated As of 6.2.0, replaced by {@link #getUserName(long,
478             *             ServiceContext)}
479             */
480            protected String getUserName(long userId, ThemeDisplay themeDisplay) {
481                    try {
482                            if (userId <= 0) {
483                                    return StringPool.BLANK;
484                            }
485    
486                            User user = UserLocalServiceUtil.getUserById(userId);
487    
488                            if (user.getUserId() == themeDisplay.getUserId()) {
489                                    return HtmlUtil.escape(user.getFirstName());
490                            }
491    
492                            String userName = user.getFullName();
493    
494                            Group group = user.getGroup();
495    
496                            if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
497                                    return HtmlUtil.escape(userName);
498                            }
499    
500                            String userDisplayURL = user.getDisplayURL(themeDisplay);
501    
502                            userName =
503                                    "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
504                                            HtmlUtil.escape(userName) + "</a>";
505    
506                            return userName;
507                    }
508                    catch (Exception e) {
509                            return StringPool.BLANK;
510                    }
511            }
512    
513            /**
514             * @deprecated As of 6.2.0, replaced by {@link #getJSONValue(String, String,
515             *             String)}
516             */
517            protected String getValue(String json, String key, String defaultValue) {
518                    return getJSONValue(json, key, defaultValue);
519            }
520    
521            protected PortletURL getViewEntryPortletURL(
522                            String className, long classPK, ServiceContext serviceContext)
523                    throws Exception {
524    
525                    AssetRendererFactory assetRendererFactory =
526                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
527                                    className);
528    
529                    if (assetRendererFactory == null) {
530                            return null;
531                    }
532    
533                    LiferayPortletResponse liferayPortletResponse =
534                            serviceContext.getLiferayPortletResponse();
535    
536                    if (liferayPortletResponse == null) {
537                            return null;
538                    }
539    
540                    if (classPK == 0) {
541                            return assetRendererFactory.getURLView(
542                                    liferayPortletResponse, WindowState.MAXIMIZED);
543                    }
544    
545                    AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
546                            classPK);
547    
548                    if (assetRenderer == null) {
549                            return null;
550                    }
551    
552                    return assetRenderer.getURLView(
553                            liferayPortletResponse, WindowState.MAXIMIZED);
554            }
555    
556            protected boolean hasPermissions(
557                            PermissionChecker permissionChecker, SocialActivity activity,
558                            String actionId, ServiceContext serviceContext)
559                    throws Exception {
560    
561                    return false;
562            }
563    
564            protected String wrapLink(String link, String title) {
565                    title = HtmlUtil.escape(title);
566    
567                    if (link == null) {
568                            return title;
569                    }
570    
571                    return buildLink(link, title);
572            }
573    
574            protected String wrapLink(
575                    String link, String key, ServiceContext serviceContext) {
576    
577                    String title = serviceContext.translate(HtmlUtil.escape(key));
578    
579                    if (link == null) {
580                            return title;
581                    }
582    
583                    return buildLink(link, title);
584            }
585    
586            private static Log _log = LogFactoryUtil.getLog(
587                    BaseSocialActivityInterpreter.class);
588    
589            private SocialActivityFeedEntry _deprecatedMarkerSocialActivityFeedEntry =
590                    new SocialActivityFeedEntry(StringPool.BLANK, StringPool.BLANK);
591    
592    }