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