com.trg.search
Class BaseSearchProcessor

java.lang.Object
  extended by com.trg.search.BaseSearchProcessor
Direct Known Subclasses:
JPASearchProcessor

public abstract class BaseSearchProcessor
extends java.lang.Object

This class provides two methods for generating query language to fulfill an ISearch.

  1. generateQL() - is used for getting the actual search results.
  2. generateRowCountQL() - is used for getting just the number of results.
Both methods return a query language sting and a list of values for filling named parameters. For example the following query and parameter list might be returned:
 select _it from com.example.Cat _it
   where _it.age > :p1 and _it.name != :p2
   
 parameter list: [3, 'Mittens']
 
This is an abstract class. A subclass must be used to implement individual query languages. Currently only HQL query language is supported ( com.trg.dao.hibernate.HibernateSearchToQLProcessor). The that implementation could be used for EQL query language as well with no or minor modifications.


Nested Class Summary
protected static class BaseSearchProcessor.AliasNode
           
protected static class BaseSearchProcessor.SearchContext
           
 
Field Summary
protected  java.util.regex.Pattern INJECTION_CHECK
          Regex pattern for a valid property name/path.
protected  MetadataUtil metadataUtil
           
protected  int qlType
           
protected static int QLTYPE_EQL
           
protected static int QLTYPE_HQL
           
protected static java.lang.String ROOT_PATH
           
protected  java.lang.String rootAlias
           
 
Constructor Summary
protected BaseSearchProcessor(int qlType, MetadataUtil metaDataUtil)
           
 
Method Summary
protected  Filter addExplicitNullChecks(Filter filter)
          Used by negate(Filter).
protected  void applyFetches(BaseSearchProcessor.SearchContext ctx, java.util.List<java.lang.String> fetches, java.util.List<Field> fields)
          Apply the fetch list to the alias tree in the search context.
protected  java.util.List<java.lang.String> checkAndCleanFetches(java.util.List<java.lang.String> fetches)
           Check for injection attack in property strings.
protected  java.util.List<Field> checkAndCleanFields(java.util.List<Field> fields)
           Check for injection attack in property strings.
protected  java.util.List<Filter> checkAndCleanFilters(java.util.List<Filter> filters)
           Check for injection attack in property strings.
protected  java.util.List<Sort> checkAndCleanSorts(java.util.List<Sort> sorts)
           Check for injection attack in property strings.
protected  java.lang.String filterToQL(BaseSearchProcessor.SearchContext ctx, Filter filter)
          Recursively generate the QL fragment for a given search filter option.
protected  java.lang.String generateFromClause(BaseSearchProcessor.SearchContext ctx, boolean doEagerFetching)
          Internal method for generating from clause.
protected  java.lang.String generateJoins(BaseSearchProcessor.SearchContext ctx, boolean doEagerFetching)
          Internal method for generating the join portion of the from clause.
protected  java.lang.String generateOrderByClause(BaseSearchProcessor.SearchContext ctx, java.util.List<Sort> sorts)
          Internal method for generating order by clause.
 java.lang.String generateQL(java.lang.Class<?> entityClass, ISearch search, java.util.List<java.lang.Object> paramList)
          Generate the QL string for a given search.
 java.lang.String generateRowCountQL(java.lang.Class<?> entityClass, ISearch search, java.util.List<java.lang.Object> paramList)
          Generate the QL string that will query the total number of results from a given search (paging is ignored).
protected  java.lang.String generateSelectClause(BaseSearchProcessor.SearchContext ctx, java.util.List<Field> fields, boolean distinct)
          Internal method for generating the select clause based on the fields of the given search.
protected  java.lang.String generateSimpleAllOrSome(BaseSearchProcessor.SearchContext ctx, java.lang.String property, Filter filter, java.lang.String operation)
           In the case of simple ALL/SOME/NONE filters, a simpler hql syntax is used (which is also compatible with collections of values).
protected  java.lang.String generateSubquery(BaseSearchProcessor.SearchContext ctx, java.lang.String property, Filter filter)
          Generate a QL string for a subquery on the given property that uses the given filter.
protected  java.lang.String generateWhereClause(BaseSearchProcessor.SearchContext ctx, java.util.List<Filter> filters, boolean isDisjunction)
          Internal method for generating where clause for given search.
protected  BaseSearchProcessor.AliasNode getAlias(BaseSearchProcessor.SearchContext ctx, java.lang.String path, boolean setFetch)
          Given a full path to an entity (ex. department.manager), return the alias to reference that entity (ex. a4_manager).
 Filter getFilterFromExample(java.lang.Object example)
           
 Filter getFilterFromExample(java.lang.Object example, ExampleOptions options)
           
 MetadataUtil getMetadataUtil()
          The MetadataUtil used by this search processor.
