To create and add a new filter

New filters are created as a com.crystaldecisions.sdk.occa.report.data.IFilter object and added to a report with a new record, saved data, or group selection formula. There are two main ways to set the filter formula:
  • Use the IFilter.setFreeEditingText method.
    This property accepts a string value for the formula.
  • Use the IFilter.setFilterItems method.
    This property accepts a FilterItems collection containing IFieldRangeFilterItem and IOperatorFilterItem objects that represent the individual expressions and operators of a selection formula. The FilterItems collection is the most flexible way to select and change the fields, values, and operators in a formula.
The com.crystaldecisions.sdk.occa.report.application.FilterController.modify method is used to apply the new filter.
In this example, you create a new filter that selects all customer records in a particular region and country. The record selection formula represented by the filter is {Customer.Region} = "CA" AND {Customer.Country} = "USA" and is created using a FilterItems collection.
  1. Retrieve the DatabaseController object.
    DatabaseController databaseController = rcd.getDatabaseController();
  2. Find the database fields to filter on.
    This example assumes that the report has a table called Customer that contains Region and Country fields. However, you can iterate through all database fields in the report, display them to a user through a UI, and allow the user to select the fields to filter on.
    Tables tables = databaseController.getDatabase().getTables();
    ITable customerTable = tables.findTableByAlias("Customer");
    Fields<IField> fields = customerTable.getDataFields();
    IField regionField = fields.findField("{Customer.Region}", FieldDisplayNameType.formulaName, java.util.Locale.US);
    IField countryField = fields.findField("{Customer.Country}", FieldDisplayNameType.formulaName, java.util.Locale.US);
  3. Create a new IFieldRangeFilterItem object that represents the primitive expression for filtering by customer region.
    The filter expression to assemble is {Customer.Region} = "CA".
      a. Instantiate the new IFieldRangeFilterItem object.
      IFieldRangeFilterItem regionFieldFilterItem = new FieldRangeFilterItem();
      b. Use the setRangeField method to set the database field representing customer region as the field to filter on.
      regionFieldFilterItem.setRangeField(regionField);
      c. Use the setOperation method to set an equality operator between the field and value of the expression.
      regionFieldFilterItem.setOperation(SelectionOperation.equal);
      d. Create a new Values collection, and add a constant value representing the country to filter on.
      Values regionValues = new Values();
      IConstantValue regionValue = new ConstantValue();
      regionValue.setValue("CA");
      regionValues.add(regionValue);
      e. Use the setValues method to set the value of the region to filter on.
      regionFieldFilterItem.setValues(regionValues);
  4. Create a new IFieldRangeFilterItem object that represents the primitive expression for filtering by customer country.
    The filter expression to assemble is {Customer.Country} = "USA".
    IFieldRangeFilterItem countryFieldFilterItem = new FieldRangeFilterItem();
    countryFieldFilterItem.setRangeField(countryField);
    countryFieldFilterItem.setOperation(SelectionOperation.equal);
    Values countryValues = new Values();
    IConstantValue countryValue = new ConstantValue();
    countryValue.setValue("USA");
    countryValues.add(countryValue);
    countryFieldFilterItem.setValues(countryValues);
  5. Create a new IOperatorFilterItem object that represents an "AND" operator between the region and country field filters.
    IOperatorFilterItem operatorFilterItem = new OperatorFilterItem();
    operatorFilterItem.setOperator("AND");
  6. Create a new FilterItems collection and add the IFieldRangeItem and IOperatorFilterItem objects in the correct order to represent the desired record selection formula.
    The position of items in the FilterItems collection represents the order of operations in the formula. Therefore, you must add the region filter expression first, followed by the "AND" operator, and finally the country filter expression.
    FilterItems filterItems = new FilterItems();
    filterItems.add(regionFieldFilterItem);
    filterItems.add(operatorFilterItem);
    filterItems.add(countryFieldFilterItem);
  7. Create a new Filter object to represent the record filter.
    Filter newRecordFilter = new Filter();
    newRecordFilter.setName("Region and Country record selection filter");
  8. Use the setFilterItems method to set the newly created FilterItems object to the filter.
    newRecordFilter.setFilterItems(filterItems);
  9. Retrieve the DataDefController object.
    DataDefController dataDefController = rcd.getDataDefController();
  10. Retrieve the FilterController object representing record selection.
    FilterController recordFilterController = dataDefController.getRecordFilterController();
  11. Call the modify method to apply the newly created record filter to the report.
    recordFilterController.modify(newRecordFilter);
