001    /**
002     * Copyright (c) 2000-2012 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            public AssetRenderer getAssetRenderer(long classPK, int type)
064                    throws PortalException, SystemException {
065    
066                    FileEntry fileEntry = null;
067                    FileVersion fileVersion = null;
068    
069                    if (type == TYPE_LATEST) {
070                            fileVersion = DLAppLocalServiceUtil.getFileVersion(classPK);
071    
072                            fileEntry = fileVersion.getFileEntry();
073                    }
074                    else {
075                            fileEntry = DLAppLocalServiceUtil.getFileEntry(classPK);
076    
077                            fileVersion = fileEntry.getFileVersion();
078                    }
079    
080                    return new DLFileEntryAssetRenderer(fileEntry, fileVersion);
081            }
082    
083            public String getClassName() {
084                    return CLASS_NAME;
085            }
086    
087            @Override
088            public Map<Long, String> getClassTypes(long[] groupIds, Locale locale)
089                    throws Exception {
090    
091                    Map<Long, String> classTypes = new HashMap<Long, String>();
092    
093                    List<DLFileEntryType> dlFileEntryTypes =
094                            DLFileEntryTypeServiceUtil.getFileEntryTypes(groupIds);
095    
096                    for (DLFileEntryType dlFileEntryType: dlFileEntryTypes) {
097                            classTypes.put(
098                                    dlFileEntryType.getFileEntryTypeId(),
099                                    dlFileEntryType.getName());
100                    }
101    
102                    return classTypes;
103            }
104    
105            public String getType() {
106                    return TYPE;
107            }
108    
109            @Override
110            public PortletURL getURLAdd(
111                            LiferayPortletRequest liferayPortletRequest,
112                            LiferayPortletResponse liferayPortletResponse)
113                    throws PortalException, SystemException {
114    
115                    HttpServletRequest request =
116                            liferayPortletRequest.getHttpServletRequest();
117    
118                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
119                            WebKeys.THEME_DISPLAY);
120    
121                    if (!DLPermission.contains(
122                                    themeDisplay.getPermissionChecker(),
123                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_DOCUMENT)) {
124    
125                            return null;
126                    }
127    
128                    long classTypeId = GetterUtil.getLong(
129                            liferayPortletRequest.getAttribute(
130                                    WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
131    
132                    if ((classTypeId > 0) &&
133                            !DLFileEntryTypePermission.contains(
134                                    themeDisplay.getPermissionChecker(), classTypeId,
135                                    ActionKeys.VIEW)) {
136    
137                            return null;
138                    }
139    
140                    PortletURL portletURL = PortletURLFactoryUtil.create(
141                            request, PortletKeys.DOCUMENT_LIBRARY,
142                            getControlPanelPlid(themeDisplay), PortletRequest.RENDER_PHASE);
143    
144                    portletURL.setParameter(
145                            "struts_action", "/document_library/edit_file_entry");
146                    portletURL.setParameter(
147                            "folderId",
148                            String.valueOf(
149                                    AssetPublisherUtil.getRecentFolderId(
150                                            liferayPortletRequest, CLASS_NAME)));
151                    portletURL.setParameter("uploader", "classic");
152    
153                    return portletURL;
154            }
155    
156            @Override
157            public boolean hasPermission(
158                            PermissionChecker permissionChecker, long classPK, String actionId)
159                    throws Exception {
160    
161                    return DLFileEntryPermission.contains(
162                            permissionChecker, classPK, actionId);
163            }
164    
165            @Override
166            public boolean isLinkable() {
167                    return _LINKABLE;
168            }
169    
170            @Override
171            protected String getIconPath(ThemeDisplay themeDisplay) {
172                    return themeDisplay.getPathThemeImages() + "/common/clip.png";
173            }
174    
175            private static final boolean _LINKABLE = true;
176    
177    }