001    /**
002     * Copyright (c) 2000-present 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.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    /**
054     * @author Brian Wing Shun Chan
055     * @author Ryan Park
056     */
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            /**
152             * @deprecated As of 6.2.0
153             */
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            /**
197             * @deprecated As of 6.2.0
198             */
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 =
257                                    serviceContext.getPortalURL() + serviceContext.getPathMain() +
258                                            "/my_sites/view?groupId=" + group.getGroupId();
259    
260                            if (group.hasPublicLayouts()) {
261                                    groupDisplayURL = groupDisplayURL + "&privateLayout=0";
262                            }
263                            else if (group.hasPrivateLayouts()) {
264                                    groupDisplayURL = groupDisplayURL + "&privateLayout=1";
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            /**
282             * @deprecated As of 6.2.0, replaced by {@link #getGroupName(long,
283             *             ServiceContext)}
284             */
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 =
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            /**
474             * @deprecated As of 6.2.0, replaced by {@link #getUserName(long,
475             *             ServiceContext)}
476             */
477            @Deprecated
478            protected String getUserName(long userId, ThemeDisplay themeDisplay) {
479                    try {
480                            if (userId <= 0) {
481                                    return StringPool.BLANK;
482                            }
483    
484                            User user = UserLocalServiceUtil.getUserById(userId);
485    
486                            if (user.getUserId() == themeDisplay.getUserId()) {
487                                    return HtmlUtil.escape(user.getFirstName());
488                            }
489    
490                            String userName = user.getFullName();
491    
492                            Group group = user.getGroup();
493    
494                            if (group.getGroupId() == themeDisplay.getScopeGroupId()) {
495                                    return HtmlUtil.escape(userName);
496                            }
497    
498                            String userDisplayURL = user.getDisplayURL(themeDisplay);
499    
500                            userName =
501                                    "<a class=\"user\" href=\"" + userDisplayURL + "\">" +
502                                            HtmlUtil.escape(userName) + "</a>";
503    
504                            return userName;
505                    }
506                    catch (Exception e) {
507                            return StringPool.BLANK;
508                    }
509            }
510    
511            /**
512             * @deprecated As of 6.2.0, replaced by {@link #getJSONValue(String, String,
513             *             String)}
514             */
515            @Deprecated
516            protected String getValue(String json, String key, String defaultValue) {
517                    return getJSONValue(json, key, defaultValue);
518            }
519    
520            protected PortletURL getViewEntryPortletURL(
521                            String className, long classPK, ServiceContext serviceContext)
522                    throws Exception {
523    
524                    AssetRendererFactory assetRendererFactory =
525                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
526                                    className);
527    
528                    if (assetRendererFactory == null) {
529                            return null;
530                    }
531    
532                    LiferayPortletResponse liferayPortletResponse =
533                            serviceContext.getLiferayPortletResponse();
534    
535                    if (liferayPortletResponse == null) {
536                            return null;
537                    }
538    
539                    if (classPK == 0) {
540                            return assetRendererFactory.getURLView(
541                                    liferayPortletResponse, WindowState.MAXIMIZED);
542                    }
543    
544                    AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
545                            classPK);
546    
547                    if (assetRenderer == null) {
548                            return null;
549                    }
550    
551                    return assetRenderer.getURLView(
552                            liferayPortletResponse, WindowState.MAXIMIZED);
553            }
554    
555            protected boolean hasPermissions(
556                            PermissionChecker permissionChecker, SocialActivity activity,
557                            String actionId, ServiceContext serviceContext)
558                    throws Exception {
559    
560                    return false;
561            }
562    
563            protected String wrapLink(String link, String title) {
564                    title = HtmlUtil.escape(title);
565    
566                    if (link == null) {
567                            return title;
568                    }
569    
570                    return buildLink(link, title);
571            }
572    
573            protected String wrapLink(
574                    String link, String key, ServiceContext serviceContext) {
575    
576                    String title = serviceContext.translate(HtmlUtil.escape(key));
577    
578                    if (link == null) {
579                            return title;
580                    }
581    
582                    return buildLink(link, title);
583            }
584    
585            private static final Log _log = LogFactoryUtil.getLog(
586                    BaseSocialActivityInterpreter.class);
587    
588            private final SocialActivityFeedEntry
589                    _deprecatedMarkerSocialActivityFeedEntry = new SocialActivityFeedEntry(
590                            StringPool.BLANK, StringPool.BLANK);
591    
592    }