Esempio n. 1
0
  @Override
  public String execute() throws Exception {
    try {

      // 获取购物车信息
      ActionContext actionContext = ActionContext.getContext();
      Map websession = actionContext.getSession();
      Cart theCart = (Cart) websession.get("cart");
      Vector<Booklist> booksincart = theCart.getBookincart();
      Map<Integer, Integer> amountmap = theCart.getAmountmap();
      int totalprice = theCart.getTotal_price();

      User theUser = (User) websession.get("user");
      int user_id = 0;
      if (theUser != null) {
        user_id = theUser.getId();
      }
      // 将购物车信息写入数据库
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      session
          .createQuery("delete from Cart where user_id = ?")
          .setString(0, user_id + "")
          .executeUpdate();
      session.getTransaction().commit();

      Cart temCart = new Cart();
      temCart.setUser_id(user_id);
      Set<Integer> keys = amountmap.keySet();
      for (int i = 0; i < booksincart.size(); i++) {
        temCart.setBook_id(booksincart.get(i).getId());
        temCart.setTotal_amount(amountmap.get(temCart.getBook_id()));
        Session insertsession = HibernateUtil.getSessionFactory().getCurrentSession();
        insertsession.beginTransaction();
        insertsession.save(temCart);
        insertsession.getTransaction().commit();
      }

      // 删除用户session
      websession.clear();
      return SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return ERROR;
    }
  }
  @Override
  public String execute() throws Exception {
    try {
      String book_id = this.id;

      Session session = HibernateUtil.getSessionFactory().getCurrentSession();
      session.beginTransaction();
      session
          .createQuery("delete from Booklist where id = ?")
          .setString(0, book_id)
          .executeUpdate();
      session.getTransaction().commit();
      return SUCCESS;

    } catch (Exception e) {
      e.printStackTrace();
      return ERROR;
    }
  }
  @Override
  public String execute() throws Exception {
    try {
      String bookname = this.bookname;
      String operator = this.operator;
      ActionContext actionContext = ActionContext.getContext();
      Map websession = actionContext.getSession();
      Cart theCart = (Cart) websession.get("cart");

      Vector<Booklist> currentbooks = theCart.getBookincart();
      Map<Integer, Integer> amountmap = theCart.getAmountmap();
      int totalprice = theCart.getTotal_price();
      // 减少商品
      if (operator.equals("-")) {
        for (int i = 0; i < currentbooks.size(); i++) {
          if (currentbooks.get(i).getBookname().equals(bookname)) {
            int id = currentbooks.get(i).getId();
            int temamount = amountmap.get(id);
            int oneprice = currentbooks.get(i).getPrice();
            totalprice -= oneprice;
            if (temamount != 1) amountmap.put(id, temamount - 1);
            else {
              amountmap.remove(id);
              currentbooks.remove(i);
            }
            break;
          }
        }
        // 存session
        theCart.setAmountmap(amountmap);
        theCart.setBookincart(currentbooks);
        theCart.setTotal_price(totalprice);
        websession.put("cart", theCart);
      }

      // 增加商品
      if (operator.equals("1")) {
        // 搜索是否已经是cart里面的商品
        Boolean already = false;
        for (int i = 0; i < currentbooks.size(); i++) {
          if (currentbooks.get(i).getBookname().equals(bookname)) {
            int id = currentbooks.get(i).getId();
            int temamount = amountmap.get(id);
            int oneprice = currentbooks.get(i).getPrice();
            totalprice += oneprice;

            amountmap.put(id, temamount + 1);
            already = true;
            break;
          }
        }
        if (already == false) {
          Session session = HibernateUtil.getSessionFactory().getCurrentSession();
          session.beginTransaction();
          List allbooks = session.createQuery("from Booklist").list();
          session.getTransaction().commit();
          for (int j = 0; j < allbooks.size(); j++) {
            Booklist theBook = (Booklist) allbooks.get(j);
            if (theBook.getBookname().equals(bookname)) {
              int id = theBook.getId();
              int oneprice = theBook.getPrice();
              totalprice += oneprice;

              currentbooks.add(theBook);
              amountmap.put(id, 1);
              break;
            }
          }
        }
        // 存session
        theCart.setAmountmap(amountmap);
        theCart.setBookincart(currentbooks);
        theCart.setTotal_price(totalprice);
        websession.put("cart", theCart);
      }
      return SUCCESS;

    } catch (Exception e) {
      e.printStackTrace();
      return ERROR;
    }
  }