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