}
@RequestMapping(value = "/console/directory/dept/submit/(*:action)", method = RequestMethod.POST)
public String consoleDeptSubmit(ModelMap model, @RequestParam("action") String action, @RequestParam("orgId") String orgId, @RequestParam(value = "parentId", required = false) String parentId, @ModelAttribute("department") Department department, BindingResult result) {
Organization organization = organizationDao.getOrganization(orgId);
Department parent = null;
if (parentId != null && parentId.trim().length() > 0) {
parent = departmentDao.getDepartment(parentId);
}
// validate ID
validator.validate(department, result);
boolean invalid = result.hasErrors();
if (!invalid) {
// check error
Collection<String> errors = new ArrayList<String>();
if ("create".equals(action)) {
// check id exist
if (departmentDao.getDepartment(department.getId()) != null) {
errors.add("console.directory.department.error.label.idExists");
} else {
department.setOrganization(organization);
if (parent != null) {
department.setParent(parent);
}
invalid = !departmentDao.addDepartment(department);
}
} else {
Department d = departmentDao.getDepartment(department.getId());
d.setName(department.getName());
d.setDescription(department.getDescription());
invalid = !departmentDao.updateDepartment(d);
}
if (!errors.isEmpty()) {
model.addAttribute("errors", errors);