Previous ‹Dependencies

Use case: Create Organization Chart

Class: net.hrider.api.usecases.CreateOrganization
Example of how to create a company and build its organization chart with departments (DEPT1), job positions (PSTA, PSTB, PSTC) and employees (EMP1, EMP2, EMP3):
 
     DEPT1
       |____ PSTA
       |       |_______ EMP1
       |       |_______ EMP2
       |
       |____ PSTB
       |       |_______ EMP3
       | 
       |____ PSTC
                    
It is required to establish the following relationships:
  • EMP1 is the supervisor of EMP2 and EMP3
  • EMP2 and EMP1 are clients of EMP3

First of all get the main anchor point to the current API version "v1":

ApiClient client = new ApiClient();
HRiderService service = new HRiderService(client);
Root root = service.get(Root.class, new Link("v1"));


The Root object allows to navigate through the rest of the API following its links (save this object in memory instead of obtaining it every time to gain efficiency).

To create the company, perform a POST request to the address of its link 'companies' root.getLinks().get("companies") passing a net.hrider.api.model.Company instance, which returns the method buildCompany() in our example, establishing its main properties and having as null the embedded and links properties, since they are read-only for all API objects. The service.create method makes the POST for us

Company company = service.create(root.getLinks().getCompanies(), buildCompany());


We have created the company!
To create the DEPT1 department follow the same logic: this time following the link departments of the created company:

Department dpt1 = service.create(company.getLinks().getDepartments(), 
        buildDepartment("DEPT1", "DEPARTMENT 1"));


Since all the job positions to create are inside the DEPT1 department, follow the link positions of dpt1 to create the positions:

Link positionsLink = dpt1.getLinks().getPositions();
Position pa = service.create(positionsLink, buildPosition("PSTA", "POSITION A", 2));
Position pb = service.create(positionsLink, buildPosition("PSTB", "POSITION B", 1));
Position pc = service.create(positionsLink, buildPosition("PSTC", "POSITION C", 1));


Now create the employees:

Link employeesLink = company.getLinks().getEmployees();
Employee emp1 = service.create(employeesLink, buildEmployee("EMP1", "Employee 1", "[email protected]"));
Employee emp2 = service.create(employeesLink, buildEmployee("EMP2", "Employee 2", "[email protected]"));
Employee emp3 = service.create(employeesLink, buildEmployee("EMP3", "Employee 3", "[email protected]"));  
      

To establish the relationship with the job position, use a PUT to the link position of each employee, including the URI of the job to relate as the content of the PUT request. Since all the objects have a link to themselves, (self), use it to obtain the URI of each job position with getHref. For other API relationships that accept more than one URI in the relationship, separate each URI with a line break (content type: text/uri-list).

service.put(emp1.getLinks().getPosition(), pa.getLinks().getSelf().getHref());
service.put(emp2.getLinks().getPosition(), pa.getLinks().getSelf().getHref());
service.put(emp3.getLinks().getPosition(), pb.getLinks().getSelf().getHref());


Now we have set that the position of EMP1 and EMP2 is PSTA, and the position of EMP3 is PSTB.
The last step is to assign the supervisors and clients:

service.put(emp2.getLinks().getSupervisor(), emp1.getLinks().getSelf().getHref());
service.put(emp3.getLinks().getSupervisor(), emp1.getLinks().getSelf().getHref());

service.put(emp3.getLinks().getClients(), 
        emp2.getLinks().getSelf().getHref() + "\n" +
        emp1.getLinks().getSelf().getHref());



Previous ‹Dependencies