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   */
42  public class DLFileEntryAssetRenderer extends BaseAssetRenderer {
43  
44      public DLFileEntryAssetRenderer(DLFileEntry entry) {
45          _entry = entry;
46      }
47  
48      public long getClassPK() {
49          return _entry.getFileEntryId();
50      }
51  
52      public String getDiscussionPath() {
53          if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
54              return "edit_file_entry_discussion";
55          }
56          else {
57              return null;
58          }
59      }
60  
61      public long getGroupId() {
62          return _entry.getGroupId();
63      }
64  
65      public String getSummary() {
66          return HtmlUtil.stripHtml(_entry.getDescription());
67      }
68  
69      public String getTitle() {
70          return _entry.getTitle();
71      }
72  
73      public PortletURL getURLEdit(
74          LiferayPortletRequest liferayPortletRequest,
75          LiferayPortletResponse liferayPortletResponse) {
76  
77          PortletURL editPortletURL = liferayPortletResponse.createRenderURL(
78              PortletKeys.DOCUMENT_LIBRARY);
79  
80          editPortletURL.setParameter(
81              "struts_action", "/document_library/edit_file_entry");
82          editPortletURL.setParameter(
83              "folderId", String.valueOf(_entry.getFolderId()));
84          editPortletURL.setParameter(
85              "name", String.valueOf(_entry.getName()));
86  
87          return editPortletURL;
88      }
89  
90      public PortletURL getURLExport(
91          LiferayPortletRequest liferayPortletRequest,
92          LiferayPortletResponse liferayPortletResponse) {
93  
94          PortletURL exportPortletURL = liferayPortletResponse.createActionURL();
95  
96          exportPortletURL.setParameter(
97              "struts_action", "/asset_publisher/get_file");
98          exportPortletURL.setParameter(
99              "groupId", String.valueOf(_entry.getGroupId()));
100         exportPortletURL.setParameter(
101             "folderId", String.valueOf(_entry.getFolderId()));
102         exportPortletURL.setParameter(
103             "title", String.valueOf(_entry.getTitle()));
104 
105         return exportPortletURL;
106     }
107 
108     public String getURLViewInContext(
109         LiferayPortletRequest liferayPortletRequest,
110         LiferayPortletResponse liferayPortletResponse,
111         String noSuchEntryRedirect) {
112 
113         ThemeDisplay themeDisplay =
114             (ThemeDisplay)liferayPortletRequest.getAttribute(
115                 WebKeys.THEME_DISPLAY);
116 
117         return themeDisplay.getPathMain() +
118             "/document_library/get_file?p_l_id=" + themeDisplay.getPlid() +
119                 "&folderId=" + _entry.getFolderId() + "&title=" +
120                     HttpUtil.encodeURL(_entry.getTitle());
121     }
122 
123     public long getUserId() {
124         return _entry.getUserId();
125     }
126 
127     public boolean hasEditPermission(PermissionChecker permissionChecker)
128         throws PortalException, SystemException {
129 
130         return DLFileEntryPermission.contains(
131             permissionChecker, _entry, ActionKeys.UPDATE);
132     }
133 
134     public boolean hasViewPermission(PermissionChecker permissionChecker)
135         throws PortalException, SystemException {
136 
137         return DLFileEntryPermission.contains(
138             permissionChecker, _entry, ActionKeys.VIEW);
139     }
140 
141     public boolean isConvertible() {
142         return true;
143     }
144 
145     public boolean isPrintable() {
146         return false;
147     }
148 
149     public String render(
150             RenderRequest renderRequest, RenderResponse renderResponse,
151             String template)
152         throws Exception {
153 
154         if (template.equals(TEMPLATE_ABSTRACT) ||
155             template.equals(TEMPLATE_FULL_CONTENT)) {
156 
157             renderRequest.setAttribute(
158                 WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, _entry);
159 
160             return "/html/portlet/document_library/asset/" + template + ".jsp";
161         }
162         else {
163             return null;
164         }
165     }
166 
167     private DLFileEntry _entry;
168 
169 }