RoleAction.java

package com.mycim.webapp.actions.setting.security.role;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.logging.Logger;
import com.mycim.framework.logging.LoggerFactory;
import com.mycim.framework.utils.beans.BeanUtils;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.framework.utils.lang.collections.CollectionUtils;
import com.mycim.framework.utils.lang.collections.MapUtils;
import com.mycim.framework.utils.lang.math.NumberUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.ObjectList;
import com.mycim.valueobject.bas.Relation;
import com.mycim.valueobject.consts.LinkTypeList;
import com.mycim.valueobject.security.Button;
import com.mycim.valueobject.security.MenuTreeNode;
import com.mycim.valueobject.security.Role;
import com.mycim.webapp.Constants;
import com.mycim.webapp.actions.SystemSetupAction;
import com.mycim.webapp.forms.security.RoleInfoForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 角色定义功能
 *
 * @author Pinyan Song
 * @version 0.0.1
 * @since 2019-8-19 14:33
 **/
public class RoleAction extends SystemSetupAction {

    private static final Logger log = LoggerFactory.getLogger(RoleAction.class);

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm theForm, HttpServletRequest request,
                              HttpServletResponse response) {

        return mapping.getInputForward();
    }

    public Map qryHasRoleGrantUserGroupAll(RoleInfoForm roleInfoForm) {
        Collection roleGrantUserGroupList = null;
        long roleRrn = roleInfoForm.getInstanceRrn();
        String hasUserGroupId = roleInfoForm.getUserGroupId();
        roleGrantUserGroupList = securityService.getHasGrantUserGroupAll(roleRrn, hasUserGroupId);
        Map json = new HashMap();
        json.put("results", roleGrantUserGroupList);
        json.put("rows", roleGrantUserGroupList.size() + "");
        return json;
    }

    public Map qryHasRoleGrantUserAll(RoleInfoForm roleInfoForm) {
        Collection roleGrantUserList = null;
        long roleRrn = roleInfoForm.getInstanceRrn();
        String hasUserId = roleInfoForm.getUserId();
        roleGrantUserList = securityService.getHasRoleGrantUserAll(roleRrn, hasUserId);
        Map json = new HashMap();
        json.put("results", roleGrantUserList);
        json.put("rows", roleGrantUserList.size() + "");
        return json;
    }

    public RoleInfoForm getRole(RoleInfoForm theForm) {
        long roleRrn = theForm.getInstanceRrn();
        Role role = new Role();
        if (roleRrn > 0) {
            role.setInstanceRrn(roleRrn);
            role = securityService.getRole(role);
            if (role != null) {
                BeanUtils.copyProperties(role, theForm);
            }
            theForm.setTransId(Constants.MODIFY_KEY);
        } else {
            role = new Role("", getNamedSpace(ObjectList.ROLE_KEY, LocalContext.getFacilityRrn()), ObjectList.ROLE_KEY);
            BeanUtils.copyProperties(role, theForm);
            theForm.setTransId(Constants.CREATE_KEY);
        }
        return theForm;
    }

    public void saveRole(RoleInfoForm roleForm) {
        String isAdd = roleForm.getTransId();
        String roleId = StringUtils.trimToUpperCase(roleForm.getInstanceId());
        Assert.isFalse(StringUtils.isBlank(roleId),
                       Errors.create().key(MessageIdList.ROLE_MISSING_ID).content("角色id为空或者不存在!").build());
        Role role = new Role(roleId, getNamedSpace(ObjectList.ROLE_KEY, LocalContext.getFacilityRrn()),
                             ObjectList.ROLE_KEY);
        if (StringUtils.isNotBlank(isAdd)) {
            if (StringUtils.trim(isAdd).equals(Constants.CREATE_KEY)) {
                role.setTransId(Constants.CREATE_KEY);
            } else if (StringUtils.trim(isAdd).equals(Constants.MODIFY_KEY)) {
                role.setTransId(Constants.MODIFY_KEY);
            }
        }
        role.setInstanceDesc(roleForm.getInstanceDesc());
        role.setTransPerformedby(LocalContext.getUserId());
        process(role);
    }

    public void saveRoleGrantMenuBtn(Map map) {
        String roleRrn = map.get("roleRrn").toString();
        String menuId = map.get("menuId").toString();
        String buttonsRrns = map.get("buttonRrns").toString();

        Assert.isFalse(StringUtils.isBlank(roleRrn),
                       Errors.create().key(MessageIdList.ROLE_MISSING_RRN).content("roleRrn为空或者不存在!").build());
        List<Relation> relationList = new ArrayList<Relation>();
        Set duplicateToRrn = new HashSet<>();
        if (StringUtils.isNotBlank(menuId)) {
            String[] menuRrns = StringUtils.split(StringUtils.substringBeforeLast(menuId, ","), ",");
            for (int i = 0; i < menuRrns.length; i++) {
                String fromrrn = StringUtils.substringBefore(menuRrns[i], "-");
                String torrn = StringUtils.substringAfter(menuRrns[i], "-");
                //校验重复数据
                if (duplicateToRrn.add(torrn)) {
                    Relation relation = new Relation();
                    relation.setFromRrn(NumberUtils.toLong(roleRrn));
                    relation.setLinkType(LinkTypeList.ROLE_MENU_KEY);
                    relation.setToRrn(NumberUtils.toLong(torrn));
                    relation.setAttributedata1(fromrrn);
                    relationList.add(relation);
                }
            }
        }

        if (StringUtils.isNotBlank(buttonsRrns)) {
            duplicateToRrn.clear();
            String[] buttonRrns = StringUtils.split(StringUtils.substringBeforeLast(buttonsRrns, ","), ",");
            for (int i = 0; i < buttonRrns.length; i++) {
                String fromrrn = roleRrn;
                String torrn = buttonRrns[i];
                if (duplicateToRrn.add(torrn)) {
                    Relation relation = new Relation();
                    relation.setFromRrn(NumberUtils.toLong(roleRrn));
                    relation.setLinkType(LinkTypeList.ROLE_TO_BUTTON_KEY);
                    relation.setToRrn(NumberUtils.toLong(torrn));
                    relation.setAttributedata1(fromrrn);
                    relationList.add(relation);
                }
            }
        }

        securityService.updateRoleMenuBtnRelation(LocalContext.getUserId(), relationList, NumberUtils.toLong(roleRrn));

    }

    public void saveRoleGrantMenu(RoleInfoForm roleForm) {
        long roleRrn = roleForm.getInstanceRrn();
        String menuId = roleForm.getMenuId();
        Assert.isFalse(roleRrn <= 0,
                       Errors.create().key(MessageIdList.ROLE_MISSING_RRN).key("roleRrn为空或者不存在!").build());
        Relation relationDelete = new Relation();
        relationDelete.setTransId(Constants.DELETE_KEY);
        Collection roleGrantMenuList = securityService.getRoleGrantMenu(roleRrn);
        for (Iterator iteratorGrant = roleGrantMenuList.iterator(); iteratorGrant.hasNext(); ) {
            HashMap grantItem = (HashMap) iteratorGrant.next();
            String torrn = grantItem.get("torrn").toString();
            relationDelete.setFromRrn(roleRrn);
            relationDelete.setLinkType(LinkTypeList.ROLE_MENU_KEY);
            relationDelete.setToRrn(new Long(torrn));
            processDetails(relationDelete);
        }

        if (StringUtils.isNotBlank(menuId)) {
            Relation relation = new Relation();
            relation.setTransId(Constants.CREATE_KEY);
            String[] menuRrns = StringUtils.split(StringUtils.substringBeforeLast(menuId, ","), ",");
            for (int i = 0; i < menuRrns.length; i++) {
                relation.setFromRrn(roleRrn);
                relation.setLinkType(LinkTypeList.ROLE_MENU_KEY);
                String fromrrn = StringUtils.substringBefore(menuRrns[i], "-");
                String torrn = StringUtils.substringAfter(menuRrns[i], "-");
                relation.setToRrn(Long.valueOf(torrn));
                relation.setAttributedata1(fromrrn);
                processDetails(relation);
            }
        }
    }

    public void deleteRole(RoleInfoForm theForm) {
        String roleId = theForm.getRoleId();
        Assert.isFalse(StringUtils.isBlank(roleId),
                       Errors.create().key(MessageIdList.ROLE_MISSING_ID).content("角色id为空或者不存在!").build());
        Role role = new Role(roleId, getNamedSpace(ObjectList.ROLE_KEY, LocalContext.getFacilityRrn()),
                             ObjectList.ROLE_KEY);
        role = (Role) getInstance(role);
        role.setTransId(Constants.DELETE_KEY);
        role.setTransPerformedby(LocalContext.getUserId());

        Relation relationDelete = new Relation();
        relationDelete.setTransId(Constants.DELETE_KEY);
        Collection roleGrantMenuList = securityService.getRoleGrantMenu(role.getInstanceRrn());
        for (Iterator iteratorGrant = roleGrantMenuList.iterator(); iteratorGrant.hasNext(); ) {
            HashMap grantItem = (HashMap) iteratorGrant.next();
            relationDelete.setFromRrn(role.getInstanceRrn());
            relationDelete.setLinkType(LinkTypeList.ROLE_MENU_KEY);
            relationDelete.setToRrn(MapUtils.getLong(grantItem, "torrn"));
            processDetails(relationDelete);
        }

        Relation relationDeleteUserGroup = new Relation();
        relationDeleteUserGroup.setTransId(Constants.DELETE_KEY);
        Collection roleGrantUserGroupList = securityService.getRoleGrantUserGroup(role.getInstanceRrn());
        for (Iterator iteratorGrantUserGroup = roleGrantUserGroupList.iterator(); iteratorGrantUserGroup.hasNext(); ) {
            HashMap grantItem = (HashMap) iteratorGrantUserGroup.next();
            relationDeleteUserGroup.setLinkType(LinkTypeList.USERGROUP_ROLE_KEY);
            relationDeleteUserGroup.setToRrn(MapUtils.getLong(grantItem, "torrn"));
            processDetails(relationDeleteUserGroup);
        }

        Relation relationDeleteUser = new Relation();
        relationDeleteUser.setTransId(Constants.DELETE_KEY);
        Collection roleGrantUserList = securityService.getRoleGrantUser(role.getInstanceRrn());
        for (Iterator iteratorGrantUser = roleGrantUserList.iterator(); iteratorGrantUser.hasNext(); ) {
            HashMap grantItem = (HashMap) iteratorGrantUser.next();
            relationDeleteUser.setLinkType(LinkTypeList.USER_ROLE_KEY);
            relationDeleteUser.setToRrn(MapUtils.getLong(grantItem, "torrn"));
            processDetails(relationDeleteUser);
        }

        process(role);

    }

    public Map qryRoleAll() {
        Collection roleList = null;
        roleList = securityService.getAllRole();
        Map json = new HashMap();
        json.put("results", roleList);
        json.put("rows", roleList.size() + "");
        return json;
    }

    public void saveRoleGrantUserGroup(RoleInfoForm roleForm) {
        String userGroupId = roleForm.getUserGroupId();
        long roleRrn = roleForm.getInstanceRrn();
        Assert.isFalse(StringUtils.isEmpty(userGroupId),
                       Errors.create().key(MessageIdList.USERGROUP_MISSING_RRN).content("用户组Rrn为空或者不存在!").build());
        Assert.isFalse(roleRrn <= 0,
                       Errors.create().key(MessageIdList.ROLE_MISSING_RRN_ARRAY).content("角色Rrn数组为空或者不存在!").build());
        Relation relation = new Relation();
        relation.setTransId(Constants.CREATE_KEY);
        String[] userGroupRrns = StringUtils.split(StringUtils.substringBeforeLast(userGroupId, ","), ",");
        for (int i = 0; i < userGroupRrns.length; i++) {
            relation.setFromRrn(new Long(userGroupRrns[i]));
            relation.setLinkType(LinkTypeList.USERGROUP_ROLE_KEY);
            relation.setToRrn(roleRrn);
            processDetails(relation);
        }
    }

    public void saveRoleGrantUser(RoleInfoForm roleForm) {
        String userRrn = roleForm.getUserId();
        long roleRrn = roleForm.getInstanceRrn();

        Assert.isFalse(StringUtils.isBlank(userRrn),
                       Errors.create().key(MessageIdList.USER_MISSING_RRN).content("用户Rrn为空或者不存在!").build());
        Assert.isFalse(roleRrn <= 0,
                       Errors.create().key(MessageIdList.ROLE_MISSING_RRN_ARRAY).content("角色Rrn数组为空或者不存在!").build());
        Relation relation = new Relation();
        relation.setTransId(Constants.CREATE_KEY);
        String[] userRrns = StringUtils.split(StringUtils.substringBeforeLast(userRrn, ","), ",");
        for (int i = 0; i < userRrns.length; i++) {
            relation.setFromRrn(new Long(userRrns[i]));
            relation.setLinkType(LinkTypeList.USER_ROLE_KEY);
            relation.setToRrn(roleRrn);
            processDetails(relation);
        }
    }

    public void deleteHasRoleGrantUserGroup(RoleInfoForm roleForm) {
        String userGroupId = roleForm.getUserGroupId();
        long roleRrn = roleForm.getInstanceRrn();
        Assert.isFalse(StringUtils.isEmpty(userGroupId),
                       Errors.create().key(MessageIdList.USERGROUP_MISSING_RRN).content("用户组Rrn为空或者不存在!").build());
        Assert.isFalse(roleRrn <= 0,
                       Errors.create().key(MessageIdList.ROLE_MISSING_RRN_ARRAY).content("角色Rrn数组为空或者不存在!").build());
        Relation relation = new Relation();
        relation.setTransId(Constants.DELETE_KEY);
        String[] userGroupRrns = StringUtils.split(StringUtils.substringBeforeLast(userGroupId, ","), ",");
        for (int i = 0; i < userGroupRrns.length; i++) {
            relation.setFromRrn(new Long(userGroupRrns[i]));
            relation.setLinkType(LinkTypeList.USERGROUP_ROLE_KEY);
            relation.setToRrn(roleRrn);
            processDetails(relation);
        }
    }

    public void deleteHasRoleGrantUser(RoleInfoForm roleForm) {
        String uerIds = roleForm.getUserId();
        long roleRrn = roleForm.getInstanceRrn();
        Assert.isFalse(StringUtils.isBlank(uerIds),
                       Errors.create().key(MessageIdList.USER_MISSING_RRN).content("用户Rrn为空或者不存在!").build());
        Assert.isFalse(roleRrn <= 0,
                       Errors.create().key(MessageIdList.ROLE_MISSING_RRN_ARRAY).content("角色Rrn数组为空或者不存在!").build());
        Relation relation = new Relation();
        relation.setTransId(Constants.DELETE_KEY);
        String[] userRrns = StringUtils.split(StringUtils.substringBeforeLast(uerIds, ","), ",");
        for (int i = 0; i < userRrns.length; i++) {
            relation.setFromRrn(new Long(userRrns[i]));
            relation.setLinkType(LinkTypeList.USER_ROLE_KEY);
            relation.setToRrn(roleRrn);
            processDetails(relation);
        }
    }

    public Map qryRoleGrantUserGroupAll(RoleInfoForm infoForm) {
        Collection userGroupList = null;
        Collection hasUserGroupList = null;
        String userGroupId = infoForm.getUserGroupId();
        long roleRrn = infoForm.getInstanceRrn();
        Collection tempMenuTree = new ArrayList();
        if (StringUtils.isNotBlank(userGroupId)) {
            userGroupList = securityService.getUserGroupById(StringUtils.trimToUpperCase(userGroupId));
        } else {
            userGroupList = securityService.getUserGroupAll();
            hasUserGroupList = securityService.getHasGrantUserGroupAll(roleRrn, "");
            for (Iterator iterator = userGroupList.iterator(); iterator.hasNext(); ) {
                HashMap grantUser = (HashMap) iterator.next();
                String grantUserId = (String) grantUser.get("instanceId");
                for (Iterator iterator2 = hasUserGroupList.iterator(); iterator2.hasNext(); ) {
                    HashMap hasGrantUser = (HashMap) iterator2.next();
                    String hasGrantUserId = (String) hasGrantUser.get("instanceId");
                    if (grantUserId.equals(hasGrantUserId)) {
                        tempMenuTree.add(grantUser);
                    }
                }
            }
            if (tempMenuTree.size() > 0) {
                userGroupList.removeAll(tempMenuTree);
            }
        }
        Map json = new HashMap();
        json.put("results", userGroupList);
        json.put("rows", userGroupList.size() + "");
        return json;
    }

    public Map qryRoleGrantUserAll(RoleInfoForm roleForm) {
        Collection userList = null;
        Collection hasUserList = null;
        String userid = roleForm.getUserId();
        long roleRrn = roleForm.getInstanceRrn();
        Collection tempMenuTree = new ArrayList();
        if (StringUtils.isNotBlank(userid)) {
            userList = securityService.getUserById(StringUtils.trimToUpperCase(userid), roleRrn);
        } else {
            userList = securityService.getUserAll();
            hasUserList = securityService.getHasRoleGrantUserAll(new Long(roleRrn).longValue(), "");
            for (Iterator iterator = userList.iterator(); iterator.hasNext(); ) {
                HashMap grantUser = (HashMap) iterator.next();
                String grantUserId = (String) grantUser.get("instanceId");
                for (Iterator iterator2 = hasUserList.iterator(); iterator2.hasNext(); ) {
                    HashMap hasGrantUser = (HashMap) iterator2.next();
                    String hasGrantUserId = (String) hasGrantUser.get("instanceId");
                    if (grantUserId.equals(hasGrantUserId)) {
                        tempMenuTree.add(grantUser);
                    }
                }
            }
            if (tempMenuTree.size() > 0) {
                userList.removeAll(tempMenuTree);
            }
        }
        Map json = new HashMap();
        json.put("results", userList);
        json.put("rows", userList.size() + "");
        return json;
    }

    public List<Map<String, Object>> qryRoleGrantTree(RoleInfoForm roleInfoForm) {
        long roleRrn = roleInfoForm.getInstanceRrn();
        Assert.isFalse(roleRrn == 0,
                       Errors.create().key(MessageIdList.ROLE_MISSING_RRN).key("roleRrn为空或者不存在!").build());

        Collection<?> roleGrantMenuList = securityService.getRoleGrantMenu(roleRrn);
        List<MenuTreeNode> rootMenuTree = securityService
                .getRootMenuTree(LocalContext.getFacilityRrn(), I18nUtils.getCurrentLanguage());

        List<Map<String, Object>> menuTree = new ArrayList<>();

        List<Map<String, Object>> nodeChildren;
        Map<Long, List<Map<String, Object>>> nodeChildrens = new HashMap<>();

        Long rootMenuRrn = 0L;
        for (MenuTreeNode menuTreeNode : rootMenuTree) {
            Map<String, Object> node = new HashMap<>();

            if (rootMenuRrn == 0) {
                rootMenuRrn = menuTreeNode.getParentMenuRrn();
            }

            node.put("text", menuTreeNode.getLabelText());
            node.put("parentMenuId", menuTreeNode.getParentMenuId());
            node.put("parentMenuRrn", menuTreeNode.getParentMenuRrn());
            node.put("fromrrn", menuTreeNode.getParentMenuRrn());
            node.put("torrn", menuTreeNode.getMenuRrn());
            node.put("program", menuTreeNode.getUrl());
            node.put("instanceId", menuTreeNode.getMenuId());
            node.put("leaf", menuTreeNode.getIsLeafNode());

            isChecked(roleGrantMenuList, node, menuTreeNode.getMenuRrn(), menuTreeNode.getParentMenuRrn());

            if (!menuTreeNode.getIsLeafNode()) {
                nodeChildren = new ArrayList<>();
                node.put("children", nodeChildren);
                nodeChildrens.put(menuTreeNode.getMenuRrn(), nodeChildren);
            }

            if (rootMenuRrn.equals(menuTreeNode.getParentMenuRrn())) {
                menuTree.add(node);
            } else {
                nodeChildren = nodeChildrens.get(menuTreeNode.getParentMenuRrn());
                nodeChildren.add(node);
            }
        }
        return menuTree;
    }

    public List<Map<String, Object>> buildAllTree(RoleInfoForm roleInfoForm) {
        long roleRrn = roleInfoForm.getInstanceRrn();

        Assert.isFalse(roleRrn == 0,
                       Errors.create().key(MessageIdList.ROLE_MISSING_RRN).key("roleRrn为空或者不存在!").build());

        Collection<?> roleGrantMenuList = securityService.getRoleGrantMenu(roleRrn);
        List<MenuTreeNode> rootMenuTree = securityService
                .getRootMenuTree(LocalContext.getFacilityRrn(), I18nUtils.getCurrentLanguage());
        Collection allButtons = securityService.getAllButtons(null);
        Collection roleButtons = securityService.getRoleButton(roleRrn);

        List<Map<String, Object>> menuTree = new ArrayList<>();

        List<Map<String, Object>> nodeChildren;
        Map<Long, List<Map<String, Object>>> nodeChildrens = new HashMap<>();

        Long rootMenuRrn = 0L;
        for (MenuTreeNode menuTreeNode : rootMenuTree) {
            Map<String, Object> node = new HashMap<>();

            if (rootMenuRrn == 0) {
                rootMenuRrn = menuTreeNode.getParentMenuRrn();
            }

            node.put("text", menuTreeNode.getLabelText());
            node.put("parentMenuId", menuTreeNode.getParentMenuId());
            node.put("parentMenuRrn", menuTreeNode.getParentMenuRrn());
            node.put("fromrrn", menuTreeNode.getParentMenuRrn());
            node.put("torrn", menuTreeNode.getMenuRrn());
            node.put("program", menuTreeNode.getUrl());
            node.put("instanceId", menuTreeNode.getMenuId());
            node.put("leaf", menuTreeNode.getIsLeafNode());
            node.put("jurisdictionType", "menu");

            isChecked(roleGrantMenuList, node, menuTreeNode.getMenuRrn(), menuTreeNode.getParentMenuRrn());

            if (!menuTreeNode.getIsLeafNode()) {
                nodeChildren = new ArrayList<>();
                node.put("children", nodeChildren);
                nodeChildrens.put(menuTreeNode.getMenuRrn(), nodeChildren);
            }

            Collection buttons = getChildButtons(menuTreeNode.getMenuRrn(), allButtons, getStringButtons(roleButtons));

            if (buttons.size() > 0 && node.get("children") == null) {
                node.put("leaf", false);
                node.put("children", buttons);
                node.put("expanded", new Boolean(true));
            }

            if (rootMenuRrn.equals(menuTreeNode.getParentMenuRrn())) {
                menuTree.add(node);
            } else {
                nodeChildren = nodeChildrens.get(menuTreeNode.getParentMenuRrn());
                if (nodeChildren != null) {
                    nodeChildren.add(node);
                }
            }
        }

        return menuTree;
    }

    private void processDetails(Relation relation) {
        String action = relation.getTransId();

        if (action.equals(Constants.CREATE_KEY)) {

            if (baseService.getRelation(relation) != null) {
                return;
            }

            securityService.addRoleRelation(relation);
        } else if (action.equals(Constants.DELETE_KEY)) {

            securityService.deleteRoleRelation(relation);
        }
    }

    private void isChecked(Collection<?> roleGrantMenuList, Map<String, Object> node, Long menuRrn,
                           Long parentMenuRrn) {
        if (CollectionUtils.isEmpty(roleGrantMenuList)) {
            node.put("checked", Boolean.FALSE);
            return;
        }
        for (Iterator<?> iterator = roleGrantMenuList.iterator(); iterator.hasNext(); ) {
            HashMap<?, ?> grantItem = (HashMap<?, ?>) iterator.next();
            Long roleMenuRrn = MapUtils.getLong(grantItem, "torrn");
            Long roleMenuParentMenuRrn = MapUtils.getLong(grantItem, "fromrrn");

            if (roleMenuRrn.equals(menuRrn) && roleMenuParentMenuRrn.equals(parentMenuRrn)) {
                node.put("checked", Boolean.TRUE);
                node.put("expanded", Boolean.TRUE);
                break;
            } else {
                node.put("checked", Boolean.FALSE);
                node.put("expanded", Boolean.FALSE);
            }
        }
    }

    private Collection getChildButtons(Long subMenuRrn, Collection allButtons, String roleButtons) {
        Collection buttons = new ArrayList();
        for (Object obj : allButtons) {
            Button button = (Button) obj;
            if (button.getMenuRrn().equals(subMenuRrn)) {
                Map map = new HashMap();
                if (StringUtils.contains(roleButtons, "{" + button.getInstanceRrn() + "}")) {
                    map.put("checked", new Boolean(true));
                } else {
                    map.put("checked", new Boolean(false));
                }
                map.put("expanded", new Boolean(true));
                map.put("leaf", new Boolean(true));
                map.put("text", button.getInstanceDesc());
                map.put("torrn", button.getInstanceRrn());
                map.put("fromrrn", button.getMenuRrn());
                map.put("jurisdictionType", "button");
                buttons.add(map);
            }
        }

        return buttons;
    }

    private String getStringButtons(Collection roleButtons) {
        StringBuffer str = new StringBuffer();
        for (Object obj : roleButtons) {
            Map map = (Map) obj;
            str.append("{" + MapUtils.getLongValue(map, "buttonRrn") + "}" + ",");
        }
        return str.toString();
    }

    private Collection getChildMenu(Collection allMenuList, Long subMenuRrn, Collection buttons, String roleButtons,
                                    String roleMenus) {
        Collection menuTree = new ArrayList();
        for (Object obj : allMenuList) {
            Map map = (Map) obj;
            String type = MapUtils.getString(map, "objectType");
            if (MapUtils.getLong(map, "fromrrn").equals(subMenuRrn) &&
                    StringUtils.contains(roleMenus, "{" + MapUtils.getLong(map, "torrn") + "}")) {
                map.put("text", MapUtils.getString(map, "instanceDesc"));
                Collection child = new ArrayList();
                if (StringUtils.equalsIgnoreCase("SUBMENU", type)) {
                    child = getChildMenu(allMenuList, MapUtils.getLong(map, "torrn"), buttons, roleButtons, roleMenus);
                } else {
                    child = checkButton(buttons, roleButtons, MapUtils.getLong(map, "torrn"));
                }
                if (child.size() > 0) {
                    map.put("children", child);
                    map.put("expanded", new Boolean(true));
                    menuTree.add(map);
                }
            }
        }
        return menuTree;
    }

    private Collection checkButton(Collection allButtons, String roleButtons, Long menuRrn) {
        Collection buttons = new ArrayList();
        for (Object obj : allButtons) {
            Button button = (Button) obj;
            if (button.getMenuRrn().equals(menuRrn)) {
                Map map = new HashMap();
                if (StringUtils.contains(roleButtons, "{" + button.getInstanceRrn() + "}")) {
                    map.put("checked", new Boolean(true));
                } else {
                    map.put("checked", new Boolean(false));
                }
                map.put("expanded", new Boolean(true));
                map.put("leaf", new Boolean(true));
                map.put("text", button.getInstanceDesc());
                map.put("torrn", button.getInstanceRrn());
                map.put("fromrrn", button.getMenuRrn());
                buttons.add(map);
            }
        }
        return buttons;
    }

    private String getStringMenu(Collection roleMenu) {
        StringBuffer str = new StringBuffer();
        for (Object obj : roleMenu) {
            Map map = (Map) obj;
            str.append("{" + MapUtils.getLongValue(map, "torrn") + "}" + ",");
        }
        return str.toString();
    }

}