资源树子节点存在权限需要保留父节点,子节点不存在权限则删除子节点。

该博客主要展示了如何使用Java代码构建树形结构数据,并进行权限分配。通过创建`MetricRepoTree`对象,建立父子节点关系,并根据给定的权限映射更新每个节点的权限。代码中涉及了集合操作、递归方法调用,以及对权限的处理逻辑,适用于数据权限管理场景。
摘要由CSDN通过智能技术生成
直接撸代码
```java
public static void main(String[] args) {
        ArrayList<MetricRepoTree> parentList = Lists.newArrayList();
        ArrayList<MetricRepoTree> result = Lists.newArrayList();
        ArrayList<MetricRepoTree> child1List = Lists.newArrayList();
        ArrayList<MetricRepoTree> subChild1List = Lists.newArrayList();
        ArrayList<MetricRepoTree> child2List = Lists.newArrayList();
        MetricRepoTree metricRepoTreeParent1 = new MetricRepoTree();
        MetricRepoTree metricRepoTreeParent2 = new MetricRepoTree();
        metricRepoTreeParent1.setId("111");
        metricRepoTreeParent2.setId("222");
        parentList.add(metricRepoTreeParent1);
        parentList.add(metricRepoTreeParent2);
        MetricRepoTree metricRepoTreeChild11 = new MetricRepoTree();
        MetricRepoTree metricRepoTreeSubChild111 = new MetricRepoTree();
        MetricRepoTree metricRepoTreeSubChild112 = new MetricRepoTree();
        metricRepoTreeSubChild111.setId("111-111-111");
        metricRepoTreeSubChild112.setId("111-111-222");
        subChild1List.add(metricRepoTreeSubChild111);
        subChild1List.add(metricRepoTreeSubChild112);
        metricRepoTreeChild11.setChildren(subChild1List);
        MetricRepoTree metricRepoTreeChild12 = new MetricRepoTree();
        MetricRepoTree metricRepoTreeSubChild121 = new MetricRepoTree();
        MetricRepoTree metricRepoTreeSubChild122 = new MetricRepoTree();
        metricRepoTreeSubChild121.setId("111-222-111");
        metricRepoTreeSubChild122.setId("111-222-222");
        metricRepoTreeChild12.setChildren(Lists.newArrayList(metricRepoTreeSubChild121,metricRepoTreeSubChild122));
        metricRepoTreeChild11.setId("111-111");
        metricRepoTreeChild12.setId("111-222");
        child1List.add(metricRepoTreeChild11);
        child1List.add(metricRepoTreeChild12);
        metricRepoTreeParent1.setChildren(child1List);
        MetricRepoTree metricRepoTreeChild21 = new MetricRepoTree();
        MetricRepoTree metricRepoTreeChild22 = new MetricRepoTree();
        metricRepoTreeChild21.setId("222-111");
        metricRepoTreeChild22.setId("222-222");
        child2List.add(metricRepoTreeChild21);
        child2List.add(metricRepoTreeChild22);
        metricRepoTreeParent2.setChildren(child2List);

        Map<String,String> resPermissionsMap = Maps.newHashMap();
        resPermissionsMap.put("111-111-111","view");
        resPermissionsMap.put("111-222","view");
        if(!CollectionUtils.isEmpty(parentList)){

            parentList.forEach(metricRepoTree -> {
                String parentPermission = resPermissionsMap.get(metricRepoTree.getId());
                metricRepoTree.setPermissions(parentPermission);
                metricRepositoryTree(metricRepoTree,resPermissionsMap);
                if(!CollectionUtils.isEmpty(metricRepoTree.getChildren()) || !StringUtils.isEmpty(parentPermission)){
                    result.add(metricRepoTree);
                }
            });
        }
        System.out.println(JsonUtil.encode(result));
    }

    private static boolean metricRepositoryTree(MetricRepoTree metricRepoTree, Map<String, String> resPermissionsMap) {
        if (null != metricRepoTree) {
            List<MetricRepoTree> childMetricRepoList = metricRepoTree.getChildren();
            if (null != childMetricRepoList && !childMetricRepoList.isEmpty()) {
                Iterator<MetricRepoTree> it = childMetricRepoList.iterator();
                while(it.hasNext()){
                    MetricRepoTree childMetric = it.next();
                    String permission = resPermissionsMap.get(childMetric.getId());
                    childMetric.setPermissions(permission);
                    boolean isWithoutAuthorize = CollectionUtils.isEmpty(childMetric.getChildren())
                            && StringUtils.isEmpty(permission);
                    if (isWithoutAuthorize || metricRepositoryTree(childMetric, resPermissionsMap)) {
                        it.remove();
                    }
                }
                if (CollectionUtils.isEmpty(childMetricRepoList) && StringUtils.isEmpty(metricRepoTree.getPermissions())) {
                    return true;
                }
            }
        }
        return false;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值