// sets the order item to a particular book present in the book database
  public boolean setOrderItem(int id, int productId) {
    Boolean flag = false;
    try {

      Session session = HibernateUtil.getSessionFactory().openSession();
      Criteria cr1 = session.createCriteria(Book.class);
      cr1.add(Restrictions.eq("productId", id));
      Object result1 = cr1.uniqueResult();
      if (result1 != null) {
        Book book = (Book) result1;
        Session session1 = HibernateUtil.getSessionFactory().openSession();
        Transaction tx1 = session1.beginTransaction();
        OrderItem oi1 = (OrderItem) session1.get(OrderItem.class, productId);
        oi1.setBook(book);
        session1.update(oi1);
        tx1.commit();
        close();
        flag = true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return flag;
  }
  // removes the new books order item request placed by the customer on delivering it
  public List<OrderItem> removeNewBookOrderItem(Book book, List<OrderItem> list) {
    try {
      Book book1 = new Book();
      for (int i = list.size() - 1; i >= 0; i--) {
        OrderItem oi = list.get(i);

        if (oi.getBook().getProductName().trim().equals(book.getProductName())
            && oi.getBook().getAuthor().trim().equals(book.getAuthor())
            && oi.getBook().getEdition().trim().equals(book.getEdition())
            && oi.getBook().getActive() == book.getActive()) {
          list.remove(i);
          book1 = oi.getBook();
          if (book1.getActive() == 0) {
            Session session1 = HibernateUtil.getSessionFactory().openSession();
            Transaction tx1 = session1.beginTransaction();
            Book book2 = (Book) session1.get(Book.class, book1.getProductId());
            if (book.getOrderitemList() != null) {
              session1.delete(book2.getInventoryItem());
            }
            session1.delete(book2);
            tx1.commit();
            close();
          }
          break;
        }
      }
    } catch (Exception e) {
      System.out.print("Can not delete");
    }

    return list;
  }
  // add new order item
  public List<OrderItem> addOrderItem(int bookId, int quantity, List<OrderItem> list) {
    try {
      begin();
      boolean flag = false;
      Session session = HibernateUtil.getSessionFactory().openSession();
      Transaction tx = session.beginTransaction();
      Book book = (Book) session.get(Book.class, bookId);
      for (int i = list.size() - 1; i >= 0; i--) {
        OrderItem oi = list.get(i);
        if (oi.getBook().getProductId() == bookId) {
          flag = true;
          list.remove(i);
          OrderItem item = new OrderItem();
          item.setQuantity(oi.getQuantity() + quantity);
          item.setBook(book);
          list.add(item);
        }
      }
      if (!flag) {
        OrderItem item = new OrderItem();
        item.setQuantity(quantity);
        item.setBook(book);
        list.add(item);
      }
      commit();
    } catch (Exception e) {
      rollback();
    }

    return list;
  }
  // delete a particular sell order item placed by the customer
  public List<OrderItem> deleteSellOrderitem(Book book, List<OrderItem> selllist) {
    boolean flag = false;
    for (int i = selllist.size() - 1; i >= 0; i--) {
      OrderItem oi = selllist.get(i);
      Book book1 = oi.getBook();
      if (book == book1) {
        selllist.remove(i);
        flag = true;
        break;
      }
    }

    return selllist;
  }
  // updates the order item status on how many delivered or not
  public Boolean updateOrderItemStatus(int orderitemId, String status) {
    Boolean flag = false;
    try {
      Session session1 = HibernateUtil.getSessionFactory().openSession();
      Transaction tx1 = session1.beginTransaction();
      OrderItem orderitem = (OrderItem) session1.get(OrderItem.class, orderitemId);
      orderitem.setItemStatus(status);
      session1.update(orderitem);
      flag = true;
      tx1.commit();
      session1.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
    return flag;
  }
 // updates the details of an order item
 public void updateItem(String status, Float cost, int oid) {
   try {
     Session session1 = HibernateUtil.getSessionFactory().openSession();
     Transaction tx1 = session1.beginTransaction();
     OrderItem oi1 = (OrderItem) session1.get(OrderItem.class, oid);
     oi1.setItemStatus(status);
     Book book = oi1.getBook();
     int bookId = book.getProductId();
     Book book1 = (Book) session1.get(Book.class, bookId);
     session1.update(oi1);
     session1.update(book1);
     tx1.commit();
     close();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  // confirm a selling order by the customer
  public boolean confirmSellingOrder(List<OrderItem> list, Person person) {
    boolean flag = false;

    try {

      if (list.size() > 0) {
        DifferentOrder order1 = new DifferentOrder();
        order1.setOrderDate(new Date());
        order1.setOrderStatus("Order to be reviewed");
        order1.setOrderType("Seller Old");
        order1.setPerson(person);

        for (int i = 0; i < list.size(); i++) {
          OrderItem oi = new OrderItem();
          oi = list.get(i);
          // oi.setOrder(order);
        }

        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.save(order1);
        tx.commit();
        session.close();

        for (int i = 0; i < list.size(); i++) {
          OrderItem oi = new OrderItem();
          oi = list.get(i);
          oi.setOrderNo(order1);
          Session session1 = HibernateUtil.getSessionFactory().openSession();
          Transaction tx1 = session1.beginTransaction();
          session1.save(oi);
          tx1.commit();
          session1.close();
        }
        flag = true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    return flag;
  }
  // updates the order item cost for the books to be sold by the customer
  public Boolean updateOrderitemCost(int orderitemId, Float cost) {
    Boolean flag = false;
    try {
      Session session1 = HibernateUtil.getSessionFactory().openSession();
      Transaction tx1 = session1.beginTransaction();
      OrderItem orderitem = (OrderItem) session1.get(OrderItem.class, orderitemId);
      Book book = orderitem.getBook();
      book.setProductCost(cost);
      orderitem.setBook(book);
      DifferentOrder order = orderitem.getOrderNo();
      order.setOrderAmount(orderitem.getQuantity() * orderitem.getBook().getProductCost());
      orderitem.setOrderNo(order);
      session1.update(order);
      session1.update(orderitem);
      flag = true;
      tx1.commit();
      session1.close();

    } catch (Exception e) {
      e.printStackTrace();
    }
    return flag;
  }
  // confirm and place the order in the database
  public Boolean confirmOrder(
      List<OrderItem> list, List<OrderItem> newlist, Address address, Card card, Person person) {

    Boolean flag = false;
    float total = 0;
    try {

      if (list.size() > 0) {
        DifferentOrder order1 = new DifferentOrder();
        order1.setOrderDate(new Date());
        order1.setOrderStatus("Order to be reviewed");
        order1.setOrderType("Buyer Old");
        order1.setPerson(person);

        for (int i = 0; i < list.size(); i++) {
          OrderItem oi = new OrderItem();
          oi = list.get(i);
          // oi.setOrder(order);
          total = oi.getQuantity() * oi.getBook().getProductCost();
        }

        order1.setOrderAmount(total);
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.save(order1);
        tx.commit();
        session.close();

        for (int i = 0; i < list.size(); i++) {
          OrderItem oi = new OrderItem();
          oi = list.get(i);
          oi.setOrderNo(order1);
          Session session1 = HibernateUtil.getSessionFactory().openSession();
          Transaction tx1 = session1.beginTransaction();
          session1.save(oi);
          tx1.commit();
          session1.close();
        }
        flag = true;
      }

      if (newlist.size() > 0) {
        DifferentOrder order1 = new DifferentOrder();
        order1.setOrderDate(new Date());
        order1.setOrderStatus("Order to be reviewed");
        order1.setOrderType("Buyer New");
        order1.setPerson(person);

        for (int i = 0; i < newlist.size(); i++) {
          OrderItem oi = new OrderItem();
          oi = newlist.get(i);
          // total= oi.getQuantity() * oi.getBook().getProductCost();
        }

        // order.setOrderAmount(total);
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction tx = session.beginTransaction();
        session.save(order1);
        tx.commit();
        session.close();

        for (int i = 0; i < newlist.size(); i++) {
          OrderItem oi = new OrderItem();
          oi = newlist.get(i);
          oi.setOrderNo(order1);
          Session session1 = HibernateUtil.getSessionFactory().openSession();
          Transaction tx1 = session1.beginTransaction();
          session1.save(oi);
          tx1.commit();
          session1.close();
        }
        flag = true;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return flag;
  }