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.asset;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
20  import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
21  import com.liferay.portal.kernel.util.HtmlUtil;
22  import com.liferay.portal.kernel.util.HttpUtil;
23  import com.liferay.portal.security.permission.ActionKeys;
24  import com.liferay.portal.security.permission.PermissionChecker;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortletKeys;
27  import com.liferay.portal.util.PropsValues;
28  import com.liferay.portal.util.WebKeys;
29  import com.liferay.portlet.asset.model.BaseAssetRenderer;
30  import com.liferay.portlet.documentlibrary.model.DLFileEntry;
31  import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
32  
33  import javax.portlet.PortletURL;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  /**
38   * <a href="DLFileEntryAssetRenderer.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Julio Camarero
41   * @author Juan Fernández
42   */
43  public class DLFileEntryAssetRenderer extends BaseAssetRenderer {
44  
45      public DLFileEntryAssetRenderer(DLFileEntry entry) {
46          _entry = entry;
47      }
48  
49      public long getClassPK() {
50          return _entry.getFileEntryId();
51      }
52  
53      public String getDiscussionPath() {
54          if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
55              return "edit_file_entry_discussion";
56          }
57          else {
58              return null;
59          }
60      }
61  
62      public long getGroupId() {
63          return _entry.getGroupId();
64      }
65  
66      public String getSummary() {
67          return HtmlUtil.stripHtml(_entry.getDescription());
68      }
69  
70      public String getTitle() {
71          return _entry.getTitle();
72      }
73  
74      public PortletURL getURLEdit(
75          LiferayPortletRequest liferayPortletRequest,
76          LiferayPortletResponse liferayPortletResponse) {
77  
78          PortletURL editPortletURL = liferayPortletResponse.createRenderURL(
79              PortletKeys.DOCUMENT_LIBRARY);
80  
81          editPortletURL.setParameter(
82              "struts_action", "/document_library/edit_file_entry");
83          editPortletURL.setParameter(
84              "folderId", String.valueOf(_entry.getFolderId()));
85          editPortletURL.setParameter(
86              "name", String.valueOf(_entry.getName()));
87  
88          return editPortletURL;
89      }
90  
91      public PortletURL getURLExport(
92          LiferayPortletRequest liferayPortletRequest,
93          LiferayPortletResponse liferayPortletResponse) {
94  
95          PortletURL exportPortletURL = liferayPortletResponse.createActionURL();
96  
97          exportPortletURL.setParameter(
98              "struts_action", "/asset_publisher/get_file");
99          exportPortletURL.setParameter(
100             "groupId", String.valueOf(_entry.getGroupId()));
101         exportPortletURL.setParameter(
102             "folderId", String.valueOf(_entry.getFolderId()));
103         exportPortletURL.setParameter(
104             "title", String.valueOf(_entry.getTitle()));
105 
106         return exportPortletURL;
107     }
108 
109     public String getURLViewInContext(
110         LiferayPortletRequest liferayPortletRequest,
111         LiferayPortletResponse liferayPortletResponse,
112         String noSuchEntryRedirect) {
113 
114         ThemeDisplay themeDisplay =
115             (ThemeDisplay)liferayPortletRequest.getAttribute(
116                 WebKeys.THEME_DISPLAY);
117 
118         return themeDisplay.getPathMain() +
119             "/document_library/get_file?p_l_id=" + themeDisplay.getPlid() +
120                 "&folderId=" + _entry.getFolderId() + "&title=" +
121                     HttpUtil.encodeURL(_entry.getTitle());
122     }
123 
124     public long getUserId() {
125         return _entry.getUserId();
126     }
127 
128     public boolean hasEditPermission(PermissionChecker permissionChecker)
129         throws PortalException, SystemException {
130 
131         return DLFileEntryPermission.contains(
132             permissionChecker, _entry, ActionKeys.UPDATE);
133     }
134 
135     public boolean hasViewPermission(PermissionChecker permissionChecker)
136         throws PortalException, SystemException {
137 
138         return DLFileEntryPermission.contains(
139             permissionChecker, _entry, ActionKeys.VIEW);
140     }
141 
142     public boolean isConvertible() {
143         return true;
144     }
145 
146     public boolean isPrintable() {
147         return false;
148     }
149 
150     public String render(
151             RenderRequest renderRequest, RenderResponse renderResponse,
152             String template)
153         throws Exception {
154 
155         if (template.equals(TEMPLATE_ABSTRACT) ||
156             template.equals(TEMPLATE_FULL_CONTENT)) {
157 
158             renderRequest.setAttribute(
159                 WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, _entry);
160 
161             return "/html/portlet/document_library/asset/" + template + ".jsp";
162         }
163         else {
164             return null;
165         }
166     }
167 
168     protected String getIconPath(ThemeDisplay themeDisplay) {
169         return themeDisplay.getPathThemeImages() + "/common/clip.png";
170     }
171 
172     private DLFileEntry _entry;
173 
174 }