Scout/Concepts/TableData

From Eclipsepedia

Jump to: navigation, search


Scout
Wiki Home
Website
DownloadGit
Community
ForumsBlogTwitter
Bugzilla
Bugzilla

TableData is the data transfert object for Table.

Contents

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 bean based tableData was introduced with Scout 3.10.0. It makes inheritance possible: the TableData has the same inheritance graph as the Table.

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, the SQL Support from Eclipse Scout works with TableData. bug 419140 (work in progress)

Array based TableData

C obj.pngAbstractTableFieldData

This kind of Table Data is used by default in FormData 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.

Example

public static class Table 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 Table() {
  }
 
  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 use a switch on getRowState(int), because the persistance operation depends on the row status:

//table is the concrete AbstractTableFieldData from the FormData. 
for (int i = 0; i < table.getRowCount(); i++) {
  switch (table.getRowState(i)) {
    case ITableHolder.STATUS_INSERTED:
      //insert the row
      break;
    case ITableHolder.STATUS_DELETED:
      //delete the row
      break;
    case ITableHolder.STATUS_UPDATED:
      //modify the row
      break;
    case ITableHolder.STATUS_NON_CHANGED:
    default:
      //Do nothing
      break;
  }
}

Bean based TableData

Important.png
Required version
The API described here requires version 3.10.0 or bigger.


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 Table extends AbstractTableFieldBeanData {
 
  private static final long serialVersionUID = 1L;
 
  public Table() {
  }
 
  @Override
  public TableRowData addRow() {
    return (TableRowData) super.addRow();
  }
 
  @Override
  public TableRowData addRow(int rowState) {
    return (TableRowData) super.addRow(rowState);
  }
 
  @Override
  public TableRowData createRow() {
    return new TableRowData();
  }
 
  @Override
  public Class<? extends AbstractTableRowData> getRowType() {
    return TableRowData.class;
  }
 
  @Override
  public TableRowData[] getRows() {
    return (TableRowData[]) super.getRows();
  }
 
  @Override
  public TableRowData rowAt(int index) {
    return (TableRowData) super.rowAt(index);
  }
 
  public void setRows(TableRowData[] rows) {
    super.setRows(rows);
  }
 
  public static class TableRowData 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;
    }
  }
}

See Also