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.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.asset.model.AssetRenderer;
027    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
028    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
029    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
030    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
031    import com.liferay.portlet.documentlibrary.model.DLFileVersion;
032    import com.liferay.portlet.documentlibrary.service.DLFileEntryLocalServiceUtil;
033    import com.liferay.portlet.documentlibrary.service.DLFileVersionLocalServiceUtil;
034    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
035    import com.liferay.portlet.documentlibrary.service.permission.DLPermission;
036    
037    import javax.portlet.PortletURL;
038    
039    /**
040     * @author Julio Camarero
041     * @author Juan Fernández
042     * @author Raymond Augé
043     */
044    public class DLFileEntryAssetRendererFactory extends BaseAssetRendererFactory {
045    
046            public static final String CLASS_NAME = DLFileEntry.class.getName();
047    
048            public static final String TYPE = "document";
049    
050            public AssetRenderer getAssetRenderer(long classPK, int type)
051                    throws PortalException, SystemException {
052    
053                    DLFileEntry fileEntry = null;
054                    DLFileVersion fileVersion = null;
055    
056                    try {
057                            fileEntry = DLFileEntryLocalServiceUtil.getFileEntry(classPK);
058    
059                            if (type == TYPE_LATEST) {
060                                    fileVersion = fileEntry.getLatestFileVersion();
061                            }
062                            else {
063                                    fileVersion = fileEntry.getFileVersion();
064                            }
065                    }
066                    catch (NoSuchFileEntryException nsfee) {
067                            fileVersion = DLFileVersionLocalServiceUtil.getFileVersion(classPK);
068                            fileEntry = fileVersion.getFileEntry();
069                    }
070    
071                    return new DLFileEntryAssetRenderer(fileEntry, fileVersion);
072            }
073    
074            public String getClassName() {
075                    return CLASS_NAME;
076            }
077    
078            public String getType() {
079                    return TYPE;
080            }
081    
082            public PortletURL getURLAdd(
083                    LiferayPortletRequest liferayPortletRequest,
084                    LiferayPortletResponse liferayPortletResponse) {
085    
086                    ThemeDisplay themeDisplay =
087                            (ThemeDisplay)liferayPortletRequest.getAttribute(
088                                    WebKeys.THEME_DISPLAY);
089    
090                    PortletURL addAssetURL = null;
091    
092                    if (DLPermission.contains(
093                                    themeDisplay.getPermissionChecker(),
094                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_DOCUMENT)) {
095    
096                            addAssetURL = liferayPortletResponse.createRenderURL(
097                                    PortletKeys.DOCUMENT_LIBRARY);
098    
099                            addAssetURL.setParameter(
100                                    "struts_action", "/document_library/edit_file_entry");
101                            addAssetURL.setParameter(
102                                    "groupId", String.valueOf(themeDisplay.getScopeGroupId()));
103                            addAssetURL.setParameter(
104                                    "folderId",
105                                    String.valueOf(
106                                            AssetPublisherUtil.getRecentFolderId(
107                                                    liferayPortletRequest, CLASS_NAME)));
108                            addAssetURL.setParameter("uploader", "classic");
109                    }
110    
111                    return addAssetURL;
112            }
113    
114            public boolean hasPermission(
115                            PermissionChecker permissionChecker, long classPK, String actionId)
116                    throws Exception {
117    
118                    return DLFileEntryPermission.contains(
119                            permissionChecker, classPK, actionId);
120            }
121    
122            protected String getIconPath(ThemeDisplay themeDisplay) {
123                    return themeDisplay.getPathThemeImages() + "/common/clip.png";
124            }
125    
126    }