private void resetForm(UserForm form) {
   form.setRole(null);
   form.setFirstName(null);
   form.setLastName(null);
   form.setDateOfBirth(null);
   form.setUserTenandId(null);
   form.setSex(null);
   form.setUserName(null);
   form.setAddress(null);
   form.setPhoneNo(null);
   form.setEmailAddress(null);
 }
 private User createUser(UserForm form, String profileId) {
   User user = new User();
   user.setUserId(form.getUserName());
   user.setProfileId(profileId);
   user.setTenantId(form.getTenantId());
   user.setCreatedBy(form.getStaffFirstName() + " " + form.getStaffLastName());
   user.setCreatedDate(new Date());
   Role role = new Role();
   role.setPrimary(STAFF_ROLE);
   role.setSecondary(form.getRole());
   user.setRole(role);
   return user;
 }
  @RequestMapping(value = "/staffcreatenewuser", method = RequestMethod.GET)
  public ModelAndView loadCreateUser(
      @RequestParam(value = "tenantid") String tenantId,
      @RequestParam(value = "profileid") String profileId) {

    UserForm userForm = new UserForm();
    userForm.setTenantId(tenantId);
    userForm.setStaffProfileId(profileId);
    ModelAndView modelAndView = new ModelAndView("staffcreatenewuser");
    modelAndView.addObject("roleType", loadRoleMap());
    modelAndView.addObject("form", userForm);
    return modelAndView;
  }
 private Profile createProfile(UserForm form) {
   Profile profile = new Profile();
   profile.setFirstName(form.getFirstName());
   profile.setLastName(form.getLastName());
   profile.setDateOfBirth(AppUtil.getDateFromStr(form.getDateOfBirth()));
   profile.setAddress(form.getAddress());
   profile.setEmailAddress(form.getEmailAddress());
   profile.setPhone(form.getPhoneNo());
   profile.setTenantId(form.getUserTenandId());
   profile.setSex(form.getSex());
   profile.setCreatedBy(form.getStaffFirstName() + " " + form.getStaffLastName());
   profile.setCreatedDate(new Date());
   return profile;
 }
  @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;
  }