public void updateStock(List<Myshopee_TransactionTO> transactionList)
      throws NoProductException, Exception {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("Group4");
    EntityManager em = null;

    try {
      em = emf.createEntityManager();
      em.getTransaction().begin();

      if (transactionList.isEmpty()) {
        throw new NoProductException();
      }

      for (Myshopee_TransactionTO transactionTO : transactionList) {
        int productId = transactionTO.getProductId();
        Myshopee_Product product = em.find(Myshopee_Product.class, productId);
        if (product != null) {
          product.setQtyInStock(product.getQtyInStock() - transactionTO.getQuantityPurchased());
        }
      }

      em.getTransaction().commit();
    } catch (NoProductException e) {
      ErrorLogger.logError(this.getClass().getName(), "updateStock", e.getMessage());
      throw e;
    } catch (Exception e) {
      ErrorLogger.logError(this.getClass().getName(), "updateStock", e.getMessage());
      throw e;
    }
  }
  public String getBillIdValue() {
    /** */
    transactionList = new ArrayList<Myshopee_TransactionTO>();

    try {
      FacesContext context = FacesContext.getCurrentInstance();
      String paramValue =
          (String) context.getExternalContext().getRequestParameterMap().get("param");
      this.billId = Integer.parseInt(paramValue);

      transactionList = new Myshopee_Wrapper().getTransactionByBillId(this.billId);

      return "success";

    } catch (NoTransactionForThisBillException e) {
      ErrorLogger.logError(this.getClass().getName(), "getBillIdValue", e.getMessage());
      transactionList = null;
      this.setMessage(e.getMessage());
      return "fail";
    } catch (Exception e) {
      ErrorLogger.logError(this.getClass().getName(), "getBillIdValue", e.getMessage());
      transactionList = null;
      this.setMessage(e.getMessage());
      return "fail";
    }
  }
  public int editProduct(Myshopee_ProductTO productTO) throws Exception {

    Myshopee_Product product = new Myshopee_Product();

    product.setProductId(productTO.getProductId());
    product.setProductName(productTO.getProductName());
    product.setQtyInStock(productTO.getQtyInStock());
    product.setPricePerUnit(productTO.getPricePerUnit());
    product.setCategory(productTO.getCategory());
    product.setAssociated_points(productTO.getAssociatedPoints());
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("Group4");
    EntityManager em = null;
    int productId = 0;
    try {
      em = emf.createEntityManager();
      em.getTransaction().begin();
      em.merge(product);
      em.getTransaction().commit();
      productId = product.getProductId();

      return productId;

    } catch (Exception e) {
      ErrorLogger.logError(this.getClass().getName(), "getProductFromId", e.getMessage());

      throw e;
    } finally {
      if (em != null) em.close();
    }
  }
  public Myshopee_ProductTO getProductFromId(int productId) throws Exception {
    EntityManager em = null;
    try {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("Group4");

      em = emf.createEntityManager();
      Myshopee_Product product = new Myshopee_Product();
      Myshopee_ProductTO productTO = new Myshopee_ProductTO();
      product = em.find(Myshopee_Product.class, productId);
      if (product != null) {
        productTO.setProductId(product.getProductId());
        productTO.setProductName(product.getProductName());
        productTO.setAssociatedPoints(productTO.getAssociatedPoints());
        productTO.setQtyInStock(product.getQtyInStock());
        productTO.setCategory(product.getCategory());
        productTO.setPricePerUnit(product.getPricePerUnit());
        return productTO;
      } else {
        throw new ProductNotFoundException();
      }
    } catch (Exception e) {
      ErrorLogger.logError(this.getClass().getName(), "getProductFromId", e.getMessage());
      throw e;
    } finally {
      if (em != null) em.close();
    }
  }
  public List<Myshopee_ProductTO> getProductsFromCategory(char category)
      throws NoProductInCategoryException, Exception {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("Group4");
    EntityManager em = null;
    List<Myshopee_ProductTO> productTOList = new ArrayList<Myshopee_ProductTO>();

    try {
      em = emf.createEntityManager();

      Query query = em.createQuery("select p from Myshopee_Product p where p.category = ?1");
      query.setParameter(1, category);
      List<Object> productList = query.getResultList();
      if (productList == null || productList.isEmpty()) {
        throw new NoProductInCategoryException();
      }

      for (Object object : productList) {
        Myshopee_Product product = (Myshopee_Product) object;

        Myshopee_ProductTO productTO = new Myshopee_ProductTO();
        productTO.setProductId(product.getProductId());
        productTO.setProductName(product.getProductName());
        productTO.setAssociatedPoints(product.getAssociated_points());
        productTO.setQtyInStock(product.getQtyInStock());
        productTO.setPricePerUnit(product.getPricePerUnit());
        productTO.setCategory(product.getCategory());

        // adding to the list of productTO
        productTOList.add(productTO);
      }
    } catch (NoProductInCategoryException e) {
      ErrorLogger.logError(this.getClass().getName(), "getProductsFromCategory", e.getMessage());
      throw e;
    } catch (Exception e) {
      ErrorLogger.logError(this.getClass().getName(), "getProductsFromCategory", e.getMessage());
      throw e;
    } finally {
      if (em != null) {
        em.close();
      }
    }

    return productTOList;
  }
  @SuppressWarnings("unchecked")
  public List<Myshopee_ProductTO> getProductFromCategory(char category) throws Exception {
    EntityManager em = null;
    try {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("Group4");
      List<Myshopee_Product> list = new ArrayList<Myshopee_Product>();
      List<Myshopee_ProductTO> list1 = new ArrayList<Myshopee_ProductTO>();
      em = emf.createEntityManager();

      Query query = em.createQuery("select p from Myshopee_Product p where p.category=?1");
      query.setParameter(1, category);
      list = query.getResultList();
      if (list.size() != 0) {
        for (int index = 0; index < list.size(); index++) {
          Myshopee_Product product = new Myshopee_Product();
          Myshopee_ProductTO productTO = new Myshopee_ProductTO();
          product = (Myshopee_Product) list.get(index);
          productTO.setProductId(product.getProductId());
          productTO.setProductName(product.getProductName());
          productTO.setAssociatedPoints(productTO.getAssociatedPoints());
          productTO.setQtyInStock(product.getQtyInStock());
          productTO.setCategory(product.getCategory());
          productTO.setPricePerUnit(product.getPricePerUnit());
          list1.add(productTO);
        }
        return list1;
      } else {
        throw new NoProductInCategoryException();
      }

    } catch (NoProductInCategoryException e) {
      ErrorLogger.logError(this.getClass().getName(), "getProductFromCategory", e.getMessage());
      throw e;
    } catch (Exception e) {
      ErrorLogger.logError(this.getClass().getName(), "getProductFromCategory", e.getMessage());
      throw e;
    } finally {
      if (em != null) em.close();
    }
  }
  public String displayTransaction() {
    /** */
    if (this.fromDate.after(this.toDate)) {
      this.setMessage("To date must be after from date");
      return "fail";

    } else {
      try {
        billList = new ArrayList<Myshopee_BillingTO>();
        billList = new Myshopee_Wrapper().getTransaction(this.toDate, this.fromDate);

        return "dsuccess";
      } catch (NoBillingForThisRangeException e) {
        billList = null;
        this.setMessage(e.getMessage());
        return "fail";
      } catch (Exception e) {
        billList = null;
        ErrorLogger.logError(this.getClass().getName(), "displayTransaction", e.getMessage());
        this.setMessage(e.getMessage());
        return "fail";
      }
    }
  }