/** * 获取子级权限 * * @param permissions * @param parentId * @return */ public static List<Map<String, Object>> getChildrenPermissions( List<Permission> permissions, Integer parentId, Set<Integer> checkedPermissionIdSet, List<Integer> childrenIdList) { List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>(); for (Permission permission : permissions) { if ((parentId == null && permission.getParentId() == null) || (parentId != null && permission.getParentId() != null && permission.getParentId().intValue() == parentId.intValue())) { if (childrenIdList == null || !childrenIdList.contains(permission.getId())) { Map<String, Object> map = new HashMap<String, Object>(); map.put("id", permission.getId()); map.put("text", permission.getName()); map.put("key", permission.getKey()); map.put( "checked", checkedPermissionIdSet == null ? false : checkedPermissionIdSet.contains(permission.getId())); map.put("order", permission.getOrder()); map.put( "children", getChildrenPermissions( permissions, permission.getId(), checkedPermissionIdSet, childrenIdList)); mapList.add(map); } } } return mapList; }
/** * 获取子级权限id集合 * * @param parentId * @param permissions * @return */ public static List<Integer> getChildrenPermissionIds( Integer parentId, List<Permission> permissions) { List<Integer> list = new ArrayList<Integer>(); for (Permission permission : permissions) { if ((parentId == null && permission.getParentId() == null) || (parentId != null && permission.getParentId() != null && parentId.intValue() == permission.getParentId().intValue())) { list.add(permission.getId()); list.addAll(getChildrenPermissionIds(permission.getId(), permissions)); } } return list; }
/** * 获取根权限 * * @param permissionList * @return */ private List<Permission> getRootPermissions(List<Permission> permissionList) { List<Permission> rootPermissions = new ArrayList<Permission>(); for (Permission permission : permissionList) { // 如果父级id是null if (permission.getParentId() == null) { rootPermissions.add(permission); continue; } Permission parentPermission = permissionDao.get(permissionList, permission.getParentId().intValue()); if (parentPermission == null) { rootPermissions.add(permission); } } return rootPermissions; }