KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > demo > CustomerController


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 CustomerController {
15
16     private Customer customer;
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 destroyFromDiscountCode() {
33         Object JavaDoc id = customer.getDiscountCode().getDiscountCode();
34         destroy();
35         EntityManager em = getEntityManager();
36         getDiscountCodeController().setDiscountCode(em.find(DiscountCode.class, id));
37         em.close();
38         return "discountCode_detail";
39     }
40
41     private DiscountCodeController getDiscountCodeController() {
42         return (DiscountCodeController) FacesContext.getCurrentInstance().
43             getExternalContext().getSessionMap().get("discountCode");
44     }
45
46     public String JavaDoc createFromDiscountCodeSetup() {
47         this.customer = new Customer();
48         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("relatedId");
49         Integer JavaDoc id = new Integer JavaDoc(param);
50         EntityManager em = getEntityManager();
51         customer.setDiscountCode(em.find(DiscountCode.class, id));
52         em.close();
53         return "customer_create";
54     }
55
56     public String JavaDoc createFromDiscountCode() {
57         create();
58         getDiscountCodeController().setDiscountCode(customer.getDiscountCode());
59         return "discountCode_detail";
60     }
61
62     public String JavaDoc createSetup() {
63         this.customer = new Customer();
64         return "customer_create";
65     }
66
67     public String JavaDoc create() {
68         EntityManager em = getEntityManager();
69         try {
70             utx.begin();
71             em.persist(customer);
72             
73             //update property discountCode of entity DiscountCode
74
DiscountCode discountCode=customer.getDiscountCode();
75             if (discountCode != null) {
76                 discountCode = em.merge(discountCode);
77                 discountCode.getCustomer().add(customer);
78                 discountCode=em.merge(discountCode);
79             }
80             
81             utx.commit();
82             addSuccessMessage("Customer was successfully created.");
83         } catch (Exception JavaDoc ex) {
84             try {
85                 addErrorMessage(ex.getLocalizedMessage());
86                 utx.rollback();
87             } catch (Exception JavaDoc e) {
88                 addErrorMessage(e.getLocalizedMessage());
89             }
90         }
91         em.close();
92         return "customer_list";
93     }
94
95     public String JavaDoc detailSetup() {
96         setCustomerFromRequestParam();
97         return "customer_detail";
98     }
99
100     public String JavaDoc editSetup() {
101         setCustomerFromRequestParam();
102         return "customer_edit";
103     }
104
105     public String JavaDoc edit() {
106         EntityManager em = getEntityManager();
107         try {
108             utx.begin();
109             
110             DiscountCode discountCodeOld = em.find(Customer.class, customer.getCustomerId()).getDiscountCode();
111             customer = em.merge(customer);
112             
113             //update property discountCode of entity DiscountCode
114
DiscountCode discountCodeNew=customer.getDiscountCode();
115             if(discountCodeNew != null) {
116                 discountCodeNew.getCustomer().add(customer);
117                 discountCodeNew=em.merge(discountCodeNew);
118             }
119             if(discountCodeOld != null) {
120                 discountCodeOld.getCustomer().remove(customer);
121                 discountCodeOld=em.merge(discountCodeOld);
122             }
123             utx.commit();
124             addSuccessMessage("Customer was successfully updated.");
125         } catch (Exception JavaDoc ex) {
126             try {
127                 addErrorMessage(ex.getLocalizedMessage());
128                 utx.rollback();
129             } catch (Exception JavaDoc e) {
130                 addErrorMessage(e.getLocalizedMessage());
131             }
132         }
133         em.close();
134         return "customer_list";
135     }
136
137     public String JavaDoc destroy() {
138         EntityManager em = getEntityManager();
139         try {
140             utx.begin();
141             Customer customer = getCustomerFromRequestParam();
142             customer = em.merge(customer);
143             
144             //update property discountCode of entity DiscountCode
145
DiscountCode discountCode = customer.getDiscountCode();
146             if (discountCode != null) {
147                 discountCode = em.merge(discountCode);
148                 discountCode.getCustomer().remove(customer);
149                 discountCode=em.merge(discountCode);
150             }
151             
152             em.remove(customer);
153             utx.commit();
154             addSuccessMessage("Customer was successfully deleted.");
155         } catch (Exception JavaDoc ex) {
156             try {
157                 addErrorMessage(ex.getLocalizedMessage());
158                 utx.rollback();
159             } catch (Exception JavaDoc e) {
160                 addErrorMessage(e.getLocalizedMessage());
161             }
162         }
163         em.close();
164         return "customer_list";
165     }
166
167     public Customer getCustomerFromRequestParam() {
168         EntityManager em = getEntityManager();
169         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("customerId");
170         Integer JavaDoc id = new Integer JavaDoc(param);
171         Customer o = em.find(Customer.class, id);
172         em.close();
173         return o;
174     }
175
176     public void setCustomerFromRequestParam() {
177         Customer customer = getCustomerFromRequestParam();
178         setCustomer(customer);
179     }
180
181     public Collection JavaDoc getCustomers() {
182         EntityManager em = getEntityManager();
183         Query q = em.createQuery("select object(o) from Customer as o");
184         q.setMaxResults(batchSize);
185         q.setFirstResult(firstItem);
186         Collection JavaDoc c = q.getResultList();
187         em.close();
188         return c;
189     }
190
191     public Customer getCustomer() {
192         return customer;
193     }
194
195     public void setCustomer(Customer customer) {
196         this.customer = customer;
197     }
198
199     public static void addErrorMessage(String JavaDoc msg) {
200         FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
201         FacesContext fc = FacesContext.getCurrentInstance();
202         fc.addMessage(null, facesMsg);
203     }
204
205     public static void addSuccessMessage(String JavaDoc msg) {
206         FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
207         FacesContext fc = FacesContext.getCurrentInstance();
208         fc.addMessage("successInfo", facesMsg);
209     }
210
211     public Customer findCustomer(Integer JavaDoc id) {
212         EntityManager em = getEntityManager();
213         Customer o = (Customer) em.find(Customer.class, id);
214         em.close();
215         return o;
216     }
217
218     public javax.faces.model.SelectItem[] getDiscountCodes() {
219         EntityManager em = getEntityManager();
220         List JavaDoc <DiscountCode> l = (List JavaDoc <DiscountCode>) em.createQuery("select o from DiscountCode as o").getResultList();
221         em.close();
222         SelectItem select[] = new SelectItem[l.size()];
223         int i = 0;
224         for(DiscountCode x : l) {
225                 select[i++] = new SelectItem(x);
226             }
227             return select;
228     }
229
230     public int getItemCount() {
231         EntityManager em = getEntityManager();
232         int count = ((Long JavaDoc) em.createQuery("select count(o) from Customer as o").getSingleResult()).intValue();em.close();
233         return count;
234     }
235
236     public int getFirstItem() {
237         return firstItem;
238     }
239
240     public int getLastItem() {
241         int size = getItemCount();
242         return firstItem + batchSize > size ? size : firstItem + batchSize;
243     }
244
245     public int getBatchSize() {
246         return batchSize;
247     }
248
249     public String JavaDoc next() {
250         if (firstItem + batchSize < getItemCount()) {
251             firstItem += batchSize;
252         }
253         return "customer_list";
254     }
255
256     public String JavaDoc prev() {
257         firstItem -= batchSize;
258         if (firstItem < 0) {
259             firstItem = 0;
260         }
261         return "customer_list";
262     }
263 }
264
Popular Tags