KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/search/basic/BasicQueryEnvelope.java,v 1.11 2004/07/28 09:35:02 ib Exp $
3  * $Revision: 1.11 $
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
25 package org.apache.slide.search.basic;
26
27 import org.jdom.Element;
28 import org.apache.slide.search.PropertyProvider;
29 import org.apache.slide.search.BadQueryException;
30 import org.apache.slide.search.QueryScope;
31 import org.apache.slide.search.SearchQuery;
32 import org.apache.slide.search.SlideUri;
33 import org.apache.slide.search.SearchQueryResult;
34 import org.apache.slide.search.InvalidScopeException;
35 import org.apache.slide.search.SearchToken;
36 import org.apache.slide.common.RequestedProperties ;
37 import org.apache.slide.common.Scope;
38 import org.apache.slide.common.Uri;
39 import org.apache.slide.common.SlideRuntimeException;
40 import org.apache.slide.common.Namespace;
41
42 import org.apache.slide.common.ServiceAccessException;
43 import org.apache.slide.store.AbstractStore ;
44
45 import java.util.HashSet JavaDoc;
46 import java.util.Set JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Map JavaDoc;
49 //import java.util.ArrayList;
50
//import java.util.List;
51
import java.util.Iterator JavaDoc;
52 import java.util.Enumeration JavaDoc;
53 import java.util.StringTokenizer JavaDoc;
54
55
56 /**
57  * Envelopes all queries that are necessary, if a scope covers several stores.
58  * For example: /mycoll is mapped to an SQL store, /mycoll/xml is mapped to an
59  * XML database. A query with scope /mycoll must be divided in two different
60  * queries, the result must be joined.
61  *
62  * @version $Revision: 1.11 $
63  *
64  **/

