KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > demo > ProductController


1 package org.demo;
2 import java.util.Collection JavaDoc;
3 import java.util.List JavaDoc;
4 import javax.annotation.Resource;
5 import javax.faces.application.FacesMessage;
6 import javax.faces.context.FacesContext;
7 import javax.faces.model.SelectItem;
8 import javax.persistence.EntityManager;
9 import javax.persistence.EntityManagerFactory;
10 import javax.persistence.PersistenceUnit;
11 import javax.persistence.Query;
12 import javax.transaction.UserTransaction JavaDoc;
13
14 public class ProductController {
15
16     private Product product;
17
18     @Resource
19     private UserTransaction JavaDoc utx;
20
21     @PersistenceUnit(unitName = "SjsasJSFTestPU")
22     private EntityManagerFactory emf;
23
24     private EntityManager getEntityManager() {
25         return emf.createEntityManager();
26     }
27
28     private int batchSize = 20;
29
30     private int firstItem = 0;
31
32     public String JavaDoc destroyFromManufacture() {
33         Object JavaDoc id = product.getManufactureId().getManufactureId();
34         destroy();
35         EntityManager em = getEntityManager();
36         getManufactureController().setManufacture(em.find(Manufacture.class, id));
37         em.close();
38         return "manufacture_detail";
39     }
40
41     private ManufactureController getManufactureController() {
42         return (ManufactureController) FacesContext.getCurrentInstance().
43             getExternalContext().getSessionMap().get("manufacture");
44     }
45
46     public String JavaDoc createFromManufactureSetup() {
47         this.product = new Product();
48         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("relatedId");
49         Integer JavaDoc id = new Integer JavaDoc(param);
50         EntityManager em = getEntityManager();
51         product.setManufactureId(em.find(Manufacture.class, id));
52         em.close();
53         return "product_create";
54     }
55
56     public String JavaDoc createFromManufacture() {
57         create();
58         getManufactureController().setManufacture(product.getManufactureId());
59         return "manufacture_detail";
60     }
61
62     public String JavaDoc destroyFromProductCode() {
63         Object JavaDoc id = product.getProductCode().getProdCode();
64         destroy();
65         EntityManager em = getEntityManager();
66         getProductCodeController().setProductCode(em.find(ProductCode.class, id));
67         em.close();
68         return "productCode_detail";
69     }
70
71     private ProductCodeController getProductCodeController() {
72         return (ProductCodeController) FacesContext.getCurrentInstance().
73             getExternalContext().getSessionMap().get("productCode");
74     }
75
76     public String JavaDoc createFromProductCodeSetup() {
77         this.product = new Product();
78         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("relatedId");
79         Integer JavaDoc id = new Integer JavaDoc(param);
80         EntityManager em = getEntityManager();
81         product.setProductCode(em.find(ProductCode.class, id));
82         em.close();
83         return "product_create";
84     }
85
86     public String JavaDoc createFromProductCode() {
87         create();
88         getProductCodeController().setProductCode(product.getProductCode());
89         return "productCode_detail";
90     }
91
92     public String JavaDoc createSetup() {
93         this.product = new Product();
94         return "product_create";
95     }
96
97     public String JavaDoc create() {
98         EntityManager em = getEntityManager();
99         try {
100             utx.begin();
101             em.persist(product);
102             
103             //update property manufactureId of entity Manufacture
104
Manufacture manufactureId=product.getManufactureId();
105             if (manufactureId != null) {
106                 manufactureId = em.merge(manufactureId);
107                 manufactureId.getProduct().add(product);
108                 manufactureId=em.merge(manufactureId);
109             }
110             
111             
112             //update property productCode of entity ProductCode
113
ProductCode productCode=product.getProductCode();
114             if (productCode != null) {
115                 productCode = em.merge(productCode);
116                 productCode.getProduct().add(product);
117                 productCode=em.merge(productCode);
118             }
119             
120             utx.commit();
121             addSuccessMessage("Product was successfully created.");
122         } catch (Exception JavaDoc ex) {
123             try {
124                 addErrorMessage(ex.getLocalizedMessage());
125                 utx.rollback();
126             } catch (Exception JavaDoc e) {
127                 addErrorMessage(e.getLocalizedMessage());
128             }
129         }
130         em.close();
131         return "product_list";
132     }
133
134     public String JavaDoc detailSetup() {
135         setProductFromRequestParam();
136         return "product_detail";
137     }
138
139     public String JavaDoc editSetup() {
140         setProductFromRequestParam();
141         return "product_edit";
142     }
143
144     public String JavaDoc edit() {
145         EntityManager em = getEntityManager();
146         try {
147             utx.begin();
148             
149             Manufacture manufactureIdOld = em.find(Product.class, product.getProductId()).getManufactureId();
150             
151             ProductCode productCodeOld = em.find(Product.class, product.getProductId()).getProductCode();
152             product = em.merge(product);
153             
154             //update property manufactureId of entity Manufacture
155
Manufacture manufactureIdNew=product.getManufactureId();
156             if(manufactureIdNew != null) {
157                 manufactureIdNew.getProduct().add(product);
158                 manufactureIdNew=em.merge(manufactureIdNew);
159             }
160             if(manufactureIdOld != null) {
161                 manufactureIdOld.getProduct().remove(product);
162                 manufactureIdOld=em.merge(manufactureIdOld);
163             }
164             
165             //update property productCode of entity ProductCode
166
ProductCode productCodeNew=product.getProductCode();
167             if(productCodeNew != null) {
168                 productCodeNew.getProduct().add(product);
169                 productCodeNew=em.merge(productCodeNew);
170             }
171             if(productCodeOld != null) {
172                 productCodeOld.getProduct().remove(product);
173                 productCodeOld=em.merge(productCodeOld);
174             }
175             utx.commit();
176             addSuccessMessage("Product was successfully updated.");
177         } catch (Exception JavaDoc ex) {
178             try {
179                 addErrorMessage(ex.getLocalizedMessage());
180                 utx.rollback();
181             } catch (Exception JavaDoc e) {
182                 addErrorMessage(e.getLocalizedMessage());
183             }
184         }
185         em.close();
186         return "product_list";
187     }
188
189     public String JavaDoc destroy() {
190         EntityManager em = getEntityManager();
191         try {
192             utx.begin();
193             Product product = getProductFromRequestParam();
194             product = em.merge(product);
195             
196             //update property manufactureId of entity Manufacture
197
Manufacture manufactureId = product.getManufactureId();
198             if (manufactureId != null) {
199                 manufactureId = em.merge(manufactureId);
200                 manufactureId.getProduct().remove(product);
201                 manufactureId=em.merge(manufactureId);
202             }
203             
204             
205             //update property productCode of entity ProductCode
206
ProductCode productCode = product.getProductCode();
207             if (productCode != null) {
208                 productCode = em.merge(productCode);
209                 productCode.getProduct().remove(product);
210                 productCode=em.merge(productCode);
211             }
212             
213             em.remove(product);
214             utx.commit();
215             addSuccessMessage("Product was successfully deleted.");
216         } catch (Exception JavaDoc ex) {
217             try {
218                 addErrorMessage(ex.getLocalizedMessage());
219                 utx.rollback();
220             } catch (Exception JavaDoc e) {
221                 addErrorMessage(e.getLocalizedMessage());
222             }
223         }
224         em.close();
225         return "product_list";
226     }
227
228     public Product getProductFromRequestParam() {
229         EntityManager em = getEntityManager();
230         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("productId");
231         Integer JavaDoc id = new Integer JavaDoc(param);
232         Product o = em.find(Product.class, id);
233         em.close();
234         return o;
235     }
236
237     public void setProductFromRequestParam() {
238         Product product = getProductFromRequestParam();
239         setProduct(product);
240     }
241
242     public Collection JavaDoc getProducts() {
243         EntityManager em = getEntityManager();
244         Query q = em.createQuery("select object(o) from Product as o");
245         q.setMaxResults(batchSize);
246         q.setFirstResult(firstItem);
247         Collection JavaDoc c = q.getResultList();
248         em.close();
249         return c;
250     }
251
252     public Product getProduct() {
253         return product;
254     }
255
256     public void setProduct(Product product) {
257         this.product = product;
258     }
259
260     public static void addErrorMessage(String JavaDoc msg) {
261         FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
262         FacesContext fc = FacesContext.getCurrentInstance();
263         fc.addMessage(null, facesMsg);
264     }
265
266     public static void addSuccessMessage(String JavaDoc msg) {
267         FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
268         FacesContext fc = FacesContext.getCurrentInstance();
269         fc.addMessage("successInfo", facesMsg);
270     }
271
272     public Product findProduct(Integer JavaDoc id) {
273         EntityManager em = getEntityManager();
274         Product o = (Product) em.find(Product.class, id);
275         em.close();
276         return o;
277     }
278
279     public javax.faces.model.SelectItem[] getManufactureIds() {
280         EntityManager em = getEntityManager();
281         List JavaDoc <Manufacture> l = (List JavaDoc <Manufacture>) em.createQuery("select o from Manufacture as o").getResultList();
282         em.close();
283         SelectItem select[] = new SelectItem[l.size()];
284         int i = 0;
285         for(Manufacture x : l) {
286                 select[i++] = new SelectItem(x);
287             }
288             return select;
289     }
290
291     public javax.faces.model.SelectItem[] getProductCodes() {
292         EntityManager em = getEntityManager();
293         List JavaDoc <ProductCode> l = (List JavaDoc <ProductCode>) em.createQuery("select o from ProductCode as o").getResultList();
294         em.close();
295         SelectItem select[] = new SelectItem[l.size()];
296         int i = 0;
297         for(ProductCode x : l) {
298                 select[i++] = new SelectItem(x);
299             }
300             return select;
301     }
302
303     public int getItemCount() {
304         EntityManager em = getEntityManager();
305         int count = ((Long JavaDoc) em.createQuery("select count(o) from Product as o").getSingleResult()).intValue();em.close();
306         return count;
307     }
308
309     public int getFirstItem() {
310         return firstItem;
311     }
312
313     public int getLastItem() {
314         int size = getItemCount();
315         return firstItem + batchSize > size ? size : firstItem + batchSize;
316     }
317
318     public int getBatchSize() {
319         return batchSize;
320     }
321
322     public String JavaDoc next() {
323         if (firstItem + batchSize < getItemCount()) {
324             firstItem += batchSize;
325         }
326         return "product_list";
327     }
328
329     public String JavaDoc prev() {
330         firstItem -= batchSize;
331         if (firstItem < 0) {
332             firstItem = 0;
333         }
334         return "product_list";
335     }
336 }
337
Popular Tags