Пример #1
0
 @Before
 public void setupSignup() {
   signupForm = new SignupForm();
   signupForm.setId((long) 1500);
   signupForm.setFirstName("Capitain");
   signupForm.setLastName("Awesome");
   signupForm.setEmail("*****@*****.**");
   signupForm.setBiography("I try to be .......... awesome...");
   signupForm.setPassword("123456");
   signupForm.setTimeSlots(getNewTimeSlotList());
   signupForm.setUserCourseList(getNewUserCourseFormAttributeList());
 }
  /**
   * Validates all changes and tries to save them to the database.
   *
   * @param principal the user that edited his profile.
   * @param signupForm the form which contains all information about the principal.
   * @param result contains error messages.
   * @param redirectAttributes used to add some attributes to the model.
   * @param session the current session especially containing the username.
   * @return the profile page if validations succeeded else returns the editProfile page with
   *     attached errors.
   */
  @RequestMapping(value = "/editProfile", method = RequestMethod.POST)
  public ModelAndView postProfile(
      Principal principal,
      @Validated(SignupForm.SignupValidatorGroup.class) SignupForm signupForm,
      BindingResult result,
      RedirectAttributes redirectAttributes,
      HttpSession session) {
    User user = userService.getPrincipalUser();
    signupForm.setEmail(user.getEmail());
    ModelAndView model;

    try {
      if (!signupForm.getPassword().equals(signupForm.getPasswordVerify())) {
        redirectAttributes.addFlashAttribute("passwordVerifyError", "Your passwords do not match!");
        redirectAttributes.addFlashAttribute("infoMessage", "Your passwords do not match!");
        return new ModelAndView("redirect:/editProfile");
      }
    } catch (Exception d) {
    }

    if (!result.hasErrors()) {
      try {
        User updatedUser = userService.saveFrom(signupForm, user);
        userService.createAndSaveUserCoursesFromForm(signupForm, user);
        redirectAttributes.addFlashAttribute(
            "infoMessage", "You successfully edited your profile!");
        session.setAttribute("username", updatedUser.getWholeName());

        model = new ModelAndView("redirect:/profile");
      } catch (InvalidUserException e) {
        model = new ModelAndView("editProfile");
        model.addObject("page_error", e.getMessage());
      }
    } else {
      model = new ModelAndView("editProfile");
      model.addObject("infoMessage", result.toString());
      System.out.println("Form has errors:\n" + result.toString());
    }
    return model;
  }