1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
package com.yaps.petstore.stateless.catalog;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.yaps.petstore.entity.catalog.Category;
import com.yaps.petstore.entity.catalog.Item;
import com.yaps.petstore.entity.catalog.Product;
import com.yaps.petstore.exception.ValidationException;
@Stateless(name="Catalogsbctg",mappedName="ejb/stateless/catalog")
public class CatalogBean implements CatalogLocal, CatalogRemote {
@PersistenceContext(unitName="petstorePU")
private EntityManager em;
public Category findCategory(Long categoryId) {
if (categoryId==null)
throw new ValidationException("categoryId is null");
Query query=em.createQuery("select c from Category c where id=:id");
query.setParameter("id",categoryId);
Category category=null;
category=(Category)query.getSingleResult();
return category;
}
public Item findItem(Long itemId) {
if (itemId==null)
throw new ValidationException("itemId est null");
Item result=null;
result=em.find(Item.class, itemId);
return result;
}
public Product findProduct(Long productId) {
if (productId==null)
throw new ValidationException("productId est null");
Product result=null;
result=em.find(Product.class, productId);
return result;
}
@SuppressWarnings("unchecked")
public List<Item> searchItems(String keyword) {
Query query;
List<Item> items;
query=em.createQuery("select i from Item i where" +
"upper (i.name) like :keyword or" +
"upper (i.product.name) like :keyword" +
"order by i.product.category.name, i.product.name");
query.setParameter("keyword", "%"+keyword.toUpperCase()+"%");
items=query.getResultList();
return items;
}
public Category createCategory(Category category) {
if (category==null)
throw new ValidationException("category est null");
em.persist(category);
return category;
}
public Item createItem(Item item, Product product) {
if (item==null || product==null)
throw new ValidationException("item ou product est null");
item.setProduct(product);
em.persist(item);
return item;
}
public Product createProduct(Product product, Category category) {
if (product==null || category==null)
throw new ValidationException("category ou product est null");
product.setCategory(category);
em.persist(product);
return product;
}
public void deleteCategory(Category category) {
if (category==null)
throw new ValidationException("category est null");
em.remove(em.merge(category));
}
public void deleteItem(Item item, Product product) {
if (item==null || product==null)
throw new ValidationException("item est null");
item.setProduct(product);
em.remove(em.merge(item));
}
public void deleteProduct(Product product) {
if (product==null)
throw new ValidationException("product est null");
em.remove(em.merge(product));
}
@SuppressWarnings("unchecked")
public List<Category> findCategories() {
Query query=em.createQuery("select c from Category c");
return query.getResultList();
}
@SuppressWarnings("unchecked")
public List<Item> findItems() {
Query query=em.createQuery("select i from Item i");
return query.getResultList();
}
@SuppressWarnings("unchecked")
public List<Product> findProducts() {
Query query=em.createQuery("select c from Product c");
return query.getResultList();
}
public Category updateCategory(Category category) {
if (category==null)
throw new ValidationException("category est null");
em.merge(category);
return category;
}
public Item updateItem(Item item, Product product) {
if (item==null || product==null )
throw new ValidationException("item ou product est null");
item.setProduct(product);
em.merge(item);
return item;
}
public Product updateProduct(Product product, Category category) {
if (product==null || category==null )
throw new ValidationException("category ou product est null");
product.setCategory(category);
em.merge(product);
return product;
}
} |
Partager