65 public class BasicQueryEnvelope extends SearchQuery implements IBasicQuery {
66     
67     
68     /**
69      * Should never be called ??
70      *
71      * @return an IBasicExpressionFactory
72      *
73      */

74     public IBasicExpressionFactory getContentExpressionFactory()
75     {
76         if (true) throw new SlideRuntimeException ("method should never be called");
77         return null;
78     }
79     
80     /**
81      * Should never be called ??
82      *
83      * @return an IBasicExpressionFactory
84      *
85      */

86     public IBasicExpressionFactory getPropertiesExpressionFactory()
87     {
88         if (true) throw new SlideRuntimeException ("method should never be called");
89         return null;
90     }
91     
92     private BasicQueryImpl topLevelQuery;
93     private QueryScope topLevelQueryScope;
94  
95     private Map JavaDoc subQueries = new HashMap JavaDoc();
96     
97     private QueryTree queryTree;
98     
99     private int queryDepth;
100     
101     private SlideUri slideUri;
102     
103     private SearchToken token;
104     
105     
106     /*
107      * Constructs a BasicQueryEnvelope
108      *
109      * @param token the searchtoken
110      * @param queryScope the scope of this query
111      *
112      * @throws InvalidScopeException
113      */

114     public BasicQueryEnvelope (SearchToken token, QueryScope queryScope)
115         throws InvalidScopeException
116     {
117         this.token = token;
118         Namespace namespace = token.getNamespace();
119         Enumeration JavaDoc stores = namespace.enumerateScopes();
120         this.slideUri = token.getSlideContext();
121         this.topLevelQueryScope = queryScope;
122         String JavaDoc slideScope = slideUri.getSlidePath(queryScope.getHref());
123         Scope topScope = new Scope (slideScope);
124         
125         Set JavaDoc ex = queryScope.getExcludedScopes();
126         int size = ex.size();
127         Scope [] excludedScopes = new Scope [size] ;
128         Iterator JavaDoc it = ex.iterator();
129         
130         for (int i = 0; i < size; i++) {
131             String JavaDoc href = (String JavaDoc)it.next();
132             excludedScopes [i] = new Scope (slideUri.getSlidePath(href));
133         }
134         
135         this.queryTree = new QueryTree (stores, topScope, excludedScopes);
136         
137         it = queryTree.iterator();
138         
139         while (it.hasNext()) {
140             Scope sScope = (Scope)it.next();
141             // System.out.println("createEnvelope for scope " + sScope);
142
BasicQueryImpl query = createSubQuery (namespace, sScope.toString(), token);
143             subQueries.put (sScope, query);
144         }
145         
146         topLevelQuery = (BasicQueryImpl) subQueries.get (topScope);
147     }
148     
149     
150     /**
151      * parses all subQueries. The scope must be calculated for each subquery.
152      *
153      * @param basicSearchElement an Element
154      * @param propertyProvider a PropertyProvider
155      *
156      * @throws BadQueryException
157      *
158      */

159     public void parseQueryElement (Element basicSearchElement,
160                                    PropertyProvider propertyProvider)
161         throws BadQueryException
162     {
163         //topLevelQuery.parseQueryElement (basicSearchElement, propertyProvider);
164
// queryDepth = topLevelQueryScope.getDepth();
165

166         // WAM restrict depth to what is defined in web.xml
167
queryDepth = Math.min (topLevelQueryScope.getDepth(), token.getMaxDepth());
168         
169         Iterator JavaDoc it = subQueries.keySet().iterator();
170         Set JavaDoc scopesToRemove = new HashSet JavaDoc ();
171         while (it.hasNext()) {
172
173             Scope scope = (Scope)it.next();
174             // System.out.println ("parseQueryElement for scope " + scope);
175
QueryScope subQueryScope = calculateSubQueryScope (scope);
176             if (subQueryScope.getDepth() >= 0) {
177                 BasicQueryImpl query = (BasicQueryImpl)subQueries.get (scope);
178                 query.parseQueryElement (basicSearchElement, propertyProvider, subQueryScope);
179             }
180             else {
181                 scopesToRemove.add (scope);
182             }
183             }
184         it = scopesToRemove.iterator();
185         while (it.hasNext()) {
186             subQueries.remove(it.next());
187         }
188     }
189     
190     /**
191      * Executes each involved query and merges the results
192      *
193      * @return a SearchQueryResult
194      *
195      * @throws ServiceAccessException
196      *
197      */

198     public SearchQueryResult execute() throws ServiceAccessException {
199         Iterator JavaDoc it = subQueries.keySet().iterator();
200         SearchQueryResult result = null;
201         
202         if (topLevelQuery.orderBy != null) {
203             result = new SearchQueryResult (topLevelQuery.orderBy.getComparator());
204         }
205         else {
206             result = new SearchQueryResult ();
207         }
208         
209         
210         
211         while (it.hasNext()) {
212             Scope scope = (Scope)it.next();
213             
214             BasicQuery query = (BasicQuery)subQueries.get (scope);
215             query.setScope (calculateSubQueryScope(scope));
216             SearchQueryResult subResult = query.execute();
217             result.add (subResult);
218             if (subResult.getStatus() != 0) {
219                 result.setStatus (subResult.getStatus());
220                 result.setDescription (subResult.getDescription());
221             }
222         }
223         
224         return result;
225     }
226     
227     /**
228      * calculates a QueryScope for the defined store
229      *
230      * @param scope the scope
231      *
232      * @return a QueryScope
233      *
234      */

235     private QueryScope calculateSubQueryScope (Scope scope) {
236         int relDepth = queryTree.relativeDepth (scope);
237         int subQueryDepth;
238         if (queryTree.hasChildren (scope)) {
239             subQueryDepth = 1;
240         }
241         else if (queryDepth == QueryScope.DEPTH_INFINITY) {
242             subQueryDepth = QueryScope.DEPTH_INFINITY;
243         }
244         else {
245             subQueryDepth = queryDepth - relDepth;
246         }
247         String JavaDoc contextPath = slideUri.getContextPath (scope.toString());
248         
249         
250         Set JavaDoc inclSet = topLevelQueryScope.getIncludeSet();
251         Set JavaDoc exclSet = topLevelQueryScope.getExcludeSet();
252         
253         QueryScope queryScope = new BasicQueryScope
254             (contextPath, subQueryDepth, inclSet, exclSet);
255         
256         return queryScope;
257     }
258     
259     
260     /**
261      * Method getDepthOfHRef
262      *
263      * @param href a String
264      *
265      * @return an int
266      *
267      */

268     private int getDepthOfHRef (String JavaDoc href) {
269         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc (href, "/");
270         return st.countTokens();
271     }
272     
273     
274     /**
275      * creates a subquery
276      *
277      * @param namespace a Namespace
278      * @param slideScope a String
279      * @param token a SearchToken
280      *
281      * @return a BasicQueryImpl
282      *
283      * @throws SlideRuntimeException
284      *
285      */

286     private BasicQueryImpl createSubQuery (Namespace namespace,
287                                            String JavaDoc slideScope,
288                                            SearchToken token)
289         throws SlideRuntimeException
290     {
291         BasicQueryImpl query = null;
292         
293 // Uri uri = new Uri (namespace, slideScope);
294
Uri uri = namespace.getUri(token.getSlideToken(), slideScope);
295         
296         
297         AbstractStore store = (AbstractStore)uri.getStore();
298         
299         // todo: can we make the query storeindependant?
300
String JavaDoc className = (String JavaDoc)store.getParameter
301             (BasicSearchLanguage.BASIC_QUERY_CLASS);
302         
303         if (className != null) {
304             try {
305                 Class JavaDoc queryClass = Class.forName (className);
306                 query = (BasicQueryImpl) queryClass.newInstance();
307                 query.init (token);
308             }
309             catch (Exception JavaDoc e) {
310                 e.printStackTrace();
311                 throw new SlideRuntimeException (e.getMessage());
312             }
313         }
314         else {
315             query = new BasicQueryImpl(token);
316         }
317         
318         return query;
319     }
320     /**
321      * Method getStore
322      *
323      * @return an AbstractStore
324      *
325      */

326     public AbstractStore getStore() {
327         return topLevelQuery.getStore();
328     }
329     
330     /**
331      * Method getScope
332      *
333      * @return a QueryScope
334      *
335      */

336     public QueryScope getScope() {
337         return topLevelQueryScope;
338     }
339     
340     /**
341      * Method getPropertyProvider
342      *
343      * @return a PropertyProvider
344      *
345      */

346     public PropertyProvider getPropertyProvider() {
347         return topLevelQuery.getPropertyProvider();
348     }
349     
350     /**
351      * Method isLimitDefined
352      *
353      * @return a boolean
354      *
355      */

356     public boolean isLimitDefined() {
357         return topLevelQuery.isLimitDefined();
358     }
359     
360     /**
361      * Method getLimit
362      *
363      * @return an int
364      *
365      */

366     public int getLimit() {
367         return topLevelQuery.getLimit();
368     }
369     
370     /**
371      * Method getExpression
372      *
373      * @return an IBasicExpression
374      *
375      */

376     public IBasicExpression getExpression() {
377         return topLevelQuery.getExpression();
378     }
379     
380     /**
381      * Method requestedProperties
382      *
383      * @return a RequestedProperties
384      *
385      */

386     public RequestedProperties requestedProperties() {
387         return topLevelQuery.requestedProperties;
388     }
389     
390     /**
391      * Method getSlidePath
392      *
393      * @return a String
394      *
395      * @throws InvalidScopeException
396      *
397      */

398     public String JavaDoc getSlidePath() throws InvalidScopeException {
399         return topLevelQuery.getSlidePath();
400     }
401     
402     /**
403      * Method getSearchToken
404      *
405      * @return a SearchToken
406      *
407      */

408     public SearchToken getSearchToken() {
409         return topLevelQuery.getSearchToken();
410     }
411     
412     public void init (SearchToken token) {
413         this.token = token;
414     }
415 }
416
Popular Tags