~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to j2ee/samples/samples_src/JsfJpaCrud/src/java/demo/web/ProductCodeController.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2007, Sun Microsystems, Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 * 
 
7
 * * Redistributions of source code must retain the above copyright notice,
 
8
 *   this list of conditions and the following disclaimer.
 
9
 * 
 
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.
 
13
 *
 
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.
 
17
 * 
 
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.
 
29
 */
 
30
 
 
31
package demo.web;
 
32
 
 
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;
 
47
 
 
48
public class ProductCodeController {
 
49
    
 
50
    /** Creates a new instance of ProductCodeController */
 
51
    public ProductCodeController() {
 
52
    }
 
53
 
 
54
    private ProductCode productCode;
 
55
 
 
56
    private DataModel model;
 
57
 
 
58
    @Resource
 
59
    private UserTransaction utx;
 
60
 
 
61
    @PersistenceUnit(unitName = "JsfJpaCrudPU")
 
62
    private EntityManagerFactory emf;
 
63
 
 
64
    private EntityManager getEntityManager() {
 
65
        return emf.createEntityManager();
 
66
    }
 
67
 
 
68
    private int batchSize = 20;
 
69
 
 
70
    private int firstItem = 0;
 
71
 
 
72
    public ProductCode getProductCode() {
 
73
        return productCode;
 
74
    }
 
75
 
 
76
    public void setProductCode(ProductCode productCode) {
 
77
        this.productCode = productCode;
 
78
        getProductController().setDetailProducts(productCode.getProductCollection());
 
79
    }
 
80
 
 
81
    public DataModel getDetailProductCodes() {
 
82
        return model;
 
83
    }
 
84
 
 
85
    public void setDetailProductCodes(Collection<ProductCode> m) {
 
86
        model = new ListDataModel(new ArrayList(m));
 
87
    }
 
88
 
 
89
    public String destroyFromProduct() {
 
90
        String param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("relatedId");
 
91
        Integer id = new Integer(param);
 
92
        destroy();
 
93
        EntityManager em = getEntityManager();
 
94
        getProductController().setProduct(em.find(Product.class, id));
 
95
        em.close();
 
96
        return "product_detail";
 
97
    }
 
98
 
 
99
    private ProductController getProductController() {
 
100
        FacesContext context = FacesContext.getCurrentInstance();
 
101
        return (ProductController) context.getApplication().getELResolver().getValue(context.getELContext(), null, "product");
 
102
    }
 
103
 
 
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());
 
111
        }
 
112
        productCode.getProductCollection().add(em.find(Product.class, id));
 
113
        em.close();
 
114
        return "productCode_create";
 
115
    }
 
116
 
 
117
    public String createFromProduct() {
 
118
        create();
 
119
        getProductController().setProduct(productCode.getProductCollection().iterator().next());
 
120
        return "product_detail";
 
121
    }
 
122
 
 
123
    public String createSetup() {
 
124
        this.productCode = new ProductCode();
 
125
        return "productCode_create";
 
126
    }
 
127
 
 
128
    public String create() {
 
129
        EntityManager em = getEntityManager();
 
130
        try {
 
131
            utx.begin();
 
132
            em.joinTransaction();
 
133
            em.persist(productCode);
 
134
 
 
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);
 
140
                }
 
141
 
 
142
            utx.commit();
 
143
            addSuccessMessage("ProductCode was successfully created.");
 
144
        } catch (Exception ex) {
 
145
            try {
 
146
                addErrorMessage(ex.getLocalizedMessage());
 
147
                utx.rollback();
 
148
            } catch (Exception e) {
 
149
                addErrorMessage(e.getLocalizedMessage());
 
150
            }
 
151
        } finally {
 
152
            em.close();
 
153
        }
 
154
        return "productCode_list";
 
155
    }
 
156
 
 
157
    public String detailSetup() {
 
158
        setProductCodeFromRequestParam();
 
159
        return "productCode_detail";
 
160
    }
 
161
 
 
162
    public String editSetup() {
 
163
        setProductCodeFromRequestParam();
 
164
        return "productCode_edit";
 
165
    }
 
166
 
 
167
    public String edit() {
 
168
        EntityManager em = getEntityManager();
 
169
        try {
 
170
            utx.begin();
 
171
            em.joinTransaction();
 
172
            productCode = em.merge(productCode);
 
173
 
 
174
            Collection<Product> productCollectionsOld = em.find(ProductCode.class, productCode.getProdCode()).getProductCollection();
 
175
 
 
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);
 
181
                }
 
