1 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 ; 46 import java.util.Set ; 47 import java.util.HashMap ; 48 import java.util.Map ; 49 import java.util.Iterator ; 52 import java.util.Enumeration ; 53 import java.util.StringTokenizer ; 54 55 56 65 public class BasicQueryEnvelope extends SearchQuery implements IBasicQuery { 66 67 68 74 public IBasicExpressionFactory getContentExpressionFactory() 75 { 76 if (true) throw new SlideRuntimeException ("method should never be called"); 77 return null; 78 } 79 80 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 subQueries = new HashMap (); 96 97 private QueryTree queryTree; 98 99 private int queryDepth; 100 101 private SlideUri slideUri; 102 103 private SearchToken token; 104 105 106 114 public BasicQueryEnvelope (SearchToken token, QueryScope queryScope) 115 throws InvalidScopeException 116 { 117 this.token = token; 118 Namespace namespace = token.getNamespace(); 119 Enumeration stores = namespace.enumerateScopes(); 120 this.slideUri = token.getSlideContext(); 121 this.topLevelQueryScope = queryScope; 122 String slideScope = slideUri.getSlidePath(queryScope.getHref()); 123 Scope topScope = new Scope (slideScope); 124 125 Set ex = queryScope.getExcludedScopes(); 126 int size = ex.size(); 127 Scope [] excludedScopes = new Scope [size] ; 128 Iterator it = ex.iterator(); 129 130 for (int i = 0; i < size; i++) { 131 String href = (String )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 BasicQueryImpl query = createSubQuery (namespace, sScope.toString(), token); 143 subQueries.put (sScope, query); 144 } 145 146 topLevelQuery = (BasicQueryImpl) subQueries.get (topScope); 147 } 148 149 150 159 public void parseQueryElement (Element basicSearchElement, 160 PropertyProvider propertyProvider) 161 throws BadQueryException 162 { 163 166 queryDepth = Math.min (topLevelQueryScope.getDepth(), token.getMaxDepth()); 168 169 Iterator it = subQueries.keySet().iterator(); 170 Set scopesToRemove = new HashSet (); 171 while (it.hasNext()) { 172 173 Scope scope = (Scope)it.next(); 174 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 198 public SearchQueryResult execute() throws ServiceAccessException { 199 Iterator 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 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 contextPath = slideUri.getContextPath (scope.toString()); 248 249 250 Set inclSet = topLevelQueryScope.getIncludeSet(); 251 Set exclSet = topLevelQueryScope.getExcludeSet(); 252 253 QueryScope queryScope = new BasicQueryScope 254 (contextPath, subQueryDepth, inclSet, exclSet); 255 256 return queryScope; 257 } 258 259 260 268 private int getDepthOfHRef (String href) { 269 StringTokenizer st = new StringTokenizer (href, "/"); 270 return st.countTokens(); 271 } 272 273 274 286 private BasicQueryImpl createSubQuery (Namespace namespace, 287 String slideScope, 288 SearchToken token) 289 throws SlideRuntimeException 290 { 291 BasicQueryImpl query = null; 292 293 Uri uri = namespace.getUri(token.getSlideToken(), slideScope); 295 296 297 AbstractStore store = (AbstractStore)uri.getStore(); 298 299 String className = (String )store.getParameter 301 (BasicSearchLanguage.BASIC_QUERY_CLASS); 302 303 if (className != null) { 304 try { 305 Class queryClass = Class.forName (className); 306 query = (BasicQueryImpl) queryClass.newInstance(); 307 query.init (token); 308 } 309 catch (Exception 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 326 public AbstractStore getStore() { 327 return topLevelQuery.getStore(); 328 } 329 330 336 public QueryScope getScope() { 337 return topLevelQueryScope; 338 } 339 340 346 public PropertyProvider getPropertyProvider() { 347 return topLevelQuery.getPropertyProvider(); 348 } 349 350 356 public boolean isLimitDefined() { 357 return topLevelQuery.isLimitDefined(); 358 } 359 360 366 public int getLimit() { 367 return topLevelQuery.getLimit(); 368 } 369 370 376 public IBasicExpression getExpression() { 377 return topLevelQuery.getExpression(); 378 } 379 380 386 public RequestedProperties requestedProperties() { 387 return topLevelQuery.requestedProperties; 388 } 389 390 398 public String getSlidePath() throws InvalidScopeException { 399 return topLevelQuery.getSlidePath(); 400 } 401 402 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 |