001    /**
002     * Copyright (c) 2000-2012 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.bookmarks.social;
016    
017    import com.liferay.portal.kernel.util.StringPool;
018    import com.liferay.portal.kernel.util.Validator;
019    import com.liferay.portal.security.permission.ActionKeys;
020    import com.liferay.portal.security.permission.PermissionChecker;
021    import com.liferay.portal.theme.ThemeDisplay;
022    import com.liferay.portlet.bookmarks.model.BookmarksEntry;
023    import com.liferay.portlet.bookmarks.service.BookmarksEntryLocalServiceUtil;
024    import com.liferay.portlet.bookmarks.service.permission.BookmarksEntryPermission;
025    import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
026    import com.liferay.portlet.social.model.SocialActivity;
027    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
028    
029    /**
030     * @author Juan Fernández
031     */
032    public class BookmarksActivityInterpreter
033            extends BaseSocialActivityInterpreter {
034    
035            public String[] getClassNames() {
036                    return _CLASS_NAMES;
037            }
038    
039            @Override
040            protected SocialActivityFeedEntry doInterpret(
041                            SocialActivity activity, ThemeDisplay themeDisplay)
042                    throws Exception {
043    
044                    PermissionChecker permissionChecker =
045                            themeDisplay.getPermissionChecker();
046    
047                    if (!BookmarksEntryPermission.contains(
048                                    permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
049    
050                            return null;
051                    }
052    
053                    String groupName = StringPool.BLANK;
054    
055                    if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
056                            groupName = getGroupName(activity.getGroupId(), themeDisplay);
057                    }
058    
059                    String creatorUserName = getUserName(
060                            activity.getUserId(), themeDisplay);
061                    String receiverUserName = getUserName(
062                            activity.getReceiverUserId(), themeDisplay);
063    
064                    int activityType = activity.getType();
065    
066                    // Link
067    
068                    BookmarksEntry entry = BookmarksEntryLocalServiceUtil.getEntry(
069                            activity.getClassPK());
070    
071                    String link =
072                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
073                                    "/bookmarks/find_entry?entryId=" + activity.getClassPK();
074    
075                    // Title
076    
077                    String titlePattern = null;
078    
079                    if (activityType == BookmarksActivityKeys.ADD_ENTRY) {
080                            if (Validator.isNull(groupName)) {
081                                    titlePattern = "activity-bookmarks-add-entry";
082                            }
083                            else {
084                                    titlePattern = "activity-bookmarks-add-entry-in";
085                            }
086                    }
087                    else if (activityType == BookmarksActivityKeys.UPDATE_ENTRY) {
088                            if (Validator.isNull(groupName)) {
089                                    titlePattern = "activity-bookmarks-update-entry";
090                            }
091                            else {
092                                    titlePattern = "activity-bookmarks-update-entry-in";
093                            }
094                    }
095    
096                    String entryTitle = getValue(
097                            activity.getExtraData(), "title", entry.getName());
098    
099                    Object[] titleArguments = new Object[] {
100                            groupName, creatorUserName, receiverUserName,
101                            wrapLink(link, entryTitle)
102                    };
103    
104                    String title = themeDisplay.translate(titlePattern, titleArguments);
105    
106                    // Body
107    
108                    String body = StringPool.BLANK;
109    
110                    return new SocialActivityFeedEntry(link, title, body);
111            }
112    
113            private static final String[] _CLASS_NAMES = new String[] {
114                    BookmarksEntry.class.getName()
115            };
116    
117    }