KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > search > basic > BasicQuery


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/BasicQuery.java,v 1.21 2004/07/28 09:35:02 ib Exp $
3  * $Revision: 1.21 $
4  * $Date: 2004/07/28 09:35:02 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.search.basic;
25
26 import org.apache.slide.common.PropertyParseException;
27 import org.apache.slide.common.RequestedProperties;
28 import org.apache.slide.common.RequestedPropertiesImpl;
29 import org.apache.slide.common.ServiceAccessException;
30 import org.apache.slide.common.Uri;
31 import org.apache.slide.search.BadQueryException;
32 import org.apache.slide.search.InvalidScopeException;
33 import org.apache.slide.search.PropertyProvider;
34 import org.apache.slide.search.QueryScope;
35 import org.apache.slide.search.SearchQuery;
36 import org.apache.slide.search.SearchQueryResult;
37 import org.apache.slide.search.SearchToken;
38 import org.apache.slide.search.SlideUri;
39 import org.apache.slide.store.AbstractStore;
40 import org.jdom.Element;
41 import org.jdom.Namespace;
42
43 /**
44  * A BasicQuery represents the query and is able to deliver a
45  * SearchQueryResult using the execute method. It serves as a base class for
46  * store specific implementations. It hosts the information about the SELECT,
47  * FROM, WHERE, ORDERBY and LIMIT. It also holds a tree of
48  * BasicSearchExpressions.
49  *
50  * @version $Revision: 1.21 $
51  */

