com.clustercontrol.accesscontrol.factory.RoleSelector.java Source code

Java tutorial

Introduction

Here is the source code for com.clustercontrol.accesscontrol.factory.RoleSelector.java

Source

/*
    
Copyright (C) since 2006 NTT DATA Corporation
    
This program is free software; you can redistribute it and/or
Modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, version 2.
    
This program 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 General Public License for more details.
    
 */

package com.clustercontrol.accesscontrol.factory;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.clustercontrol.accesscontrol.bean.ObjectPrivilegeFilterInfo;
import com.clustercontrol.accesscontrol.bean.RoleIdConstant;
import com.clustercontrol.accesscontrol.bean.RoleSettingTreeConstant;
import com.clustercontrol.accesscontrol.bean.RoleTreeItem;
import com.clustercontrol.accesscontrol.model.ObjectPrivilegeInfo;
import com.clustercontrol.accesscontrol.model.RoleInfo;
import com.clustercontrol.accesscontrol.model.SystemPrivilegeInfo;
import com.clustercontrol.accesscontrol.model.UserInfo;
import com.clustercontrol.accesscontrol.util.QueryUtil;
import com.clustercontrol.fault.HinemosUnknown;
import com.clustercontrol.fault.PrivilegeNotFound;
import com.clustercontrol.fault.RoleNotFound;
import com.clustercontrol.fault.UserNotFound;
import com.clustercontrol.util.MessageConstant;

/**
 * ?<BR>
 *
 * @version 1.0.0
 * @since 3.2.0
 */
public class RoleSelector {

    /** ? */
    private static Log m_log = LogFactory.getLog(RoleSelector.class);

    /**
     * ??<BR>
     * 
     * @return ?
     * @throws UserNotFound
     * @throws HinemosUnknown
     */
    public static ArrayList<RoleInfo> getRoleInfoList() {
        m_log.debug("getting all role...");

        // ?
        List<RoleInfo> roles = QueryUtil.getAllShowRole();

        m_log.debug("successful in getting all role.");
        if (roles == null || roles.isEmpty()) {
            return new ArrayList<>();
        } else {
            return new ArrayList<>(roles);
        }
    }

    /**
     * ???<BR>
     * 
     * @return 
     * @throws HinemosUnknown
     * @throws RoleNotFound 
     */
    public static RoleInfo getRoleInfo(String roleId) throws HinemosUnknown, RoleNotFound {
        if (roleId == null || roleId.isEmpty())
            return null;

        try {
            // ?
            return QueryUtil.getRolePK(roleId);
        } catch (RoleNotFound e) {
            throw e;
        } catch (Exception e) {
            m_log.warn("getRoleInfo() failure to get user. : userId = " + roleId, e);
            throw new HinemosUnknown(e.getMessage() + " : failure to get role.", e);
        }
    }

    /**
     * ????
     * <P>
     * <ol>
     * <li>?(?)?????</li>
     * <li>???</li>
     * <li>????????????</li>
     * </ol>
     * 
     * @param locale 
     * @param userId ?ID
     * @return {@link com.clustercontrol.accesscontrol.bean.RoleTreeItem}?
     * @throws UserNotFound
     * @throws HinemosUnknown 
     * 
     */
    public RoleTreeItem getRoleTree(Locale locale, String userId) throws UserNotFound, HinemosUnknown {

        // ??
        RoleTreeItem top = new RoleTreeItem();

        // ?
        RoleInfo rootRoleItem = new RoleInfo();
        rootRoleItem.setRoleId(RoleSettingTreeConstant.ROOT_ID);
        rootRoleItem.setRoleName(MessageConstant.ROLE.getMessage());
        RoleTreeItem root = new RoleTreeItem(top, rootRoleItem);

        // ?
        List<RoleInfo> list = QueryUtil.getAllShowRole();
        if (list != null) {
            m_log.debug("roleList size = " + list.size());
        } else {
            throw new HinemosUnknown("roleList is null");
        }

        for (RoleInfo roleInfo : list) {
            if (roleInfo.getUserInfoList() != null) {
                m_log.debug("userList size = " + roleInfo.getUserInfoList().size());
            }

            // ?RoleTreeItem?
            RoleTreeItem role = new RoleTreeItem(root, roleInfo);

            for (UserInfo userInfo : roleInfo.getUserInfoList()) {
                // ?RoleTreeItem?
                new RoleTreeItem(role, userInfo);
            }
        }
        removeParent(top);
        return top;
    }

