All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xlcloud.console.menu.MenuActivationBean Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.xlcloud.console.menu;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.xlcloud.console.context.EntitlementEngine;

/**
 * This class is used to identify which menu item is active.
 * Active menu item gets the "menu-item active" html class property.
 * It also indicates which menu group contains active element and should be expanded.
 * 
 * @author Konrad Król, AMG.net
 */
@Named
@RequestScoped
public class MenuActivationBean {
    
    @Inject
    private MenuCollectorBean menuCollectorBean;
    
    @Inject
    private EntitlementEngine entitlementEngine;
    
    private SortedSet filteredGroups;
    
    private MenuItem activeMenuItem;
    
    private MenuGroup activeMenuGroup;
    
    private int activeMenuGroupIndex;
    
    /**
     * Initializes bean and finds active menu item.
     */
    @PostConstruct
    public void initialize() {
        String servletPath = getServletPathFromRequest();
        
        filteredGroups = new TreeSet();
        CollectionUtils.select(menuCollectorBean.getGroups(), new MenuGroupAllowancePredicate(entitlementEngine), filteredGroups);
        
        activeMenuItem = menuCollectorBean.getActiveMenuItem(servletPath);
        
        if (activeMenuItem != null) {
            activeMenuGroupIndex = getIndexOfMenuGroupByLabel(activeMenuItem.getGroupLabel().getLabel());
        }
    }
    
    /**
     * Gets the value of {@link #activeMenuItem}.
     * @return value of {@link #activeMenuItem}
     */
    public MenuItem getActiveMenuItem() {
        return activeMenuItem;
    }
    
    /**
     * Gets the value of {@link #activeMenuGroup}.
     * @return value of {@link #activeMenuGroup}
     */
    public MenuGroup getActiveMenuGroup() {
        return activeMenuGroup;
    }
    
    /**
     * Gets the value of {@link #activeMenuGroupIndex}.
     * @return value of {@link #activeMenuGroupIndex}
     */
    public int getActiveMenuGroupIndex() {
        return activeMenuGroupIndex;
    }
    
    /**
     * Sets the value of {@link #activeMenuGroupIndex}.
     * @param activeMenuGroupIndex - value
     */
    public void setActiveMenuGroupIndex(int activeMenuGroupIndex) {
        this.activeMenuGroupIndex = activeMenuGroupIndex;
    }
    
    /**
     * Returns menu groups filtered by {@link MenuGroupAllowancePredicate}.
     * @return filtered groups
     */
    public List getFilteredGroups() {
        return new ArrayList<>(filteredGroups);
    }
    
    /**
     * Returns html class property that indicates, whether menu item is active or not.
     * @param menuItem menu item
     * @return {@code "menu-item active"} when the menu item is active, {@code "menu-item inactive"} otherwise.
     */
    public String activationClassForItem(MenuItem menuItem) {
        return (menuItem.equals(activeMenuItem) ? "menu-item active" : "menu-item inactive");
    }
    
    /**
     * Rerturns the list of filtered menu items for the specified group.
     * @param menuGroup menu group
     * @return filtered menu items
     */
    public List getFilteredMenuItemsFor(MenuGroup menuGroup) {
        return menuGroup.getFilteredMenuItems(entitlementEngine);
    }
    
    /**
     * Sets the value of {@link #menuCollectorBean}.
     * @param menuCollectorBean - value
     */
    public void setMenuCollectorBean(MenuCollectorBean menuCollectorBean) {
        this.menuCollectorBean = menuCollectorBean;
    }
    
    /**
     * Sets the value of {@link #entitlementEngine}.
     * @param entitlementEngine - value
     */
    public void setEntitlementEngine(EntitlementEngine entitlementEngine) {
        this.entitlementEngine = entitlementEngine;
    }

    private static String getServletPathFromRequest() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
        return request.getServletPath();
    }
    
    private int getIndexOfMenuGroupByLabel(String requestedGroupLabel) {
        List filteredGroupLabels = new ArrayList();
        CollectionUtils.collect(filteredGroups, new MenuGroupLabelTransformer(), filteredGroupLabels);
        
        return filteredGroupLabels.indexOf(requestedGroupLabel);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy