1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.social;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Group;
25  import com.liferay.portal.security.permission.ActionKeys;
26  import com.liferay.portal.security.permission.PermissionChecker;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
30  import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
31  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
32  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
33  import com.liferay.portlet.social.model.SocialActivity;
34  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
35  
36  /**
37   * <a href="DLActivityInterpreter.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Ryan Park
40   *
41   */
42  public class DLActivityInterpreter extends BaseSocialActivityInterpreter {
43  
44      public String[] getClassNames() {
45          return _CLASS_NAMES;
46      }
47  
48      protected SocialActivityFeedEntry doInterpret(
49              SocialActivity activity, ThemeDisplay themeDisplay)
50          throws Exception {
51  
52          PermissionChecker permissionChecker =
53              themeDisplay.getPermissionChecker();
54  
55          DLFileEntry fileEntry = DLFileEntryLocalServiceUtil.getDLFileEntry(
56              activity.getClassPK());
57  
58          if (!DLFileEntryPermission.contains(
59                  permissionChecker, fileEntry, ActionKeys.VIEW)) {
60  
61              return null;
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.getURLPortal() + themeDisplay.getPathMain() +
73                  "/document_library/get_file?folderId=" +
74                      fileEntry.getFolderId() + "&name=" + fileEntry.getName();
75  
76          // Title
77  
78          String groupName = StringPool.BLANK;
79  
80          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
81              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
82  
83              groupName = group.getDescriptiveName();
84          }
85  
86          String titlePattern = null;
87  
88          if (activityType == DLActivityKeys.ADD_FILE_ENTRY) {
89              titlePattern = "activity-document-library-add-file";
90          }
91          else if (activityType == DLActivityKeys.UPDATE_FILE_ENTRY) {
92              titlePattern = "activity-document-library-update-file";
93          }
94  
95          if (Validator.isNotNull(groupName)) {
96              titlePattern += "-in";
97          }
98  
99          Object[] titleArguments = new Object[] {creatorUserName, groupName};
100 
101         String title = themeDisplay.translate(titlePattern, titleArguments);
102 
103         // Body
104 
105         StringBuilder sb = new StringBuilder();
106 
107         sb.append("<a href=\"");
108         sb.append(link);
109         sb.append("\">");
110         sb.append(cleanContent(fileEntry.getTitle()));
111         sb.append("</a><br />");
112         sb.append(cleanContent(fileEntry.getDescription()));
113 
114         String body = sb.toString();
115 
116         return new SocialActivityFeedEntry(link, title, body);
117     }
118 
119     private static final String[] _CLASS_NAMES = new String[] {
120         DLFileEntry.class.getName()
121     };
122 
123 }