p align="justify">}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/DAL;Entities.Product;java.util.List;javax.persistence.EntityManager;javax.persistence.EntityTransaction;javax.persistence.Query;
/**
*
*author Humanity
*/class ProductRepository {EntityManager entityManager; String GetAllProductsQuery= select p from Product p raquo ;; ProductRepository (EntityManager entityManager) {.entityManager=entityManager;
} List lt; Product gt; GetAllProducts () {query=entityManager.createQuery (GetAllProductsQuery); query.getResultList ();
} Product GetProductById (int productId) {product=entityManager.find (Product.class, productId); product;
} boolean InsertProduct (Product product) {t=entityManager.getTransaction () ;. begin (); {. merge (product);
//entityManager.persist (product) ;. commit (); true;
} catch (Exception ex) {.rollback (); false;
}
} void UpdateProduct (Product product) {t=entityManager.getTransaction () ;. begin (); {. refresh (product) ;. flush () ;. commit ();
} catch (Exception ex) {.rollback ();
}
} void DeleteProduct (int productId) {t=entityManager.getTransaction () ;. begin (); {Product=GetProductById (productId) ;. remove (Product) ;. flush () ;. commit ();
} catch (Exception ex) {.rollback ();
}
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/DAL;Entities.Product;Entities.Shipment;java.lang.Math;java.util.List;javax.persistence.EntityManager;javax.persistence.EntityTransaction;javax.persistence.Query;
/**
*
*author Humanity
*/class ShipmentRepository {EntityManager entityManager; String GetAllShipmentsQuery= select s from Shipment s raquo ;; String GetShipmentsForProductQuery= select s from Shipment s where s.productid.id =% 1 $ d ; String GetShipmentsForPersonQuery= select s from Shipment s where s.responsiblepersonid.id =% 1 $ d raquo ;; String GetShipmentsForPersonAndProductQuery=
select s from Shipment s where s.responsiblepersonid.id =% 1 $ d and s.productid.id =% 2 $ d raquo ;; ShipmentRepository (EntityManager entityManager) {.entityManager=entityManager;
} List lt; Shipment gt; GetAllShipments () {query=entityManager.createQuery (GetAllShipmentsQuery); query.getResultList ();
} Shipment GetShipmentById (int shipmentId) {shipment=entityManager.find (Shipment.class, shipmentId); shipment;
} List lt; Shipment gt; GetShipmentForProduct (int productId) {query=entityManager.createQuery (String.format (GetShipmentsForProductQuery, productId)); query.getResultList ();
} List lt; Shipment gt; GetShipmentForPerson (int personId) {query=entityManager.createQuery (String.format (GetShipmentsForPersonQuery, personId)); query.getResultList ();
} List lt; Shipment gt; GetShipmentList (int personId, int productId) {(productId == 0) {(personId == 0) {GetAllShipments ();
} else {GetShipmentForPerson (personId);
}
} else {(personId == 0) {GetShipmentForProduct (productId);
} else {query=entityManager.createQuery (String.format (GetShipmentsForPersonAndProductQuery, personId, productId)); query.getResultList ();
}
}
} boolean InsertShipment (Shipment shipment) {p=StockDatabase.getInstance().getProducts().GetProductById(shipment.getProductid().getId());(shipment.getQuantity() lt; 0 amp; amp; .getQuantityinstock () lt; Math.abs (shipment.getQuantity ())) {
//trying to take TOO MUCH from stock ... false;
} finalQuantity=p.getQuantityinstock () + shipment.getQuantity () ;. setQuantityinstock (finalQuantity); t=entityManager.getTransaction (); {.begin () ;. persist (shipment) ;. persist (p);
//entityManager.flush () ;. commit (); true;
} catch (Exception ex) {.rollback (); false;
}
} void UpdateShipment (Shipment ...