/** * 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; }
/** * 自定义保存资源. <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<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; }