KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > demo > OrdersController


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 OrdersController {
15
16     private Orders orders;
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 destroyFromCustomer() {
33         Object JavaDoc id = orders.getCustomerId().getCustomerId();
34         destroy();
35         EntityManager em = getEntityManager();
36         getCustomerController().setCustomer(em.find(Customer.class, id));
37         em.close();
38         return "customer_detail";
39     }
40
41     private CustomerController getCustomerController() {
42         return (CustomerController) FacesContext.getCurrentInstance().
43             getExternalContext().getSessionMap().get("customer");
44     }
45
46     public String JavaDoc createFromCustomerSetup() {
47         this.orders = new Orders();
48         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("relatedId");
49         Integer JavaDoc id = new Integer JavaDoc(param);
50         EntityManager em = getEntityManager();
51         orders.setCustomerId(em.find(Customer.class, id));
52         em.close();
53         return "orders_create";
54     }
55
56     public String JavaDoc createFromCustomer() {
57         create();
58         getCustomerController().setCustomer(orders.getCustomerId());
59         return "customer_detail";
60     }
61
62     public String JavaDoc destroyFromProduct() {
63         Object JavaDoc id = orders.getProductId().getProductId();
64         destroy();
65         EntityManager em = getEntityManager();
66         getProductController().setProduct(em.find(Product.class, id));
67         em.close();
68         return "product_detail";
69     }
70
71     private ProductController getProductController() {
72         return (ProductController) FacesContext.getCurrentInstance().
73             getExternalContext().getSessionMap().get("product");
74     }
75
76     public String JavaDoc createFromProductSetup() {
77         this.orders = new Orders();
78         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("relatedId");
79         Integer JavaDoc id = new Integer JavaDoc(param);
80         EntityManager em = getEntityManager();
81         orders.setProductId(em.find(Product.class, id));
82         em.close();
83         return "orders_create";
84     }
85
86     public String JavaDoc createFromProduct() {
87         create();
88         getProductController().setProduct(orders.getProductId());
89         return "product_detail";
90     }
91
92     public String JavaDoc createSetup() {
93         this.orders = new Orders();
94         return "orders_create";
95     }
96
97     public String JavaDoc create() {
98         EntityManager em = getEntityManager();
99         try {
100             utx.begin();
101             em.persist(orders);
102             
103             //update property customerId of entity Customer
104
Customer customerId=orders.getCustomerId();
105             if (customerId != null) {
106                 customerId = em.merge(customerId);
107                 customerId.getOrders().add(orders);
108                 customerId=em.merge(customerId);
109             }
110             
111             
112             //update property productId of entity Product
113
Product productId=orders.getProductId();
114             if (productId != null) {
115                 productId = em.merge(productId);
116                 productId.getOrders().add(orders);
117                 productId=em.merge(productId);
118             }
119             
120             utx.commit();
121             addSuccessMessage("Orders 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 "orders_list";
132     }
133
134     public String JavaDoc detailSetup() {
135         setOrdersFromRequestParam();
136         return "orders_detail";
137     }
138
139     public String JavaDoc editSetup() {
140         setOrdersFromRequestParam();
141         return "orders_edit";
142     }
143
144     public String JavaDoc edit() {
145         EntityManager em = getEntityManager();
146         try {
147             utx.begin();
148             
149             Customer customerIdOld = em.find(Orders.class, orders.getOrderNum()).getCustomerId();
150             
151             Product productIdOld = em.find(Orders.class, orders.getOrderNum()).getProductId();
152             orders = em.merge(orders);
153             
154             //update property customerId of entity Customer
155
Customer customerIdNew=orders.getCustomerId();
156             if(customerIdNew != null) {
157                 customerIdNew.getOrders().add(orders);
158                 customerIdNew=em.merge(customerIdNew);
159             }
160             if(customerIdOld != null) {
161                 customerIdOld.getOrders().remove(orders);
162                 customerIdOld=em.merge(customerIdOld);
163             }
164             
165             //update property productId of entity Product
166
Product productIdNew=orders.getProductId();
167             if(productIdNew != null) {
168                 productIdNew.getOrders().add(orders);
169                 productIdNew=em.merge(productIdNew);
170             }
171             if(productIdOld != null) {
172                 productIdOld.getOrders().remove(orders);
173                 productIdOld=em.merge(productIdOld);
174             }
175             utx.commit();
176             addSuccessMessage("Orders 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 "orders_list";
187     }
188
189     public String JavaDoc destroy() {
190         EntityManager em = getEntityManager();
191         try {
192             utx.begin();
193             Orders orders = getOrdersFromRequestParam();
194             orders = em.merge(orders);
195             
196             //update property customerId of entity Customer
197
Customer customerId = orders.getCustomerId();
198             if (customerId != null) {
199                 customerId = em.merge(customerId);
200                 customerId.getOrders().remove(orders);
201                 customerId=em.merge(customerId);
202             }
203             
204             
205             //update property productId of entity Product
206
Product productId = orders.getProductId();
207             if (productId != null) {
208                 productId = em.merge(productId);
209                 productId.getOrders().remove(orders);
210                 productId=em.merge(productId);
211             }
212             
213             em.remove(orders);
214             utx.commit();
215             addSuccessMessage("Orders 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 "orders_list";
226     }
227
228     public Orders getOrdersFromRequestParam() {
229         EntityManager em = getEntityManager();
230         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("orderNum");
231         Integer JavaDoc id = new Integer JavaDoc(param);
232         Orders o = em.find(Orders.class, id);
233         em.close();
234         return o;
235     }
236
237     public void setOrdersFromRequestParam() {
238         Orders orders = getOrdersFromRequestParam();
239         setOrders(orders);
240     }
241
242     public Collection JavaDoc getOrderss() {
243         EntityManager em = getEntityManager();
244         Query q = em.createQuery("select object(o) from Orders 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 Orders getOrders() {
253         return orders;
254     }
255
256     public void setOrders(Orders orders) {
257         this.orders = orders;
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 Orders findOrders(Integer JavaDoc id) {
273         EntityManager em = getEntityManager();
274         Orders o = (Orders) em.find(Orders.class, id);
275         em.close();
276         return o;
277     }
278
279     public javax.faces.model.SelectItem[] getCustomerIds() {
280         EntityManager em = getEntityManager();
281         List JavaDoc <Customer> l = (List JavaDoc <Customer>) em.createQuery("select o from Customer as o").getResultList();
282         em.close();
283         SelectItem select[] = new SelectItem[l.size()];
284         int i = 0;
285         for(Customer x : l) {
286                 select[i++] = new SelectItem(x);
287             }
288             return select;
289     }
290
291     public javax.faces.model.SelectItem[] getProductIds() {
292         EntityManager em = getEntityManager();
293         List JavaDoc <Product> l = (List JavaDoc <Product>) em.createQuery("select o from Product as o").getResultList();
294         em.close();
295         SelectItem select[] = new SelectItem[l.size()];
296         int i = 0;
297         for(Product 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 Orders 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 "orders_list";
327     }
328
329     public String JavaDoc prev() {
330         firstItem -= batchSize;
331         if (firstItem < 0) {
332             firstItem = 0;
333         }
334         return "orders_list";
335     }
336 }
337
Popular Tags