1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.documentlibrary.social;
24  
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.security.permission.ActionKeys;
28  import com.liferay.portal.security.permission.PermissionChecker;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
31  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
32  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
33  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
34  import com.liferay.portlet.social.model.SocialActivity;
35  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
36  
37  /**
38   * <a href="DLActivityInterpreter.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Ryan Park
41   *
42   */
43  public class DLActivityInterpreter extends BaseSocialActivityInterpreter {
44  
45      public String[] getClassNames() {
46          return _CLASS_NAMES;
47      }
48  
49      protected SocialActivityFeedEntry doInterpret(
50              SocialActivity activity, ThemeDisplay themeDisplay)
51          throws Exception {
52  
53          PermissionChecker permissionChecker =
54              themeDisplay.getPermissionChecker();
55  
56          DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getDLFileEntry(
57              activity.getClassPK());
58  
59          if (!DLFileEntryPermission.contains(
60                  permissionChecker, fileEntry, ActionKeys.VIEW)) {
61  
62              return null;
63          }
64  
65          String groupName = StringPool.BLANK;
66  
67          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
68              groupName = getGroupName(activity.getGroupId(), themeDisplay);
69          }
70  
71          String creatorUserName = getUserName(
72              activity.getUserId(), themeDisplay);
73  
74          int activityType = activity.getType();
75  
76          // Link
77  
78          String link =
79              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
80                  "/document_library/get_file?folderId=" +
81                      fileEntry.getFolderId() + "&name=" + fileEntry.getName();
82  
83          // Title
84  
85          String titlePattern = null;
86  
87          if (activityType == DLActivityKeys.ADD_FILE_ENTRY) {
88              titlePattern = "activity-document-library-add-file";
89          }
90          else if (activityType == DLActivityKeys.UPDATE_FILE_ENTRY) {
91              titlePattern = "activity-document-library-update-file";
92          }
93  
94          if (Validator.isNotNull(groupName)) {
95              titlePattern += "-in";
96          }
97  
98          String fileTitle = wrapLink(link, cleanContent(fileEntry.getTitle()));
99  
100         Object[] titleArguments = new Object[] {
101             groupName, creatorUserName, fileTitle
102         };
103 
104         String title = themeDisplay.translate(titlePattern, titleArguments);
105 
106         // Body
107 
108         StringBuilder sb = new StringBuilder();
109 
110         String fileEntryLink =
111             themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
112                 "/document_library/find_file_entry?fileEntryId=" +
113                     fileEntry.getFileEntryId();
114 
115         sb.append(wrapLink(fileEntryLink, "view-document", themeDisplay));
116         sb.append(StringPool.SPACE);
117 
118         String folderLink =
119             themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
120                 "/document_library/find_folder?folderId=" +
121                     fileEntry.getFolderId();
122 
123         sb.append(wrapLink(folderLink, "go-to-folder", themeDisplay));
124 
125         String body = sb.toString();
126 
127         return new SocialActivityFeedEntry(link, title, body);
128     }
129 
130     private static final String[] _CLASS_NAMES = new String[] {
131         DLFileEntry.class.getName()
132     };
133 
134 }