KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > demo > ProductCodeController


1 package org.demo;
2 import java.util.Collection JavaDoc;
3 import javax.annotation.Resource;
4 import javax.faces.application.FacesMessage;
5 import javax.faces.context.FacesContext;
6 import javax.persistence.EntityManager;
7 import javax.persistence.EntityManagerFactory;
8 import javax.persistence.PersistenceUnit;
9 import javax.persistence.Query;
10 import javax.transaction.UserTransaction JavaDoc;
11
12 public class ProductCodeController {
13
14     private ProductCode productCode;
15
16     @Resource
17     private UserTransaction JavaDoc utx;
18
19     @PersistenceUnit(unitName = "SjsasJSFTestPU")
20     private EntityManagerFactory emf;
21
22     private EntityManager getEntityManager() {
23         return emf.createEntityManager();
24     }
25
26     private int batchSize = 20;
27
28     private int firstItem = 0;
29
30     public String JavaDoc createSetup() {
31         this.productCode = new ProductCode();
32         return "productCode_create";
33     }
34
35     public String JavaDoc create() {
36         EntityManager em = getEntityManager();
37         try {
38             utx.begin();
39             em.persist(productCode);
40             utx.commit();
41             addSuccessMessage("ProductCode was successfully created.");
42         } catch (Exception JavaDoc ex) {
43             try {
44                 addErrorMessage(ex.getLocalizedMessage());
45                 utx.rollback();
46             } catch (Exception JavaDoc e) {
47                 addErrorMessage(e.getLocalizedMessage());
48             }
49         }
50         em.close();
51         return "productCode_list";
52     }
53
54     public String JavaDoc detailSetup() {
55         setProductCodeFromRequestParam();
56         return "productCode_detail";
57     }
58
59     public String JavaDoc editSetup() {
60         setProductCodeFromRequestParam();
61         return "productCode_edit";
62     }
63
64     public String JavaDoc edit() {
65         EntityManager em = getEntityManager();
66         try {
67             utx.begin();
68             productCode = em.merge(productCode);
69             utx.commit();
70             addSuccessMessage("ProductCode was successfully updated.");
71         } catch (Exception JavaDoc ex) {
72             try {
73                 addErrorMessage(ex.getLocalizedMessage());
74                 utx.rollback();
75             } catch (Exception JavaDoc e) {
76                 addErrorMessage(e.getLocalizedMessage());
77             }
78         }
79         em.close();
80         return "productCode_list";
81     }
82
83     public String JavaDoc destroy() {
84         EntityManager em = getEntityManager();
85         try {
86             utx.begin();
87             ProductCode productCode = getProductCodeFromRequestParam();
88             productCode = em.merge(productCode);
89             em.remove(productCode);
90             utx.commit();
91             addSuccessMessage("ProductCode was successfully deleted.");
92         } catch (Exception JavaDoc ex) {
93             try {
94                 addErrorMessage(ex.getLocalizedMessage());
95                 utx.rollback();
96             } catch (Exception JavaDoc e) {
97                 addErrorMessage(e.getLocalizedMessage());
98             }
99         }
100         em.close();
101         return "productCode_list";
102     }
103
104     public ProductCode getProductCodeFromRequestParam() {
105         EntityManager em = getEntityManager();
106         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("prodCode");
107         String JavaDoc id = param;
108         ProductCode o = em.find(ProductCode.class, id);
109         em.close();
110         return o;
111     }
112
113     public void setProductCodeFromRequestParam() {
114         ProductCode productCode = getProductCodeFromRequestParam();
115         setProductCode(productCode);
116     }
117
118     public Collection JavaDoc getProductCodes() {
119         EntityManager em = getEntityManager();
120         Query q = em.createQuery("select object(o) from ProductCode as o");
121         q.setMaxResults(batchSize);
122         q.setFirstResult(firstItem);
123         Collection JavaDoc c = q.getResultList();
124         em.close();
125         return c;
126     }
127
128     public ProductCode getProductCode() {
129         return productCode;
130     }
131
132     public void setProductCode(ProductCode productCode) {
133         this.productCode = productCode;
134     }
135
136     public static void addErrorMessage(String JavaDoc msg) {
137         FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
138         FacesContext fc = FacesContext.getCurrentInstance();
139         fc.addMessage(null, facesMsg);
140     }
141
142     public static void addSuccessMessage(String JavaDoc msg) {
143         FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
144         FacesContext fc = FacesContext.getCurrentInstance();
145         fc.addMessage("successInfo", facesMsg);
146     }
147
148     public ProductCode findProductCode(String JavaDoc id) {
149         EntityManager em = getEntityManager();
150         ProductCode o = (ProductCode) em.find(ProductCode.class, id);
151         em.close();
152         return o;
153     }
154
155     public int getItemCount() {
156         EntityManager em = getEntityManager();
157         int count = ((Long JavaDoc) em.createQuery("select count(o) from ProductCode as o").getSingleResult()).intValue();em.close();
158         return count;
159     }
160
161     public int getFirstItem() {
162         return firstItem;
163     }
164
165     public int getLastItem() {
166         int size = getItemCount();
167         return firstItem + batchSize > size ? size : firstItem + batchSize;
168     }
169
170     public int getBatchSize() {
171         return batchSize;
172     }
173
174     public String JavaDoc next() {
175         if (firstItem + batchSize < getItemCount()) {
176             firstItem += batchSize;
177         }
178         return "productCode_list";
179     }
180
181     public String JavaDoc prev() {
182         firstItem -= batchSize;
183         if (firstItem < 0) {
184             firstItem = 0;
185         }
186         return "productCode_list";
187     }
188 }
189
Popular Tags