errors.addAll(validateEmploymentDate(employeeStartDate, employeeEndDate));
if (errors.isEmpty()) {
boolean passwordReset = false;
User u = userDao.getUserById(user.getId());
u.setFirstName(user.getFirstName());
u.setLastName(user.getLastName());
u.setEmail(user.getEmail());
if (user.getPassword() != null && !user.getPassword().trim().isEmpty()) {
u.setConfirmPassword(user.getPassword());
if (us != null) {
passwordReset = true;
u.setPassword(us.encryptPassword(user.getUsername(), user.getPassword()));
} else {
//md5 password
u.setPassword(StringUtil.md5Base16(user.getPassword()));
}
}
//set roles
if (user.getRoles() != null && user.getRoles().size() > 0) {
Set roles = new HashSet();
for (String roleId : (Set<String>) user.getRoles()) {
roles.add(roleDao.getRole(roleId));
}
u.setRoles(roles);
}
u.setTimeZone(user.getTimeZone());
u.setActive(user.getActive());
invalid = !userDao.updateUser(u);
if (us != null && !invalid) {
us.updateUserPostProcessing(u);
if (passwordReset) {
us.passwordResetPostProcessing(u);
}
}
}
}
if (!errors.isEmpty()) {
model.addAttribute("errors", errors);
invalid = true;
}
}
if (invalid) {
Collection<Organization> organizations = organizationDao.getOrganizationsByFilter(null, "name", false, null, null);
model.addAttribute("organizations", organizations);
model.addAttribute("roles", roleDao.getRoles(null, "name", false, null, null));
model.addAttribute("timezones", TimeZoneUtil.getList());
Map<String, String> status = new HashMap<String, String>();
status.put("1", "Active");
status.put("0", "Inactive");
model.addAttribute("status", status);
model.addAttribute("user", user);
model.addAttribute("employeeCode", employeeCode);
model.addAttribute("employeeRole", employeeRole);
model.addAttribute("employeeOrganization", employeeOrganization);
model.addAttribute("employeeDepartment", employeeDepartment);
model.addAttribute("employeeGrade", employeeGrade);
model.addAttribute("employeeStartDate", employeeStartDate);
model.addAttribute("employeeEndDate", employeeEndDate);
model.addAttribute("employeeDepartmentHod", employeeDepartmentHod);
if (us != null) {
if ("create".equals(action)) {
model.addAttribute("userFormFooter", us.getUserCreationFormFooter());
} else {
model.addAttribute("userFormFooter", us.getUserEditingFormFooter(user));
}
} else {
model.addAttribute("userFormFooter", "");
}
if ("create".equals(action)) {
return "console/directory/userCreate";
} else {
return "console/directory/userEdit";
}
} else {
String prevDepartmentId = null;
//set employment detail
Employment employment = null;
if ("create".equals(action)) {
employment = new Employment();
} else {
try {
employment = (Employment) userDao.getUserById(user.getId()).getEmployments().iterator().next();
} catch (Exception e) {
employment = new Employment();
}
}
prevDepartmentId = employment.getDepartmentId();
employment.setUserId(user.getId());
employment.setEmployeeCode(employeeCode);
employment.setRole(employeeRole);
employment.setOrganizationId((employeeOrganization != null && !employeeOrganization.isEmpty()) ? employeeOrganization : null);
employment.setDepartmentId((employeeDepartment != null && !employeeDepartment.isEmpty()) ? employeeDepartment : null);
employment.setGradeId((employeeGrade != null && !employeeGrade.isEmpty()) ? employeeGrade : null);
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
if (employeeStartDate != null && employeeStartDate.trim().length() > 0) {
employment.setStartDate(df.parse(employeeStartDate));
} else {
employment.setStartDate(null);
}
if (employeeEndDate != null && employeeEndDate.trim().length() > 0) {
employment.setEndDate(df.parse(employeeEndDate));
} else {
employment.setEndDate(null);
}
} catch (Exception e) {
LogUtil.error(getClass().getName(), e, "Set Employee Date error");
}
if (employment.getId() == null) {
employment.setUser(user);
employmentDao.addEmployment(employment);
} else {
employmentDao.updateEmployment(employment);
}
//Hod
if ("yes".equals(employeeDepartmentHod) && employeeDepartment != null && employeeDepartment.trim().length() > 0) {
if (prevDepartmentId != null) {
User prevHod = userDao.getHodByDepartmentId(prevDepartmentId);
if (prevHod != null) {
employmentDao.unassignUserAsDepartmentHOD(prevHod.getId(), prevDepartmentId);
}
}
employmentDao.assignUserAsDepartmentHOD(user.getId(), employeeDepartment);
} else {
if (prevDepartmentId != null) {
User prevHod = userDao.getHodByDepartmentId(prevDepartmentId);
if (prevHod != null && prevHod.getId().equals(user.getId())) {
employmentDao.unassignUserAsDepartmentHOD(prevHod.getId(), prevDepartmentId);
}
}
}
String contextPath = WorkflowUtil.getHttpServletRequest().getContextPath();