001    /**
002     * Copyright (c) 2000-2013 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.repository.model.FileEntry;
022    import com.liferay.portal.kernel.repository.model.FileVersion;
023    import com.liferay.portal.kernel.util.HtmlUtil;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.security.permission.PermissionChecker;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortletKeys;
028    import com.liferay.portal.util.PropsValues;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.asset.model.BaseAssetRenderer;
031    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
032    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
033    
034    import java.util.Locale;
035    
036    import javax.portlet.PortletRequest;
037    import javax.portlet.PortletURL;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    /**
042     * @author Julio Camarero
043     * @author Juan Fern??ndez
044     * @author Sergio Gonz??lez
045     */
046    public class DLFileEntryAssetRenderer extends BaseAssetRenderer {
047    
048            public DLFileEntryAssetRenderer(
049                    FileEntry fileEntry, FileVersion fileVersion) {
050    
051                    _fileEntry = fileEntry;
052                    _fileVersion = fileVersion;
053            }
054    
055            @Override
056            public long getClassPK() {
057                    if (!_fileVersion.isApproved() && _fileVersion.isDraft() &&
058                            !_fileVersion.isPending() &&
059                            !_fileVersion.getVersion().equals(
060                                    DLFileEntryConstants.VERSION_DEFAULT)) {
061    
062                            return _fileVersion.getFileVersionId();
063                    }
064                    else {
065                            return _fileEntry.getFileEntryId();
066                    }
067            }
068    
069            @Override
070            public String getDiscussionPath() {
071                    if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
072                            return "edit_file_entry_discussion";
073                    }
074                    else {
075                            return null;
076                    }
077            }
078    
079            @Override
080            public long getGroupId() {
081                    return _fileEntry.getGroupId();
082            }
083    
084            @Override
085            public String getSummary(Locale locale) {
086                    return HtmlUtil.stripHtml(_fileEntry.getDescription());
087            }
088    
089            @Override
090            public String getTitle(Locale locale) {
091                    return _fileVersion.getTitle();
092            }
093    
094            @Override
095            public PortletURL getURLEdit(
096                            LiferayPortletRequest liferayPortletRequest,
097                            LiferayPortletResponse liferayPortletResponse)
098                    throws Exception {
099    
100                    PortletURL portletURL = liferayPortletResponse.createLiferayPortletURL(
101                            getControlPanelPlid(liferayPortletRequest),
102                            PortletKeys.DOCUMENT_LIBRARY, PortletRequest.RENDER_PHASE);
103    
104                    portletURL.setParameter(
105                            "struts_action", "/document_library/edit_file_entry");
106                    portletURL.setParameter(
107                            "fileEntryId", String.valueOf(_fileEntry.getFileEntryId()));
108    
109                    return portletURL;
110            }
111    
112            @Override
113            public PortletURL getURLExport(
114                    LiferayPortletRequest liferayPortletRequest,
115                    LiferayPortletResponse liferayPortletResponse) {
116    
117                    PortletURL portletURL = liferayPortletResponse.createActionURL();
118    
119                    portletURL.setParameter("struts_action", "/asset_publisher/get_file");
120                    portletURL.setParameter(
121                            "groupId", String.valueOf(_fileEntry.getRepositoryId()));
122                    portletURL.setParameter(
123                            "folderId", String.valueOf(_fileEntry.getFolderId()));
124                    portletURL.setParameter("title", String.valueOf(_fileEntry.getTitle()));
125    
126                    return portletURL;
127            }
128    
129            @Override
130            public String getURLViewInContext(
131                    LiferayPortletRequest liferayPortletRequest,
132                    LiferayPortletResponse liferayPortletResponse,
133                    String noSuchEntryRedirect) {
134    
135                    return getURLViewInContext(
136                            liferayPortletRequest, noSuchEntryRedirect,
137                            "/document_library/find_file_entry", "fileEntryId",
138                            _fileEntry.getFileEntryId());
139            }
140    
141            @Override
142            public long getUserId() {
143                    return _fileEntry.getUserId();
144            }
145    
146            @Override
147            public String getUserName() {
148                    return _fileEntry.getUserName();
149            }
150    
151            @Override
152            public String getUuid() {
153                    return _fileEntry.getUuid();
154            }
155    
156            @Override
157            public boolean hasEditPermission(PermissionChecker permissionChecker)
158                    throws PortalException, SystemException {
159    
160                    return DLFileEntryPermission.contains(
161                            permissionChecker, _fileEntry.getFileEntryId(), ActionKeys.UPDATE);
162            }
163    
164            @Override
165            public boolean hasViewPermission(PermissionChecker permissionChecker)
166                    throws PortalException, SystemException {
167    
168                    return DLFileEntryPermission.contains(
169                            permissionChecker, _fileEntry.getFileEntryId(), ActionKeys.VIEW);
170            }
171    
172            @Override
173            public boolean isConvertible() {
174                    return true;
175            }
176    
177            @Override
178            public boolean isPrintable() {
179                    return false;
180            }
181    
182            @Override
183            public String render(
184                            RenderRequest renderRequest, RenderResponse renderResponse,
185                            String template)
186                    throws Exception {
187    
188                    if (template.equals(TEMPLATE_ABSTRACT) ||
189                            template.equals(TEMPLATE_FULL_CONTENT)) {
190    
191                            renderRequest.setAttribute(
192                                    WebKeys.DOCUMENT_LIBRARY_FILE_ENTRY, _fileEntry);
193                            renderRequest.setAttribute(
194                                    WebKeys.DOCUMENT_LIBRARY_FILE_VERSION, _fileVersion);
195    
196                            return "/html/portlet/document_library/asset/" + template + ".jsp";
197                    }
198                    else {
199                            return null;
200                    }
201            }
202    
203            @Override
204            protected String getIconPath(ThemeDisplay themeDisplay) {
205                    return themeDisplay.getPathThemeImages() + "/file_system/small/" +
206                            _fileEntry.getIcon() + ".png";
207            }
208    
209            private FileEntry _fileEntry;
210            private FileVersion _fileVersion;
211    
212    }