Java implementation of Jquery EasyUI Tree tree structure (entity transformation VO)

Keywords: Java Attribute less

The previous OA project, there is a tree of organizational structure, because it is done separately, I do Controller and page, other groups do Service and Dao, because there has been no discussion about the framework of the page to do better, leading to the Dao layer of data is not Easyui Tree can identify, in fact, the background returns a tree-shaped structure, but they return. The ** id is not the id that Easyui Tree can recognize; they return the name, not the text that Easyui Tree can recognize; they return the **, not the children that Easyui Tree can recognize, because others have done it well, so it is not convenient for others to redefine the format, so I can only re-seal the object returned by Service by turning VO. Install, make it support Easyui, not much to say, directly on the code.

This is VO code; the main one is ID,text,children, and these are the three to turn.

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;
    }
}

This is the tool class for transformation (as long as it is organized, without members)

public class TreeNoteUtil {

    /**
     * Get the parent node menu
     * @param treesList All tree menu sets
     * @return
     */
    public final static List<OrganizationVo> getFatherNode(List<TeamOrganizational> treesList){
        List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>();

        for (TeamOrganizational teamOrganizational : treesList) {
            if (teamOrganizational.getFatherId()==null) {//If pId If null, the node is the parent
                //Recursive acquisition of child nodes under parent nodes
                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;
    }

    /**
     * Recursive acquisition of sub-nodes under sub-nodes
     * @param pId ID of parent node
     * @param treesLists All menu tree sets
     * @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();
                    //Recursively retrieves the child node under the child node, that is, in the settings tree control children
                    newTree.setChildren(getChildrenNode(mt.getId(), treesLists));
                    //Setting Tree Control attributes Attribute data
                    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;
    }

}

This is the tool class for transformation (with organization, with members)

public class TreeNoteUtil2 {

    /**
     * Get the parent node menu
     * @param treesList All tree menu sets
     * @return
     */
    public final static List<OrganizationVo> getFatherNode(List<TeamOrganizational> treesList){
        List<OrganizationVo> newTrees = new ArrayList<OrganizationVo>();

        for (TeamOrganizational teamOrganizational : treesList) {

            if (teamOrganizational.getFatherId()==null) {//If pId If null, the node is the parent
                //Recursive acquisition of child nodes under parent nodes
                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;
    }

    /**
     * Recursive acquisition of sub-nodes under sub-nodes
     * @param pId ID of parent node
     * @param treesLists All menu tree sets
     * @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();
                    //Recursively retrieves the child node under the child node, that is, in the settings tree control children
                    newTree.setChildren(getChildrenNode(mt.getId(), treesLists,treesUserLists));
                    //Setting Tree Control attributes Attribute data
                    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;
    }


}

So much code, mainly using the idea of recursion, because you do not know how many levels, or more convenient to use recursion, although the loop is faster than recursion, occupying less memory, but recursive return has its advantages;

At the same time, look carefully at the two tool classes. The second tool class has more operations to add employees than the first tool class, but it won't use recursion here, because there won't be any employees under the staff. Why do I emphasize this? Because I wrote the first tool class first, always thinking about how to recurse. When I wrote the second tool class, people always add again and again, and look for it for a long time. Unless the reason, it suddenly occurred to me that there were no employees under the staff, so I suddenly realized...

Posted by jrmontg on Fri, 14 Dec 2018 22:18:03 -0800