KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > web > MyAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.exceptions.web;
20
21 import java.util.Collection JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30 import javax.persistence.EntityTransaction;
31 import javax.persistence.Persistence;
32 import javax.persistence.Query;
33 import org.apache.struts.action.Action;
34
35 /**
36  *
37  * @author Jan Horvath
38  */

39 public class MyAction extends Action{
40
41     EntityManagerFactory emf = Persistence.createEntityManagerFactory("StrutsExceptionsPU",new java.util.HashMap JavaDoc());
42     
43     protected List JavaDoc getAll(Class JavaDoc entity) {
44         List JavaDoc result = null;
45         EntityManager em = getEM();
46         try {
47             result = (List JavaDoc) em.createQuery("SELECT e FROM " + entity.getSimpleName() + " e").getResultList();
48         } catch(Exception JavaDoc e) {
49             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e);
50             throw new RuntimeException JavaDoc(e);
51         } finally {
52             em.close();
53         }
54         return result;
55     }
56     
57     protected Collection JavaDoc getDistinct(Class JavaDoc entity, String JavaDoc column) {
58         List JavaDoc result = null;
59         EntityManager em = getEM();
60         try {
61             Query q = em.createNativeQuery("SELECT DISTINCT x." + column + " FROM " + entity.getSimpleName() + " x");
62             result = q.getResultList();
63             
64         } catch(Exception JavaDoc e) {
65             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e);
66             throw new RuntimeException JavaDoc(e);
67         } finally {
68             em.close();
69         }
70         return result;
71     }
72     
73     private StringBuffer JavaDoc getQueryFromParam(Hashtable JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> params) {
74         StringBuffer JavaDoc query = new StringBuffer JavaDoc();
75         for(Enumeration JavaDoc<String JavaDoc> en = params.keys(); en.hasMoreElements(); ) {
76             String JavaDoc key = en.nextElement();
77             List JavaDoc l = params.get(key);
78             StringBuffer JavaDoc sub = new StringBuffer JavaDoc();
79             for(Iterator JavaDoc<String JavaDoc> it = l.iterator(); it.hasNext(); ){
80                 String JavaDoc val = it.next();
81                 sub.append("x." + key + " = '" + val + "'");
82                 if (it.hasNext()) {
83                     sub.append(" OR ");
84                 }
85             }
86             if (sub.length() > 0) query.append(" (" + sub + ") ");
87             if (en.hasMoreElements()) query.append(" AND ");
88         }
89         return query;
90     }
91     
92     protected Collection JavaDoc find(Class JavaDoc entity, Hashtable JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> params) {
93         List JavaDoc result = null;
94         EntityManager em = getEM();
95         try {
96             StringBuffer JavaDoc query = getQueryFromParam(params);
97             StringBuffer JavaDoc finalQuery = new StringBuffer JavaDoc("SELECT x FROM " + entity.getSimpleName() + " x");
98             if (query.length() > 0) {
99                 finalQuery.append(" WHERE ");
100                 finalQuery.append(query);
101             }
102             Query q = em.createQuery(finalQuery.toString());
103             result = q.getResultList();
104         } catch(Exception JavaDoc e) {
105             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e);
106             throw new RuntimeException JavaDoc(e);
107         } finally {
108             em.close();
109         }
110         return result;
111     }
112     
113     protected Collection JavaDoc findUnmapped() {
114         List JavaDoc result = null;
115         EntityManager em = getEM();
116         try {
117             Query q = em.createQuery("SELECT i FROM Issue i WHERE i.issuezillaid = 0");
118             result = q.getResultList();
119         } catch(Exception JavaDoc e) {
120             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e);
121             throw new RuntimeException JavaDoc(e);
122         } finally {
123             em.close();
124         }
125         return result;
126     }
127     
128     protected String JavaDoc count(Class JavaDoc entity, Hashtable JavaDoc<String JavaDoc, List JavaDoc<String JavaDoc>> params) {
129         String JavaDoc result = null;
130         EntityManager em = getEM();
131         try {
132             StringBuffer JavaDoc query = getQueryFromParam(params);
133             StringBuffer JavaDoc finalQuery = new StringBuffer JavaDoc("SELECT x FROM " + entity.getSimpleName() + " x");
134             if (query.length() > 0) {
135                 finalQuery.append(" WHERE ");
136                 finalQuery.append(query);
137             }
138             System.err.println(finalQuery.toString());
139             Query q = em.createQuery(finalQuery.toString());
140             result = "" + q.getResultList().size();
141             
142         } catch(Exception JavaDoc e) {
143             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e);
144             throw new RuntimeException JavaDoc(e);
145         } finally {
146             em.close();
147         }
148         return result;
149     }
150     
151     protected Object JavaDoc getEntity(Class JavaDoc entity, Object JavaDoc key) {
152         Object JavaDoc result = null;
153         EntityManager em = getEM();
154         try {
155             result = em.find(entity, key);
156         } catch(Exception JavaDoc e) {
157             Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e);
158             throw new RuntimeException JavaDoc(e);
159         } finally {
160             em.close();
161         }
162         return result;
163     }
164     
165     private EntityManager getEM() {
166         EntityManager em = emf.createEntityManager();
167         return em;
168     }
169     
170     public void persist(Object JavaDoc object) {
171         EntityManager em = getEM();
172         try {
173             em.getTransaction().begin();
174             em.persist(object);
175             em.getTransaction().commit();
176         }
177         catch (Exception JavaDoc e) {
178             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,
179                                                                          "exception caught", e);
180             EntityTransaction transaction = em.getTransaction();
181             if (transaction.isActive()) transaction.rollback();
182         }
183         finally {
184             em.close();
185         }
186     }
187     
188     public void merge(Object JavaDoc object) {
189         EntityManager em = getEM();
190         try {
191             em.getTransaction().begin();
192             em.merge(object);
193             em.getTransaction().commit();
194         }
195         catch (Exception JavaDoc e) {
196             java.util.logging.Logger.getLogger(getClass().getName()).log(java.util.logging.Level.SEVERE,
197                                                                          "exception caught", e);
198             EntityTransaction transaction = em.getTransaction();
199             if (transaction.isActive()) transaction.rollback();
200         }
201         finally {
202             em.close();
203         }
204     }
205
206 }
207
Popular Tags