1 19 package org.netbeans.modules.exceptions.web; 20 21 import java.util.Collection ; 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.logging.Level ; 27 import java.util.logging.Logger ; 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 39 public class MyAction extends Action{ 40 41 EntityManagerFactory emf = Persistence.createEntityManagerFactory("StrutsExceptionsPU",new java.util.HashMap ()); 42 43 protected List getAll(Class entity) { 44 List result = null; 45 EntityManager em = getEM(); 46 try { 47 result = (List ) em.createQuery("SELECT e FROM " + entity.getSimpleName() + " e").getResultList(); 48 } catch(Exception e) { 49 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e); 50 throw new RuntimeException (e); 51 } finally { 52 em.close(); 53 } 54 return result; 55 } 56 57 protected Collection getDistinct(Class entity, String column) { 58 List 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 e) { 65 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e); 66 throw new RuntimeException (e); 67 } finally { 68 em.close(); 69 } 70 return result; 71 } 72 73 private StringBuffer getQueryFromParam(Hashtable <String , List <String >> params) { 74 StringBuffer query = new StringBuffer (); 75 for(Enumeration <String > en = params.keys(); en.hasMoreElements(); ) { 76 String key = en.nextElement(); 77 List l = params.get(key); 78 StringBuffer sub = new StringBuffer (); 79 for(Iterator <String > it = l.iterator(); it.hasNext(); ){ 80 String 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 find(Class entity, Hashtable <String , List <String >> params) { 93 List result = null; 94 EntityManager em = getEM(); 95 try { 96 StringBuffer query = getQueryFromParam(params); 97 StringBuffer finalQuery = new StringBuffer ("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 e) { 105 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e); 106 throw new RuntimeException (e); 107 } finally { 108 em.close(); 109 } 110 return result; 111 } 112 113 protected Collection findUnmapped() { 114 List 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 e) { 120 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e); 121 throw new RuntimeException (e); 122 } finally { 123 em.close(); 124 } 125 return result; 126 } 127 128 protected String count(Class entity, Hashtable <String , List <String >> params) { 129 String result = null; 130 EntityManager em = getEM(); 131 try { 132 StringBuffer query = getQueryFromParam(params); 133 StringBuffer finalQuery = new StringBuffer ("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 e) { 143 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e); 144 throw new RuntimeException (e); 145 } finally { 146 em.close(); 147 } 148 return result; 149 } 150 151 protected Object getEntity(Class entity, Object key) { 152 Object result = null; 153 EntityManager em = getEM(); 154 try { 155 result = em.find(entity, key); 156 } catch(Exception e) { 157 Logger.getLogger(getClass().getName()).log(Level.SEVERE,"exception caught", e); 158 throw new RuntimeException (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 object) { 171 EntityManager em = getEM(); 172 try { 173 em.getTransaction().begin(); 174 em.persist(object); 175 em.getTransaction().commit(); 176 } 177 catch (Exception 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 object) { 189 EntityManager em = getEM(); 190 try { 191 em.getTransaction().begin(); 192 em.merge(object); 193 em.getTransaction().commit(); 194 } 195 catch (Exception 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 |