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