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