1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.social;
16  
17  import com.liferay.portal.kernel.util.HtmlUtil;
18  import com.liferay.portal.kernel.util.StringBundler;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.security.permission.ActionKeys;
22  import com.liferay.portal.security.permission.PermissionChecker;
23  import com.liferay.portal.theme.ThemeDisplay;
24  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
25  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
26  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
27  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
28  import com.liferay.portlet.social.model.SocialActivity;
29  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
30  
31  /**
32   * <a href="DLActivityInterpreter.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Ryan Park
35   */
36  public class DLActivityInterpreter extends BaseSocialActivityInterpreter {
37  
38      public String[] getClassNames() {
39          return _CLASS_NAMES;
40      }
41  
42      protected SocialActivityFeedEntry doInterpret(
43              SocialActivity activity, ThemeDisplay themeDisplay)
44          throws Exception {
45  
46          PermissionChecker permissionChecker =
47              themeDisplay.getPermissionChecker();
48  
49          DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getDLFileEntry(
50              activity.getClassPK());
51  
52          if (!DLFileEntryPermission.contains(
53                  permissionChecker, fileEntry, ActionKeys.VIEW)) {
54  
55              return null;
56          }
57  
58          String groupName = StringPool.BLANK;
59  
60          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
61              groupName = getGroupName(activity.getGroupId(), themeDisplay);
62          }
63  
64          String creatorUserName = getUserName(
65              activity.getUserId(), themeDisplay);
66  
67          int activityType = activity.getType();
68  
69          // Link
70  
71          String link =
72              themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
73                  "/document_library/get_file?groupId=" + fileEntry.getGroupId() +
74                      "&folderId=" + fileEntry.getFolderId() + "&name=" +
75                          fileEntry.getName();
76  
77          // Title
78  
79          String titlePattern = null;
80  
81          if (activityType == DLActivityKeys.ADD_FILE_ENTRY) {
82              titlePattern = "activity-document-library-add-file";
83          }
84          else if (activityType == DLActivityKeys.UPDATE_FILE_ENTRY) {
85              titlePattern = "activity-document-library-update-file";
86          }
87  
88          if (Validator.isNotNull(groupName)) {
89              titlePattern += "-in";
90          }
91  
92          String fileTitle = wrapLink(
93              link, HtmlUtil.escape(cleanContent(fileEntry.getTitle())));
94  
95          Object[] titleArguments = new Object[] {
96              groupName, creatorUserName, fileTitle
97          };
98  
99          String title = themeDisplay.translate(titlePattern, titleArguments);
100 
101         // Body
102 
103         StringBundler sb = new StringBundler(3);
104 
105         String fileEntryLink =
106             themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
107                 "/document_library/find_file_entry?fileEntryId=" +
108                     fileEntry.getFileEntryId();
109 
110         sb.append(wrapLink(fileEntryLink, "view-document", themeDisplay));
111         sb.append(StringPool.SPACE);
112 
113         String folderLink =
114             themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
115                 "/document_library/find_folder?groupId=" +
116                     fileEntry.getGroupId() + "&folderId=" +
117                         fileEntry.getFolderId();
118 
119         sb.append(wrapLink(folderLink, "go-to-folder", themeDisplay));
120 
121         String body = sb.toString();
122 
123         return new SocialActivityFeedEntry(link, title, body);
124     }
125 
126     private static final String[] _CLASS_NAMES = new String[] {
127         DLFileEntry.class.getName()
128     };
129 
130 }