KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > cmp > jdbc > metadata > JDBCDeclaredQueryMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins.cmp.jdbc.metadata;
23
24 import java.lang.reflect.Method JavaDoc;
25
26 import org.w3c.dom.Element JavaDoc;
27
28 import org.jboss.deployment.DeploymentException;
29 import org.jboss.metadata.MetaData;
30
31
32 /**
33  * Imutable class contains information about a declated query.
34  *
35  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
36  * @version $Revision: 37459 $
37  */

38 public final class JDBCDeclaredQueryMetaData implements JDBCQueryMetaData
39 {
40    /**
41     * The method to which this query is bound.
42     */

43    private final Method JavaDoc method;
44
45    /**
46     * The user specified additional columns to be added to the select clause.
47     */

48    private final String JavaDoc additionalColumns;
49
50    /**
51     * The user specified from clause.
52     */

53    private final String JavaDoc from;
54
55    /**
56     * The user specified where clause.
57     */

58    private final String JavaDoc where;
59
60    /**
61     * The user specified order clause.
62     */

63    private final String JavaDoc order;
64
65    /**
66     * The other clause is appended to the end of the sql. This is useful for
67     * hints to the query engine.
68     */

69    private final String JavaDoc other;
70
71    /**
72     * Should the select be DISTINCT?
73     */

74    private final boolean distinct;
75
76    /**
77     * The name of the ejb from which the field will be selected.
78     */

79    private final String JavaDoc ejbName;
80
81    /**
82     * The name of the cmp-field to be selected.
83     */

84    private final String JavaDoc fieldName;
85
86    /**
87     * The aliase that is used for the main select table.
88     */

89    private final String JavaDoc alias;
90    /**
91     * Read ahead meta data.
92     */

93    private final JDBCReadAheadMetaData readAhead;
94
95    /**
96     * Should the query return Local or Remote beans.
97     */

98    private final boolean resultTypeMappingLocal;
99
100    private final Class JavaDoc compiler;
101
102    private final boolean lazyResultSetLoading;
103
104    /**
105     * Constructs a JDBCDeclaredQueryMetaData which is defined by the
106     * declared-sql xml element and is invoked by the specified method.
107     * Inherits unspecified values from the defaults.
108     *
109     * @param defaults the default values to use
110     * @param readAhead the read-ahead properties for this query
111     */

112    public JDBCDeclaredQueryMetaData(JDBCDeclaredQueryMetaData defaults,
113                                     JDBCReadAheadMetaData readAhead,
114                                     Class JavaDoc compiler,
115                                     boolean lazyResultSetLoading)
116       throws DeploymentException
117    {
118       this.method = defaults.getMethod();
119       this.readAhead = readAhead;
120
121       this.from = defaults.getFrom();
122       this.where = defaults.getWhere();
123       this.order = defaults.getOrder();
124       this.other = defaults.getOther();
125
126       this.resultTypeMappingLocal = defaults.isResultTypeMappingLocal();
127
128       this.distinct = defaults.isSelectDistinct();
129       this.ejbName = defaults.getEJBName();
130       this.fieldName = defaults.getFieldName();
131       this.alias = defaults.getAlias();
132       this.additionalColumns = defaults.getAdditionalColumns();
133
134       this.compiler = compiler;
135       this.lazyResultSetLoading = lazyResultSetLoading;
136    }
137
138
139    /**
140     * Constructs a JDBCDeclaredQueryMetaData which is defined by the
141     * declared-sql xml element and is invoked by the specified method.
142     *
143     * @param queryElement the xml Element which contains the metadata about
144     * this query
145     * @param method the method which invokes this query
146     * @param readAhead the read-ahead properties for this query
147     */

148    public JDBCDeclaredQueryMetaData(boolean isResultTypeMappingLocal,
149                                     Element JavaDoc queryElement,
150                                     Method JavaDoc method,
151                                     JDBCReadAheadMetaData readAhead,
152                                     Class JavaDoc compiler,
153                                     boolean lazyResultSetLoading)
154       throws DeploymentException
155    {
156       this.compiler = compiler;
157       this.lazyResultSetLoading = lazyResultSetLoading;
158       
159       this.method = method;
160       this.readAhead = readAhead;
161
162       from = nullIfEmpty(MetaData.getOptionalChildContent(queryElement, "from"));
163       where = nullIfEmpty(MetaData.getOptionalChildContent(queryElement, "where"));
164       order = nullIfEmpty(MetaData.getOptionalChildContent(queryElement, "order"));
165       other = nullIfEmpty(MetaData.getOptionalChildContent(queryElement, "other"));
166
167       this.resultTypeMappingLocal = isResultTypeMappingLocal;
168
169       // load ejbSelect info
170
Element JavaDoc selectElement =
171          MetaData.getOptionalChild(queryElement, "select");
172
173       if(selectElement != null)
174       {
175          // should select use distinct?
176
distinct =
177             (MetaData.getOptionalChild(selectElement, "distinct") != null);
178
179          if(method.getName().startsWith("ejbSelect"))
180          {
181             ejbName = MetaData.getUniqueChildContent(selectElement, "ejb-name");
182             fieldName = nullIfEmpty(MetaData.getOptionalChildContent(selectElement, "field-name"));
183          }
184          else
185          {
186             // the ejb-name and field-name elements are not allowed for finders
187
if(MetaData.getOptionalChild(selectElement, "ejb-name") != null)
188             {
189                throw new DeploymentException(
190                   "The ejb-name element of declared-sql select is only " +
191                   "allowed for ejbSelect queries."
192                );
193             }
194             if(MetaData.getOptionalChild(selectElement, "field-name") != null)
195             {
196                throw new DeploymentException(
197                   "The field-name element of declared-sql select is only " +
198                   "allowed for ejbSelect queries."
199                );
200             }
201             ejbName = null;
202             fieldName = null;
203          }
204          alias = nullIfEmpty(MetaData.getOptionalChildContent(selectElement, "alias"));
205          additionalColumns = nullIfEmpty(MetaData.getOptionalChildContent(selectElement, "additional-columns"));
206       }
207       else
208       {
209          if(method.getName().startsWith("ejbSelect"))
210          {
211             throw new DeploymentException(
212                "The select element of " +
213                "declared-sql is required for ejbSelect queries."
214             );
215          }
216          distinct = false;
217          ejbName = null;
218          fieldName = null;
219          alias = null;
220          additionalColumns = null;
221       }
222    }
223
224    // javadoc in parent class
225
public Method JavaDoc getMethod()
226    {
227       return method;
228    }
229
230    // javadoc in parent class
231
public boolean isResultTypeMappingLocal()
232    {
233       return resultTypeMappingLocal;
234    }
235
236    /**
237     * Gets the read ahead metadata for the query.
238     *
239     * @return the read ahead metadata for the query.
240     */

241    public JDBCReadAheadMetaData getReadAhead()
242    {
243       return readAhead;
244    }
245
246    public Class JavaDoc getQLCompilerClass()
247    {
248       return compiler;
249    }
250
251    /**
252     * Gets the sql FROM clause of this query.
253     *
254     * @return a String which contains the sql FROM clause
255     */

256    public String JavaDoc getFrom()
257    {
258       return from;
259    }
260
261    /**
262     * Gets the sql WHERE clause of this query.
263     *
264     * @return a String which contains the sql WHERE clause
265     */

266    public String JavaDoc getWhere()
267    {
268       return where;
269    }
270
271    /**
272     * Gets the sql ORDER BY clause of this query.
273     *
274     * @return a String which contains the sql ORDER BY clause
275     */

276    public String JavaDoc getOrder()
277    {
278       return order;
279    }
280
281    /**
282     * Gets other sql code which is appended to the end of the query.
283     * This is userful for supplying hints to the query engine.
284     *
285     * @return a String which contains additional sql code which is
286     * appended to the end of the query
287     */

288    public String JavaDoc getOther()
289    {
290       return other;
291    }
292
293    /**
294     * Should the select be DISTINCT?
295     *
296     * @return true if the select clause should contain distinct
297     */

298    public boolean isSelectDistinct()
299    {
300       return distinct;
301    }
302
303    /**
304     * The name of the ejb from which the field will be selected.
305     *
306     * @return the name of the ejb from which a field will be selected, or null
307     * if returning a whole ejb
308     */

309    public String JavaDoc getEJBName()
310    {
311       return ejbName;
312    }
313
314    /**
315     * The name of the cmp-field to be selected.
316     *
317     * @return the name of the cmp-field to be selected or null if returning a
318     * whole ejb
319     */

320    public String JavaDoc getFieldName()
321    {
322       return fieldName;
323    }
324
325    /**
326     * The alias that is used for the select table.
327     *
328     * @return the alias that is used for the table from which the entity or
329     * field is selected.
330     */

331    public String JavaDoc getAlias()
332    {
333       return alias;
334    }
335
336    /**
337     * Additional columns that should be added to the select clause. For example,
338     * columns that are used in an order by clause.
339     *
340     * @return additional columns that should be added to the select clause
341     */

342    public String JavaDoc getAdditionalColumns()
343    {
344       return additionalColumns;
345    }
346
347    public boolean isLazyResultSetLoading()
348    {
349       return lazyResultSetLoading;
350    }
351
352    /**
353     * Compares this JDBCDeclaredQueryMetaData against the specified object.
354     * Returns true if the objects are the same. Two JDBCDeclaredQueryMetaData
355     * are the same if they are both invoked by the same method.
356     *
357     * @param o the reference object with which to compare
358     * @return true if this object is the same as the object argument; false
359     * otherwise
360     */

361    public boolean equals(Object JavaDoc o)
362    {
363       if(o instanceof JDBCDeclaredQueryMetaData)
364       {
365          return ((JDBCDeclaredQueryMetaData) o).method.equals(method);
366       }
367       return false;
368    }
369
370    /**
371     * Returns a hashcode for this JDBCDeclaredQueryMetaData. The hashcode is
372     * computed by the method which invokes this query.
373     *
374     * @return a hash code value for this object
375     */

376    public int hashCode()
377    {
378       return method.hashCode();
379    }
380
381    /**
382     * Returns a string describing this JDBCDeclaredQueryMetaData. The exact
383     * details of the representation are unspecified and subject to change,
384     * but the following may be regarded as typical:
385     * <p/>
386     * "[JDBCDeclaredQueryMetaData: method=public org.foo.User findByName(
387     * java.lang.String)]"
388     *
389     * @return a string representation of the object
390     */

391    public String JavaDoc toString()
392    {
393       return "[JDBCDeclaredQueryMetaData : method=" + method + "]";
394    }
395
396    private static String JavaDoc nullIfEmpty(String JavaDoc s)
397    {
398       if(s != null && s.trim().length() == 0)
399       {
400          s = null;
401       }
402       return s;
403    }
404 }
405
Popular Tags