    /**
     * ??ID??<BR>
     * 
     * @param ID???????ADMINISTRATORS
     * @return ID
     * @throws HinemosUnknown
     */
    public static ArrayList<String> getOwnerRoleIdList(String userId) throws HinemosUnknown {

        ArrayList<String> list = new ArrayList<String>();
        List<RoleInfo> roleList = null;
        if (userId == null || userId.isEmpty()) {
            // ?
            roleList = QueryUtil.getAllShowRole();
        } else {
            try {
                // ?
                UserInfo userInfo = QueryUtil.getUserPK(userId);
                if (userInfo != null) {
                    roleList = userInfo.getRoleList();
                }
            } catch (UserNotFound e) {
                // ???
            } catch (Exception e) {
                m_log.warn("getUserRoleInfo() failure to get user. : userId = " + userId, e);
                throw new HinemosUnknown(e.getMessage() + " : failure to get user.", e);
            }
        }
        if (roleList != null && roleList.size() > 0) {
            Iterator<RoleInfo> itr = roleList.iterator();
            while (itr.hasNext()) {
                RoleInfo roleInfo = itr.next();
                if (!roleInfo.getRoleId().equals(RoleIdConstant.INTERNAL)) {
                    list.add(roleInfo.getRoleId());
                }
            }
        }
        return list;
    }

    /**
     * web????????????????????
     * ?????
     * ????
     * @param roleSettinsgTreeItem
     */
    private void removeParent(RoleTreeItem roleSettinsgTreeItem) {
        roleSettinsgTreeItem.setParent(null);
        for (RoleTreeItem child : roleSettinsgTreeItem.getChildren()) {
            removeParent(child);
        }
    }

    /**
     * ???<BR>
     * 
     * @return ??
     * @throws HinemosUnknown
     */
    public static ArrayList<SystemPrivilegeInfo> getSystemPrivilegeInfoList() {

        ArrayList<SystemPrivilegeInfo> list = new ArrayList<SystemPrivilegeInfo>();
        List<SystemPrivilegeInfo> systemPrivileges = null;

        m_log.debug("getting all system privilege...");

        // ?
        systemPrivileges = QueryUtil.getAllSystemPrivilege();

        if (systemPrivileges != null && systemPrivileges.size() > 0) {
            list.addAll(systemPrivileges);
        }

        m_log.debug("successful in getting all system privilege.");
        return list;
    }

    /**
     * ??ID???????<BR>
     * 
     * @param userId ID
     * @return ??
     * @throws HinemosUnknown
     */
    public static ArrayList<SystemPrivilegeInfo> getSystemPrivilegeInfoListByUserId(String userId) {
        // ?
        return QueryUtil.getSystemPrivilegeByUserId(userId);
    }

    /**
     * ?????????<BR>
     * 
     * @param editType 
     * @return ??
     * @throws HinemosUnknown
     */
    public static ArrayList<SystemPrivilegeInfo> getSystemPrivilegeInfoListByEditType(String editType) {
        // ?
        List<SystemPrivilegeInfo> list = QueryUtil.getSystemPrivilegeByEditType(editType);

        if (list == null || list.isEmpty()) {
            return new ArrayList<>();
        } else {
            return new ArrayList<>(list);
        }

    }

    /**
     * ????<BR>
     * 
     * @param objectType
     * @param objectId
     * @param roleId
     * @param objectPrivilege
     * @return ?
     * @throws HinemosUnknown
     */
    public static ObjectPrivilegeInfo getObjectPrivilegeInfo(String objectType, String objectId, String roleId,
            String objectPrivilege) throws HinemosUnknown {

        ObjectPrivilegeInfo info = null;

        if (objectType != null && objectType.compareTo("") != 0 && objectId != null && objectId.compareTo("") != 0
                && roleId != null && roleId.compareTo("") != 0 && objectPrivilege != null
                && objectPrivilege.compareTo("") != 0) {
            try {
                // ??
                info = QueryUtil.getObjectPrivilegePK(objectType, objectId, roleId, objectPrivilege);
            } catch (PrivilegeNotFound e) {
                // ???
            } catch (Exception e) {
                m_log.warn("getRoleInfo() failure to get user. : userId = " + roleId, e);
                throw new HinemosUnknown(e.getMessage() + " : failure to get role.", e);
            }
        }
        return info;
    }

    /**
     * ???<BR>
     * 
     * @return ??
     * @throws HinemosUnknown
     */
    public static ArrayList<ObjectPrivilegeInfo> getObjectPrivilegeInfoList(ObjectPrivilegeFilterInfo filter) {

        ArrayList<ObjectPrivilegeInfo> list = new ArrayList<ObjectPrivilegeInfo>();
        List<ObjectPrivilegeInfo> objectPrivileges = null;

        if (filter == null) {
            // ?
            objectPrivileges = QueryUtil.getAllObjectPrivilege();
        } else {
            // ?
            objectPrivileges = QueryUtil.getAllObjectPrivilegeByFilter(filter.getObjectType(), filter.getObjectId(),
                    filter.getRoleId(), filter.getObjectPrivilege());
        }

        if (objectPrivileges != null && objectPrivileges.size() > 0) {
            list.addAll(objectPrivileges);
        }
        return list;
    }

}