001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.Map;
044    
045    import javax.portlet.PortletRequest;
046    import javax.portlet.PortletURL;
047    
048    import javax.servlet.http.HttpServletRequest;
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);
080            }
081    
082            public String getClassName() {
083                    return CLASS_NAME;
084            }
085    
086            @Override
087            public Map<Long, String> getClassTypes(long[] groupIds) throws Exception {
088                    Map<Long, String> classTypes = new HashMap<Long, String>();
089    
090                    List<DLFileEntryType> dlFileEntryTypes =
091                            DLFileEntryTypeServiceUtil.getFileEntryTypes(groupIds);
092    
093                    for (DLFileEntryType dlFileEntryType: dlFileEntryTypes) {
094                            classTypes.put(
095                                    dlFileEntryType.getFileEntryTypeId(),
096                                    dlFileEntryType.getName());
097                    }
098    
099                    return classTypes;
100            }
101    
102            public String getType() {
103                    return TYPE;
104            }
105    
106            @Override
107            public PortletURL getURLAdd(
108                            LiferayPortletRequest liferayPortletRequest,
109                            LiferayPortletResponse liferayPortletResponse)
110                    throws PortalException, SystemException {
111    
112                    HttpServletRequest request =
113                            liferayPortletRequest.getHttpServletRequest();
114    
115                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
116                            WebKeys.THEME_DISPLAY);
117    
118                    if (!DLPermission.contains(
119                                    themeDisplay.getPermissionChecker(),
120                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_DOCUMENT)) {
121    
122                            return null;
123                    }
124    
125                    long classTypeId = GetterUtil.getLong(
126                            liferayPortletRequest.getAttribute(
127                                    WebKeys.ASSET_RENDERER_FACTORY_CLASS_TYPE_ID));
128    
129                    if ((classTypeId > 0) &&
130                            !DLFileEntryTypePermission.contains(
131                                    themeDisplay.getPermissionChecker(), classTypeId,
132                                    ActionKeys.VIEW)) {
133    
134                            return null;
135                    }
136    
137                    PortletURL portletURL = PortletURLFactoryUtil.create(
138                            request, PortletKeys.DOCUMENT_LIBRARY,
139                            getControlPanelPlid(themeDisplay), PortletRequest.RENDER_PHASE);
140    
141                    portletURL.setParameter(
142                            "struts_action", "/document_library/edit_file_entry");
143                    portletURL.setParameter(
144                            "folderId",
145                            String.valueOf(
146                                    AssetPublisherUtil.getRecentFolderId(
147                                            liferayPortletRequest, CLASS_NAME)));
148                    portletURL.setParameter("uploader", "classic");
149    
150                    return portletURL;
151            }
152    
153            @Override
154            public boolean hasPermission(
155                            PermissionChecker permissionChecker, long classPK, String actionId)
156                    throws Exception {
157    
158                    return DLFileEntryPermission.contains(
159                            permissionChecker, classPK, actionId);
160            }
161    
162            @Override
163            public boolean isLinkable() {
164                    return _LINKABLE;
165            }
166    
167            @Override
168            protected String getIconPath(ThemeDisplay themeDisplay) {
169                    return themeDisplay.getPathThemeImages() + "/common/clip.png";
170            }
171    
172            private static final boolean _LINKABLE = true;
173    
174    }