2
* Copyright (c) 2007, Sun Microsystems, Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* * Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* * Redistributions in binary form must reproduce the above copyright notice,
11
* this list of conditions and the following disclaimer in the documentation
12
* and/or other materials provided with the distribution.
14
* * Neither the name of Sun Microsystems, Inc. nor the names of its contributors
15
* may be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28
* THE POSSIBILITY OF SUCH DAMAGE.
33
import demo.model.Product;
34
import demo.model.ProductCode;
35
import java.util.ArrayList;
36
import java.util.Collection;
37
import javax.annotation.Resource;
38
import javax.faces.application.FacesMessage;
39
import javax.faces.context.FacesContext;
40
import javax.faces.model.DataModel;
41
import javax.faces.model.ListDataModel;
42
import javax.persistence.EntityManager;
43
import javax.persistence.EntityManagerFactory;
44
import javax.persistence.PersistenceUnit;
45
import javax.persistence.Query;
46
import javax.transaction.UserTransaction;
48
public class ProductCodeController {
50
/** Creates a new instance of ProductCodeController */
51
public ProductCodeController() {
54
private ProductCode productCode;
56
private DataModel model;
59
private UserTransaction utx;
61
@PersistenceUnit(unitName = "JsfJpaCrudPU")
62
private EntityManagerFactory emf;
64
private EntityManager getEntityManager() {
65
return emf.createEntityManager();
68
private int batchSize = 20;
70
private int firstItem = 0;
72
public ProductCode getProductCode() {
76
public void setProductCode(ProductCode productCode) {
77
this.productCode = productCode;
78
getProductController().setDetailProducts(productCode.getProductCollection());
81
public DataModel getDetailProductCodes() {
85
public void setDetailProductCodes(Collection<ProductCode> m) {
86
model = new ListDataModel(new ArrayList(m));
89
public String destroyFromProduct() {
90
String param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("relatedId");
91
Integer id = new Integer(param);
93
EntityManager em = getEntityManager();
94
getProductController().setProduct(em.find(Product.class, id));
96
return "product_detail";
99
private ProductController getProductController() {
100
FacesContext context = FacesContext.getCurrentInstance();
101
return (ProductController) context.getApplication().getELResolver().getValue(context.getELContext(), null, "product");
104
public String createFromProductSetup() {
105
this.productCode = new ProductCode();
106
String param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("relatedId");
107
Integer id = new Integer(param);
108
EntityManager em = getEntityManager();
109
if (productCode.getProductCollection() == null) {
110
productCode.setProductCollection(new ArrayList());
112
productCode.getProductCollection().add(em.find(Product.class, id));
114
return "productCode_create";
117
public String createFromProduct() {
119
getProductController().setProduct(productCode.getProductCollection().iterator().next());
120
return "product_detail";
123
public String createSetup() {
124
this.productCode = new ProductCode();
125
return "productCode_create";
128
public String create() {
129
EntityManager em = getEntityManager();
132
em.joinTransaction();
133
em.persist(productCode);
135
//update property productCollection of entity Product
136
for(Product productCollection : productCode.getProductCollection()){
137
productCollection = em.merge(productCollection);
138
productCollection.setProductCode(productCode);
139
productCollection=em.merge(productCollection);
143
addSuccessMessage("ProductCode was successfully created.");
144
} catch (Exception ex) {
146
addErrorMessage(ex.getLocalizedMessage());
148
} catch (Exception e) {
149
addErrorMessage(e.getLocalizedMessage());
154
return "productCode_list";
157
public String detailSetup() {
158
setProductCodeFromRequestParam();
159
return "productCode_detail";
162
public String editSetup() {
163
setProductCodeFromRequestParam();
164
return "productCode_edit";
167
public String edit() {
168
EntityManager em = getEntityManager();
171
em.joinTransaction();
172
productCode = em.merge(productCode);
174
Collection<Product> productCollectionsOld = em.find(ProductCode.class, productCode.getProdCode()).getProductCollection();
176
//update property productCollection of entity Product
177
Collection <Product> productCollectionsNew = productCode.getProductCollection();
178
for(Product productCollectionNew : productCollectionsNew) {
179
productCollectionNew.setProductCode(productCode);
180
productCollectionNew=em.merge(productCollectionNew);
182
for(Product productCollectionOld : productCollectionsOld) {
183
productCollectionOld.setProductCode(null);
184
productCollectionOld=em.merge(productCollectionOld);
188
addSuccessMessage("ProductCode was successfully updated.");
189
} catch (Exception ex) {
191
addErrorMessage(ex.getLocalizedMessage());
193
} catch (Exception e) {
194
addErrorMessage(e.getLocalizedMessage());
199
return "productCode_list";
202
public String destroy() {
203
EntityManager em = getEntityManager();
206
em.joinTransaction();
207
ProductCode productCode = getProductCodeFromRequestParam();
208
productCode = em.merge(productCode);
210
//update property productCollection of entity Product
211
Collection<Product> productCollections = productCode.getProductCollection();
212
for(Product productCollection : productCollections) {
213
productCollection = em.merge(productCollection);
214
productCollection.setProductCode(null);
215
productCollection=em.merge(productCollection);
218
em.remove(productCode);
220
addSuccessMessage("ProductCode was successfully deleted.");
221
} catch (Exception ex) {
223
addErrorMessage(ex.getLocalizedMessage());
225
} catch (Exception e) {
226
addErrorMessage(e.getLocalizedMessage());
231
return "productCode_list";
234
public ProductCode getProductCodeFromRequestParam() {
235
EntityManager em = getEntityManager();
237
ProductCode o = null;
239
o = (ProductCode) model.getRowData();
242
String param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("prodCode");
243
o = em.find(ProductCode.class, param);
251
public void setProductCodeFromRequestParam() {
252
ProductCode productCode = getProductCodeFromRequestParam();
253
setProductCode(productCode);
256
public DataModel getProductCodes() {
257
EntityManager em = getEntityManager();
259
Query q = em.createQuery("select object(o) from ProductCode as o");
260
q.setMaxResults(batchSize);
261
q.setFirstResult(firstItem);
262
model = new ListDataModel(q.getResultList());
269
public static void addErrorMessage(String msg) {
270
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
271
FacesContext fc = FacesContext.getCurrentInstance();
272
fc.addMessage(null, facesMsg);
275
public static void addSuccessMessage(String msg) {
276
FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
277
FacesContext fc = FacesContext.getCurrentInstance();
278
fc.addMessage("successInfo", facesMsg);
281
public ProductCode findProductCode(String id) {
282
EntityManager em = getEntityManager();
284
ProductCode o = (ProductCode) em.find(ProductCode.class, id);
291
public int getItemCount() {
292
EntityManager em = getEntityManager();
294
int count = ((Long) em.createQuery("select count(o) from ProductCode as o").getSingleResult()).intValue();
301
public int getFirstItem() {
305
public int getLastItem() {
306
int size = getItemCount();
307
return firstItem + batchSize > size ? size : firstItem + batchSize;
310
public int getBatchSize() {
314
public String next() {
315
if (firstItem + batchSize < getItemCount()) {
316
firstItem += batchSize;
318
return "productCode_list";
321
public String prev() {
322
firstItem -= batchSize;
326
return "productCode_list";