private void loopMenu(List<Map<String, Object>> menuList, Menu menu) { Map<String, Object> row = Maps.newHashMap(); menuList.add(row); row.put("id", menu.getId()); row.put("name", menu.getTitle()); row.put("open", menu.getInitOpen()); row.put("disabled", menu.getDisabled()); List<Menu> children = menu.getChildren(); if (!CollectionUtils.isEmpty(children)) { List<Map<String, Object>> childrenList = Lists.newArrayList(); row.put("children", childrenList); for (Menu child : children) { loopMenu(childrenList, child); } } }
@MetaData(title = "移动/复制") public HttpHeaders doDrag() { Set<Menu> operationEntities = Sets.newHashSet(); Menu target = null; String targetId = this.getParameter("targetId"); if (StringUtils.isNotBlank(targetId)) { target = getEntityService().findOne(targetId); } Collection<Menu> entities = this.getEntitiesByParameterIds(); String moveType = this.getParameter("moveType"); String copy = this.getParameter("copy", "false"); if (BooleanUtils.toBoolean(copy)) { Collection<Menu> copyEntities = Lists.newArrayList(); for (Menu menu : entities) { copyEntities.add(cloneNewEntity(menu, "children", "parent")); } entities = copyEntities; } if (TreeNodeConstant.TreeNodeDragType.prev.name().equals(moveType) || TreeNodeConstant.TreeNodeDragType.next.name().equals(moveType)) { int max = 0; for (Menu menu : entities) { if (menu.getOrderRank() > max) { max = menu.getOrderRank(); } } Iterable<Menu> children = null; Menu targetParent = target.getParent(); if (targetParent == null) { children = menuService.findRoots(); } else { children = target.getParent().getChildren(); } if (TreeNodeConstant.TreeNodeDragType.prev.name().equals(moveType)) { List<Menu> childrenPrev = Lists.newArrayList(); for (Menu menu : children) { if (menu.getOrderRank() >= target.getOrderRank()) { menu.setOrderRank(menu.getOrderRank() + max); operationEntities.add(menu); } else { childrenPrev.add(menu); } } int maxPrev = 0; for (Menu menu : childrenPrev) { if (menu.getOrderRank() > maxPrev) { maxPrev = menu.getOrderRank(); } } for (Menu menu : entities) { menu.setOrderRank(menu.getOrderRank() + maxPrev); operationEntities.add(menu); } } else if (TreeNodeConstant.TreeNodeDragType.next.name().equals(moveType)) { for (Menu menu : children) { if (menu.getOrderRank() > target.getOrderRank()) { menu.setOrderRank(menu.getOrderRank() + max); operationEntities.add(menu); } } for (Menu menu : entities) { menu.setOrderRank(menu.getOrderRank() + target.getOrderRank()); operationEntities.add(menu); } } } else if (TreeNodeConstant.TreeNodeDragType.inner.name().equals(moveType)) { Iterable<Menu> children = null; if (target == null) { children = menuService.findRoots(); } else { children = target.getChildren(); } int max = 0; for (Menu menu : children) { if (menu.getOrderRank() > max) { max = menu.getOrderRank(); } } for (Menu menu : entities) { menu.setOrderRank(menu.getOrderRank() + max); menu.setParent(target); operationEntities.add(menu); } } else { throw new IllegalArgumentException("moveType=" + moveType); } getEntityService().save(operationEntities); setModel(OperationResult.buildSuccessResult("更新菜单所属上级节点操作完成")); return buildDefaultHttpHeaders(); }