Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Scout/Concepts/TableData"

(Replaced content with "The Scout documentation has been moved to https://eclipsescout.github.io/.")
 
Line 1: Line 1:
{{ScoutPage|cat=Shared}}
+
The Scout documentation has been moved to https://eclipsescout.github.io/.
TableData is the data transfert object for {{ScoutLink|Concepts|Table}}.
+
 
+
== Description ==
+
TableData is contained in a {{ScoutLink|Concepts|FormData}} or in a {{ScoutLink|Concepts|TablePageData}} to hold the content of a {{ScoutLink|Concepts|Table}} (of a {{ScoutLink|Concepts|TableField}} or of a {{ScoutLink|Concepts|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 {{ScoutLink|Concepts|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:
+
<source lang="java">
+
  SQL.selectInto("SELECT PERSON_NR, PERSON_NAME FROM PERSONS INTO :{id}, :{name}", formData.getPersonTable());
+
</source>
+
 
+
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|Required version|The API described here requires version 3.10.0 or bigger. With Mars-M5 (version 4.3.0) this becomes the default}}
+
 
+
{{ScoutJavadoc|AbstractTableFieldBeanData|C}}
+
 
+
This kind of TableData is used by default in TablePageData. It is also possible to use it for a Table Field using the {{ScoutJavadoc|FormData|A}} 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:
+
<source lang="java">
+
@FormData(sdkCommand = SdkCommand.USE, value = AbstractTableFieldBeanData.class, defaultSubtypeSdkCommand = DefaultSubtypeSdkCommand.CREATE)
+
public class TableField extends AbstractTableField {
+
  //... Table inner class.
+
}
+
</source>
+
 
+
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: <source lang="java">@FormData(sdkCommand = SdkCommand.USE, value = AbstractTableFieldBeanData.class, defaultSubtypeSdkCommand = DefaultSubtypeSdkCommand.CREATE)</source>
+
** class declaration: <source lang="java">AbstractTableBeanField<T extends ITable> extends AbstractTableField<T></source>
+
** no table included
+
* AbstractPersonTableField
+
** annotation: <source lang="java">@FormData(sdkCommand = SdkCommand.CREATE, value = AbstractPersonTableFieldData.class)</source>
+
** form-field declaration: <source lang="java">AbstractPersonTableField<T extends PersonTable> extends AbstractTableBeanField<T></source>
+
** table: <source lang="java">PersonTable extends AbstractExtensibleTable</source>
+
** table columns: IdColumn (Long) and NameColumn (String)
+
* PersonWithAgeTableField
+
** no specific FormData annotation
+
** form-field declaration: <source lang="java">PersonWithAgeTableField extends AbstractPersonTableField<PersonWithAgeTable></source>
+
** table: <source lang="java">PersonWithAgeTable extends PersonTable</source>
+
** table columns: AgeColumn (Integer)
+
 
+
=== Example ===
+
<source lang="java">
+
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;
+
    }
+
  }
+
}
+
</source>
+
 
+
=== 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:
+
<source lang="java">
+
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());
+
  }
+
}
+
</source>
+
 
+
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 <tt>AbstractTableRowData.getRowState()</tt>, because the persistance operation depends on the row status:
+
<source lang="java">
+
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;
+
    }
+
  }
+
}
+
</source>
+
 
+
== Array based TableData ==
+
{{ScoutJavadoc|AbstractTableFieldData|C}}
+
 
+
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 <code>AbstractArrayTableField</code> (introduced with Mars-M5) instead of <code>AbstractTableField</code>. You can also work with the @FormData annotation.
+
 
+
<source lang="java">
+
@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");
+
      }
+
    }
+
  }
+
}
+
</source>
+
 
+
 
+
=== Example ===
+
<source lang="java">
+
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;
+
    }
+
  }
+
}
+
</source>
+
 
+
=== 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:
+
<source lang="java">
+
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());
+
  }
+
}
+
</source>
+
 
+
Notice the clearRows() before the for-each loop. This is important if your formData already contains data.
+
 
+
Using the method <tt>AbstractTableFieldData.addRow(Object[])</tt> is not recommended. It is not typed and not safe during refactoring on the client side.
+
 
+
<source lang="java">
+
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[])
+
  }
+
}
+
</source>
+
 
+
When you map the data back in your business object, you might use a switch on <tt>getRowState(int)</tt>, because the persistance operation depends on the row status:
+
 
+
<source lang="java">
+
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;
+
    }
+
  }     
+
}
+
</source>
+
 
+
== See Also ==
+
* {{ScoutLink|Concepts|FormData}}
+
* {{ScoutLink|Concepts|TablePageData}}
+
* {{ScoutLink|Concepts|Shared Plug-In|Shared Plug-In}}
+

Latest revision as of 05:22, 14 March 2024

The Scout documentation has been moved to https://eclipsescout.github.io/.

Back to the top