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.GetterUtil;
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.WebKeys;
029    import com.liferay.portlet.PortletURLFactoryUtil;
030    import com.liferay.portlet.asset.model.AssetRenderer;
031    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
032    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
033    import com.liferay.portlet.documentlibrary.model.DLFileEntry;
034    import com.liferay.portlet.documentlibrary.model.DLFileEntryType;
035    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
036    import com.liferay.portlet.documentlibrary.service.DLFileEntryTypeServiceUtil;
037    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryPermission;
038    import com.liferay.portlet.documentlibrary.service.permission.DLFileEntryTypePermission;
039    import com.liferay.portlet.documentlibrary.service.permission.DLPermission;
040    
041    import java.util.HashMap;
042    import java.util.List;
043    import java.util.Locale;
044    import java.util.Map;
045    
046    import javax.portlet.PortletRequest;
047    import javax.portlet.PortletURL;
048    
049    import javax.servlet.http.HttpServletRequest;
050    
051    /**
052     * @author Julio Camarero
053     * @author Juan Fern??ndez
054     * @author Raymond Aug??
055     * @author Sergio Gonz??lez
056     */
057    public class DLFileEntryAssetRendererFactory extends BaseAssetRendererFactory {
058    
059            public static final String CLASS_NAME = DLFileEntry.class.getName();
060    
061            public static final String TYPE = "document";
062    
063            @Override
064            public AssetRenderer getAssetRenderer(long classPK, int type)
065                    throws PortalException, SystemException {
066    
067                    FileEntry fileEntry = null;
068                    FileVersion fileVersion = null;
069    
070                    if (type == TYPE_LATEST) {
071                            fileVersion = DLAppLocalServiceUtil.getFileVersion(classPK);
072    
073                            fileEntry = fileVersion.getFileEntry();
074                    }
075                    else {
076                            fileEntry = DLAppLocalServiceUtil.getFileEntry(classPK);
077    
078                            fileVersion = fileEntry.getFileVersion();
079                    }
080    
081                    return new DLFileEntryAssetRenderer(fileEntry, fileVersion);
082            }
083    
084            @Override
085            public String getClassName() {
086                    return CLASS_NAME;
087            }
088    
089            @Override
090            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
091                    throws Exception {
092    
093                    Map<Long, String> classTypes = new HashMap<Long, String>();
094    
095                    List<DLFileEntryType> dlFileEntryTypes =
096                            DLFileEntryTypeServiceUtil.getFileEntryTypes(groupIds);
097    
098                    for (DLFileEntryType dlFileEntryType : dlFileEntryTypes) {
099                            classTypes.put(
100                                    dlFileEntryType.getFileEntryTypeId(),
101                                    dlFileEntryType.getName());
102                    }
103    
104                    return classTypes;
105            }
106    
107            @Override
108            public String getType() {
109                    return TYPE;
110            }
111    
112            @Override
113            public PortletURL getURLAdd(
114                            LiferayPortletRequest liferayPortletRequest,
115                            LiferayPortletResponse liferayPortletResponse)
116                    throws PortalException, SystemException {
117    
118                    HttpServletRequest request =
119                            liferayPortletRequest.getHttpServletRequest();
120    
121                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
122                            WebKeys.THEME_DISPLAY);
123    
124                    if (!DLPermission.contains(
125                                    themeDisplay.getPermissionChecker(),
126                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_DOCUMENT)) {
127    
128                            return null;
129                    }
130    
131                    long classTypeId = GetterUtil.getLong(
132                            liferayPortletRequest.getAttribute(
133                                    WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
134    
135                    if ((classTypeId > 0) &&
136                            !DLFileEntryTypePermission.contains(
137                                    themeDisplay.getPermissionChecker(), classTypeId,
138                                    ActionKeys.VIEW)) {
139    
140                            return null;
141                    }
142    
143                    PortletURL portletURL = PortletURLFactoryUtil.create(
144                            request, PortletKeys.DOCUMENT_LIBRARY,
145                            getControlPanelPlid(themeDisplay), PortletRequest.RENDER_PHASE);
146    
147                    portletURL.setParameter(
148                            "struts_action", "/document_library/edit_file_entry");
149                    portletURL.setParameter(
150                            "folderId",
151                            String.valueOf(
152                                    AssetPublisherUtil.getRecentFolderId(
153                                            liferayPortletRequest, CLASS_NAME)));
154                    portletURL.setParameter("uploader", "classic");
155    
156                    return portletURL;
157            }
158    
159            @Override
160            public boolean hasPermission(
161                            PermissionChecker permissionChecker, long classPK, String actionId)
162                    throws Exception {
163    
164                    return DLFileEntryPermission.contains(
165                            permissionChecker, classPK, actionId);
166            }
167    
168            @Override
169            public boolean isLinkable() {
170                    return _LINKABLE;
171            }
172    
173            @Override
174            protected String getIconPath(ThemeDisplay themeDisplay) {
175                    return themeDisplay.getPathThemeImages() + "/common/clip.png";
176            }
177    
178            private static final boolean _LINKABLE = true;
179    
180    }