Show TOC Start of Content Area

Background documentation Using Query Filter in Application Service  Locate the document in its SAP Library structure

Task

You can filter entity instancequeries by setting comparison conditions.

To do this in your application service, use the following class:

Class: com.sap.caf.rt.bol.util.QueryFilter

DC: caf/runtime/impl

Remote Persistency

For remote persistency, you need to correctly map the query filter attributes to an external operation, according to its semantics.

For more information about attributes mapping, see Mapping Service Attributes.

Single and Multiple Query Filters

When you create a findBy operation, you can choose the attributes for which query filters are applied.

For each attribute, you can also choose if it will have single (by default) or multiple query filters.

For more information, see Operations Tab Page.

Note

If you have chosen multiple query filters, the generated Java code will contain java.util.List  instead of QueryFilter. In that case, you need to create an instance of java.util.List and fill it with instances of QueryFilter.

If you work with multiple query filters, consider the following:

     Among the query filters of one attribute, a logical OR rule is applied (only one condition needs to be satisfied).

     Among the different query filter groups, a logical AND rule is applied (all conditions need to be satisfied).

Syntax

You can apply the following QueryFilter constructors:

Syntax

QueryFilter(valueLow); // Default condition ("==")

QueryFilter(valueLow, condition);

QueryFilter(valueLow, valueHigh); // Between condition ("<>")

For each query filter, you should enter one or two comparison values and a comparison conditions.

The following examples illustrate how to use them:

Example

QueryFilter("STRING_SAMPLE");

// Condition is true if the String value is equal (default) to "STRING_SAMPLE".

QueryFilter(0, ">");

// Condition is true if the int value is greater than 0.

QueryFilter(3.5, 4.5);

// Condition is true if the float value is between 3.5 and 4.5.

QueryFilter(42, "!=");

// Condition is true if the number is not equal to 42

QueryFilter("STRING_SAMPLE", "!=");

// CAUTION: Condition is true if the String value is between "STRING_SAMPLE" and "!=". This does not mean that the condition is true if the String value is not equal to "STRING_SAMPLE"  

 

The last example shows you that you cannot use all conditions for all value types.

For more information, see the table below.

Conditions and Types For Which They Are Available

Condition and Description

boolean

String

Others

== (default)

Must be equal to valueLow.

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

!=

Must not be equal to valueLow.

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

<

Must be less than valueLow.

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

>

Must be greater than valueLow.

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

<=

Must be less or equal to valueLow.

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

>=

Must be greater or equal to valueLow.

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

<>

Must be between valueLow and valueHigh.

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

This graphic is explained in the accompanying text

Custom Query Filter

You can also create a query filter using a suitable constructor (for example, the default one).

To do this:

...

       1.      Create a query filter using a suitable constructor (for example, the default one).

       2.      Indicate the value data type (can be int, long, float, double, BigDecimal, Date, String, boolean).

       3.      Set values for condition, valueLow and valueHigh (if the condition is "<>").

For example, if you need a query filter with condition "!=" for String (see the example above), use the following code:

Example

QueryFilter qf = new QueryFilter();

// the default constructor

qf.isString = true;    

// Set the type of the filter.

qf.datatype = QueryFilter.DATATYPE_STRING;

// Redundant with the previous line, but still must be set.

qf.condition = “!=”;

// Set condition

qf.valueLow = “STRING_SAMPLE”;   

// Use a variable corresponding to qf.datatype

 

When your condition is "<>", you need to set valueHigh, too.

The following query filter does the same as QueryFilter(5, 10):

Example

QueryFilter qf = new QueryFilter();

qf.isInt = true;         

qf.datatype = QueryFilter.DATATYPE_INT;    

qf.condition = “<>”;

qf.intValueLow = 5; 

qf.intValueHigh = 10;

 

End of Content Area