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

Java tutorial

Introduction

Here is the source code for com.liferay.layout.admin.web.internal.control.menu.ToggleControlsProductNavigationControlMenuEntry.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.language.LanguageUtil;
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.service.permission.PortletPermissionUtil;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ResourceBundleUtil;
import com.liferay.portal.kernel.util.SessionClicks;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.util.PropsValues;
import com.liferay.product.navigation.control.menu.BaseProductNavigationControlMenuEntry;
import com.liferay.product.navigation.control.menu.ProductNavigationControlMenuEntry;
import com.liferay.product.navigation.control.menu.constants.ProductNavigationControlMenuCategoryKeys;

import java.util.Collections;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;

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

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

    @Override
    public Map<String, Object> getData(HttpServletRequest request) {
        return _data;
    }

    @Override
    public String getIcon(HttpServletRequest request) {
        String stateCss = null;

        String toggleControls = GetterUtil
                .getString(SessionClicks.get(request, "com.liferay.frontend.js.web_toggleControls", "visible"));

        if (toggleControls.equals("visible")) {
            stateCss = "view";
        } else {
            stateCss = "hidden";
        }

        return stateCss;
    }

    @Override
    public String getLabel(Locale locale) {
        ResourceBundle resourceBundle = ResourceBundleUtil.getBundle("content.Language", locale, getClass());

        return LanguageUtil.get(resourceBundle, "toggle-controls");
    }

    @Override
    public String getLinkCssClass(HttpServletRequest request) {
        return "toggle-controls visible-xs";
    }

    @Override
    public String getURL(HttpServletRequest request) {
        return "javascript:;";
    }

    @Override
    public boolean isShow(HttpServletRequest request) throws PortalException {
        ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);

        Layout layout = themeDisplay.getLayout();

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

        Group group = layout.getGroup();

        if (group.hasStagingGroup() && !group.isStagingGroup() && PropsValues.STAGING_LIVE_GROUP_LOCKING_ENABLED) {

            return false;
        }

        if (!(hasUpdateLayoutPermission(themeDisplay) || hasCustomizePermission(themeDisplay)
                || hasPortletConfigurationPermission(themeDisplay))) {

            return false;
        }

        return super.isShow(request);
    }

    protected boolean hasCustomizePermission(ThemeDisplay themeDisplay) throws PortalException {

        Layout layout = themeDisplay.getLayout();
        LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet();

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

        if (!layoutTypePortlet.isCustomizable() || !layoutTypePortlet.isCustomizedView()) {

            return false;
        }

        if (LayoutPermissionUtil.contains(themeDisplay.getPermissionChecker(), layout, ActionKeys.CUSTOMIZE)) {

            return true;
        }

        return false;
    }

    protected boolean hasPortletConfigurationPermission(ThemeDisplay themeDisplay) throws PortalException {

        return PortletPermissionUtil.hasConfigurationPermission(themeDisplay.getPermissionChecker(),
                themeDisplay.getSiteGroupId(), themeDisplay.getLayout(), ActionKeys.CONFIGURATION);
    }

    protected boolean hasUpdateLayoutPermission(ThemeDisplay themeDisplay) throws PortalException {

        return LayoutPermissionUtil.contains(themeDisplay.getPermissionChecker(), themeDisplay.getLayout(),
                ActionKeys.UPDATE);
    }

    private static final Map<String, Object> _data = Collections.<String, Object>singletonMap("qa-id",
            "showControls");

}