private Profile createProfile(CreateAccountForm createAccountForm) { Profile profile = new Profile(); profile.setFirstName(createAccountForm.getFirstName()); profile.setLastName(createAccountForm.getLastName()); profile.setDateOfBirth(AppUtil.getDateFromStr(createAccountForm.getDateOfBirth())); profile.setAddress(createAccountForm.getAddress()); profile.setEmailAddress(createAccountForm.getEmailAddress()); profile.setPhone(createAccountForm.getPhoneNo()); profile.setTenantId(createAccountForm.getTenantId()); profile.setSex(createAccountForm.getSex()); profile.setCreatedBy( createAccountForm.getStaffFirstName() + " " + createAccountForm.getStaffLastName()); profile.setCreatedDate(new Date()); return profile; }
@RequestMapping(value = "/staffcreateaccount", method = RequestMethod.POST) public ModelAndView createAccountFormSubmit( @ModelAttribute("form") CreateAccountForm createAccountForm, BindingResult result) { log.debug("Entering - CreateAccountForm : {}", createAccountForm.toString()); ModelAndView modelAndView = new ModelAndView("staffcreateaccount"); boolean isSuccess = true; validate(createAccountForm, result); if (createAccountForm.getDateOfBirth() != null) { if (!AppUtil.isAValidDDMMYYYYDate(createAccountForm.getDateOfBirth())) { log.error("Invalid date format " + createAccountForm.getDateOfBirth()); isSuccess = false; result.addError( new ObjectError( "dateOfBirth", "Invalid dateOfBirth. Please use the format " + DATE_FORMAT)); } } if (isSuccess && !result.hasErrors()) { String tenantId = createAccountForm.getTenantId(); Profile staffProfile = profileService.getProfileById(createAccountForm.getStaffProfileId(), tenantId); if (staffProfile != null) { createAccountForm.setStaffFirstName(staffProfile.getFirstName()); createAccountForm.setStaffLastName(staffProfile.getLastName()); } // Create Profile String profileId = profileService.saveProfile(createProfile(createAccountForm)); String accountNo = null; try { // Create Use Account loginService.CreateUser(createUser(createAccountForm, profileId)); } catch (Exception e) { log.error("Error Creating User {} ", e); isSuccess = false; profileService.deleteProfile(profileId, tenantId); } try { // Create Account accountNo = accountService.saveAccount(createAccount(createAccountForm, profileId)); log.debug("Account No. " + accountNo); } catch (Exception e) { log.error("Error Creating Account {} ", e); isSuccess = false; loginService.deleteUser(createAccountForm.getUserName(), tenantId); profileService.deleteProfile(profileId, tenantId); } if (isSuccess) { modelAndView.addObject( "successMessage", "Account '" + accountNo + "' successfully created !!!"); resetForm(createAccountForm); } else { result.addError(new ObjectError("account", "Error Opening new account")); } } modelAndView.addObject("form", createAccountForm); modelAndView.addObject("accountTypeList", loadAccountTypeMap()); log.debug("Existing.........."); return modelAndView; }