Changing the default query operator in JHeadstart Search Areas
By Sandra Muller on Jul 30, 2008
In JHeadstart Quick Search and Advanced Search, for each search item you can define a Query Operator. This operator determines how to query the data. Examples are contains, endsWith and greaterThan. See the JHeadstart Developer's Guide, section 7.2.4 for instructions how to change the Query Operator for an item. You can even let the application user choose the query operator.
By default, the ‘StartsWith’ operator is used for String items. In all other cases the equality operator is used.
But what if you want all your String items to use a different query operator? You don't want to explicitly set the query operator for every item.
The solution is: modify the JHeadstart Generator Template that creates the Search Beans, and add a managed property to the managed bean to set the defaultStringOperator to another value.
The next question becomes: what value to use? The 'StartsWith' operator applies the 'like' operator, and adds a wildcard % at the end of the search term. What if you want to keep on using the 'like' operator, but without the added wildcard?
You could check the allowable values of the Query Operator property in the JHeadstart Application Definition (at Item level). Unfortunately it does not list a "like" operator. You could choose the "is" operator, but then any wildcards typed in by the end user would be taken literally. You want the use of wildcards, but only if typed in!
Section 22.214.171.124 of the JHeadstart Developer's Guide explains that JHeadstart's QueryCondition class is responsible for translating the query operator names to the appropriate SQL operators and wildcard usage. Taking a look at the source code of oracle.jheadstart.model.QueryCondition (in JDeveloper, select a JHeadstart project, click Control+Minus and type in QueryCondition), sheds some light on the issue. If the query operator is "startsWith", the SQL operator is set to "like" and the wildcardUsage is set to "suffix". If the operator does not match any of the predefined values, no transformation is applied, and the passed in value is set directly as SQL operator. So, to achieve that the SQL operator is set to "like" and no wildcard is added automatically, we must set the query operator to "like".
To summarize, here are the steps to apply:
- Copy the JHeadstart template default/misc/facesConfig/searchBean.vm to your location for custom templates, for example custom/searchBeanDefaultStringOperator.vm. For more information about using custom templates, see the JHeadstart Developer's Guide, section 4.7.2.
- Add the following lines just before the last line that contains the end of the bean( </managed-bean> ):
- Modify your config/customTemplateBindings.jtp file (you may have used a different name) and add a line:
SEARCH_BEAN=custom/searchBeanDefaultStringOperator.vmIf you don't have such a file yet, you can create one in the config folder. It can be empty besides the above line.
- If you didn't have a customTemplateBindings.jtp yet, you will also have to specify in your Application Definition file(s) that you want to use this configuration file. You do that at Service level, in the Templates tab, the first property. Set Template Binding File (.jtp) to customTemplateBindings.jtp.
- Run the JHeadstart Application Generator for the Application Definition(s).