182
            for(Product productCollectionOld : productCollectionsOld) {
 
183
                    productCollectionOld.setProductCode(null);
 
184
                    productCollectionOld=em.merge(productCollectionOld);
 
185
                }
 
186
 
 
187
            utx.commit();
 
188
            addSuccessMessage("ProductCode was successfully updated.");
 
189
        } catch (Exception ex) {
 
190
            try {
 
191
                addErrorMessage(ex.getLocalizedMessage());
 
192
                utx.rollback();
 
193
            } catch (Exception e) {
 
194
                addErrorMessage(e.getLocalizedMessage());
 
195
            }
 
196
        } finally {
 
197
            em.close();
 
198
        }
 
199
        return "productCode_list";
 
200
    }
 
201
 
 
202
    public String destroy() {
 
203
        EntityManager em = getEntityManager();
 
204
        try {
 
205
            utx.begin();
 
206
            em.joinTransaction();
 
207
            ProductCode productCode = getProductCodeFromRequestParam();
 
208
            productCode = em.merge(productCode);
 
209
 
 
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);
 
216
                }
 
217
 
 
218
            em.remove(productCode);
 
219
            utx.commit();
 
220
            addSuccessMessage("ProductCode was successfully deleted.");
 
221
        } catch (Exception ex) {
 
222
            try {
 
223
                addErrorMessage(ex.getLocalizedMessage());
 
224
                utx.rollback();
 
225
            } catch (Exception e) {
 
226
                addErrorMessage(e.getLocalizedMessage());
 
227
            }
 
228
        } finally {
 
229
            em.close();
 
230
        }
 
231
        return "productCode_list";
 
232
    }
 
233
 
 
234
    public ProductCode getProductCodeFromRequestParam() {
 
235
        EntityManager em = getEntityManager();
 
236
        try{
 
237
            ProductCode o = null;
 
238
            if (model != null) {
 
239
                o = (ProductCode) model.getRowData();
 
240
                o = em.merge(o);
 
241
            } else {
 
242
                String param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("prodCode");
 
243
                o = em.find(ProductCode.class, param);
 
244
            }
 
245
            return o;
 
246
        } finally {
 
247
            em.close();
 
248
        }
 
249
    }
 
250
 
 
251
    public void setProductCodeFromRequestParam() {
 
252
        ProductCode productCode = getProductCodeFromRequestParam();
 
253
        setProductCode(productCode);
 
254
    }
 
255
 
 
256
    public DataModel getProductCodes() {
 
257
        EntityManager em = getEntityManager();
 
258
        try{
 
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());
 
263
            return model;
 
264
        } finally {
 
265
            em.close();
 
266
        }
 
267
    }
 
268
 
 
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);
 
273
    }
 
274
 
 
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);
 
279
    }
 
280
 
 
281
    public ProductCode findProductCode(String id) {
 
282
        EntityManager em = getEntityManager();
 
283
        try{
 
284
            ProductCode o = (ProductCode) em.find(ProductCode.class, id);
 
285
            return o;
 
286
        } finally {
 
287
            em.close();
 
288
        }
 
289
    }
 
290
 
 
291
    public int getItemCount() {
 
292
        EntityManager em = getEntityManager();
 
293
        try{
 
294
            int count = ((Long) em.createQuery("select count(o) from ProductCode as o").getSingleResult()).intValue();
 
295
            return count;
 
296
        } finally {
 
297
            em.close();
 
298
        }
 
299
    }
 
300
 
 
301
    public int getFirstItem() {
 
302
        return firstItem;
 
303
    }
 
304
 
 
305
    public int getLastItem() {
 
306
        int size = getItemCount();
 
307
        return firstItem + batchSize > size ? size : firstItem + batchSize;
 
308
    }
 
309
 
 
310
    public int getBatchSize() {
 
311
        return batchSize;
 
312
    }
 
313
 
 
314
    public String next() {
 
315
        if (firstItem + batchSize < getItemCount()) {
 
316
            firstItem += batchSize;
 
317
        }
 
318
        return "productCode_list";
 
319
    }
 
320
 
 
321
    public String prev() {
 
322
        firstItem -= batchSize;
 
323
        if (firstItem < 0) {
 
324
            firstItem = 0;
 
325
        }
 
326
        return "productCode_list";
 
327
    }
 
328
    
 
329
}