English

How To: Use subfields with Java Connector

Summary

When you do a query by putting a Filter on a map layer, you are limiting the returned records to only those satisfying the query. You may also want to limit the returned fields to only those that are of interest to you. The way to do this is to call Filter.addSubField() and pass the names of the fields desired, as in the following code:

Code:
filter.addSubField("AREA");
filter.addSubField("STATE_FIPS");
filter.setWhereExpression("AREA =0.23612");
tLayer.setFilterObject(filter);

Where filter is an instance of com.esri.aims.mtier.model.map.layer.query.Filter, tLayer is the target layer the filter applies to, and the "AREA" and "STATE_FIPS" are the two fields whose values you'd like to know.

In the response, only these two fields will be returned (plus field "#SHAPE#" at the start, which is always returned). However, tLayer.getRecordset().getTableDesc().getCount() is still the total number of fields in the target layer.

Procedure

One of the ways to deal with it is to:

  1. Build an array of strings to remember the fields that are passed to Filter.addSubField and are thus really returned:

    Code:
    String[] subfields={"AREA","STATE_FIPS"};
    filter.addSubField(subfields[0]);
    filter.addSubField(subfields[1]);
    ...
    tLayer.setFilterObject(filter);

  2. Display only these fields:

    Code:
    out.println("<table border='1'><tr>");
    for (int j=0; j<subfields.length; j++) {
    out.println("<td>"+subfields[j] + "</td>");
    }
    out.println("</tr>");
    for (int i=0; i < tLayer.getRecordset().getCount(); i++) {
    out.println("<tr>");
    for (int j=0; j<subfields.length; j++) {
    out.println("<td>"+tLayer.getRecordset().getRecords(i).getFieldValue(j) + "</td>");
    }
    }
    out.println("</tr></td></table>");

    Note:
    1. The order of the fields in the returned recordset is fixed, so they're in the same order as they appear in the original map service. So even if your code is like:

    Code:
    filter.addSubField("STATE_FIPS");
    filter.addSubField("AREA);

    In the returned recordset, field "AREA" still appears before field "STATE_FIPS". Therefore, good practice would be to organize your array of field names in the same order as they appear in the map service.

    2. Function getSubFieldsCount() in Filter also returns the correct number of fields, including field "#SHAPE#". In the above sample code, filter.getSubFieldsCount() would return 3.