daily pastebin goal
42%
SHARE
TWEET

Untitled

a guest May 16th, 2018 121 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package edu.pjwstk.sri.lab2.dao;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.annotation.Resource;
  6. import javax.ejb.EJBContext;
  7. import javax.ejb.Lock;
  8. import javax.ejb.LockType;
  9. import javax.ejb.Stateless;
  10. import javax.ejb.TransactionAttribute;
  11. import javax.ejb.TransactionAttributeType;
  12. import javax.ejb.TransactionManagement;
  13. import javax.ejb.TransactionManagementType;
  14. import javax.persistence.EntityManager;
  15. import javax.persistence.PersistenceContext;
  16. import javax.persistence.TypedQuery;
  17. import javax.rmi.CORBA.Util;
  18.  
  19. import edu.pjwstk.sri.lab2.dto.Cart;
  20. import edu.pjwstk.sri.lab2.dto.OrderItem;
  21. import edu.pjwstk.sri.lab2.model.Product;
  22.  
  23. /**
  24.  * DAO for Product
  25.  */
  26. @Stateless
  27. @TransactionManagement(TransactionManagementType.CONTAINER)
  28. public class ProductDao {
  29.    
  30.     @Resource
  31.     private EJBContext context;
  32.    
  33.     @PersistenceContext(unitName = "sri2-persistence-unit")
  34.     private EntityManager em;
  35.      @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  36.     public void create(Product entity) {
  37.         em.persist(entity);
  38.     }
  39.      @TransactionAttribute(TransactionAttributeType.REQUIRED)
  40.     public void deleteById(Long id) {
  41.         Product entity = em.find(Product.class, id);
  42.         if (entity != null) {
  43.             em.remove(entity);
  44.         }
  45.     }
  46.      @TransactionAttribute(TransactionAttributeType.NEVER)
  47.     public Product findById(Long id) {
  48.         return em.find(Product.class, id);
  49.     }
  50.      @TransactionAttribute(TransactionAttributeType.REQUIRED)
  51.     public Product update(Product entity) {
  52.          
  53.         return em.merge(entity);
  54.        
  55.     }
  56.      
  57.      public void makeOrder(Cart cart) {
  58.          try {
  59.              for(OrderItem oi : cart.getCartItems()) {
  60.                     Product prod_database = findById(oi.getProduct().getId());
  61.                     prod_database.setStock(prod_database.getStock() - oi.getAmount());
  62.                     update(prod_database);
  63.                 }
  64.              }catch(Throwable th){
  65.                     context.setRollbackOnly();
  66.                 }
  67.      }
  68.  
  69.      @Lock(LockType.READ)
  70.      @TransactionAttribute(TransactionAttributeType.NEVER)
  71.     public List<Product> listAll(Integer startPosition, Integer maxResult) {
  72.         TypedQuery<Product> findAllQuery = em
  73.                 .createQuery(
  74.                         "SELECT DISTINCT p FROM Product p LEFT JOIN FETCH p.category ORDER BY p.id",
  75.                         Product.class);
  76.         if (startPosition != null) {
  77.             findAllQuery.setFirstResult(startPosition);
  78.         }
  79.         if (maxResult != null) {
  80.             findAllQuery.setMaxResults(maxResult);
  81.         }
  82.         return findAllQuery.getResultList();
  83.     }
  84. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top