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.imagegallery.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.security.permission.PermissionChecker;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortletKeys;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.asset.model.AssetRenderer;
027    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
028    import com.liferay.portlet.assetpublisher.util.AssetPublisherUtil;
029    import com.liferay.portlet.imagegallery.model.IGImage;
030    import com.liferay.portlet.imagegallery.service.IGImageLocalServiceUtil;
031    import com.liferay.portlet.imagegallery.service.permission.IGImagePermission;
032    import com.liferay.portlet.imagegallery.service.permission.IGPermission;
033    
034    import javax.portlet.PortletURL;
035    
036    /**
037     * @author Julio Camarero
038     * @author Juan Fernández
039     * @author Raymond Augé
040     */
041    public class IGImageAssetRendererFactory extends BaseAssetRendererFactory {
042    
043            public static final String CLASS_NAME = IGImage.class.getName();
044    
045            public static final String TYPE = "image";
046    
047            public AssetRenderer getAssetRenderer(long classPK, int type)
048                    throws PortalException, SystemException {
049    
050                    IGImage image = IGImageLocalServiceUtil.getImage(classPK);
051    
052                    return new IGImageAssetRenderer(image);
053            }
054    
055            public String getClassName() {
056                    return CLASS_NAME;
057            }
058    
059            public String getType() {
060                    return TYPE;
061            }
062    
063            public PortletURL getURLAdd(
064                    LiferayPortletRequest liferayPortletRequest,
065                    LiferayPortletResponse liferayPortletResponse) {
066    
067                    ThemeDisplay themeDisplay =
068                            (ThemeDisplay)liferayPortletRequest.getAttribute(
069                                    WebKeys.THEME_DISPLAY);
070    
071                    PortletURL addAssetURL = null;
072    
073                    if (IGPermission.contains(
074                                    themeDisplay.getPermissionChecker(),
075                                    themeDisplay.getScopeGroupId(), ActionKeys.ADD_IMAGE)) {
076    
077                            addAssetURL = liferayPortletResponse.createRenderURL(
078                                    PortletKeys.IMAGE_GALLERY);
079    
080                            addAssetURL.setParameter(
081                                    "struts_action", "/image_gallery/edit_image");
082                            addAssetURL.setParameter(
083                                    "groupId", String.valueOf(themeDisplay.getScopeGroupId()));
084                            addAssetURL.setParameter(
085                                    "folderId",
086                                    String.valueOf(
087                                            AssetPublisherUtil.getRecentFolderId(
088                                                    liferayPortletRequest, CLASS_NAME)));
089                            addAssetURL.setParameter("uploader", "classic");
090                    }
091    
092                    return addAssetURL;
093            }
094    
095            public boolean hasPermission(
096                            PermissionChecker permissionChecker, long classPK, String actionId)
097                    throws Exception {
098    
099                    return IGImagePermission.contains(
100                            permissionChecker, classPK, actionId);
101            }
102    
103            protected String getIconPath(ThemeDisplay themeDisplay) {
104                    return themeDisplay.getPathThemeImages() + "/file_system/small/bmp.png";
105            }
106    
107    }