001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * // Copyright (c) 1998, 2007, Oracle. All rights reserved.
005: *
006: *
007: * The contents of this file are subject to the terms of either the GNU
008: * General Public License Version 2 only ("GPL") or the Common Development
009: * and Distribution License("CDDL") (collectively, the "License"). You
010: * may not use this file except in compliance with the License. You can obtain
011: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
012: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
013: * language governing permissions and limitations under the License.
014: *
015: * When distributing the software, include this License Header Notice in each
016: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
017: * Sun designates this particular file as subject to the "Classpath" exception
018: * as provided by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the License
020: * Header, with the fields enclosed by brackets [] replaced by your own
021: * identifying information: "Portions Copyrighted [year]
022: * [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * If you wish your version of this file to be governed by only the CDDL or
027: * only the GPL Version 2, indicate your decision by adding "[Contributor]
028: * elects to include this software in this distribution under the [CDDL or GPL
029: * Version 2] license." If you don't indicate a single choice of license, a
030: * recipient has the option to distribute your version of this file under
031: * either the CDDL, the GPL Version 2 or to extend the choice of license to
032: * its licensees as provided above. However, if you add GPL Version 2 code
033: * and therefore, elected the GPL Version 2 license, then the option applies
034: * only if the new code is made subject to such option by the copyright
035: * holder.
036: */
037: package oracle.toplink.essentials.internal.ejb.cmp3;
038:
039: import java.util.Map;
040:
041: import javax.persistence.FlushModeType;
042: import javax.persistence.Query;
043:
044: import oracle.toplink.essentials.exceptions.EJBQLException;
045: import oracle.toplink.essentials.expressions.Expression;
046: import oracle.toplink.essentials.queryframework.DatabaseQuery;
047: import oracle.toplink.essentials.queryframework.ResultSetMappingQuery;
048: import oracle.toplink.essentials.threetier.ServerSession;
049: import oracle.toplink.essentials.internal.ejb.cmp3.transaction.*;
050: import oracle.toplink.essentials.internal.localization.ExceptionLocalization;
051:
052: /**
053: * <p>
054: * <b>Purpose</b>: Contains the implementation of the EntityManager.
055: * <p>
056: * <b>Description</b>: This class provides the implementation for the combined TopLink
057: * and EJB3.0 EntityManager class.
058: * <p>
059: * <b>Responsibilities</b>:It is responcible for tracking transaction state and the
060: * objects within that transaction.
061: * @see javax.persistence.EntityManager
062: * @see oracle.toplink.essentials.ejb.cmp3.EntityManager
063: */
064:
065: /* @author gyorke
066: * @since TopLink 10.1.3 EJB 3.0 Preview
067: */
068:
069: public class EntityManagerImpl
070: extends
071: oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl
072: implements oracle.toplink.essentials.ejb.cmp3.EntityManager {
073:
074: private FlushModeType flushMode;
075:
076: /**
077: * Constructor returns an EntityManager assigned to the a particular ServerSession.
078: * @param sessionName the ServerSession name that should be used.
079: * This constructor can potentially throw TopLink exceptions regarding the existence, or
080: * errors with the specified session.
081: */
082: public EntityManagerImpl(String sessionName,
083: boolean propagatePersistenceContext, boolean extended) {
084: super (sessionName, propagatePersistenceContext, extended);
085: flushMode = FlushModeType.AUTO;
086: }
087:
088: /**
089: * Constructor called from the EntityManagerFactory to create an EntityManager
090: * @param serverSession the serverSession assigned to this deployment.
091: */
092: public EntityManagerImpl(ServerSession serverSession,
093: boolean propagatePersistenceContext, boolean extended) {
094: this (serverSession, null, propagatePersistenceContext, extended);
095: }
096:
097: /**
098: * Constructor called from the EntityManagerFactory to create an EntityManager
099: * @param serverSession the serverSession assigned to this deployment.
100: * Note: The properties argument is provided to allow properties to be passed into this EntityManager,
101: * but there are currently no such properties implemented
102: */
103: public EntityManagerImpl(ServerSession serverSession,
104: Map properties, boolean propagePersistenceContext,
105: boolean extended) {
106: super (serverSession, properties, propagePersistenceContext,
107: extended);
108: flushMode = FlushModeType.AUTO;
109: }
110:
111: /**
112: * Constructor called from the EntityManagerFactory to create an EntityManager
113: * @param factory the EntityMangerFactoryImpl that created this entity manager.
114: * Note: The properties argument is provided to allow properties to be passed into this EntityManager,
115: * but there are currently no such properties implemented
116: */
117: public EntityManagerImpl(EntityManagerFactoryImpl factory,
118: Map properties, boolean propagePersistenceContext,
119: boolean extended) {
120: super (factory, properties, propagePersistenceContext, extended);
121: flushMode = FlushModeType.AUTO;
122: }
123:
124: /**
125: * Merge the state of the given entity into the
126: * current persistence context, using the unqualified
127: * class name as the entity name.
128: * @param entity
129: * @return the instance that the state was merged to
130: */
131: public <T> T merge(T entity) {
132: try {
133: verifyOpen();
134: return (T) mergeInternal(entity);
135: } catch (RuntimeException e) {
136: this .transaction.setRollbackOnlyInternal();
137: throw e;
138: }
139: }
140:
141: /**
142: * Find by primary key.
143: * @param entityClass
144: * @param primaryKey
145: * @return the found entity instance
146: * or null if the entity does not exist
147: * @throws IllegalArgumentException if the first argument does
148: * not denote an entity type or the second argument is not a valid type for that
149: * entity's primary key
150: */
151: public <T> T find(Class<T> entityClass, Object primaryKey) {
152: try {
153: verifyOpen();
154: return (T) findInternal(entityClass, primaryKey);
155: } catch (RuntimeException e) {
156: this .transaction.setRollbackOnlyInternal();
157: throw e;
158: }
159: }
160:
161: /**
162: * Get an instance, whose state may be lazily fetched.
163: * If the requested instance does not exist in the database,
164: * throws EntityNotFoundException when the instance state is first accessed.
165: * (The container is permitted to throw EntityNotFoundException when get is called.)
166: * The application should not expect that the instance state will
167: * be available upon detachment, unless it was accessed by the
168: * application while the entity manager was open.
169: * @param entityClass
170: * @param primaryKey
171: * @return the found entity instance
172: * @throws IllegalArgumentException if the first argument does
173: * not denote an entity type or the second
174: * argument is not a valid type for that
175: * entity's primary key
176: * @throws EntityNotFoundException if the entity state
177: * cannot be accessed
178: */
179: public <T> T getReference(Class<T> entityClass, Object primaryKey) {
180: try {
181: verifyOpen();
182: Object returnValue = findInternal(entityClass, primaryKey);
183: if (returnValue == null) {
184: Object[] o = { primaryKey };
185: String message = ExceptionLocalization.buildMessage(
186: "no_entities_retrieved_for_get_reference", o);
187: throw new javax.persistence.EntityNotFoundException(
188: message);
189: }
190: return (T) returnValue;
191: } catch (RuntimeException e) {
192: this .transaction.setRollbackOnlyInternal();
193: throw e;
194: }
195: }
196:
197: /**
198: * Create an instance of Query for executing an
199: * EJBQL query.
200: * @param ejbqlString an EJBQL query string
201: * @return the new query instance
202: */
203: public Query createQuery(String ejbqlString) {
204:
205: try {
206: verifyOpen();
207:
208: EJBQueryImpl ejbqImpl;
209:
210: try {
211: ejbqImpl = new EJBQueryImpl(ejbqlString, this );
212: }
213:
214: catch (EJBQLException ex) {
215: throw new IllegalArgumentException(
216: ExceptionLocalization
217: .buildMessage("wrap_ejbql_exception"),
218: ex);
219: }
220:
221: return ejbqImpl;
222: } catch (RuntimeException e) {
223: this .transaction.setRollbackOnlyInternal();
224: throw e;
225: }
226: }
227:
228: /**
229: * Create an instance of Query for executing a
230: * named query (in EJBQL or native SQL).
231: * @param name the name of a query defined in metadata
232: * @return the new query instance
233: */
234: public Query createNamedQuery(String name) {
235: try {
236: verifyOpen();
237: return new EJBQueryImpl(name, this , true);
238: } catch (RuntimeException e) {
239: this .transaction.setRollbackOnlyInternal();
240: throw e;
241: }
242: }
243:
244: /**
245: * Create an instance of Query for executing
246: * a native SQL query.
247: * @param sqlString a native SQL query string
248: * @return the new query instance
249: */
250: public Query createNativeQuery(String sqlString) {
251: try {
252: verifyOpen();
253: return new EJBQueryImpl(EJBQueryImpl.buildSQLDatabaseQuery(
254: sqlString, false), this );
255: } catch (RuntimeException e) {
256: this .transaction.setRollbackOnlyInternal();
257: throw e;
258: }
259: }
260:
261: /**
262: * This method is used to create a query using SQL. The class, must be the expected
263: * return type.
264: */
265: public Query createNativeQuery(String sqlString, Class resultType) {
266: try {
267: verifyOpen();
268: DatabaseQuery query = createNativeQueryInternal(sqlString,
269: resultType);
270: return new EJBQueryImpl(query, this );
271: } catch (RuntimeException e) {
272: this .transaction.setRollbackOnlyInternal();
273: throw e;
274: }
275: }
276:
277: /**
278: * Create an instance of Query for executing
279: * a native SQL query.
280: * @param sqlString a native SQL query string
281: * @param resultSetMapping the name of the result set mapping
282: * @return the new query instance
283: * @throws IllegalArgumentException if query string is not valid
284: */
285: public Query createNativeQuery(String sqlString,
286: String resultSetMapping) {
287: try {
288: verifyOpen();
289: ResultSetMappingQuery query = new ResultSetMappingQuery();
290: query.setSQLResultSetMappingName(resultSetMapping);
291: query.setSQLString(sqlString);
292: query.setIsUserDefined(true);
293: return new EJBQueryImpl(query, this );
294: } catch (RuntimeException e) {
295: this .transaction.setRollbackOnlyInternal();
296: throw e;
297: }
298: }
299:
300: /**
301: * Get the flush mode that applies to all objects contained
302: * in the persistence context.
303: * @return flushMode
304: */
305: public FlushModeType getFlushMode() {
306: try {
307: verifyOpen();
308: return flushMode;
309: } catch (RuntimeException e) {
310: this .transaction.setRollbackOnlyInternal();
311: throw e;
312: }
313: }
314:
315: /**
316: * Set the flush mode that applies to all objects contained
317: * in the persistence context.
318: * @param flushMode
319: */
320: public void setFlushMode(FlushModeType flushMode) {
321: try {
322: verifyOpen();
323: this .flushMode = flushMode;
324: } catch (RuntimeException e) {
325: this .transaction.setRollbackOnlyInternal();
326: throw e;
327: }
328: }
329:
330: /**
331: * This method is used to create a query using a Toplink Expression and the return type.
332: */
333: public javax.persistence.Query createQuery(Expression expression,
334: Class resultType) {
335: try {
336: verifyOpen();
337: DatabaseQuery query = createQueryInternal(expression,
338: resultType);
339: return new EJBQueryImpl(query, this );
340: } catch (RuntimeException e) {
341: this .transaction.setRollbackOnlyInternal();
342: throw e;
343: }
344: }
345:
346: /**
347: * Returns the resource-level transaction object.
348: * The EntityTransaction instance may be used serially to
349: * begin and commit multiple transactions.
350: * @return EntityTransaction instance
351: * @throws IllegalStateException if invoked on a JTA
352: * EntityManager.
353: */
354: public javax.persistence.EntityTransaction getTransaction() {
355: try {
356: return ((TransactionWrapper) transaction).getTransaction();
357: } catch (RuntimeException e) {
358: this .transaction.setRollbackOnlyInternal();
359: throw e;
360: }
361: }
362:
363: /**
364: * Internal method. Indicates whether flushMode is AUTO.
365: * @return boolean
366: */
367: public boolean isFlushModeAUTO() {
368: return flushMode == FlushModeType.AUTO;
369: }
370:
371: protected void setJTATransactionWrapper() {
372: transaction = new JTATransactionWrapper(this );
373: }
374:
375: protected void setEntityTransactionWrapper() {
376: transaction = new EntityTransactionWrapper(this);
377: }
378: }
|