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

org.xlcloud.console.entitlements.controllers.EntitlementDictionaryBean 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.entitlements.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.xlcloud.console.context.EntitlementEngine;
import org.xlcloud.console.context.IdentityContext;
import org.xlcloud.console.controllers.request.RequestParameters;
import org.xlcloud.console.controllers.request.path.ConsolePathParams;
import org.xlcloud.console.entitlements.EntitlementDictionaryRepository;
import org.xlcloud.console.entitlements.ExplicitEntitlementDictionary;
import org.xlcloud.console.entitlements.nodes.EntitlementNode;
import org.xlcloud.console.entitlements.nodes.predicates.EntitlementAvailabilityPredicate;
import org.xlcloud.console.entitlements.scope.predicates.EntitlementDictionaryPredicate;
import org.xlcloud.console.extensions.scope.ViewScoped;

/**
 * The controller class, which is used to render and edit the entitlements tree.
 * @author "Konrad Król", AMG.net
 */
@Named
@ViewScoped
public class EntitlementDictionaryBean {
    
    @Inject
    private EntitlementDictionaryRepository repository;

    @Inject
    private EntitlementEngine entitlementEngine;
    
    @Inject
    private IdentityContext identityContext;
    
    @Inject
    private RequestParameters requestParameters;

    private TreeNode root;

    private TreeNode[] selectedNodes;

    private EntitlementDictionaryPredicate predicate;
    
    private Long requestedAccountId;
    
    /**
     * Initializes the bean, by storing the requested account id.
     */
    @PostConstruct
    public void initialize() {
        if(requestParameters.hasParameter(ConsolePathParams.ACCOUNT_ID)) {
            requestedAccountId = requestParameters.getParameterAsLong(ConsolePathParams.ACCOUNT_ID);
        }
    }

    /**
     * @return selected tree nodes
     */
    public TreeNode[] getSelectedNodes() {
        return selectedNodes;
    }

    /**
     * Sets the value of selected tree nodes.
     * @param selectedNodes
     */
    public void setSelectedNodes(TreeNode[] selectedNodes) {
        this.selectedNodes = selectedNodes;
    }

    /**
     * @return the root node, which contains other tree nodes, representing entitlement dictionaries
     *  from the {@link EntitlementDictionaryRepository}
     */
    public TreeNode getRoot() {
        if(root == null) {
            rebuildTree();
        }
        
        return root;
    }

    /**
     * Converts selected tree nodes to the list of explicit entitlement dictionaries.
     * @return selected explicit entitlement dictionaries
     */
    public List getSelectedExplicitEntitlementDictionaries() {
        List entitlementDictionaries = new ArrayList();

        for (TreeNode treeNode : selectedNodes) {
            if (treeNode.getData() instanceof ExplicitEntitlementDictionary) {
                entitlementDictionaries.add((ExplicitEntitlementDictionary) treeNode.getData());
            }
        }

        return entitlementDictionaries;
    }

    /**
     * Sets the predicate for entitlement visibility.
     * When the tree is rebuild it verifies if each entitlement matches the given predicate.
     * If not it is not presented in the tree.
     * It is useful to exclude entitlement dictionaries that can not be assigned to the specific subject
     *  (global user, account user, group, etc).
     * @param predicate
     */
    public void setPredicate(EntitlementDictionaryPredicate predicate) {
        if(this.predicate != predicate) {
            this.predicate = predicate;
            rebuildTree();
        }
    }
    
    /**
     * Sets the value of {@link #repository}.
     * @param repository repository
     */
    public void setRepository(EntitlementDictionaryRepository repository) {
        this.repository = repository;
    }

    /**
     * Sets the value of {@link #entitlementEngine}.
     * @param entitlementEngine entitlement engine
     */
    public void setEntitlementEngine(EntitlementEngine entitlementEngine) {
        this.entitlementEngine = entitlementEngine;
    }
    
    /**
     * @return the value of the requested account id
     */
    public Long getRequestedAccountId() {
        return requestedAccountId;
    }

    private void rebuildTree() {
        root = new DefaultTreeNode();
        EntitlementNode highLevelNode = new EntitlementNode(repository.getMainEntitlementDictionary(),
                new EntitlementAvailabilityPredicate(entitlementEngine, identityContext, predicate), root);
        initializeSelection(root);
        highLevelNode.setExpanded(true);
    }
    
    private void initializeSelection(TreeNode node) {
        if(node.getData() instanceof ExplicitEntitlementDictionary) {
            if(((ExplicitEntitlementDictionary) node.getData()).isInitialDefault()) {
                node.setSelected(true);
            }
        } else {
            for(TreeNode child : node.getChildren()) {
                initializeSelection(child);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy