It is now time to enhance the CompanyHandler
class to generate the required JSON to load the tree store and display the company tree. We will create two new methods to implement this functionality.
The CompanyHandler.getTreeNodeId()
helper method generates a unique ID based on the ID of the EntityItem
class. It will be used to generate type-specific IDs for each node.
private String getTreeNodeId(EntityItem obj){ String id = null; if(obj instanceof Company){ id = "C_" + obj.getId(); } else if(obj instanceof Project){ id = "P_" + obj.getId(); } else if(obj instanceof Task){ id = "T_" + obj.getId(); } return id; }
The CompanyHandler getCompanyTreeJson()
method is mapped to the company/tree.json
URL and has the following definition:
@RequestMapping(value="/tree", method=RequestMethod.GET, produces={"application/json"}) @ResponseBody public String getCompanyTreeJson(HttpServletRequest request) { User sessionUser = getSessionUser(request); Result<List<Company>> ar = companyService.findAll(sessionUser.getUsername()); if (ar.isSuccess()) { JsonObjectBuilder builder = Json.createObjectBuilder(); builder.add("success", true); JsonArrayBuilder companyChildrenArrayBuilder = Json.createArrayBuilder(); for(Company company : ar.getData()){ List<Project> projects = company.getProjects(); JsonArrayBuilder projectChildrenArrayBuilder = Json.createArrayBuilder(); for(Project project : projects){ List<Task> tasks = project.getTasks(); JsonArrayBuilder taskChildrenArrayBuilder = Json.createArrayBuilder(); for(Task task : tasks){ taskChildrenArrayBuilder.add( Json.createObjectBuilder() .add("id", getTreeNodeId(task)) .add("text", task.getTaskName()) .add("leaf", true) ); } projectChildrenArrayBuilder.add( Json.createObjectBuilder() .add("id", getTreeNodeId(project)) .add("text", project.getProjectName()) .add("leaf", tasks.isEmpty()) .add("expanded", tasks.size() > 0) .add("children", taskChildrenArrayBuilder) ); } companyChildrenArrayBuilder.add( Json.createObjectBuilder() .add("id", getTreeNodeId(company)) .add("text", company.getCompanyName()) .add("leaf", projects.isEmpty()) .add("expanded", projects.size() > 0) .add("children", projectChildrenArrayBuilder) ); } builder.add("children", companyChildrenArrayBuilder); return toJsonString(builder.build()); } else { return getJsonErrorMsg(ar.getMsg()); } }
This method performs the following tasks:
JsonArrayBuilder
object with the name companyChildrenArrayBuilder
to hold the set of company JsonObjectBuilder
instances that will be created in the main for
loop when iterating through the company list.JsonObjectBuilder
tree node representation to the projectChildrenArrayBuilder JsonArrayBuilder
instance. The projectChildrenArrayBuilder
instance is then added as the children
property of the owning company JsonObjectBuilder
instance.JsonObjectBuilder
tree node representation to the taskChildrenArrayBuilder JsonArrayBuilder
instance. The taskChildrenArrayBuilder
instance is then added as the children
property of the owning project, the JsonObjectBuilder
instance.companyChildrenArrayBuilder
as the children
property of the builder
instance that will be used to build and return JSON from the method with success
property true
.The getCompanyTreeJson
method returns a hierarchical JSON structure that encapsulates the relationship between the company, project, and task in a format that can be consumed by the CompanyTree
store.
3.145.73.207