Jump to: navigation, search

Scout/Concepts/TableData


Scout
Wiki Home
Website
DownloadGit
Community
ForumsBlogTwitter
Bugzilla
Bugzilla

TableData is the data transfert object for Table.

Description

TableData is contained in a FormData or in a TablePageData to hold the content of a Table (of a TableField or of a TablePage).

There are two different kinds of TableData:

  • Array based TableData
  • Bean based TableData

Concrete TableData extends abstract classes (different one depending on the type of table data, see below) and are generated by the SDK given the concrete table (in the TableField or in the TablePage). The SDK decide the type TableData given the FormData anotation.

The bean based tableData was introduced with Scout 3.10.0. It makes inheritance possible: the TableData has the same inheritance graph as the Table. Inheritance is not supported by Array-based table Data.

Usage

Client side, you can import and export the table content of a TableField with importFormData() and exportFormData(). For a table in a TablePage, there is a importPageData() function.

Server side, you can use the built in SQL Support from Eclipse Scout. Reading from a Database:

  SQL.selectInto("SELECT PERSON_NR, PERSON_NAME FROM PERSONS INTO :{id}, :{name}", formData.getPersonTable());

You can also fill and read the TableData by ourself. In this case, the syntax will depend on the type of table data you have chosen(array based or bean based).

Bean based TableData

Important.png
Required version
The API described here requires version 3.10.0 or bigger. With Mars-M5 (version 4.3.0) this becomes the default


C obj.pngAbstractTableFieldBeanData

This kind of TableData is used by default in TablePageData. It is also possible to use it for a Table Field using the A obj.pngFormData to control the generation of FormData.

In bean based TableData, a row is represented by a bean. Like in any bean, a java field, a setter and a getter is generated for each column.

Using bean based TableData for TableField

To use it in FormData for Table Field, the table field needs to be annotated with:

@FormData(sdkCommand = SdkCommand.USE, value = AbstractTableFieldBeanData.class, defaultSubtypeSdkCommand = DefaultSubtypeSdkCommand.CREATE)
public class TableField extends AbstractTableField {
  //... Table inner class.
}

If you work with template (for a table field), you need to add an empty TableField template (that does not contain any Table) on top of your hierarchy to indicate that want to use bean based table data:

  • AbstractTableBeanField
    • annotation:
      @FormData(sdkCommand = SdkCommand.USE, value = AbstractTableFieldBeanData.class, defaultSubtypeSdkCommand = DefaultSubtypeSdkCommand.CREATE)
    • class declaration:
      AbstractTableBeanField<T extends ITable> extends AbstractTableField<T>
    • no table included
  • AbstractPersonTableField
    • annotation:
      @FormData(sdkCommand = SdkCommand.CREATE, value = AbstractPersonTableFieldData.class)
    • form-field declaration:
      AbstractPersonTableField<T extends PersonTable> extends AbstractTableBeanField<T>
    • table:
      PersonTable extends AbstractExtensibleTable
    • table columns: IdColumn (Long) and NameColumn (String)
  • PersonWithAgeTableField
    • no specific FormData annotation
    • form-field declaration:
      PersonWithAgeTableField extends AbstractPersonTableField<PersonWithAgeTable>
    • table:
      PersonWithAgeTable extends PersonTable
    • table columns: AgeColumn (Integer)

Example

public static class PersonTable extends AbstractTableFieldBeanData {
 
  private static final long serialVersionUID = 1L;
 
  public PersonTable() {
  }
 
  @Override
  public PersonTableRowData addRow() {
    return (PersonTableRowData) super.addRow();
  }
 
  @Override
  public PersonTableRowData addRow(int rowState) {
    return (PersonTableRowData) super.addRow(rowState);
  }
 
  @Override
  public PersonTableRowData createRow() {
    return new PersonTableRowData();
  }
 
  @Override
  public Class<? extends AbstractTableRowData> getRowType() {
    return PersonTableRowData.class;
  }
 
  @Override
  public PersonTableRowData[] getRows() {
    return (PersonTableRowData[]) super.getRows();
  }
 
  @Override
  public PersonTableRowData rowAt(int index) {
    return (PersonTableRowData) super.rowAt(index);
  }
 
  public void setRows(PersonTableRowData[] rows) {
    super.setRows(rows);
  }
 
  public static class PersonTableRowData extends AbstractTableRowData {
 
    private static final long serialVersionUID = 1L;
    public static final String id = "id";
    public static final String name = "name";
    private Long m_id;
    private String m_name;
 
    public TableRowData() {
    }
 
    public Long getId() {
      return m_id;
    }
 
    public void setId(Long id) {
      m_id = id;
    }
 
    public String getName() {
      return m_name;
    }
 
    public void setName(String name) {
      m_name = name;
    }
  }
}

Usage example

In the server you might want to map the data from an object (like the Person POJO in this case) to the TableData in the form data. Here is how you can do it:

private void mapPersonsToFormData(AFormData formData, List<Person> persons) {
  PersonTable table = formData.getPersonTable();
  table.clearRows();
  for (Person person : persons) {
    PersonTableRowData personRow = table.addRow();
    personRow.setId(person.getId());
    personRow.setName(person.getName());
  }
}

Notice the clearRows() before the for-each loop. This is important if your formData already contains data.

When you map the data back in your business object, you might use a switch on AbstractTableRowData.getRowState(), because the persistance operation depends on the row status:

