private Account createAccount(CreateAccountForm createAccountForm, String profileId) {
   Account account = new Account();
   account.setProfileId(profileId);
   account.setType(createAccountForm.getAccountType());
   account.setCreatedBy(
       createAccountForm.getStaffFirstName() + " " + createAccountForm.getStaffLastName());
   account.setCreatedDate(new Date());
   account.setTenantId(createAccountForm.getTenantId());
   return account;
 }
 private void resetForm(CreateAccountForm createAccountForm) {
   createAccountForm.setAccountType(null);
   createAccountForm.setFirstName(null);
   createAccountForm.setLastName(null);
   createAccountForm.setDateOfBirth(null);
   createAccountForm.setBranchCode(null);
   createAccountForm.setSex(null);
   createAccountForm.setUserName(null);
   createAccountForm.setAddress(null);
   createAccountForm.setPhoneNo(null);
   createAccountForm.setEmailAddress(null);
 }
 private User createUser(CreateAccountForm createAccountForm, String profileId) {
   User user = new User();
   user.setUserId(createAccountForm.getUserName());
   user.setProfileId(profileId);
   user.setTenantId(createAccountForm.getTenantId());
   user.setCreatedBy(
       createAccountForm.getStaffFirstName() + " " + createAccountForm.getStaffLastName());
   user.setCreatedDate(new Date());
   Role role = new Role();
   role.setPrimary(CUSTOMER_ROLE);
   user.setRole(role);
   return user;
 }
 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.GET)
  public ModelAndView createAccountFormLoad(
      @RequestParam(value = "tenantid") String tenantId,
      @RequestParam(value = "profileid") String profileId) {

    log.debug("Entering....");
    CreateAccountForm createAccountForm = new CreateAccountForm();
    createAccountForm.setTenantId(tenantId);
    createAccountForm.setStaffProfileId(profileId);
    ModelAndView modelAndView = new ModelAndView("staffcreateaccount");
    modelAndView.addObject("accountTypeList", loadAccountTypeMap());

    modelAndView.addObject("form", createAccountForm);
    log.debug("Existing..........");
    return modelAndView;
  }
  @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;
  }