@RequestParam(value = "employeeDepartmentHod", required = false) String employeeDepartmentHod, @RequestParam(value = "employeeGrade", required = false) String employeeGrade,
@RequestParam(value = "employeeStartDate", required = false) String employeeStartDate, @RequestParam(value = "employeeEndDate", required = false) String employeeEndDate) {
// validate ID
validator.validate(user, result);
UserSecurity us = DirectoryUtil.getUserSecurity();
boolean invalid = result.hasErrors();
if (!invalid) {
// check error
Collection<String> errors = new ArrayList<String>();
if ("create".equals(action)) {
// check username exist
if (directoryManager.getUserByUsername(user.getUsername()) != null || (us != null && us.isDataExist(user.getUsername()))) {
errors.add(ResourceBundleUtil.getMessage("console.directory.user.error.label.usernameExists"));
}
if (us != null) {
Collection<String> validationErrors = us.validateUserOnInsert(user);
if (validationErrors != null && !validationErrors.isEmpty()) {
errors.addAll(validationErrors);
}
}
errors.addAll(validateEmploymentDate(employeeStartDate, employeeEndDate));
if (errors.isEmpty()) {
user.setId(user.getUsername());
if (user.getPassword() != null && !user.getPassword().trim().isEmpty()) {
user.setConfirmPassword(user.getPassword());
if (us != null) {
user.setPassword(us.encryptPassword(user.getUsername(), user.getPassword()));
} else {
//md5 password
user.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));
}
user.setRoles(roles);
}
invalid = !userDao.addUser(user);
if (us != null && !invalid) {
us.insertUserPostProcessing(user);
}
}
} else {
user.setUsername(user.getId());
if (us != null) {
Collection<String> validationErrors = us.validateUserOnUpdate(user);
if (validationErrors != null && !validationErrors.isEmpty()) {
errors.addAll(validationErrors);
}
}
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", "");
}