View Javadoc
1   package ejava.examples.ejbwar.inventory.ejb;
2   
3   import java.util.List;
4   import java.util.UUID;
5   
6   import javax.ejb.Stateless;
7   import javax.ejb.TransactionAttribute;
8   import javax.ejb.TransactionAttributeType;
9   import javax.inject.Inject;
10  import javax.persistence.EntityManager;
11  
12  import org.slf4j.Logger;
13  import org.slf4j.LoggerFactory;
14  
15  import ejava.examples.ejbwar.inventory.bo.Categories;
16  import ejava.examples.ejbwar.inventory.bo.Category;
17  import ejava.examples.ejbwar.inventory.bo.Product;
18  import ejava.examples.ejbwar.inventory.bo.Products;
19  import ejava.examples.ejbwar.inventory.cdi.Inventory;
20  import ejava.examples.ejbwar.inventory.dao.InventoryDAO;
21  
22  /**
23   * This class implements the core transactional business logic for the 
24   * inventory management. It is implemented as a no interface @Stateless
25   * session bean and has JAX-RS and RMI facades that deal with technology-
26   * specific communications with remote clients.
27   */
28  @Stateless
29  public class InventoryMgmtEJB {
30      private static final Logger logger = LoggerFactory.getLogger(InventoryMgmtEJB.class);
31      
32  	@Inject
33  	private InventoryDAO dao;
34      @Inject @Inventory
35      private EntityManager em; //used for demo
36  	
37  	/**
38  	 * Returns a list of categories that match the name provided
39  	 * @param name
40  	 * @return
41  	 */
42  	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
43  	public Categories findCategoryByName(String name, int offset, int limit) {
44  		List<Category> categories = dao.findCategoryByName("%" + name + "%", offset, limit);
45  		for (Category category : categories) {
46  			category.setProductCount(categories.size());
47  			dao.detachCategory(category); //detach before manipulating collection 
48  			category.setProducts(null);
49  		}
50  		return new Categories(categories, 0, 0);
51  	}
52  	
53  
54  	@TransactionAttribute(TransactionAttributeType.REQUIRED)
55  	public Category createOrGetCategory(String name) {
56  		List<Category> categories = dao.findCategoryByName(name, 0, 1); 
57  		if (categories.size()==0) {
58  			Category category=new Category(name);
59  			dao.createCategory(category);
60  			return category;
61  		}
62  		else {
63  			return categories.get(0);
64  		}
65  	}
66  
67  	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
68  	public Category getCategory(int id) {
69  		return dao.getCategory(id);
70  	}
71  
72  	@TransactionAttribute(TransactionAttributeType.REQUIRED)
73  	public void deleteCategory(int id) {
74  		Category category = dao.getCategory(id);
75  		if (category != null) {
76  			dao.deleteCategory(category);
77  		}
78  	}
79  
80  
81  	@TransactionAttribute(TransactionAttributeType.REQUIRED)
82  	public Product addProduct(Product product, String categoryName) {
83  	    product.setProtectedValue(UUID.randomUUID().toString());
84  		dao.addProduct(product);
85  		Category category = createOrGetCategory(categoryName);
86  		category.getProducts().add(product);
87  		product = dao.getProduct(product.getId());
88  		return product;
89  	}
90  	
91  	@TransactionAttribute(TransactionAttributeType.REQUIRED)
92  	public Product updateProduct(Product product) {
93  	    logger.info("updating existing product: {}", dao.getProduct(product.getId()));
94          logger.info("to these values: {}", product);
95  		dao.updateProduct(product);
96  		em.flush();
97  		em.clear();
98  		Product dbResult = dao.getProduct(product.getId());
99  		logger.info("resulting product: {}", dbResult);
100 		return dbResult;
101 	}
102 	
103 	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
104 	public Products findProductByName(String name, int offset, int limit) {
105 		return new Products(dao.findProductsByName("%" + name + "%", offset, limit), offset, limit);
106 	}
107 
108 	@TransactionAttribute(TransactionAttributeType.SUPPORTS)
109 	public Product getProduct(int id) {
110 		return dao.getProduct(id);
111 	}
112 
113 	@TransactionAttribute(TransactionAttributeType.REQUIRED)
114 	public void deleteProduct(Product product) {
115 		dao.deleteProduct(product);
116 	}
117 }