根据父子节点构造树形json

来源:互联网 发布:英语听力复读软件 编辑:程序博客网 时间:2024/06/13 23:19

1.写一个业务实体类

import java.util.List;/** * @作者: 恒 * @创建时间: 2016年10月31日 上午10:21:44 * 功能:orgchart实体类 */public class OrgChart {       private String id;       private String pId;       private String name;       private String title;       private String className;       private String photo;       private String empCnt;       private List<OrgChart> children;       //===================================get set===========================================   public String getId() {return id;}public void setId(String id) {this.id = id;}public String getpId() {return pId;}public void setpId(String pId) {this.pId = pId;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public List<OrgChart> getChildren() {return children;}public void setChildren(List<OrgChart> children) {this.children = children;}public String getPhoto() {return photo;}public void setPhoto(String photo) {this.photo = photo;}public String getEmpCnt() {return empCnt;}public void setEmpCnt(String empCnt) {this.empCnt = empCnt;}}

2 创建业务实体操作类

import java.util.ArrayList;import java.util.HashMap;import java.util.List;import cn.ghr.common.EhrUtil;/** * @作者: 恒 * @创建时间: 2016年11月1日 上午10:34:09  * 功能:组织架构图数据源 */public class OrgDataSource {private List<OrgChart> orgList;    /**     * 构造函数     * @param orgList     */public OrgDataSource(List<HashMap<String, Object>> orgList) {super();this.orgList = new ArrayList<>();for (HashMap<String, Object> hashMap : orgList) {OrgChart orgChart = new OrgChart();orgChart.setId(EhrUtil.toString(hashMap.get("id")));orgChart.setpId(EhrUtil.toString(hashMap.get("pId")));orgChart.setName(EhrUtil.toString(hashMap.get("name")));orgChart.setTitle(EhrUtil.toString(hashMap.get("title")));orgChart.setPhoto(EhrUtil.toString(hashMap.get("photo")));orgChart.setClassName(EhrUtil.toString(hashMap.get("className")));orgChart.setEmpCnt("在职人数:"+EhrUtil.toString(hashMap.get("empCnt")));this.orgList.add(orgChart);}}/** * @param orgChart * @return * @作者: 恒 * @创建时间: 2016年11月1日上午9:50:26  * 功能:是否有子节点 */public boolean isHasChild(OrgChart orgChart) {boolean isRootNode = false;for (OrgChart org : orgList) {if (org.getpId().equals(orgChart.getId())) {isRootNode = true;break;}}return isRootNode;}/** * @param orgChart * @return * @作者: 恒 * @创建时间: 2016年11月2日下午3:28:18  * 功能:是否有父节点 */public boolean isHasParent(OrgChart orgChart) {boolean isRootNode = false;for (OrgChart org : orgList) {if (orgChart.getpId().equals(org.getId())) {isRootNode = true;break;}}return isRootNode;}/** * @return * @作者: 恒 * @创建时间: 2016年11月1日上午10:03:25  * 功能:获取所有有根节点的orgChart */public List<OrgChart> getRootOrgs() {List<OrgChart> rootOrg = new ArrayList<>();for (OrgChart org : orgList) {if (!isHasParent(org)) {rootOrg.add(org);}}return rootOrg;}/** * @param orgChart * @return * @作者: 恒 * @创建时间: 2016年11月1日上午10:32:44 * 功能:获取父节点的所有子节点 */public List<OrgChart> getChildOrgs(OrgChart orgChart) {List<OrgChart> childOrgs = new ArrayList<>();for (OrgChart org : orgList) {if (org.getpId().equals(orgChart.getId())) {childOrgs.add(org);}}return childOrgs;}/** * @param org * @作者: 恒 * @创建时间: 2016年11月1日上午10:33:06 * 功能:递归获取所有根节点 */public void buildChildOrgs(OrgChart org) {List<OrgChart> children = getChildOrgs(org);if (!children.isEmpty()) {for (OrgChart child : children) {buildChildOrgs(child);}org.setChildren(children);}}/** * @return * @作者: 恒 * @创建时间: 2016年11月3日下午6:07:20  * 功能:构造部门架构图结构 */public List<OrgChart> buildOrgChart() {List<OrgChart> treeOrgs = new ArrayList<OrgChart>();List<OrgChart> rootOrgs = getRootOrgs();for (OrgChart rootOrg : rootOrgs) {buildChildOrgs(rootOrg);treeOrgs.add(rootOrg);}return treeOrgs;}/** * @return * @作者: 恒 * @创建时间: 2016年11月3日下午6:10:04  * 功能:获取岗位根节点 */public OrgChart getDeptRoot() {OrgChart deptRoot=new OrgChart();for (OrgChart org : orgList) {if (org.getId().equals("-1")) {deptRoot=org;}}    return deptRoot;}    /**     * @return     * @作者: 恒     * @创建时间: 2016年11月3日下午6:23:47     * 功能:获取岗位根节点一级子节点     */public List<OrgChart> getDeptChilds(OrgChart orgChart){List<OrgChart> childOrgs = new ArrayList<>();for (OrgChart org : orgList) {if (!isHasParent(org)&&!org.getId().equals(orgChart.getId())) {childOrgs.add(org);}}return childOrgs;}/** * @return * @作者: 恒 * @创建时间: 2016年11月3日下午6:06:30  * 功能:递归为岗位一级子节点递归添加节点 */public List<OrgChart> buildDeptChart() {List<OrgChart> treeOrgs = new ArrayList<OrgChart>();OrgChart orgRoot = getDeptRoot();List<OrgChart> children = getDeptChilds(orgRoot);if (!children.isEmpty()) {for (OrgChart child : children) {buildChildOrgs(child);}orgRoot.setChildren(children);}treeOrgs.add(orgRoot);return treeOrgs;}/** * @return * @作者: 恒 * @创建时间: 2016年11月1日下午2:11:17  * 功能:构造树形结构 */public List<OrgChart> buildTree() {List<OrgChart> treeOrgs = new ArrayList<OrgChart>();List<OrgChart> rootOrgs = getRootOrgs();for (OrgChart rootOrg : rootOrgs) {buildChildOrgs(rootOrg);treeOrgs.add(rootOrg);}return treeOrgs;}// ===========================get// set=========================================public List<OrgChart> getOrgList() {return orgList;}public void setOrgList(List<OrgChart> orgList) {this.orgList = orgList;}



原创粉丝点击