This project has retired. For details please refer to its Attic page.
JPAUtil xref
View Javadoc
1   /*
2    * Copyright 2001-2008 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  
18  package org.apache.juddi.util;
19  
20  import javax.persistence.EntityTransaction;
21  import javax.persistence.EntityManager;
22  
23  import javax.persistence.Query;
24  import java.util.List;
25  
26  import org.apache.juddi.config.PersistenceManager;
27  
28  
29  /**
30   * @author <a href="mailto:jfaath@apache.org">Jeff Faath</a>
31   * 
32   * Example use:
33  	public void test() {
34  		Object object = JPAUtil.getEntity(Tmodel.class, "uddi:juddi.apache.org:joepublisher:kEYGENERATOR");
35  		System.out.println("object=" + object);
36  	}
37   */
38  @Deprecated
39  public class JPAUtil {
40  	//REMOVE Comment from Code Review: This class does not seem to be in use. Do we need it?
41  	
42  	public static void persistEntity(Object uddiEntity, Object entityKey) {
43  		EntityManager em = PersistenceManager.getEntityManager();
44  		EntityTransaction tx = em.getTransaction();
45  		try {
46  			tx.begin();
47  	
48  			Object existingUddiEntity = em.find(uddiEntity.getClass(), entityKey);
49  			if (existingUddiEntity != null)
50  				em.remove(existingUddiEntity);
51  			
52  			em.persist(uddiEntity);
53  	
54  			tx.commit();
55  		} finally {
56  			if (tx.isActive()) {
57  				tx.rollback();
58  			}
59  			em.close();
60  		}
61  	}
62  	
63  	public static Object getEntity(Class<?> entityClass, Object entityKey) {
64  		EntityManager em = PersistenceManager.getEntityManager();
65  		EntityTransaction tx = em.getTransaction();
66  		try {
67  			tx.begin();
68  	
69  			Object obj = em.find(entityClass, entityKey);
70  			
71  			tx.commit();
72  			return obj;
73  		} finally {
74  			if (tx.isActive()) {
75  				tx.rollback();
76  			}
77  			em.close();
78  		}
79  	}
80  
81  	public static void deleteEntity(Class<?> entityClass, Object entityKey) {
82  		EntityManager em = PersistenceManager.getEntityManager();
83  		EntityTransaction tx = em.getTransaction();
84  		try {
85  			tx.begin();
86  	
87  			Object obj = em.find(entityClass, entityKey);
88  			em.remove(obj);
89  			
90  			tx.commit();
91  		} finally {
92  			if (tx.isActive()) {
93  				tx.rollback();
94  			}
95  			em.close();
96  		}
97  	}
98  	
99  	public static List<?> runQuery(String qry, int maxRows, int listHead) {
100 		EntityManager em = PersistenceManager.getEntityManager();
101 		EntityTransaction tx = em.getTransaction();
102 		try {
103 			tx.begin();
104 			Query q = em.createQuery(qry);
105 			q.setMaxResults(maxRows);
106 			q.setFirstResult(listHead);
107 			List<?> ret =  q.getResultList();
108 			tx.commit();
109 			return ret;
110 		} finally {
111 			if (tx.isActive()) {
112 				tx.rollback();
113 			}
114 			em.close();
115 		}
116 		
117 	}
118 	
119 	public static void runUpdateQuery(String qry) {
120 		EntityManager em = PersistenceManager.getEntityManager();
121 		EntityTransaction tx = em.getTransaction();
122 		try {
123 			tx.begin();
124 			
125 			Query q = em.createQuery(qry);
126 			q.executeUpdate();
127 	
128 			tx.commit();
129 		} finally {
130 			if (tx.isActive()) {
131 				tx.rollback();
132 			}
133 			em.close();
134 		}
135 	}
136 	
137 	public static void removeAuthTokens() {
138 		
139 		EntityManager em = PersistenceManager.getEntityManager();
140 		EntityTransaction tx = em.getTransaction();
141 		try {
142 			tx.begin();
143 	
144 			Query qry = em.createQuery("delete from AuthToken");
145 			qry.executeUpdate();
146 			
147 			tx.commit();
148 		} finally {
149 			if (tx.isActive()) {
150 				tx.rollback();
151 			}
152 			em.close();
153 		}
154 
155 	}
156 
157 }