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