001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.documentlibrary.asset;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
020    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
021    import com.liferay.portal.kernel.util.HtmlUtil;
022    import com.liferay.portal.security.permission.ActionKeys;
023    import com.liferay.portal.security.permission.PermissionChecker;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortletKeys;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.asset.model.BaseAssetRenderer;
029    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
031    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
032    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
033    
034    import javax.portlet.PortletURL;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    /**
039     * @author Julio Camarero
040     * @author Juan Fernández
041     */
042    public class DLFileEntryAssetRenderer extends BaseAssetRenderer {
043    
044            public DLFileEntryAssetRenderer(
045                    DLFileEntry fileEntry, DLFileVersion fileVersion) {
046    
047                    _fileEntry = fileEntry;
048                    _fileVersion = fileVersion;
049            }
050    
051            public long getClassPK() {
052                    if (!_fileVersion.isApproved() &&
053                            (_fileVersion.getVersion() !=
054                                    DLFileEntryConstants.DEFAULT_VERSION)) {
055    
056                            return _fileVersion.getFileVersionId();
057                    }
058                    else {
059                            return _fileEntry.getFileEntryId();
060                    }
061            }
062    
063            public String getDiscussionPath() {
064                    if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
065                            return "edit_file_entry_discussion";
066                    }
067                    else {
068                            return null;
069                    }
070            }
071    
072            public long getGroupId() {
073                    return _fileEntry.getGroupId();
074            }
075    
076            public String getSummary() {
077                    return HtmlUtil.stripHtml(_fileEntry.getDescription());
078            }
079    
080            public String getTitle() {
081                    return _fileEntry.getTitle();
082            }
083    
084            public PortletURL getURLEdit(
085                    LiferayPortletRequest liferayPortletRequest,
086                    LiferayPortletResponse liferayPortletResponse) {
087    
088                    PortletURL editPortletURL = liferayPortletResponse.createRenderURL(
089                            PortletKeys.DOCUMENT_LIBRARY);
090    
091                    editPortletURL.setParameter(
092                            "struts_action", "/document_library/edit_file_entry");
093                    editPortletURL.setParameter(
094                            "folderId", String.valueOf(_fileEntry.getFolderId()));
095                    editPortletURL.setParameter(
096                            "name", String.valueOf(_fileEntry.getName()));
097    
098                    return editPortletURL;
099            }
100    
101            public PortletURL getURLExport(
102                    LiferayPortletRequest liferayPortletRequest,
103                    LiferayPortletResponse liferayPortletResponse) {
104    
105                    PortletURL exportPortletURL = liferayPortletResponse.createActionURL();
106    
107                    exportPortletURL.setParameter(
108                            "struts_action", "/asset_publisher/get_file");
109                    exportPortletURL.setParameter(
110                            "groupId", String.valueOf(_fileEntry.getGroupId()));
111                    exportPortletURL.setParameter(
112                            "folderId", String.valueOf(_fileEntry.getFolderId()));
113                    exportPortletURL.setParameter(
114                            "title", String.valueOf(_fileEntry.getTitle()));
115    
116                    return exportPortletURL;
117            }
118    
119            public String getURLViewInContext(
120                    LiferayPortletRequest liferayPortletRequest,
121                    LiferayPortletResponse liferayPortletResponse,
122                    String noSuchEntryRedirect) {
123    
124                    ThemeDisplay themeDisplay =
125                            (ThemeDisplay)liferayPortletRequest.getAttribute(
126                                    WebKeys.THEME_DISPLAY);
127    
128                    return themeDisplay.getPathMain() +
129                            "/document_library/find_file_entry?p_l_id=" +
130                                    themeDisplay.getPlid() + "&fileEntryId=" +
131                                            _fileEntry.getFileEntryId();
132            }
133    
134            public long getUserId() {
135                    return _fileEntry.getUserId();
136            }
137    
138            public String getUuid() {
139                    return _fileEntry.getUuid();
140            }
141    
142            public boolean hasEditPermission(PermissionChecker permissionChecker)
143                    throws PortalException, SystemException {
144    
145                    return DLFileEntryPermission.contains(
146                            permissionChecker, _fileEntry, ActionKeys.UPDATE);
147            }
148    
149            public boolean hasViewPermission(PermissionChecker permissionChecker)
150                    throws PortalException, SystemException {
151    
152                    return DLFileEntryPermission.contains(
153                            permissionChecker, _fileEntry, ActionKeys.VIEW);
154            }
155    
156            public boolean isConvertible() {
157                    return true;
158            }
159    
160            public boolean isPrintable() {
161                    return false;
162            }
163    
164            public String render(
165                            RenderRequest renderRequest, RenderResponse renderResponse,
166                            String template)
167                    throws Exception {
168    
169                    if (template.equals(TEMPLATE_ABSTRACT) ||
170                            template.equals(TEMPLATE_FULL_CONTENT)) {
171    
172                            renderRequest.setAttribute(
173                                    WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, _fileEntry);
174                            renderRequest.setAttribute(
175                                    WebKeys.DOCUMENT_LIBRARY_FILE_VERSION, _fileVersion);
176    
177                            return "/html/portlet/document_library/asset/" + template + ".jsp";
178                    }
179                    else {
180                            return null;
181                    }
182            }
183    
184            protected String getIconPath(ThemeDisplay themeDisplay) {
185                    return themeDisplay.getPathThemeImages() + "/common/clip.png";
186            }
187    
188            private DLFileEntry _fileEntry;
189            private DLFileVersion _fileVersion;
190    
191    }