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