KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > ejb > cmp3 > EJBQueryImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.ejb.cmp3;
23
24 import java.util.*;
25 import javax.persistence.Query;
26 import javax.persistence.TemporalType;
27 import javax.persistence.FlushModeType;
28 import javax.persistence.NoResultException;
29 import javax.persistence.NonUniqueResultException;
30 import oracle.toplink.essentials.queryframework.*;
31 import oracle.toplink.essentials.ejb.cmp3.*;
32 import oracle.toplink.essentials.internal.helper.*;
33
34 /**
35 * Concrete EJB 3.0 query class
36 *
37 * To do:
38 * Internationalize exceptions
39 * Change TopLink exceptions to exception types used by the spec
40 * Named Parameters
41 * Report Query
42 * firstResultIndex set in query framework
43 * temporal type parameters
44 * Change single result to use ReadObjectQuery
45 **/

46 public class EJBQueryImpl extends oracle.toplink.essentials.internal.ejb.cmp3.base.EJBQueryImpl implements EJBQuery {
47
48     /**
49      * Base constructor for EJBQueryImpl. Initializes basic variables.
50      * An EJBQLQueryImpl may only ever be tied to one entityManager.
51      */

52     protected EJBQueryImpl(EntityManagerImpl entityManager) {
53         super(entityManager);
54     }
55
56     /**
57      * Create an EJBQueryImpl with a TopLink query.
58      * @param query
59      * @param entityManager
60      */

61     public EJBQueryImpl(DatabaseQuery query, EntityManagerImpl entityManager) {
62         super(query, entityManager);
63     }
64
65     /**
66      * Build an EJBQueryImpl based on the given ejbql string
67      * @param ejbql
68      * @param entityManager
69      */

70     public EJBQueryImpl(String JavaDoc ejbql, EntityManagerImpl entityManager) {
71         super(ejbql, entityManager);
72     }
73
74     /**
75      * Create an EJBQueryImpl with either a query name or an ejbql string
76      * @param queryDescription
77      * @param entityManager
78      * @param isNamedQuery determines whether to treat the query description as ejbql or a query name
79      */

80     public EJBQueryImpl(String JavaDoc queryDescription, EntityManagerImpl entityManager, boolean isNamedQuery) {
81         super(queryDescription, entityManager, isNamedQuery);
82     }
83
84     /**
85      * Convert the given object to the class represented by the given temporal type.
86      * @return an object represting the given TemporalType
87      */

88     protected Object JavaDoc convertTemporalType(Object JavaDoc value, TemporalType type) {
89         ConversionManager conversionManager = ((oracle.toplink.essentials.internal.sessions.AbstractSession)getEntityManager().getActiveSession()).getDatasourcePlatform().getConversionManager();
90         if (type == TemporalType.TIME) {
91             return conversionManager.convertObject(value, ClassConstants.TIME);
92         } else if (type == TemporalType.TIMESTAMP) {
93             return conversionManager.convertObject(value, ClassConstants.TIMESTAMP);
94         } else if (type == TemporalType.DATE) {
95             return conversionManager.convertObject(value, ClassConstants.SQLDATE);
96         }
97         return value;
98     }
99
100     /**
101      * Return the entityManager this query is tied to.
102      */

103     public EntityManager getEntityManager() {
104         return (EntityManager)entityManager;
105     }
106
107     /**
108     * Set the position of the first result to retrieve.
109     * @param start position of the first result, numbered from 0
110     * @return the same query instance
111     */

112     public Query setFirstResult(int startPosition) {
113         setFirstResultInternal(startPosition);
114         return this;
115     }
116
117     /**
118     * Set the flush mode type to be used for the query execution.
119     * @param flushMode
120     */

121     public Query setFlushMode(FlushModeType flushMode) {
122         if (flushMode == null) {
123             getDatabaseQuery().setFlushOnExecute(null);
124         } else {
125             getDatabaseQuery().setFlushOnExecute(flushMode == FlushModeType.AUTO);
126         }
127         return this;
128     }
129
130     /**
131     * Set an implementation-specific hint.
132     * If the hint name is not recognized, it is silently ignored.
133     * @param hintName
134     * @param value
135     * @return the same query instance
136     * @throws IllegalArgumentException if the second argument is not
137     * valid for the implementation
138     */

139     public Query setHint(String JavaDoc hintName, Object JavaDoc value) {
140         setHintInternal(hintName, value);
141         return this;
142     }
143
144     /**
145     * Set the maximum number of results to retrieve.
146     * @param maxResult
147     * @return the same query instance
148     */

149     public Query setMaxResults(int maxResult) {
150         setMaxResultsInternal(maxResult);
151         return this;
152     }
153
154     /**
155     * Bind an argument to a named parameter.
156     * @param name the parameter name
157     * @param value
158     * @return the same query instance
159     */

160     public Query setParameter(String JavaDoc name, Object JavaDoc value) {
161         setParameterInternal(name, value);
162         return this;
163     }
164
165     /**
166     * Bind an instance of java.util.Date to a named parameter.
167     * @param name
168     * @param value
169     * @param temporalType
170     * @return the same query instance
171     */

172     public Query setParameter(String JavaDoc name, Date value, TemporalType temporalType) {
173         return setParameter(name, convertTemporalType(value, temporalType));
174     }
175
176     /**
177     * Bind an instance of java.util.Calendar to a named parameter.
178     * @param name
179     * @param value
180     * @param temporalType
181     * @return the same query instance
182     */

183     public Query setParameter(String JavaDoc name, Calendar value, TemporalType temporalType) {
184         return setParameter(name, convertTemporalType(value, temporalType));
185     }
186
187     /**
188     * Bind an argument to a positional parameter.
189     * @param position
190     * @param value
191     * @return the same query instance
192     */

193     public Query setParameter(int position, Object JavaDoc value) {
194         setParameterInternal(position, value);
195         return this;
196     }
197
198     /**
199     * Bind an instance of java.util.Date to a positional parameter.
200     * @param position
201     * @param value
202     Query API Enterprise JavaBeans 3.0, Early Draft Entity Beans
203     Sun Microsystems Inc
204     * @param temporalType
205     * @return the same query instance
206     */

207     public Query setParameter(int position, Date value, TemporalType temporalType) {
208         return setParameter(position, convertTemporalType(value, temporalType));
209     }
210
211     /**
212     * Bind an instance of java.util.Calendar to a positional parameter.
213     * @param position
214     * @param value
215     * @param temporalType
216     * @return the same query instance
217     */

218     public Query setParameter(int position, Calendar value, TemporalType temporalType) {
219         return setParameter(position, convertTemporalType(value, temporalType));
220     }
221
222     protected void throwNoResultException(String JavaDoc message) {
223         throw new NoResultException(message);
224     }
225     
226     protected void throwNonUniqueResultException(String JavaDoc message) {
227         throw new NonUniqueResultException(message);
228     }
229 }
230
Popular Tags