/** * 并合子类资源到父类中 * * @param list 资源集合 */ public List<Resource> mergeResourcesToParent(List<Resource> list, ResourceType resourceType) { List<Resource> result = new ArrayList<Resource>(); for (Iterator<Resource> it = list.iterator(); it.hasNext(); ) { Resource resource = it.next(); if (resource.getParent() == null && !StringUtils.equals(resourceType.getValue(), resource.getType())) { mergeResourcesToParent(list, resource, resourceType); result.add(resource); } } return result; }
/** * 遍历list中的数据,如果数据的父类与parent相等,将数据加入到parent的children中 * * @param list 资源集合 */ private void mergeResourcesToParent( List<Resource> list, Resource parent, ResourceType resourceType) { if (!parent.getLeaf()) { return; } parent.setChildren(new ArrayList<Resource>()); for (Iterator<Resource> iterator = list.iterator(); iterator.hasNext(); ) { Resource entity = iterator.next(); if (!StringUtils.equals(entity.getType(), resourceType.getValue()) && StringUtils.equals(entity.getParentId(), parent.getId())) { entity.setChildren(null); mergeResourcesToParent(list, entity, resourceType); parent.getChildren().add(entity); } } }