001    /**
002     * Copyright (c) 2000-2010 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.documentlibrary.social;
016    
017    import com.liferay.portal.kernel.util.HtmlUtil;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
025    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
026    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
027    import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
028    import com.liferay.portlet.social.model.SocialActivity;
029    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
030    
031    /**
032     * @author Ryan Park
033     */
034    public class DLActivityInterpreter extends BaseSocialActivityInterpreter {
035    
036            public String[] getClassNames() {
037                    return _CLASS_NAMES;
038            }
039    
040            protected SocialActivityFeedEntry doInterpret(
041                            SocialActivity activity, ThemeDisplay themeDisplay)
042                    throws Exception {
043    
044                    PermissionChecker permissionChecker =
045                            themeDisplay.getPermissionChecker();
046    
047                    DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getDLFileEntry(
048                            activity.getClassPK());
049    
050                    if (!DLFileEntryPermission.contains(
051                                    permissionChecker, fileEntry, ActionKeys.VIEW)) {
052    
053                            return null;
054                    }
055    
056                    String groupName = StringPool.BLANK;
057    
058                    if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
059                            groupName = getGroupName(activity.getGroupId(), themeDisplay);
060                    }
061    
062                    String creatorUserName = getUserName(
063                            activity.getUserId(), themeDisplay);
064    
065                    int activityType = activity.getType();
066    
067                    // Link
068    
069                    String link =
070                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
071                                    "/document_library/get_file?groupId=" + fileEntry.getGroupId() +
072                                            "&folderId=" + fileEntry.getFolderId() + "&name=" +
073                                                    fileEntry.getName();
074    
075                    // Title
076    
077                    String titlePattern = null;
078    
079                    if (activityType == DLActivityKeys.ADD_FILE_ENTRY) {
080                            titlePattern = "activity-document-library-add-file";
081                    }
082                    else if (activityType == DLActivityKeys.UPDATE_FILE_ENTRY) {
083                            titlePattern = "activity-document-library-update-file";
084                    }
085    
086                    if (Validator.isNotNull(groupName)) {
087                            titlePattern += "-in";
088                    }
089    
090                    String fileTitle = wrapLink(
091                            link, HtmlUtil.escape(cleanContent(fileEntry.getTitle())));
092    
093                    Object[] titleArguments = new Object[] {
094                            groupName, creatorUserName, fileTitle
095                    };
096    
097                    String title = themeDisplay.translate(titlePattern, titleArguments);
098    
099                    // Body
100    
101                    StringBundler sb = new StringBundler(3);
102    
103                    String fileEntryLink =
104                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
105                                    "/document_library/find_file_entry?fileEntryId=" +
106                                            fileEntry.getFileEntryId();
107    
108                    sb.append(wrapLink(fileEntryLink, "view-document", themeDisplay));
109                    sb.append(StringPool.SPACE);
110    
111                    String folderLink =
112                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
113                                    "/document_library/find_folder?groupId=" +
114                                            fileEntry.getGroupId() + "&folderId=" +
115                                                    fileEntry.getFolderId();
116    
117                    sb.append(wrapLink(folderLink, "go-to-folder", themeDisplay));
118    
119                    String body = sb.toString();
120    
121                    return new SocialActivityFeedEntry(link, title, body);
122            }
123    
124            private static final String[] _CLASS_NAMES = new String[] {
125                    DLFileEntry.class.getName()
126            };
127    
128    }