52 public abstract class BasicQuery extends SearchQuery implements IBasicQuery {
53     
54     /**
55      * Message of a BadQueryException that is thrown if the query element
56      * is <code>null</code>.
57      */

58     public static final String JavaDoc NO_QUERY_ELEMENT = "No query element";
59     
60     /**
61      * The provider which delivers the expression compiler to use.
62      */

63     protected IBasicExpressionCompilerProvider expressionCompilerProvider = null;
64     
65     /**
66      * Message of a BadQueryException that is thrown if the query element
67      * does not contain a &lt;select&gt; element.
68      */

69     public static final String JavaDoc SELECT_ELEMENT_MISSING = "Required element <select> not supplied";
70     
71     /**
72      * Message of a BadQueryException that is thrown if the query element
73      * does not contain a &lt;from&gt; element.
74      */

75     public static final String JavaDoc FROM_ELEMENT_MISSING = "Required element <from> not supplied";
76     
77     /**
78      * Message of a BadQueryException that is thrown if the query element
79      * neither contains a &lt;prop&gt; nor a &lt;allprop&gt; element.
80      */

81     public static final String JavaDoc PROP_OR_ALLPROP_ELEMENT_MISSING = "Required element <prop> or <allprop> not supplied";
82     
83     private IBasicExpressionFactory contentExpressionFactory;
84     private IBasicExpressionFactory propertiesExpressionFactory;
85     
86     
87     /** the element describing this query */
88     protected Element queryElement;
89     
90     /** the namespace for this query */
91     protected Namespace namespace;
92     
93     /** the scope of this query, <FROM> */
94     protected QueryScope queryScope;
95     
96     /** the element describing the WHERE clauise */
97     protected Element whereElement;
98     
99     /** List of requested properties, <SELECT> */
100     protected RequestedProperties requestedProperties;
101     
102     /** <LIMIT> */
103     protected int limit;
104     
105     /** ORDER BY */
106     protected OrderBy orderBy;
107     
108     /** indicates, if a limit is defined */
109     protected boolean limitDefined = false;
110     
111     /** The store for this query, may be used to access store parameters */
112     protected AbstractStore store;
113     
114     /** the top level expression in the <WHERE> clause */
115     protected IBasicExpression rootExpression;
116     
117     /** used to get the slidePath */
118     protected SlideUri slideUri;
119     
120     /** The provider for the properties */
121     protected PropertyProvider propertyProvider;
122     
123     
124     protected BasicQuery () {}
125     
126     protected BasicQuery (SearchToken searchToken) {
127         init(searchToken);
128     }
129     
130     public void init (SearchToken token) {
131         this.searchToken = token;
132         slideUri = searchToken.getSlideContext();
133         this.expressionCompilerProvider = new ExpressionCompilerProvider();
134     }
135     
136     
137     /**
138      * returns the factory for content expressions. May be null, if store does
139      * not provide search capabilities and no indexer is defined.
140      *
141      * @return an IBasicExpressionFactory
142      *
143      */

144     public IBasicExpressionFactory getContentExpressionFactory ()
145     {
146         if (contentExpressionFactory == null)
147             contentExpressionFactory =
148                 store.getContentIndexer().getBasicExpressionFactory();
149         
150         return contentExpressionFactory;
151     }
152     
153     
154     /**
155      * returns the factory for property expressions. May be null, if store does
156      * not provide search capabilities and no indexer is defined.
157      *
158      * @return an IBasicExpressionFactory
159      *
160      */

161     public IBasicExpressionFactory getPropertiesExpressionFactory ()
162     {
163         propertiesExpressionFactory =
164             store.getPropertiesIndexer().getBasicExpressionFactory();
165         
166         return propertiesExpressionFactory;
167     }
168     
169     
170     /**
171      * Method getStore
172      *
173      * @return an AbstractStore
174      *
175      */

176     public AbstractStore getStore () {
177         return store;
178     }
179     
180     /**
181      * Method getSlidePath
182      *
183      * @return a String
184      *
185      * @throws InvalidScopeException
186      *
187      */

188     public String JavaDoc getSlidePath () throws InvalidScopeException {
189         return slideUri.getSlidePath (queryScope.getHref());
190     }
191     
192     /**
193      * Method getSearchToken
194      *
195      * @return a SearchToken
196      *
197      */

198     public SearchToken getSearchToken (){
199         return searchToken;
200     }
201     
202     /**
203      * Method getPropertyProvider
204      *
205      * @return a PropertyProvider
206      *
207      */

208     public PropertyProvider getPropertyProvider () {
209         return propertyProvider;
210     }
211     
212     /**
213      * Builds the internal structure from the JDOM tree. Concrete implementations
214      * may use parseQueryElementWithoutExpression to create most of the
215      * objects describing the query.
216      *
217      * @param basicSearchElement the (root) expression Element.
218      * @param propertyProvider the PropertyProvider to use (may be
219      * <code>null</code>).
220      *
221      * @throws BadQueryException
222      */

223     public void parseQueryElement (Element basicSearchElement, PropertyProvider propertyProvider) throws BadQueryException {
224         
225         queryScope = getScope(basicSearchElement);
226         this.propertyProvider = propertyProvider;
227         
228         // might be null in testsuite
229
if (searchToken.getNamespace() != null) {
230             // Uri uri = new Uri (searchToken.getNamespace(), slideUri.getSlidePath(queryScope.getHref()));
231
Uri uri = searchToken.getNamespace().getUri(this.getSearchToken().getSlideToken(), slideUri.getSlidePath(queryScope.getHref()));
232             store = (AbstractStore)uri.getStore();
233         }
234         
235         parseQuery(basicSearchElement, propertyProvider);
236     }
237     
238     /**
239      * builds the internal structure from the JDOM tree. Concrete implementations
240      * may use {@link #parseQueryWithoutExpression parseQueryWithoutExpression}
241      * to create most of the objects describing the query.
242      *
243      * @param basicSearchElement the (root) expression Element.
244      * @param propertyProvider the PropertyProvider to use (may be
245      * <code>null</code>).
246      *
247      * @throws BadQueryException
248      */

249     public abstract void parseQuery (Element basicSearchElement, PropertyProvider propertyProvider)
250         throws BadQueryException;
251     
252     /**
253      * Executes a request. A store specific implementation should overwrite
254      * this to optimize the execution.
255      *
256      * @return a SearchQueryResult
257      *
258      * @throws ServiceAccessException
259      *
260      */

261     public abstract SearchQueryResult execute () throws ServiceAccessException;
262     
263     /**
264      * builds the internal structure from the JDOM tree. It may be used by the
265      * concrete implementation of BasicQuery. It does NOT create the tree of
266      * Expressions. This must be done in the specific implementation.
267      *
268      * @param basicSearchElement an Element
269      *
270      * @throws BadQueryException
271      */

272     protected void parseQueryWithoutExpression (Element basicSearchElement) throws BadQueryException {
273         
274         if (basicSearchElement == null)
275             throw new BadQueryException (NO_QUERY_ELEMENT);
276         
277         namespace = basicSearchElement.getNamespace();
278         
279         Element selectElement = basicSearchElement.getChild
280             (Literals.SELECT, namespace);
281         
282         // SELECT is mandatory
283
if (selectElement == null)
284             throw new BadQueryException (SELECT_ELEMENT_MISSING);
285         
286         Element fromElement = basicSearchElement.getChild
287             (Literals.FROM, namespace);
288         
289         // FROM is mandatory
290
if (fromElement == null) {
291             throw new BadQueryException (FROM_ELEMENT_MISSING);
292         }
293         
294         whereElement = basicSearchElement.getChild
295             (Literals.WHERE, namespace);
296         
297         Element orderByElement = basicSearchElement.getChild
298             (Literals.ORDERBY, namespace);
299         
300         Element limitElement = basicSearchElement.getChild
301             (Literals.LIMIT, namespace);
302         
303         Element propElement = selectElement.getChild (Literals.PROP, namespace);
304         if (propElement == null) {
305             propElement = selectElement.getChild (Literals.ALLPROP, namespace);
306         }
307         
308         if (propElement == null) {
309             throw new BadQueryException(PROP_OR_ALLPROP_ELEMENT_MISSING);
310         }
311         
312         try {
313             requestedProperties = new RequestedPropertiesImpl (propElement);
314         }
315         catch (PropertyParseException e) {
316             throw new BadQueryException(e.getMessage(), e);
317         }
318         
319         queryScope = new BasicQueryScope (fromElement);
320         
321         if (orderByElement != null) {
322             orderBy = new OrderBy ();
323             orderBy.init (orderByElement);
324         }
325         
326         if (limitElement != null) {
327             limit = new Integer JavaDoc (limitElement.getTextTrim()).intValue();
328             limitDefined = true;
329         }
330     }
331     
332     /**
333      * QueryScope accessor
334      *
335      * @return the Scope
336      *
337      */

338     public QueryScope getScope () {
339         return queryScope;
340     }
341     
342     /**
343      * Method getSelectedProperties
344      *
345      * @return a SelectedPropertyList
346      */

347     public RequestedProperties requestedProperties () {
348         return requestedProperties;
349     }
350     
351     /**
352      * Method getExpression
353      *
354      * @return a BasicExpression
355      *
356      */

357     public IBasicExpression getExpression () {
358         return rootExpression;
359     }
360     
361     
362     /**
363      * Method isLimitDefined
364      *
365      * @return true if <limit> was specified
366      */

367     public boolean isLimitDefined () {
368         return limitDefined;
369     }
370     
371     /**
372      * Method getLimit
373      *
374      * @return the value of <limit>
375      */

376     public int getLimit () {
377         return limit;
378     }
379     
380     /**
381      * Method setScope
382      *
383      * @param queryScope a QueryScope
384      *
385      */

386     public void setScope (QueryScope queryScope) {
387         this.queryScope = queryScope;
388     }
389     
390     /**
391      * Method getOrderBy
392      *
393      * @return an OrderBy
394      *
395      */

396     public OrderBy getOrderBy () {
397         return orderBy;
398     }
399     
400     
401     /**
402      * For debugging purpose.
403      *
404      * @return this query in string representation
405      *
406      */

407     public String JavaDoc toString () {
408         
409         String JavaDoc result =
410             "SELECT [" + requestedProperties + "] FROM [" + queryScope + "] "
411             + "WHERE [" + rootExpression + "]";
412         
413         return result;
414     }
415     
416     /**
417      * Needed to decide, which implementation of BasicQuery to load
418      *
419      * @param basicSearchElementJDOM an Element
420      *
421      * @return a QueryScope
422      *
423      * @throws BadQueryException
424      *
425      */

426     public static QueryScope getScope(Element basicSearchElementJDOM)
427         throws BadQueryException
428     {
429         if (basicSearchElementJDOM == null)
430             throw new BadQueryException (NO_QUERY_ELEMENT);
431         
432         Namespace namespace = basicSearchElementJDOM.getNamespace();
433         Element fromElement = basicSearchElementJDOM.getChild
434             (Literals.FROM, namespace);
435         
436         // FROM is mandatory
437
if (fromElement == null)
438             throw new BadQueryException (FROM_ELEMENT_MISSING);
439         
440         return new BasicQueryScope (fromElement);
441     }
442     
443     /**
444      * This IBasicExpressionCompilerProvider implementation returns a
445      * BasicQueryCompiler instance in method getCompiler().
446      *
447      * @version $Revision: 1.21 $
448      *
449      **/

450     public static class ExpressionCompilerProvider implements IBasicExpressionCompilerProvider {
451         
452         /**
453          * Returns an IBasicExpressionCompiler for the given parameters.
454          *
455          * @param query the IBasicQuery.
456          * @param propertyProvider the PropertyProvider to use (may be
457          * <code>null</code>).
458          *
459          * @return an IBasicExpressionCompiler for the given parameters.
460          */

461         public IBasicExpressionCompiler getCompiler(IBasicQuery query, PropertyProvider propertyProvider) throws BadQueryException {
462             return new BasicExpressionCompiler(query, propertyProvider);
463         }
464     }
465 }
466
467
Popular Tags