标签:实体 his java实现 架构 blog etc style turn stat
前一阵做的OA项目,有一个是组织架构的树,因为是分开做的,我做的是Controller和页面,其他组做的Service和Dao,因为之前一直没有商量页面用什么框架做比较好,导致,Dao层取出来的数据都不是Easyui Tree所能识别的,其实后台返回的也是树形的结构,但是他们返回来的 **id,而不是Easyui Tree所能识别的id;他们返回的是name,,而不是Easyui Tree所能识别的text,他们返回的是****,,而不是Easyui Tree所能识别的children,因为别人已经做好了,所以也不方便让别人重新去定义格式,所以我这边只能通过转VO,把Service返回的对象重新进行封装,使之支持Easyui ,话不多说,直接上代码。
这段是VO代码;其中主要的就是ID,text,children,要转的就是这三个
public class OrganizationVo implements Serializable{ private Integer id; private Integer pid; private Integer teamId; private String text; private String state; private String leader; private Integer sort; private List<OrganizationVo> children; public OrganizationVo(){} public OrganizationVo(Integer id,Integer pid, Integer teamId,String leader,Integer sort,String text,String state, List<OrganizationVo> children) { this.id = id; this.pid = pid; this.teamId = teamId; this.text = text; this.sort = sort; this.leader = leader; this.state = state; this.children = children; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getTeamId() { return teamId; } public void setTeamId(Integer teamId) { this.teamId = teamId; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getLeader() { return leader; } public void setLeader(String leader) { this.leader = leader; } public Integer getSort() { return sort; } public void setSort(Integer sort) { this.sort = sort; } public String getState() { return state; } public void setState(String state) { this.state = state; } public List<OrganizationVo> getChildren() { return children; } public void setChildren(List<OrganizationVo> children) { this.children = children; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } }
这个是转换的工具类(只要组织,不带成员)
public class TreeNoteUtil { /** * 获取父节点菜单 * @param treesList 所有树菜单集合 * @return */ public final static List<OrganizationVo> getFatherNode(List<TeamOrganizational> treesList){ List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>(); for (TeamOrganizational teamOrganizational : treesList) { if (teamOrganizational.getFatherId()==null) {//如果pId为空,则该节点为父节点 //递归获取父节点下的子节点 OrganizationVo newTree = new OrganizationVo(); newTree.setId(teamOrganizational.getId()); newTree.setText(teamOrganizational.getName()); newTree.setTeamId(teamOrganizational.getTeamId()); List<TeamOrganizational> treesLists = teamOrganizational.getSonTeamOrganizationals(); newTree.setChildren(getChildrenNode(teamOrganizational.getId(), treesLists)); newTrees.add(newTree); } } return newTrees; } /** * 递归获取子节点下的子节点 * @param pId 父节点的ID * @param treesLists 所有菜单树集合 * @return */ private final static List<OrganizationVo> getChildrenNode(Integer pId, List<TeamOrganizational> treesLists){ List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>(); for (TeamOrganizational mt : treesLists) { if (mt.getFatherId() != null) { if (mt.getFatherId() == pId) { OrganizationVo newTree = new OrganizationVo(); //递归获取子节点下的子节点,即设置树控件中的children newTree.setChildren(getChildrenNode(mt.getId(), treesLists)); //设置树控件attributes属性的数据 newTree.setId(mt.getId()); newTree.setText(mt.getName()); newTree.setPid(mt.getFatherId()); newTree.setTeamId(mt.getTeamId()); newTree.setSort(mt.getSort()); newTree.setLeader(mt.getLeader()); newTrees.add(newTree); } } } return newTrees; } }
这个是转换的工具类(带组织,带成员)
public class TreeNoteUtil2 { /** * 获取父节点菜单 * @param treesList 所有树菜单集合 * @return */ public final static List<OrganizationVo> getFatherNode(List<TeamOrganizational> treesList){ List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>(); for (TeamOrganizational teamOrganizational : treesList) { if (teamOrganizational.getFatherId()==null) {//如果pId为空,则该节点为父节点 //递归获取父节点下的子节点 OrganizationVo newTree = new OrganizationVo(); newTree.setId(teamOrganizational.getId()); newTree.setText(teamOrganizational.getName()); newTree.setTeamId(teamOrganizational.getTeamId()); List<TeamOrganizational> treesLists = teamOrganizational.getSonTeamOrganizationals(); List<OaUser> treesUserLists = teamOrganizational.getOaUsers(); newTree.setChildren(getChildrenNode(teamOrganizational.getId(), treesLists,treesUserLists)); newTrees.add(newTree); } } return newTrees; } /** * 递归获取子节点下的子节点 * @param pId 父节点的ID * @param treesLists 所有菜单树集合 * @return */ private final static List<OrganizationVo> getChildrenNode(Integer pId, List<TeamOrganizational> treesLists,List<OaUser> treesUserLists) { List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>(); for (TeamOrganizational mt : treesLists) { if (mt.getFatherId() != null) { if (mt.getFatherId() == pId) { OrganizationVo newTree = new OrganizationVo(); //递归获取子节点下的子节点,即设置树控件中的children newTree.setChildren(getChildrenNode(mt.getId(), treesLists,treesUserLists)); //设置树控件attributes属性的数据 newTree.setId(mt.getId()); newTree.setText(mt.getName()); newTree.setPid(mt.getFatherId()); newTree.setTeamId(mt.getTeamId()); newTree.setSort(mt.getSort()); newTree.setLeader(mt.getLeader()); if(mt.getOaUsers().size()>0){ newTree.setChildren(getOaUsers(mt.getOaUsers())); } newTrees.add(newTree); } } } if(treesUserLists.size()>0){ for (OaUser oaUser:treesUserLists){ OrganizationVo newTree = new OrganizationVo(); newTree.setId(oaUser.getId()); newTree.setText(oaUser.getRealname()); newTrees.add(newTree); } } return newTrees; } public static List<OrganizationVo> getOaUsers(List<OaUser> oaUsers){ List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>(); if (oaUsers.size()>0){ for (OaUser oaUser:oaUsers){ OrganizationVo newTree = new OrganizationVo(); newTree.setId(oaUser.getId()); newTree.setText(oaUser.getRealname()); newTrees.add(newTree); } }else{ } return newTrees; } }
代码就这么多,主要是运用了递归的思想,因为在你不知道有多少层级的情况下,还是用递归比较方便,虽然循环比递归的速度快,占用内存小,但递归还是有其优势的;
同时,仔细看那两个工具类,第二个工具类比第一个工具类多了添加员工的操作,但是这里就不会使用递归了,因为员工下面不会再有员工了,为什么我会强调这个,因为我先写的是第一个工具类,总想着如何递归,写第二个的时候,人员总是重复添加,好久都找不到原因,后来才忽然想到,员工下面是没有员工的,于是恍然大悟。。。。
Jquery EasyUI Tree树形结构的Java实现(实体转换VO)
标签:实体 his java实现 架构 blog etc style turn stat
原文地址:http://www.cnblogs.com/kanyun/p/7273737.html