Example: 
The following code creates a new filter that selects all customer records in a particular region and country, and applies the filter to the report. The record selection formula represented by the filter is {Customer.Region} = "CA" AND {Customer.Country} = "USA"
void createNewRecordFilter (ReportClientDocument rcd)  throws ReportSDKException
{
  DatabaseController databaseController = rcd.getDatabaseController();
  Tables tables = databaseController.getDatabase().getTables();
  ITable customerTable = tables.findTableByAlias("Customer");
  Fields<IField> fields = customerTable.getDataFields();
  IField regionField = fields.findField("{Customer.Region}", FieldDisplayNameType.formulaName, java.util.Locale.US);
  IField countryField = fields.findField("{Customer.Country}", FieldDisplayNameType.formulaName, java.util.Locale.US);
 
  IFieldRangeFilterItem regionFieldFilterItem = new FieldRangeFilterItem();
  regionFieldFilterItem.setRangeField(regionField);
  regionFieldFilterItem.setOperation(SelectionOperation.equal);
  Values regionValues = new Values();
  IConstantValue regionValue = new ConstantValue();
  regionValue.setValue("CA");
  regionValues.add(regionValue);
  regionFieldFilterItem.setValues(regionValues);
 
  IFieldRangeFilterItem countryFieldFilterItem = new FieldRangeFilterItem();
  countryFieldFilterItem.setRangeField(countryField);
  countryFieldFilterItem.setOperation(SelectionOperation.equal);
  Values countryValues = new Values();
  IConstantValue countryValue = new ConstantValue();
  countryValue.setValue("USA");
  countryValues.add(countryValue);
  countryFieldFilterItem.setValues(countryValues);

  IOperatorFilterItem operatorFilterItem = new OperatorFilterItem();
  operatorFilterItem.setOperator("AND");
 
  FilterItems filterItems = new FilterItems();
  filterItems.add(regionFieldFilterItem);
  filterItems.add(operatorFilterItem);
  filterItems.add(countryFieldFilterItem);
         
  Filter newRecordFilter = new Filter();
  newRecordFilter.setName("Region and Country record selection filter");
  newRecordFilter.setFilterItems(filterItems);
 
  DataDefController dataDefController = rcd.getDataDefController();
  FilterController recordFilterController = dataDefController.getRecordFilterController();
  recordFilterController.modify(newRecordFilter);
}
This list includes the classes used by the sample code:
  • com.crystaldecisions.sdk.occa.report.application.DatabaseController
  • com.crystaldecisions.sdk.occa.report.application.DataDefController
  • com.crystaldecisions.sdk.occa.report.application.FilterController
  • com.crystaldecisions.sdk.occa.report.data.IFieldRangeFilterItem
  • com.crystaldecisions.sdk.occa.report.data.FieldRangeFilterItem
  • com.crystaldecisions.sdk.occa.report.data.FieldDisplayNameType
  • com.crystaldecisions.sdk.occa.report.data.IField
  • com.crystaldecisions.sdk.occa.report.data.Field
  • com.crystaldecisions.sdk.occa.report.data.Fields
  • com.crystaldecisions.sdk.occa.report.data.IConstantValue
  • com.crystaldecisions.sdk.occa.report.data.ConstantValue
  • com.crystaldecisions.sdk.occa.report.data.Values
  • com.crystaldecisions.sdk.occa.report.data.IOperatorFilterItem
  • com.crystaldecisions.sdk.occa.report.data.OperatorFilterItem
  • com.crystaldecisions.sdk.occa.report.data.IFilter
  • com.crystaldecisions.sdk.occa.report.data.Filter
  • com.crystaldecisions.sdk.occa.report.data.FilterItem
  • com.crystaldecisions.sdk.occa.report.data.FilterItems
  • com.crystaldecisions.sdk.occa.report.data.SelectionOperation
  • com.crystaldecisions.sdk.occa.report.data.ITable
  • com.crystaldecisions.sdk.occa.report.data.Tables
  • com.crystaldecisions.sdk.occa.report.lib.ReportSDKException