1 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 52 public abstract class BasicQuery extends SearchQuery implements IBasicQuery { 53 54 58 public static final String NO_QUERY_ELEMENT = "No query element"; 59 60 63 protected IBasicExpressionCompilerProvider expressionCompilerProvider = null; 64 65 69 public static final String SELECT_ELEMENT_MISSING = "Required element <select> not supplied"; 70 71 75 public static final String FROM_ELEMENT_MISSING = "Required element <from> not supplied"; 76 77 81 public static final String PROP_OR_ALLPROP_ELEMENT_MISSING = "Required element <prop> or <allprop> not supplied"; 82 83 private IBasicExpressionFactory contentExpressionFactory; 84 private IBasicExpressionFactory propertiesExpressionFactory; 85 86 87 88 protected Element queryElement; 89 90 91 protected Namespace namespace; 92 93 94 protected QueryScope queryScope; 95 96 97 protected Element whereElement; 98 99 100 protected RequestedProperties requestedProperties; 101 102 103 protected int limit; 104 105 106 protected OrderBy orderBy; 107 108 109 protected boolean limitDefined = false; 110 111 112 protected AbstractStore store; 113 114 115 protected IBasicExpression rootExpression; 116 117 118 protected SlideUri slideUri; 119 120 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 144 public IBasicExpressionFactory getContentExpressionFactory () 145 { 146 if (contentExpressionFactory == null) 147 contentExpressionFactory = 148 store.getContentIndexer().getBasicExpressionFactory(); 149 150 return contentExpressionFactory; 151 } 152 153 154 161 public IBasicExpressionFactory getPropertiesExpressionFactory () 162 { 163 propertiesExpressionFactory = 164 store.getPropertiesIndexer().getBasicExpressionFactory(); 165 166 return propertiesExpressionFactory; 167 } 168 169 170 176 public AbstractStore getStore () { 177 return store; 178 } 179 180 188 public String getSlidePath () throws InvalidScopeException { 189 return slideUri.getSlidePath (queryScope.getHref()); 190 } 191 192 198 public SearchToken getSearchToken (){ 199 return searchToken; 200 } 201 202 208 public PropertyProvider getPropertyProvider () { 209 return propertyProvider; 210 } 211 212 223 public void parseQueryElement (Element basicSearchElement, PropertyProvider propertyProvider) throws BadQueryException { 224 225 queryScope = getScope(basicSearchElement); 226 this.propertyProvider = propertyProvider; 227 228 if (searchToken.getNamespace() != null) { 230 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 249 public abstract void parseQuery (Element basicSearchElement, PropertyProvider propertyProvider) 250 throws BadQueryException; 251 252 261 public abstract SearchQueryResult execute () throws ServiceAccessException; 262 263 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 if (selectElement == null) 284 throw new BadQueryException (SELECT_ELEMENT_MISSING); 285 286 Element fromElement = basicSearchElement.getChild 287 (Literals.FROM, namespace); 288 289 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 (limitElement.getTextTrim()).intValue(); 328 limitDefined = true; 329 } 330 } 331 332 338 public QueryScope getScope () { 339 return queryScope; 340 } 341 342 347 public RequestedProperties requestedProperties () { 348 return requestedProperties; 349 } 350 351 357 public IBasicExpression getExpression () { 358 return rootExpression; 359 } 360 361 362 367 public boolean isLimitDefined () { 368 return limitDefined; 369 } 370 371 376 public int getLimit () { 377 return limit; 378 } 379 380 386 public void setScope (QueryScope queryScope) { 387 this.queryScope = queryScope; 388 } 389 390 396 public OrderBy getOrderBy () { 397 return orderBy; 398 } 399 400 401 407 public String toString () { 408 409 String result = 410 "SELECT [" + requestedProperties + "] FROM [" + queryScope + "] " 411 + "WHERE [" + rootExpression + "]"; 412 413 return result; 414 } 415 416 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 if (fromElement == null) 438 throw new BadQueryException (FROM_ELEMENT_MISSING); 439 440 return new BasicQueryScope (fromElement); 441 } 442 443 450 public static class ExpressionCompilerProvider implements IBasicExpressionCompilerProvider { 451 452 461 public IBasicExpressionCompiler getCompiler(IBasicQuery query, PropertyProvider propertyProvider) throws BadQueryException { 462 return new BasicExpressionCompiler(query, propertyProvider); 463 } 464 } 465 } 466 467 | Popular Tags |