InventoryMgmtEJB.java

  1. package ejava.examples.ejbwar.inventory.ejb;

  2. import java.util.List;
  3. import java.util.UUID;

  4. import javax.ejb.Stateless;
  5. import javax.ejb.TransactionAttribute;
  6. import javax.ejb.TransactionAttributeType;
  7. import javax.inject.Inject;
  8. import javax.persistence.EntityManager;

  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;

  11. import ejava.examples.ejbwar.inventory.bo.Categories;
  12. import ejava.examples.ejbwar.inventory.bo.Category;
  13. import ejava.examples.ejbwar.inventory.bo.Product;
  14. import ejava.examples.ejbwar.inventory.bo.Products;
  15. import ejava.examples.ejbwar.inventory.cdi.Inventory;
  16. import ejava.examples.ejbwar.inventory.dao.InventoryDAO;

  17. /**
  18.  * This class implements the core transactional business logic for the
  19.  * inventory management. It is implemented as a no interface @Stateless
  20.  * session bean and has JAX-RS and RMI facades that deal with technology-
  21.  * specific communications with remote clients.
  22.  */
  23. @Stateless
  24. public class InventoryMgmtEJB {
  25.     private static final Logger logger = LoggerFactory.getLogger(InventoryMgmtEJB.class);
  26.    
  27.     @Inject
  28.     private InventoryDAO dao;
  29.     @Inject @Inventory
  30.     private EntityManager em; //used for demo
  31.    
  32.     /**
  33.      * Returns a list of categories that match the name provided
  34.      * @param name
  35.      * @return
  36.      */
  37.     @TransactionAttribute(TransactionAttributeType.SUPPORTS)
  38.     public Categories findCategoryByName(String name, int offset, int limit) {
  39.         List<Category> categories = dao.findCategoryByName("%" + name + "%", offset, limit);
  40.         for (Category category : categories) {
  41.             category.setProductCount(categories.size());
  42.             dao.detachCategory(category); //detach before manipulating collection
  43.             category.setProducts(null);
  44.         }
  45.         return new Categories(categories, 0, 0);
  46.     }
  47.    

  48.     @TransactionAttribute(TransactionAttributeType.REQUIRED)
  49.     public Category createOrGetCategory(String name) {
  50.         List<Category> categories = dao.findCategoryByName(name, 0, 1);
  51.         if (categories.size()==0) {
  52.             Category category=new Category(name);
  53.             dao.createCategory(category);
  54.             return category;
  55.         }
  56.         else {
  57.             return categories.get(0);
  58.         }
  59.     }

  60.     @TransactionAttribute(TransactionAttributeType.SUPPORTS)
  61.     public Category getCategory(int id) {
  62.         return dao.getCategory(id);
  63.     }

  64.     @TransactionAttribute(TransactionAttributeType.REQUIRED)
  65.     public void deleteCategory(int id) {
  66.         Category category = dao.getCategory(id);
  67.         if (category != null) {
  68.             dao.deleteCategory(category);
  69.         }
  70.     }


  71.     @TransactionAttribute(TransactionAttributeType.REQUIRED)
  72.     public Product addProduct(Product product, String categoryName) {
  73.         product.setProtectedValue(UUID.randomUUID().toString());
  74.         dao.addProduct(product);
  75.         Category category = createOrGetCategory(categoryName);
  76.         category.getProducts().add(product);
  77.         product = dao.getProduct(product.getId());
  78.         return product;
  79.     }
  80.    
  81.     @TransactionAttribute(TransactionAttributeType.REQUIRED)
  82.     public Product updateProduct(Product product) {
  83.         logger.info("updating existing product: {}", dao.getProduct(product.getId()));
  84.         logger.info("to these values: {}", product);
  85.         dao.updateProduct(product);
  86.         em.flush();
  87.         em.clear();
  88.         Product dbResult = dao.getProduct(product.getId());
  89.         logger.info("resulting product: {}", dbResult);
  90.         return dbResult;
  91.     }
  92.    
  93.     @TransactionAttribute(TransactionAttributeType.SUPPORTS)
  94.     public Products findProductByName(String name, int offset, int limit) {
  95.         return new Products(dao.findProductsByName("%" + name + "%", offset, limit), offset, limit);
  96.     }

  97.     @TransactionAttribute(TransactionAttributeType.SUPPORTS)
  98.     public Product getProduct(int id) {
  99.         return dao.getProduct(id);
  100.     }

  101.     @TransactionAttribute(TransactionAttributeType.REQUIRED)
  102.     public void deleteProduct(Product product) {
  103.         dao.deleteProduct(product);
  104.     }
  105. }