// 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; }
// 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; }
// 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; }