/** * @param entity * @param id * @param isCascade 是否级联 * @return * @throws DaoException * @throws SystemException * @throws ServiceException */ public TreeNode getTreeNode(Resource entity, Long id, boolean isCascade) throws DaoException, SystemException, ServiceException { TreeNode node = this.resourceToTreeNode(entity, null, true); List<Resource> subResources = this.getByParentId(entity.getId(), StatusState.normal.getValue()); if (subResources.size() > 0) { if (isCascade) { // 递归查询子节点 List<TreeNode> children = Lists.newArrayList(); for (Resource d : subResources) { boolean isInclude = true; // 是否包含到节点树 TreeNode treeNode = null; treeNode = getTreeNode(d, id, true); // 排除自身 if (id != null) { if (!d.getId().equals(id)) { treeNode = getTreeNode(d, id, true); } else { isInclude = false; } } else { treeNode = getTreeNode(d, id, true); } if (isInclude) { children.add(treeNode); node.setState(TreeNode.STATE_CLOASED); } else { node.setState(TreeNode.STATE_OPEN); } } node.setChildren(children); } } return node; }
/** * 根据用户ID查找用户拥有的URL权限 * * @param userId 用户ID * @return List<String> 用户拥有的markUrl地址 */ public List<String> getUserAuthoritysByUserId(Long userId) throws DaoException, SystemException, ServiceException { List<String> userAuthoritys = Lists.newArrayList(); List<TreeNode> treeNodes = this.getResourceTreeByUserId(userId); for (TreeNode node : treeNodes) { Object obj = node.getAttributes().get("markUrl"); if (obj != null) { String markUrl = (String) obj; if (StringUtils.isNotBlank(markUrl)) { userAuthoritys.add(markUrl); } } // 二级目录 List<TreeNode> childrenNodes = node.getChildren(); for (TreeNode childrenNode : childrenNodes) { Object childrenObj = childrenNode.getAttributes().get("markUrl"); if (childrenObj != null) { String markUrl = (String) childrenObj; if (StringUtils.isNotBlank(markUrl)) { userAuthoritys.add(markUrl); } } } } return userAuthoritys; }
/** * Resource转TreeNode * * @param resource 资源 * @param resourceType 资源类型 * @param isCascade 是否级联 * @return * @throws DaoException * @throws SystemException * @throws ServiceException */ private TreeNode resourceToTreeNode(Resource resource, Integer resourceType, boolean isCascade) throws DaoException, SystemException, ServiceException { if (resourceType != null) { if (!resourceType.equals(resource.getType())) { return null; } } TreeNode treeNode = new TreeNode(resource.getId().toString(), resource.getName(), resource.getIconCls()); // 自定义属性 url Map<String, Object> attributes = Maps.newHashMap(); attributes.put("url", resource.getUrl()); attributes.put("markUrl", resource.getMarkUrl()); attributes.put("code", resource.getCode()); attributes.put("type", resource.getType()); treeNode.setAttributes(attributes); if (isCascade) { List<TreeNode> childrenTreeNodes = Lists.newArrayList(); for (Resource subResource : resource.getSubResources()) { TreeNode node = resourceToTreeNode(subResource, resourceType, isCascade); if (node != null) { childrenTreeNodes.add(node); } } treeNode.setChildren(childrenTreeNodes); } return treeNode; }