@RequestMapping(method = RequestMethod.POST)
 public ModelAndView processForm(LoginForm loginForm, BindingResult result) {
   ModelAndView modelAndView = null;
   boolean isSuccess = false;
   validate(loginForm, result);
   if (!result.hasErrors()) {
     User user =
         loginService.login(
             loginForm.getUserName(), loginForm.getTenantId(), loginForm.getPassword());
     if (user != null && user.getProfileId() != null) {
       if ("CUSTOMER".equals(user.getRole().getPrimary())) {
         modelAndView =
             accountController.getAccountOverview(user.getProfileId(), loginForm.getTenantId());
         isSuccess = true;
       } else {
         log.debug("The user don't have a CUSTOMER ROLE");
         result.addError(new ObjectError("role", "You are not authorized to login"));
       }
     } else {
       result.addError(new ObjectError("password", "Username or Password is wrong"));
     }
   }
   if (!isSuccess) {
     modelAndView = new ModelAndView("login");
     // loginForm = new LoginForm();
     loginForm.setPassword(null);
     loginForm.setUserName(null);
     modelAndView.addObject("form", loginForm);
   }
   return modelAndView;
 }
  @RequestMapping(value = "/staffchangepasswordsubmit", method = RequestMethod.POST)
  public ModelAndView changePasswordSubmit(
      @ModelAttribute("form") ChangePasswordForm form, BindingResult result) {

    log.debug("Entering ....");
    ModelAndView modelAndView = new ModelAndView("staffchangepassword");

    validateChangePassword(form, result);

    if (!result.hasErrors()) {
      if (form.getNewPassword().equals(form.getNewPasswordRep())) {
        User user = loginService.getUserByProfileId(form.getTenantId(), form.getStaffProfileId());
        if (user != null && form.getCurrentPassword().equals(user.getPassword())) {
          user.setPassword(form.getNewPassword());
          loginService.updateUser(user);
          modelAndView.addObject("successMessage", "Password changed successfully !!!");
        } else {
          result.addError(new ObjectError("currentPassword", "Current password is wrong"));
        }

      } else {
        result.addError(new ObjectError("newPassword", "New password doesn't match"));
      }
    }
    modelAndView.addObject("form", form);
    log.debug("Existing..........");
    return modelAndView;
  }
  @RequestMapping(value = "/staffcreatenewusersubmit", method = RequestMethod.POST)
  public ModelAndView createUserSubmit(
      @ModelAttribute("form") UserForm userForm, BindingResult result) {

    log.debug("Entering....");
    ModelAndView modelAndView = new ModelAndView("staffcreatenewuser");

    boolean isSuccess = true;
    validateUserForm(userForm, result);
    if (userForm.getDateOfBirth() != null) {
      if (!AppUtil.isAValidDDMMYYYYDate(userForm.getDateOfBirth())) {
        log.error("Invalid date format " + userForm.getDateOfBirth());
        isSuccess = false;
        result.addError(
            new ObjectError(
                "dateOfBirth", "Invalid dateOfBirth. Please use the format " + DATE_FORMAT));
      }
    }
    if (isSuccess && !result.hasErrors()) {

      String tenantId = userForm.getTenantId();
      Profile staffProfile = profileService.getProfileById(userForm.getStaffProfileId(), tenantId);
      if (staffProfile != null) {
        userForm.setStaffFirstName(staffProfile.getFirstName());
        userForm.setStaffLastName(staffProfile.getLastName());
      }

      // Create Profile
      String profileId = profileService.saveProfile(createProfile(userForm));
      try {
        // Create Use Account
        loginService.CreateUser(createUser(userForm, profileId));

      } catch (Exception e) {
        log.error("Error Creating User {} ", e);
        isSuccess = false;
        profileService.deleteProfile(profileId, tenantId);
      }

      if (isSuccess) {
        modelAndView.addObject(
            "successMessage", "User '" + userForm.getUserName() + "' successfully created !!!");
        resetForm(userForm);
      } else {
        result.addError(new ObjectError("userAccount", "Error creating new user"));
      }
    }
    modelAndView.addObject("roleType", loadRoleMap());
    modelAndView.addObject("form", userForm);
    log.debug("Existing..........");
    return modelAndView;
  }
  /**
   * Change Password page load for staff
   *
   * @param tenantId
   * @param profileId
   * @return
   */
  @RequestMapping(value = "/staffchangepassword", method = RequestMethod.GET)
  public ModelAndView changePassword(
      @RequestParam(value = "tenantid") String tenantId,
      @RequestParam(value = "profileid") String profileId) {

    log.debug("Entering....");
    ChangePasswordForm form = new ChangePasswordForm();
    ModelAndView modelAndView = new ModelAndView("staffchangepassword");
    form.setTenantId(tenantId);
    form.setStaffProfileId(profileId);
    User user = loginService.getUserByProfileId(form.getTenantId(), form.getStaffProfileId());
    if (user != null && user.getRole() != null) {
      Role role = user.getRole();
      if (STAFF_ADMIN_ROLE.equals(role.getSecondary())) {
        modelAndView.addObject("role", STAFF_ADMIN_ROLE);
      }
    }

    modelAndView.addObject("form", form);
    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;
  }