com.liferay.layout.admin.web.internal.control.menu.CustomizationSettingsProductNavigationControlMenuEntry.java Source code

Java tutorial

Introduction

Here is the source code for com.liferay.layout.admin.web.internal.control.menu.CustomizationSettingsProductNavigationControlMenuEntry.java

Source

/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package com.liferay.layout.admin.web.internal.control.menu;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.model.Group;
import com.liferay.portal.kernel.model.Layout;
import com.liferay.portal.kernel.model.LayoutTypePortlet;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.service.permission.LayoutPermissionUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.product.navigation.control.menu.BaseJSPProductNavigationControlMenuEntry;
import com.liferay.product.navigation.control.menu.ProductNavigationControlMenuEntry;
import com.liferay.product.navigation.control.menu.constants.ProductNavigationControlMenuCategoryKeys;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
 * @author Julio Camarero
 */
@Component(immediate = true, property = {
        "product.navigation.control.menu.category.key=" + ProductNavigationControlMenuCategoryKeys.TOOLS,
        "product.navigation.control.menu.entry.order:Integer=400" }, service = {
                CustomizationSettingsProductNavigationControlMenuEntry.class,
                ProductNavigationControlMenuEntry.class })
public class CustomizationSettingsProductNavigationControlMenuEntry extends BaseJSPProductNavigationControlMenuEntry
        implements ProductNavigationControlMenuEntry {

    public static final String CUSTOMIZATION_SETTINGS_LAYOUT_UPDATE_PERMISSION = "CUSTOMIZATION_SETTINGS_LAYOUT_UPDATE_PERMISSION";

    @Override
    public String getIconJspPath() {
        return "/control/menu/customization_settings.jsp";
    }

    public boolean hasUpdateLayoutPermission(ThemeDisplay themeDisplay) throws PortalException {

        if (LayoutPermissionUtil.contains(themeDisplay.getPermissionChecker(), themeDisplay.getLayout(),
                ActionKeys.UPDATE)) {

            return true;
        }

        return false;
    }

    @Override
    public boolean includeIcon(HttpServletRequest request, HttpServletResponse response) throws IOException {

        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

        try {
            request.setAttribute(CUSTOMIZATION_SETTINGS_LAYOUT_UPDATE_PERMISSION,
                    hasUpdateLayoutPermission(themeDisplay));
        } catch (PortalException pe) {
            _log.error(pe, pe);
        }

        return super.includeIcon(request, response);
    }

    @Override
    public boolean isShow(HttpServletRequest request) throws PortalException {
        Boolean show = (Boolean) request.getAttribute(_SHOW);

        if (show != null) {
            return show;
        }

        show = _isShow(request);

        request.setAttribute(_SHOW, show);

        return show;
    }

    @Override
    @Reference(target = "(osgi.web.symbolicname=com.liferay.layout.admin.web)", unbind = "-")
    public void setServletContext(ServletContext servletContext) {
        super.setServletContext(servletContext);
    }

    protected boolean isCustomizableLayout(ThemeDisplay themeDisplay) throws PortalException {

        Layout layout = themeDisplay.getLayout();

        Group group = layout.getGroup();

        if (group.isLayoutPrototype() || group.isLayoutSetPrototype() || group.isStagingGroup()
                || group.isUserGroup()) {

            return false;
        }

        LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet();

        if (!layout.isTypePortlet() || (layoutTypePortlet == null)) {
            return false;
        }

        if (layout.isCustomizable() && hasUpdateLayoutPermission(themeDisplay)) {

            return true;
        }

        if (!layoutTypePortlet.isCustomizable()) {
            return false;
        }

        if (!LayoutPermissionUtil.containsWithoutViewableGroup(themeDisplay.getPermissionChecker(), layout, false,
                ActionKeys.CUSTOMIZE)) {

            return false;
        }

        return true;
    }

    private boolean _isShow(HttpServletRequest request) throws PortalException {
        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

        Layout layout = themeDisplay.getLayout();

        if (layout.isTypeControlPanel()) {
            return false;
        }

        if (!isCustomizableLayout(themeDisplay)) {
            return false;
        }

        return super.isShow(request);
    }

    private static final String _SHOW = CustomizationSettingsProductNavigationControlMenuEntry.class + "#_SHOW";

    private static final Log _log = LogFactoryUtil
            .getLog(CustomizationSettingsProductNavigationControlMenuEntry.class);

}