// saveOrUpdate or update page @RequestMapping(value = "/admin/employees", method = RequestMethod.POST) public String saveOrUpdateEmployee( @ModelAttribute("employeeForm") @Validated EmployeeForm employeeForm, BindingResult result, final RedirectAttributes redirectAttributes, Model model) { if (result.hasErrors()) { populateDefaultModel(model); return "admin/employeeForm"; } else { redirectAttributes.addFlashAttribute("css", "success"); if (employeeForm.isNew()) { redirectAttributes.addFlashAttribute("msg", "Employee added successfully!"); } else { redirectAttributes.addFlashAttribute("msg", "Employee updated successfully!"); } Employee employee; employee = convertEmployeeFormToEmployee(employeeForm); employeeServiceDao.saveOrUpdate(employee); return "redirect:/admin/employees/" + employee.getId(); } }
private EmployeeForm convertEmployeeToEmployeeForm(Employee employee) { EmployeeForm employeeForm = new EmployeeForm(); if (employee.getPosition() == Position.Cook) { Cook cook = (Cook) employee; List<String> cookedDishesIdList = new ArrayList<>(); List<CookedDishes> cookedDishes = cook.getCookedDishes(); cookedDishes.forEach(dish -> cookedDishesIdList.add(dish.getId().toString())); String cookedDishesString = convertListToDelimitedString(cookedDishesIdList); employeeForm.setCookedDishes(cookedDishesString); populateEmployeeForm(employeeForm, cook); if ("".equals(employeeForm.getCookedDishes())) { employeeForm.setCookedDishes("No cooked dishes"); } return employeeForm; } else if (employee.getPosition() == Position.Waiter) { Waiter waiter = (Waiter) employee; List<String> ordersIdList = new ArrayList<>(); List<Orders> orders = waiter.getOrders(); orders.forEach(order -> ordersIdList.add(order.getId().toString())); String ordersString = convertListToDelimitedString(ordersIdList); employeeForm.setOrders(ordersString); populateEmployeeForm(employeeForm, waiter); if ("".equals(employeeForm.getOrders())) { employeeForm.setOrders("No orders"); } return employeeForm; } else { populateEmployeeForm(employeeForm, employee); return employeeForm; } }
private void populateEmployeeForm(EmployeeForm employeeForm, Employee employee) { employeeForm.setId(employee.getId()); employeeForm.setName(employee.getName()); employeeForm.setSurname(employee.getSurname()); employeeForm.setPhoneNumber(employee.getPhoneNumber()); employeeForm.setDateOfBirth(employee.getDateOfBirth()); employeeForm.setPosition(employee.getPosition()); employeeForm.setSalary(employee.getSalary()); }