public Employee createEmployee(String name, long salary) { Employee emp = new Employee(); emp.setName(name); emp.setSalary(salary); em.persist(emp); return emp; }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); printHtmlHeader(out); // process request String action = request.getParameter("action"); if (action == null) { // do nothing if no action requested } else if (action.equals("CreateEmp")) { Employee emp = empService.createEmployee( request.getParameter("empName"), parseLong(request.getParameter("salary"))); out.println("Created " + emp); } else if (action.equals("CreateDept")) { Department dept = deptService.createDepartment(request.getParameter("deptName")); out.println("Created " + dept); } else if (action.equals("SetEmployeeDept")) { Employee emp = empService.setEmployeeDepartment( parseInt(request.getParameter("empId")), parseInt(request.getParameter("deptId"))); out.println("Updated " + emp + "</br>"); out.println(emp.getDepartment() + " with Employees:</br>"); out.println(emp.getDepartment().getEmployees()); } else if (action.equals("FindAll")) { Collection<Employee> emps = empService.findAllEmployees(); if (emps.isEmpty()) { out.println("No Employees found "); } else { out.println("Found Employees: </br>"); for (Employee emp : emps) { out.println(emp + "<br/>"); } } out.println("</br>"); Collection<Department> depts = deptService.findAllDepartments(); if (depts.isEmpty()) { out.println("No Departments found "); } else { out.println("Found Departments: </br>"); for (Department dept : depts) { out.println( dept + " with " + dept.getEmployees().size() + " employees " + dept.getEmployees() + "<br/>"); } } } printHtmlFooter(out); }