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