KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > demo > ManufactureController


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 ManufactureController {
13
14     private Manufacture manufacture;
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.manufacture = new Manufacture();
32         return "manufacture_create";
33     }
34
35     public String JavaDoc create() {
36         EntityManager em = getEntityManager();
37         try {
38             utx.begin();
39             em.persist(manufacture);
40             utx.commit();
41             addSuccessMessage("Manufacture 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 "manufacture_list";
52     }
53
54     public String JavaDoc detailSetup() {
55         setManufactureFromRequestParam();
56         return "manufacture_detail";
57     }
58
59     public String JavaDoc editSetup() {
60         setManufactureFromRequestParam();
61         return "manufacture_edit";
62     }
63
64     public String JavaDoc edit() {
65         EntityManager em = getEntityManager();
66         try {
67             utx.begin();
68             manufacture = em.merge(manufacture);
69             utx.commit();
70             addSuccessMessage("Manufacture 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 "manufacture_list";
81     }
82
83     public String JavaDoc destroy() {
84         EntityManager em = getEntityManager();
85         try {
86             utx.begin();
87             Manufacture manufacture = getManufactureFromRequestParam();
88             manufacture = em.merge(manufacture);
89             em.remove(manufacture);
90             utx.commit();
91             addSuccessMessage("Manufacture 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 "manufacture_list";
102     }
103
104     public Manufacture getManufactureFromRequestParam() {
105         EntityManager em = getEntityManager();
106         String JavaDoc param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("manufactureId");
107         Integer JavaDoc id = new Integer JavaDoc(param);
108         Manufacture o = em.find(Manufacture.class, id);
109         em.close();
110         return o;
111     }
112
113     public void setManufactureFromRequestParam() {
114         Manufacture manufacture = getManufactureFromRequestParam();
115         setManufacture(manufacture);
116     }
117
118     public Collection JavaDoc getManufactures() {
119         EntityManager em = getEntityManager();
120         Query q = em.createQuery("select object(o) from Manufacture 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 Manufacture getManufacture() {
129         return manufacture;
130     }
131
132     public void setManufacture(Manufacture manufacture) {
133         this.manufacture = manufacture;
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 Manufacture findManufacture(Integer JavaDoc id) {
149         EntityManager em = getEntityManager();
150         Manufacture o = (Manufacture) em.find(Manufacture.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 Manufacture 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 "manufacture_list";
179     }
180
181     public String JavaDoc prev() {
182         firstItem -= batchSize;
183         if (firstItem < 0) {
184             firstItem = 0;
185         }
186         return "manufacture_list";
187     }
188 }
189
Popular Tags