KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > ResourceLocalEntityManager


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.jpa;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.EntityNotFoundException;
24 import javax.persistence.EntityTransaction;
25 import javax.persistence.FlushModeType;
26 import javax.persistence.LockModeType;
27 import javax.persistence.PersistenceException;
28 import javax.persistence.Query;
29
30 import org.apache.cayenne.CayenneRuntimeException;
31 import org.apache.cayenne.DataChannel;
32 import org.apache.cayenne.DataObjectUtils;
33 import org.apache.cayenne.ObjectContext;
34 import org.apache.cayenne.PersistenceState;
35 import org.apache.cayenne.Persistent;
36 import org.apache.cayenne.access.Transaction;
37 import org.apache.cayenne.query.EJBQLQuery;
38
39 public class ResourceLocalEntityManager implements EntityManager, CayenneEntityManager {
40
41     protected EntityTransaction transaction;
42     protected ResourceLocalEntityManagerFactory factory;
43     protected FlushModeType flushMode;
44     protected boolean open;
45     protected ObjectContext context;
46
47     public ResourceLocalEntityManager(ObjectContext context,
48             ResourceLocalEntityManagerFactory factory) {
49
50         if (factory == null) {
51             throw new IllegalArgumentException JavaDoc("Null entity manager factory");
52         }
53
54         this.open = true;
55         this.context = context;
56         this.factory = factory;
57     }
58
59     /**
60      * Returns a DataChannel of the peer ObjectContext.
61      */

62     public DataChannel getChannel() {
63         return context.getChannel();
64     }
65
66     /**
67      * Returns parent EntityManagerFactory.
68      */

69     protected ResourceLocalEntityManagerFactory getFactory() {
70         return factory;
71     }
72
73     /**
74      * Close an application-managed EntityManager. After an EntityManager has been closed,
75      * all methods on the EntityManager instance will throw the IllegalStateException
76      * except for isOpen, which will return false. This method can only be called when the
77      * EntityManager is not associated with an active transaction.
78      *
79      * @throws IllegalStateException if the EntityManager is associated with an active
80      * transaction or if the EntityManager is container-managed.
81      */

82     public void close() {
83         checkClosed();
84
85         if (transaction != null && transaction.isActive()) {
86             throw new IllegalStateException JavaDoc("Active transaction in progress");
87         }
88
89         open = false;
90     }
91
92     public boolean isOpen() {
93         return open && (factory == null || factory.isOpen());
94     }
95
96     public Object JavaDoc getDelegate() {
97         return factory.getProvider();
98     }
99
100     /**
101      * Make an instance managed and persistent.
102      *
103      * @param entity an object to be made persistent
104      * @throws IllegalArgumentException if not an entity.
105      */

106     public void persist(Object JavaDoc entity) {
107         checkClosed();
108         context.registerNewObject(entity);
109     }
110
111     /**
112      * Merge the state of the given entity into the current persistence context. Cayenne:
113      * Is this like localObject(s)?
114      *
115      * @param entity
116      * @return the instance that the state was merged to
117      * @throws IllegalArgumentException if instance is not an entity or is a removed
118      * entity
119      */

120     public <T> T merge(T entity) {
121         checkClosed();
122         checkNotRemoved(entity);
123         Persistent persistent = (Persistent) entity;
124         return (T) context.localObject(persistent.getObjectId(), persistent);
125     }
126
127     /**
128      * Remove the entity instance.
129      *
130      * @param entity
131      * @throws IllegalArgumentException if not an entity or if a detached entity.
132      */

133     public void remove(Object JavaDoc entity) {
134         checkClosed();
135         checkAttached(entity);
136         context.deleteObject((Persistent) entity);
137     }
138
139     /**
140      * Find by primary key.
141      *
142      * @param entityClass
143      * @param primaryKey
144      * @return the found entity instance or null if the entity does not exist
145      * @throws IllegalArgumentException if the first argument does not denote an entity
146      * type or the second argument is not a valid type for that
147      */

148     public <T> T find(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey) {
149         checkClosed();
150         return (T) DataObjectUtils.objectForPK(context, entityClass, primaryKey);
151     }
152
153     /**
154      * Get an instance, whose state may be lazily fetched. If the requested instance does
155      * not exist in the database, throws EntityNotFoundException when the instance state
156      * is first accessed. (The persistence provider runtime is permitted to throw the
157      * EntityNotFoundException when getReference is called.) The application should not
158      * expect that the instance state will be available upon detachment, unless it was
159      * accessed by the application while the entity manager was open.
160      *
161      * @param entityClass
162      * @param primaryKey
163      * @return the found entity instance
164      * @throws IllegalArgumentException if the first argument does not denote an entity
165      * type or the second argument is not a valid type for that entity’s
166      * primary key
167      * @throws EntityNotFoundException if the entity state cannot be accessed
168      */

169     public <T> T getReference(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey) {
170         checkClosed();
171
172         // TODO: force refresh?
173
T ref = find(entityClass, primaryKey);
174
175         if (ref == null) {
176             throw new EntityNotFoundException("Could not find "
177                     + entityClass.toString()
178                     + " with primary key value "
179                     + primaryKey.toString());
180         }
181
182         return ref;
183     }
184
185     /**
186      * Synchronize the persistence context to the underlying database.
187      *
188      * @throws PersistenceException if the flush fails
189      */

190     public void flush() {
191         checkClosed();
192
193         try {
194             context.commitChanges();
195         }
196         catch (CayenneRuntimeException e) {
197             throw new PersistenceException(e);
198         }
199     }
200
201     /**
202      * Set the flush mode that applies to all objects contained in the persistence
203      * context.
204      *
205      * @param flushMode
206      */

207     public void setFlushMode(FlushModeType flushMode) {
208         checkClosed();
209
210         this.flushMode = flushMode;
211     }
212
213     /**
214      * Get the flush mode that applies to all objects contained in the persistence
215      * context.
216      *
217      * @return flushMode
218      */

219     public FlushModeType getFlushMode() {
220         checkClosed();
221
222         return flushMode;
223     }
224
225     /**
226      * Refresh the state of the instance from the database, overwriting changes made to
227      * the entity, if any.
228      *
229      * @param entity
230      * @throws IllegalArgumentException if not an entity or entity is not managed
231      * @throws EntityNotFoundException if the entity no longer exists in the database
232      */

233     public void refresh(Object JavaDoc entity) {
234         checkClosed();
235         // TODO: Andrus, 2/10/2006 - implement
236
throw new UnsupportedOperationException JavaDoc("TODO");
237     }
238
239     /**
240      * Clear the persistence context, causing all managed entities to become detached.
241      * Changes made to entities that have not been flushed to the database will not be
242      * persisted.
243      */

244     public void clear() {
245         checkClosed();
246         // TODO: Andrus, 2/10/2006 - implement
247
throw new UnsupportedOperationException JavaDoc("TODO");
248     }
249
250     /**
251      * Check if the instance belongs to the current persistence context.
252      *
253      * @throws IllegalArgumentException if not an entity
254      */

255     public boolean contains(Object JavaDoc entity) {
256         checkClosed();
257         Persistent p = (Persistent) entity;
258         return context.getGraphManager().getNode(p.getObjectId()) == p;
259     }
260
261     /**
262      * Create an instance of Query for executing an EJB QL statement.
263      *
264      * @param ejbqlString an EJB QL query string
265      * @return the new query instance
266      * @throws IllegalArgumentException if query string is not valid
267      */

268     public Query createQuery(String JavaDoc ejbqlString) {
269         checkClosed();
270
271         JpaQuery query = new JpaQuery(context);
272         query.setQuery(new EJBQLQuery(ejbqlString));
273         return query;
274     }
275
276     /**
277      * Create an instance of Query for executing a named query (in EJB QL or native SQL).
278      *
279      * @param name the name of a query defined in metadata
280      * @return the new query instance
281      * @throws IllegalArgumentException if a query has not been defined with the given
282      * name
283      */

284     public Query createNamedQuery(String JavaDoc name) {
285         checkClosed();
286         return new JpaQuery(context, name);
287     }
288
289     public Query createNativeQuery(String JavaDoc sqlString, Class JavaDoc resultClass) {
290         checkClosed();
291         return new JpaNativeQuery(context, sqlString, resultClass);
292     }
293
294     /**
295      * Create an instance of Query for executing a native SQL statement, e.g., for update
296      * or delete.
297      *
298      * @param sqlString a native SQL query string
299      * @return the new query instance
300      */

301     public Query createNativeQuery(String JavaDoc sqlString) {
302         checkClosed();
303
304         return new JpaNativeQuery(context, sqlString, factory
305                 .getPersistenceUnitInfo()
306                 .getPersistenceUnitName());
307     }
308
309     /**
310      * Create an instance of Query for executing a native SQL query.
311      *
312      * @param sqlString a native SQL query string
313      * @param resultSetMapping the name of the result set mapping
314      * @return the new query instance
315      */

316     public Query createNativeQuery(String JavaDoc sqlString, String JavaDoc resultSetMapping) {
317         checkClosed();
318
319         // TODO: Andrus, 2/10/2006 - implement
320
throw new UnsupportedOperationException JavaDoc("TODO");
321     }
322
323     /**
324      * Indicates to the EntityManager that a JTA transaction is active. This method should
325      * be called on a JTA application managed EntityManager that was created outside the
326      * scope of the active transaction to associate it with the current JTA transaction.
327      * <p>
328      * This implementation throws a JpaProviderException, as it only supports
329      * resource-local operation.
330      *
331      * @throws JpaProviderException as this impementation only supports resource-local
332      * operation.
333      */

334     public void joinTransaction() {
335         throw new JpaProviderException(
336                 "'joinTransaction' is called on a RESOURCE_LOCAL EntityManager");
337     }
338
339     public void lock(Object JavaDoc entity, LockModeType lockMode) {
340         // TODO: andrus, 8/15/2006 - noop
341
}
342
343     /**
344      * Return the resource-level transaction object. The EntityTransaction instance may be
345      * used serially to begin and commit multiple transactions.
346      *
347      * @return EntityTransaction instance
348      */

349     public EntityTransaction getTransaction() { // note - allowed to be called on a closed
350
if (transaction == null) {
351             this.transaction = new JpaTransaction(
352                     Transaction.internalTransaction(null),
353                     this);
354         }
355
356         return transaction;
357     }
358
359     /**
360      * Checks if an entity is attached to the current EntityManager, throwing
361      * IllegalArgumentException if not.
362      */

363     protected void checkAttached(Object JavaDoc entity) throws IllegalArgumentException JavaDoc {
364         Persistent p = (Persistent) entity;
365         if (p.getPersistenceState() == PersistenceState.TRANSIENT
366                 || p.getObjectContext() == null) {
367             throw new IllegalArgumentException JavaDoc("entity is detached: " + entity);
368         }
369     }
370
371     /**
372      * Checks if an entity is not removed in the current EntityManager, throwing
373      * IllegalArgumentException if it is.
374      */

375     protected void checkNotRemoved(Object JavaDoc entity) throws IllegalArgumentException JavaDoc {
376         Persistent p = (Persistent) entity;
377         if (p.getPersistenceState() == PersistenceState.DELETED) {
378             throw new IllegalArgumentException JavaDoc("entity is removed: " + entity);
379         }
380     }
381
382     /**
383      * Throws an exception if called on closed factory.
384      */

385     protected void checkClosed() throws IllegalStateException JavaDoc {
386         if (!isOpen()) {
387             throw new IllegalStateException JavaDoc(
388                     "An attempt to access closed EntityManagerFactory.");
389         }
390     }
391 }
392
Popular Tags