private void mapPersonsFromFormData(AFormData formData, List<Person> persons) {
  PersonTable table = formData.getPersonTable();
  for (PersonTableRowData personRow : table.getRows()) {
    switch (personRow.getRowState()) {
      case ITableHolder.STATUS_INSERTED:
        //insert a new person corresponding to the row:
        insertPerson(persons, personRow.getId(), personRow.getName());
        break;
      case ITableHolder.STATUS_DELETED:
        //delete the person corresponding to the row:
        deletePerson(persons, personRow.getId());
        break;
      case ITableHolder.STATUS_UPDATED:
        //modify the person corresponding to the row:
        updatePerson(persons, personRow.getId(), personRow.getName());
        break;
      case ITableHolder.STATUS_NON_CHANGED:
      default:
        //Do nothing
        break;
    }
  }
}

Array based TableData

C obj.pngAbstractTableFieldData

This kind of Table Data is was the default in FormData until Mars-M5 and is not available for TablePage. It stores the row content as array, and provides some getter and setter methods to ensure that the row content is properly type casted.

Using array based TableData for TableField

The easiest way to do so is to extend from the class AbstractArrayTableField (introduced with Mars-M5) instead of AbstractTableField. You can also work with the @FormData annotation.

@FormData(value = AbstractTableFieldData.class, sdkCommand = SdkCommand.USE, defaultSubtypeSdkCommand = DefaultSubtypeSdkCommand.CREATE)
public class CitiesTableField extends AbstractTableField<CitiesTableField.Table> {
 
  @Override
  protected String getConfiguredLabel() {
    return TEXTS.get("Person");
  }
 
  @Order(10.0)
  public class Table extends AbstractExtensibleTable {
 
    public CityColumn getCityColumn() {
      return getColumnSet().getColumnByClass(CityColumn.class);
    }
 
    public ZipCodeColumn getZipCodeColumn() {
      return getColumnSet().getColumnByClass(ZipCodeColumn .class);
    }
 
    @Order(10.0)
    public class ZipCodeColumn extends AbstractStringColumn {
 
      @Override
      protected String getConfiguredHeaderText() {
        return TEXTS.get("ZipCode");
      }
    }
 
    @Order(20.0)
    public class CityColumn extends AbstractStringColumn {
 
      @Override
      protected String getConfiguredHeaderText() {
        return TEXTS.get("City");
      }
    }
  }
}


Example

public static class PersonTable extends AbstractTableFieldData {
 
  private static final long serialVersionUID = 1L;
  public static final int ID_COLUMN_ID = 0;
  public static final int NAME_COLUMN_ID = 1;
 
  public PersonTable() {
  }
 
  public Long getId(int row) {
    return (Long) getValueInternal(row, ID_COLUMN_ID);
  }
 
  public void setId(int row, Long id) {
    setValueInternal(row, ID_COLUMN_ID, id);
  }
 
  public String getName(int row) {
    return (String) getValueInternal(row, NAME_COLUMN_ID);
  }
 
  public void setName(int row, String name) {
    setValueInternal(row, NAME_COLUMN_ID, name);
  }
 
  @Override
  public int getColumnCount() {
    return 2;
  }
 
  @Override
  public Object getValueAt(int row, int column) {
    switch (column) {
      case ID_COLUMN_ID:
        return getId(row);
      case NAME_COLUMN_ID:
        return getName(row);
      default:
        return null;
    }
  }
 
  @Override
  public void setValueAt(int row, int column, Object value) {
    switch (column) {
      case ID_COLUMN_ID:
        setId(row, (Long) value);
        break;
      case NAME_COLUMN_ID:
        setName(row, (String) value);
        break;
    }
  }
}

Usage example

In the server you might want to map the data from an object (like the Person POJO in this case) to the TableData in the form data. Here is how you can do it:

private void mapPersonToFormData(AFormData formData, List<Person> persons) {
  PersonTable table = formData.getPersonTable();
  table.clearRows();
  for (Person person : persons) {
    int rowNum = table.addRow();
    table.setId(rowNum, person.getId());
    table.setName(rowNum, person.getName());
  }
}

Notice the clearRows() before the for-each loop. This is important if your formData already contains data.

Using the method AbstractTableFieldData.addRow(Object[]) is not recommended. It is not typed and not safe during refactoring on the client side.

private void mapPersonToFormData(AFormData formData, List<Person> persons) {
  //This pattern is not recommended:
  PersonTable table = formData.getPersonTable();
  table.clearRows();
  for (Person person : persons) {
    table.addRow(new Object[]{person.getId(), person.getName()}); //Do not use AbstractTableFieldData#addRow(Object[])
  }
}

When you map the data back in your business object, you might use a switch on getRowState(int), because the persistance operation depends on the row status:

private void mapPersonsFromFormData(AFormData formData, List<Person> persons) {
  PersonTable table = formData.getPersonTable();
  for (int rowNum = 0; rowNum < table.getRowCount(); rowNum++) {
    switch (table.getRowState(rowNum)) {
      case ITableHolder.STATUS_INSERTED:
        //insert a new person corresponding to the row:
        insertPerson(persons, table.getId(rowNum), table.getName(rowNum));
        break;
      case ITableHolder.STATUS_DELETED:
        //delete the person corresponding to the row:
        deletePerson(persons, table.getId(rowNum));
        break;
      case ITableHolder.STATUS_UPDATED:
        //modify the person corresponding to the row:
        updatePerson(persons, table.getId(rowNum), table.getName(rowNum));
        break;
      case ITableHolder.STATUS_NON_CHANGED:
      default:
        //Do nothing
        break;
    }
  }      
}

See Also