KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > manager > EntityManagerProxy


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.manager;
31
32 import com.caucho.util.L10N;
33
34 import javax.persistence.EntityManager;
35 import javax.persistence.EntityTransaction;
36 import javax.persistence.FlushModeType;
37 import javax.persistence.LockModeType;
38 import javax.persistence.Query;
39 import java.util.logging.Logger JavaDoc;
40
41 /**
42  * The Entity manager
43  */

44 public class EntityManagerProxy implements EntityManager {
45   private static final L10N L = new L10N(EntityManagerProxy.class);
46   private static final Logger JavaDoc log
47     = Logger.getLogger(EntityManagerProxy.class.getName());
48
49   private AmberPersistenceUnit _persistenceUnit;
50
51   public EntityManagerProxy(AmberPersistenceUnit persistenceUnit)
52   {
53     _persistenceUnit = persistenceUnit;
54   }
55
56   /**
57    * Makes the instance managed.
58    */

59   public void persist(Object JavaDoc entity)
60   {
61     getCurrent().persist(entity);
62   }
63
64   /**
65    * Merges the state of the entity into the current context.
66    */

67   public <T> T merge(T entity)
68   {
69     return getCurrent().merge(entity);
70   }
71
72   /**
73    * Remove the instance.
74    */

75   public void remove(Object JavaDoc entity)
76   {
77     getCurrent().remove(entity);
78   }
79
80   /**
81    * Find by the primary key.
82    */

83   /*
84     public Object find(String entityName, Object primaryKey)
85     {
86     return getCurrent().find(entityName, primaryKey);
87     }
88   */

89
90   /**
91    * Find by the primary key.
92    */

93   public <T> T find(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey)
94   {
95     return getCurrent().find(entityClass, primaryKey);
96   }
97
98   /**
99    * Find by the primary key.
100    */

101   public <T> T getReference(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey)
102   {
103     return getCurrent().getReference(entityClass, primaryKey);
104   }
105
106   /**
107    * Returns the flush mode.
108    */

109   public FlushModeType getFlushMode()
110   {
111     return getCurrent().getFlushMode();
112   }
113
114   /**
115    * Returns the flush mode.
116    */

117   public void setFlushMode(FlushModeType mode)
118   {
119     getCurrent().setFlushMode(mode);
120   }
121
122   /**
123    * Locks the object.
124    */

125   public void lock(Object JavaDoc entity, LockModeType lockMode)
126   {
127     getCurrent().lock(entity, lockMode);
128   }
129
130   /**
131    * Clears the manager.
132    */

133   public void clear()
134   {
135     getCurrent().clear();
136   }
137
138   /**
139    * Synchronize with the database.
140    */

141   public void flush()
142   {
143     getCurrent().flush();
144   }
145
146   /**
147    * Joins the transaction.
148    */

149   public void joinTransaction()
150   {
151     throw new UnsupportedOperationException JavaDoc();
152   }
153
154   /**
155    * Gets the delegate.
156    */

157   public Object JavaDoc getDelegate()
158   {
159     throw new UnsupportedOperationException JavaDoc();
160   }
161
162   /**
163    * Clears the manager.
164    */

165   public void close()
166   {
167     throw new IllegalStateException JavaDoc(L.l("Container-manager persistence context may not be closed."));
168   }
169
170   /**
171    * Creates a query.
172    */

173   public Query createQuery(String JavaDoc sql)
174   {
175     return getCurrent().createQuery(sql);
176   }
177
178   /**
179    * Creates an instance of the named query
180    */

181   public Query createNamedQuery(String JavaDoc sql)
182   {
183     return getCurrent().createNamedQuery(sql);
184   }
185
186   /**
187    * Creates a query.
188    */

189   public Query createNativeQuery(String JavaDoc sql)
190   {
191     return getCurrent().createNativeQuery(sql);
192   }
193
194   /**
195    * Creates a query.
196    */

197   public Query createNativeQuery(String JavaDoc sql, String JavaDoc map)
198   {
199     return getCurrent().createNativeQuery(sql, map);
200   }
201
202   /**
203    * Creates a query.
204    */

205   public Query createNativeQuery(String JavaDoc sql, Class JavaDoc retType)
206   {
207     return getCurrent().createNativeQuery(sql, retType);
208   }
209
210   /**
211    * Refresh the state of the instance from the database.
212    */

213   public void refresh(Object JavaDoc entity)
214   {
215     getCurrent().refresh(entity);
216   }
217
218   /**
219    * Returns true if the entity belongs to the current context.
220    */

221   public boolean contains(Object JavaDoc entity)
222   {
223     return getCurrent().contains(entity);
224   }
225
226   /**
227    * Returns the entity manager transaction.
228    */

229   public EntityTransaction getTransaction()
230   {
231     return getCurrent().getTransaction();
232   }
233
234   /**
235    * Returns true if open.
236    */

237   public boolean isOpen()
238   {
239     return true;
240   }
241
242   /**
243    * Returns the current entity manager.
244    */

245   private AmberConnection getCurrent()
246   {
247     return _persistenceUnit.getThreadConnection();
248   }
249
250   public String JavaDoc toString()
251   {
252     AmberConnection aConn = getCurrent();
253
254     if (aConn != null)
255       return "EntityManagerProxy[" + aConn.getPersistenceUnit().getName() + "]";
256     else
257       return "EntityManagerProxy[closed]";
258   }
259 }
260
Popular Tags