public String perform(HttpServletRequest request) {
    HttpSession session = request.getSession(false);
    List<String> errors = new ArrayList<String>();
    request.setAttribute("errors", errors);

    User user = (User) request.getSession(false).getAttribute("user");
    Blog[] blogList = null;
    try {
      ReplyBlogForm form = formBeanFactory.create(request);
      request.setAttribute("form", form);
      request.setAttribute("session", currentSession);
      if (!form.isPresent()) {
        return "view-post-reply.jsp";
      }

      errors.addAll(form.getValidationErrors());
      if (errors.size() > 0) {
        request.setAttribute("form", null);
        return "view-post-reply.jsp";
      }

      // store the user reply posts under the blog subject in the database
      Blog movieBlog = new Blog();
      String subject = currentSession.getBlogSubject();
      movieBlog.setBlogSubject(subject);
      movieBlog.setBlogPost(form.getReplyPost());
      movieBlog.setUserName(user.getUserName());
      blogDAO.create(movieBlog);

      blogList = blogDAO.getBlogs(subject);
      currentSession.setBlogSubject(subject);
      currentSession.setBlogList(blogList);

      request.setAttribute("session", currentSession);

      // if user is not logged in do not give option to post a reply on the blog
      if (user == null) {
        return "view-only-post.jsp";
      } else {
        return "view-post-reply.jsp";
      }
    } catch (DAOException e) {
      e.printStackTrace();
      return "error.jsp";
    } catch (FormBeanException e) {
      errors.add(e.getMessage());
      return "manage.jsp";
    }
  }
Exemple #2
0
  @Override
  public String perform(HttpServletRequest request) {

    LoginForm form = formBeanFactory.create(request);

    List<String> errors = prepareErrors(request);

    if (!form.isPresent()) {
      return "home.jsp";
    }
    request.setAttribute("form", form);
    errors.addAll(form.getValidationErrors());
    if (errors.size() != 0) {
      return "home.jsp";
    }

    User user;
    try {
      user = userDAO.lookup(form.getUserName());
    } catch (DAOException e) {
      errors.add(e.getMessage());
      return "home.jsp";
    }

    if (user == null) {
      errors.add("Username not found");
      return "home.jsp";
    }

    if (!user.checkPassword(form.getPassword())) {
      errors.add("Incorrect password");
      return "home.jsp";
    }

    HttpSession session = request.getSession();
    session.setAttribute("user", user);

    request.setAttribute("success", "Welcome back! " + user.getUserName());
    return "home.jsp";
  }
  @Override
  public String perform(HttpServletRequest request) {
    ExchangeForm form = formBeanFactory.create(request);
    List<String> errors = prepareErrors(request);
    User curUser = (User) request.getSession().getAttribute("user");
    if (curUser == null) {
      errors.add("You are not logged in");
      return "browse.do";
    }
    if (!form.isPresent()) {
      return "browse.do";
    }
    errors.addAll(form.getValidationErrors());
    if (errors.size() != 0) return "browse.do";

    Exchange xchg = null;
    User admin = null;
    try {
      xchg = exchangeDAO.lookup(form.getExchangeIdAsInt());
      admin = userDAO.lookup("Admin");
      curUser = userDAO.lookup(curUser.getUserName());
    } catch (DAOException e) {
      errors.add(e.getMessage());
      return "browse.do";
    }
    request.getSession().setAttribute("user", curUser);
    if (xchg == null) {
      errors.add("Transaction not found");
      return "browse.do";
    }
    Item item = null;
    try {
      item = itemDAO.getItemById(xchg.getItem().getId());
    } catch (DAOException e) {
      errors.add(e.getMessage());
      return "browse.do";
    }
    if (!xchg.getPoster().getUserName().equals(curUser.getUserName())) {
      errors.add("You are not the owner of the item");
      return "showMyItems.do";
    }
    if (xchg.getStatus() == Exchange.CLOSED || item.getStatus() == Item.CLOSED) {
      errors.add("Item already closed");
      return "showMyItems.do";
    }
    try {
      Transaction.begin();
      if (xchg.getRespondType() == Exchange.ANSWER_REQUEST_FOR_CREDIT) {
        userDAO.transferCredit(item.getCredit(), xchg.getPoster(), xchg.getResponder());
        curUser.setCredit(curUser.getCredit() - item.getCredit());
      }
      String url1 = "<a href=&quot;showItems.do?itemId=" + item.getId() + "&quot;>item</a>";
      itemDAO.closeItem(item.getId());
      exchangeDAO.setSuccessTransaction(xchg.getId());
      messageDAO.send(
          admin,
          curUser,
          "Transaction on (" + item.getItemName() + ") complete",
          "You have accepted the request from ("
              + "<a href=&quot;redirectSend.do?receiver="
              + xchg.getResponder().getUserName()
              + "&quot;>"
              + xchg.getResponder().getUserName()
              + "</a>). Your "
              + url1
              + " is now closed.");
      userDAO.updateNewMsgCount(curUser.getUserName(), 1);
      messageDAO.send(
          admin,
          xchg.getResponder(),
          "Transaction on (" + item.getItemName() + ") complete",
          "The <a href=&quot;redirectSend.do?receiver="
              + item.getOwner().getUserName()
              + "&quot;>owner</a> of "
              + url1
              + " has accepted your request");

      userDAO.updateNewMsgCount(xchg.getResponder().getUserName(), 1);
      Exchange[] pending = exchangeDAO.findItemPendingTransactions(item);
      for (Exchange each : pending) {
        messageDAO.send(
            admin,
            each.getResponder(),
            "Transaction on (" + item.getItemName() + ") dismissed",
            "The item you have reponded to is now closed");
        userDAO.updateNewMsgCount(each.getResponder().getUserName(), 1);
      }
      exchangeDAO.closeItemTransaction(item);
      Transaction.commit();
    } catch (RollbackException e) {
      errors.add(e.getMessage());
      return "showMyItems.do";
    } finally {
      if (Transaction.isActive()) Transaction.rollback();
    }
    try {
      curUser = userDAO.lookup(curUser.getUserName());
    } catch (DAOException e) {
      errors.add(e.getMessage());
      return "showMyItems.do";
    }
    request.getSession().setAttribute("user", curUser);
    request.setAttribute("success", "Congrats, your transaction has been made");
    return "showMyItems.do";
  }