protected  java.lang.String getPathRef(BaseSearchProcessor.SearchContext ctx, java.lang.String path)
          Given a full path to a property (ex. department.manager.salary), return the reference to that property that uses the appropriate alias (ex.
protected  Filter negate(Filter filter)
          Return a filter that negates the given filter.
protected  java.lang.String param(BaseSearchProcessor.SearchContext ctx, java.lang.Object value)
          Add value to paramList and return the named parameter string ":pX".
protected  java.lang.Object prepareValue(java.lang.Class<?> rootClass, java.lang.String property, java.lang.Object value, boolean isCollection)
          Convert a property value to the expected type for that property.
protected  void securityCheckProperty(java.lang.String property)
          Used by securityCheck() to check a property string for injection attack.
 void setRootAlias(java.lang.String alias)
          This is the string used to represent the root entity of the search within the query.
protected  java.lang.String[] splitPath(BaseSearchProcessor.SearchContext ctx, java.lang.String path)
          Split a path into two parts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

QLTYPE_HQL

protected static int QLTYPE_HQL

QLTYPE_EQL

protected static int QLTYPE_EQL

qlType

protected int qlType

metadataUtil

protected MetadataUtil metadataUtil

rootAlias

protected java.lang.String rootAlias

ROOT_PATH

protected static final java.lang.String ROOT_PATH
See Also:
Constant Field Values

INJECTION_CHECK

protected java.util.regex.Pattern INJECTION_CHECK
Regex pattern for a valid property name/path.

Constructor Detail

BaseSearchProcessor

protected BaseSearchProcessor(int qlType,
                              MetadataUtil metaDataUtil)
Method Detail

getMetadataUtil

public MetadataUtil getMetadataUtil()
The MetadataUtil used by this search processor. This can only be set in the constructor.


setRootAlias

public void setRootAlias(java.lang.String alias)
This is the string used to represent the root entity of the search within the query. The default value is "_it". It may be necessary to use a different alias if there are entities in the data model with the name or property "_it".


generateQL

public java.lang.String generateQL(java.lang.Class<?> entityClass,
                                   ISearch search,
                                   java.util.List<java.lang.Object> paramList)
Generate the QL string for a given search. Fill paramList with the values to be used for the query. All parameters within the query string are specified as named parameters ":pX", where X is the index of the parameter value in paramList.


generateRowCountQL

public java.lang.String generateRowCountQL(java.lang.Class<?> entityClass,
                                           ISearch search,
                                           java.util.List<java.lang.Object> paramList)
Generate the QL string that will query the total number of results from a given search (paging is ignored). Fill paramList with the values to be used for the query. All parameters within the query string are specified as named parameters ":pX", where X is the index of the parameter value in paramList. NOTE: Returns null if column operators are used in the search. Such a search will always return 1 row.


generateSelectClause

protected java.lang.String generateSelectClause(BaseSearchProcessor.SearchContext ctx,
                                                java.util.List<Field> fields,
                                                boolean distinct)
Internal method for generating the select clause based on the fields of the given search.


applyFetches

protected void applyFetches(BaseSearchProcessor.SearchContext ctx,
                            java.util.List<java.lang.String> fetches,
                            java.util.List<Field> fields)
Apply the fetch list to the alias tree in the search context.


generateFromClause

protected java.lang.String generateFromClause(BaseSearchProcessor.SearchContext ctx,
                                              boolean doEagerFetching)
Internal method for generating from clause. This method should be called after generating other clauses because it relies on the aliases they create. This method takes every path that is called for in the other clauses and makes it available as an alias using left joins. It also adds join fetching for properties specified by fetches if doEagerFetching is true. NOTE: When using eager fetching, applyFetches() must be executed first.


generateJoins

protected java.lang.String generateJoins(BaseSearchProcessor.SearchContext ctx,
                                         boolean doEagerFetching)
Internal method for generating the join portion of the from clause. This method should be called after generating other clauses because it relies on the aliases they create. This method takes every path that is called for in the other clauses and makes it available as an alias using left joins. It also adds join fetching for properties specified by fetches if doEagerFetching is true . NOTE: When using eager fetching, applyFetches() must be executed first.


generateOrderByClause

protected java.lang.String generateOrderByClause(BaseSearchProcessor.SearchContext ctx,
                                                 java.util.List<Sort> sorts)
Internal method for generating order by clause. Uses sort options from search.


generateWhereClause

protected java.lang.String generateWhereClause(BaseSearchProcessor.SearchContext ctx,
                                               java.util.List<Filter> filters,
                                               boolean isDisjunction)
Internal method for generating where clause for given search. Uses filter options from search.


filterToQL

protected java.lang.String filterToQL(BaseSearchProcessor.SearchContext ctx,
                                      Filter filter)
Recursively generate the QL fragment for a given search filter option.


generateSubquery

protected java.lang.String generateSubquery(BaseSearchProcessor.SearchContext ctx,
                                            java.lang.String property,
                                            Filter filter)
Generate a QL string for a subquery on the given property that uses the given filter. This is used by SOME, ALL and NONE filters.

Parameters:
ctx - - a new context just for this sub-query
property - - the property of the main query that points to the collection on which to query
filter - - the filter to use for the where clause of the sub-query

generateSimpleAllOrSome

protected java.lang.String generateSimpleAllOrSome(BaseSearchProcessor.SearchContext ctx,
                                                   java.lang.String property,
                                                   Filter filter,
                                                   java.lang.String operation)

In the case of simple ALL/SOME/NONE filters, a simpler hql syntax is used (which is also compatible with collections of values). Simple filters include ALL/SOME/NONE filters with exactly one sub-filter where that filter applies to the elements of the collection directly (as opposed to their properties) and the operator is =, !=, <, <=, >, or >=.

For example:

 Filter.some("some_collection_of_strings", Filter.equal("", "Bob")
 Filter.all("some_collection_of_numbers", Filter.greaterThan(null, 23)
 
If the filter meets these criteria as a simple ALL/SOME/NONE filter, the QL string for the filter will be returned. If not, null is returned.

Parameters:
ctx - - the context of the SOME/ALL/NONE filter
property - - the property of the SOME/ALL/NONE filter
filter - - the sub-filter that is the value of the SOME/ALL/NONE filter
operation - - a string used to fill in the collection operation. The value should be either "some" or "all".

prepareValue

protected java.lang.Object prepareValue(java.lang.Class<?> rootClass,
                                        java.lang.String property,
                                        java.lang.Object value,
                                        boolean isCollection)
Convert a property value to the expected type for that property. Ex. a Long to and Integer.

Parameters:
isCollection - true if the value is a collection of values, for example with IN and NOT_IN operators.
Returns:
the converted value.

negate

protected Filter negate(Filter filter)
Return a filter that negates the given filter.


addExplicitNullChecks

protected Filter addExplicitNullChecks(Filter filter)
Used by negate(Filter). There's a complication with null values in the database so that !(x == 1) is not the opposite of (x == 1). Rather !(x == 1 and x != null) is the same as (x == 1). This method applies the null check explicitly to all filters included in the given filter tree.


param

protected java.lang.String param(BaseSearchProcessor.SearchContext ctx,
                                 java.lang.Object value)
Add value to paramList and return the named parameter string ":pX".


getPathRef

protected java.lang.String getPathRef(BaseSearchProcessor.SearchContext ctx,
                                      java.lang.String path)
Given a full path to a property (ex. department.manager.salary), return the reference to that property that uses the appropriate alias (ex. a4_manager.salary).


splitPath

protected java.lang.String[] splitPath(BaseSearchProcessor.SearchContext ctx,
                                       java.lang.String path)
Split a path into two parts. The first part will need to be aliased. The second part will be a property of that alias. For example: (department.manager.salary) would return [department.manager, salary].


getAlias

protected BaseSearchProcessor.AliasNode getAlias(BaseSearchProcessor.SearchContext ctx,
                                                 java.lang.String path,
                                                 boolean setFetch)
Given a full path to an entity (ex. department.manager), return the alias to reference that entity (ex. a4_manager). If there is no alias for the given path, one will be created.


checkAndCleanFields

protected java.util.List<Field> checkAndCleanFields(java.util.List<Field> fields)
  1. Check for injection attack in property strings.
  2. The field list may not contain nulls.


checkAndCleanFetches

protected java.util.List<java.lang.String> checkAndCleanFetches(java.util.List<java.lang.String> fetches)
  1. Check for injection attack in property strings.
  2. Remove null fetches from the list.


checkAndCleanSorts

protected java.util.List<Sort> checkAndCleanSorts(java.util.List<Sort> sorts)
  1. Check for injection attack in property strings.
  2. Remove null sorts from the list.


checkAndCleanFilters

protected java.util.List<Filter> checkAndCleanFilters(java.util.List<Filter> filters)
  1. Check for injection attack in property strings.
  2. Check for values that are incongruent with the operator.
  3. Remove null filters from the list.
  4. Simplify out junctions (and/or) that have only one sub-filter.
  5. Remove filters that require sub-filters but have none (and/or/not/some/all/none)


securityCheckProperty

protected void securityCheckProperty(java.lang.String property)
Used by securityCheck() to check a property string for injection attack.


getFilterFromExample

public Filter getFilterFromExample(java.lang.Object example)

getFilterFromExample

public Filter getFilterFromExample(java.lang.Object example,
                                   ExampleOptions options)


Copyright © 2008-2010 eBM WebSourcing. All Rights Reserved.