Ejemplo n.º 1
0
  @RequestMapping(value = "login", method = RequestMethod.POST)
  public ModelAndView login(
      @Valid LoginForm form, Model model, BindingResult results, RedirectAttributes redirects) {
    ModelAndView mav = new ModelAndView();

    if (!session.isNew()) {
      LoginValidator valid = new LoginValidator();

      valid.validate(form, results);

      if (results.hasErrors()) {

        String emailOK = "";
        String passwordOK = "";

        if (form.isEmailOK() == false) {
          emailOK = "Email is not verified. Please, try changing it.";
        }
        if (form.isPasswordOK() == false) {
          passwordOK = "Password is not verified. Please, try changing it.";
        }

        mav.addObject("emailOK", emailOK);
        mav.addObject("passwordOK", passwordOK);
        mav = setModelAndView(mav, "index");

        List<Product> list = (ArrayList<Product>) mav.getModel().get("products");
        if (list.size() == 1) {
          mav.setViewName("noIndex");
        }

        mav.addObject("form", form);

        return mav;
      } else {
        MainLink link = se.casdev.spring.web.controller.Controller.link;

        Customer user = link.getUserLink().login(form.getEmail(), form.getPassword());
        session.setAttribute("user", user);

        if (session.getAttribute("user") == null) {
          String emailOK = "Email and password doesn't fit together";
          String passwordOK = "";
          mav.addObject("emailOK", emailOK);
          mav.addObject("passwordOK", passwordOK);
          mav = setModelAndView(mav, "index");

          List<Product> list = (ArrayList<Product>) mav.getModel().get("products");
          if (list.size() == 1) {
            mav.setViewName("noIndex");
          }

          mav.addObject("form", new LoginForm());

          return mav;

        } else {

          mav.addObject("cancelledorder", "");
          mav = setModelAndView(mav, "home");

          List<Product> list = (ArrayList<Product>) mav.getModel().get("products");
          if (list.size() == 1) {
            mav.setViewName("noHome");
          }

          return mav;
        }
      }

    } else {
      String emailOK = "";
      String passwordOK = "";
      mav.addObject("emailOK", emailOK);
      mav.addObject("passwordOK", passwordOK);
      mav = setModelAndView(mav, "index");

      List<Product> list = (ArrayList<Product>) mav.getModel().get("products");
      if (list.size() == 1) {
        mav.setViewName("noIndex");
      }

      mav.addObject("form", new LoginForm());
    }

    return mav;
  }