Writing a Custom Result Set Filter

Writing a custom result set filter allows you to define the application processing logic you require. A custom filter requires that you write a Java filter class and save the compiled Java class file to location that is accessible from Unwired Workspace.

A custom result set filter allows you to process the record set returned by the attribute properties you configure so that it can be better consumed by the device client application you create. Sometimes, a result set returned from a data source requires unique processing and the filter you create performs that function before the information is downloaded to the client.

Note: For more information on the javadocs for the Filter API, go to either:

%SUP_HOME%\Servers\UnwiredServer\tomcat\webapps\onepage\docs\javadoc\index.html

or

http://<host>:port/onepage/docs/javadoc/index.html

  1. (Required) Create a record set filter class that implements the com.sybase.uep.eis.ResultSetFilter interface.

    This interface defines how a custom filter for the data is called.

    For example, the following code segment sets the package name and import the required classes:
    package com.mycompany.myname;
    import java.sql.ResultSet;
    import java.util.Map;
  2. (Recommended) Implement the sybase.uep.eis.ResultSetFilterMetaData interface on your filter class as required by your business requirements.

    Without this interface, you need to execute a chain of mobile business object operations and filters with real data before you can get actual results of the output columns and their data types. This can impact information on the data source that may need to be reverted in that system. By implementing this interface, the operation does not need to be executed first. Instead, the getMetaData method is used to obtain the necessary column or data type information.

    The following example sets the package name but uses a different combination of classes than in the example for step 1:

    package com.mycompany.myname;
    import java.sql.ResultSetMetaData;
    import java.util.Map;
      
  3. Depending on the interfaces you implement, call the appropriate method.

    ResultSetFilter performs the filtering of data in the first option documented in step 1. Each filter defines a distinct set of arguments for its own use. Therefore, use the only arguments with the specific filter that defines these arguments in the getArguments() method, rather than use all filters and data source operations.

    The ResultSet passed in contains the grid data. Data should be considered to be read only. Do not use operations that change or transform data.

    public interface ResultSetFilter {
       ResultSet filter(ResultSet in, Map<String, Object> arguments) throws
         Exception;
       Map <String, Class> getArguments();
    }
    ResultSetFilterMetaData should then be used to show the how filtered data of the second option documented in step 1 appears. Use this interface to avoid executing a data source operation to generate a sample data set returned and thereby avoid extraneous executions.
    public interface ResultSetFilterMetaData {
       ResultSetMetaData getMetaData(ResultSetMetaData in, Map<String,
          Object> arguments) throws Exception;
    }
    Note: If the filter returns different columns depending on the argument values supplied, then the filter may not work reliably. If arguments affect the metadata, ensure that the arguments have constant values in the final mobile business object definition, so the schema does not change dynamically.
  4. Implement the class you have created, defining any custom processing logic at this time.

    For example, to create a filter that adds an integer valued column called "rowCount" as the first column in the result set, you might implement the class as follows:

    import java.sql.ResultSet;
    import java.sql.ResultSetMetaData;
    import java.util.HashMap;
    import java.util.Map;
    
    import com.sun.rowset.CachedRowSetImpl;
    import javax.sql.RowSetMetaData;
    
    import javax.sql.rowset.RowSetMetaDataImpl;
    
    public class AddRowCountFilter
        implements com.sybase.eis.ResultSetFilter, com.sybase.eis.ResultSetFilterMetaData
    {
    
          public ResultSet filter(ResultSet in, Map<String, Object> arguments)
                throws Exception
             {
                CachedRowSetImpl crsi = new CachedRowSetImpl();
                ResultSetMetaData rsmd = in.getMetaData();
                crsi.setMetaData(createMetaData(rsmd));
                in.moveToCurrentRow();
                in.beforeFirst();
    
                for (int i = 0; in.next(); i++)
                {
                    crsi.moveToInsertRow();
                    crsi.updateInt(1, i + 1);
    
                for (int j = 0; j < rsmd.getColumnCount(); j++)
                {
                     crsi.updateObject(j + 2, in.getObject(j + 1));
                }
                crsi.insertRow();
             }
             crsi.moveToCurrentRow();
             crsi.beforeFirst();
             
             return crsi;
    }
    
         public Map<String, Class> getArguments()
         {
              return new HashMap<String, Class>();
         }
    
          public ResultSetMetaData getMetaData(ResultSetMetaData in, Map<String, Object> arguments)
                   throws Exception
    
          {
               return createMetaData(in);
          }
    
           private RowSetMetaData createMetaData(ResultSetMetaData rsmd) throws Exception
           {
                RowSetMetaData md = new RowSetMetaDataImpl();
                md.setColumnCount(rsmd.getColumnCount() + 1);
                md.setColumnLabel(1, "rowCount");
                md.setColumnName(1, "rowCount");
                md.setColumnType(1, java.sql.Types.INTEGER);
                
                for (int i = 0; i < rsmd.getColumnCount(); i++)
                {
                    md.setColumnLabel(i + 2, rsmd.getColumnLabel(i + 1));
                    md.setColumnName(i + 2, rsmd.getColumnName(i + 1));
                    md.setColumnType(i + 2, rsmd.getColumnType(i + 1));
                }
                 return md;
            }
    }
  5. Save the classes you have created to an accessible Unwired WorkSpace location. This allows you to select the class when you configure result set filters for your mobile business object.
Related concepts
Result Set Filters


Created September 17, 2009. Send feedback on this help topic to Sybase Technical Publications: pubs@sybase.com