/** * @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; }
/** * 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; }
/** * 查找需要拦截的所有url规则 * * @return */ public List<String> getAllInterceptorUrls() throws DaoException, SystemException, ServiceException { List<String> markUrls = Lists.newArrayList(); // 查找所有资源 // List<Resource> resources = this.findBy("NEI_status",StatusState.delete.getValue()); List<Resource> resources = this.getAll(); for (Resource resource : resources) { if (StringUtils.isNotBlank(resource.getMarkUrl())) { markUrls.add(resource.getMarkUrl()); } } return markUrls; }
/** * 自定义保存资源. <br> * 说明:如果保存的资源类型为“功能” 则将所有子资源都设置为“功能”类型 * * @param entity 资源对象 * @throws DaoException * @throws SystemException * @throws ServiceException */ public void saveResource(Resource entity) throws DaoException, SystemException, ServiceException { Assert.notNull(entity, "参数[entity]为空!"); this.saveEntity(entity); if (entity.getType() != null && ResourceState.function.getValue().equals(entity.getType())) { List<Resource> subResources = entity.getSubResources(); while (!Collections3.isEmpty(subResources)) { Iterator<Resource> iterator = subResources.iterator(); while (iterator.hasNext()) { Resource subResource = iterator.next(); subResource.setType(ResourceState.function.getValue()); iterator.remove(); subResources = ListUtils.union(subResources, subResource.getSubResources()); super.update(subResource); } } } }
/** * Resource转Easy UI Menu * * @param resource * @param isCascade 是否递归遍历子节点 * @return * @throws DaoException * @throws SystemException * @throws ServiceException */ public Menu resourceToMenu(Resource resource, boolean isCascade) throws DaoException, SystemException, ServiceException { Assert.notNull(resource, "参数resource不能为空"); if (ResourceState.menu.getValue().equals(resource.getType())) { Menu menu = new Menu(); menu.setId(resource.getId().toString()); menu.setText(resource.getName()); menu.setHref(resource.getUrl()); if (isCascade) { List<Menu> childrenMenus = Lists.newArrayList(); for (Resource subResource : resource.getSubResources()) { if (ResourceState.menu.getValue().equals(subResource.getType())) { childrenMenus.add(resourceToMenu(subResource, true)); } } menu.setChildren(childrenMenus); } return menu; } return null; }
public List<Resource> getResourcesByUserId(Long userId, Resource parentResource) throws DaoException, SystemException, ServiceException { List<Resource> list = new ArrayList<Resource>(); List<Resource> resources = this.getResourcesByUserId(userId); if (null == parentResource) { for (Resource resource : resources) { if (resource != null && resource.getParentResource() == null && StatusState.normal.getValue().equals(resource.getStatus())) { list.add(resource); } } } else { for (Resource resource : resources) { if (resource != null && resource.getParentResource() != null && resource.getParentResource().getId().equals(parentResource.getId()) && StatusState.normal.getValue().equals(resource.getStatus())) { list.add(resource); } } } return list; }
public List<Menu> getAppMenusByUserId(Long userId) { List<Menu> menus = Lists.newArrayList(); List<Resource> resources = Lists.newArrayList(); User user = userManager.loadById(userId); User superUser = userManager.getSuperUser(); if (user != null && superUser != null && user.getId() == superUser.getId()) { // 超级用户 resources = super.getAll(); } else if (user != null) { resources = getResourcesByUserId(userId); } for (Resource resource : resources) { if (StringUtils.isNotBlank(resource.getUrl())) { if (ResourceState.menu.getValue().equals(resource.getType())) { Menu menu = new Menu(); menu.setId(resource.getId().toString()); menu.setText(resource.getName()); menu.setHref(resource.getUrl()); menus.add(menu); } } } return menus; }