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"

(Array based TableData)
(Add more usage example.)
Line 11: Line 11:
 
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).
 
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.
+
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 ===
 
=== 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.
 
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)
+
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).
  
 
== Array based TableData ==
 
== Array based TableData ==
Line 25: Line 30:
 
=== Example ===
 
=== Example ===
 
<source lang="java">
 
<source lang="java">
public static class Table extends AbstractTableFieldData {
+
public static class PersonTable extends AbstractTableFieldData {
  
 
   private static final long serialVersionUID = 1L;
 
   private static final long serialVersionUID = 1L;
Line 31: Line 36:
 
   public static final int NAME_COLUMN_ID = 1;
 
   public static final int NAME_COLUMN_ID = 1;
  
   public Table() {
+
   public PersonTable() {
 
   }
 
   }
  
Line 82: Line 87:
  
 
=== Usage example ===
 
=== Usage example ===
In the server, you might use a switch on <tt>getRowState(int)</tt>, because the persistance operation depends on the row status:
+
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">
 
<source lang="java">
//table is the concrete AbstractTableFieldData from the FormData.
+
private void mapPersonToFormData(AFormData formData, List<Person> persons) {
for (int i = 0; i < table.getRowCount(); i++) {
+
  PersonTable table = formData.getPersonTable();
   switch (table.getRowState(i)) {
+
   table.clearRows();
     case ITableHolder.STATUS_INSERTED:
+
  for (Person person : persons) {
      //insert the row
+
     int rowNum = table.addRow();
      break;
+
     table.setId(rowNum, person.getId());
     case ITableHolder.STATUS_DELETED:
+
     table.setName(rowNum, person.getName());
      //delete the row
+
      break;
+
    case ITableHolder.STATUS_UPDATED:
+
      //modify the row
+
      break;
+
     case ITableHolder.STATUS_NON_CHANGED:
+
    default:
+
      //Do nothing
+
      break;
+
 
   }
 
   }
 +
}
 +
</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>
 
</source>
Line 140: Line 179:
 
=== Example ===
 
=== Example ===
 
<source lang="java">
 
<source lang="java">
public static class Table extends AbstractTableFieldBeanData {
+
public static class PersonTable extends AbstractTableFieldBeanData {
  
 
   private static final long serialVersionUID = 1L;
 
   private static final long serialVersionUID = 1L;
  
   public Table() {
+
   public PersonTable() {
 
   }
 
   }
  
 
   @Override
 
   @Override
   public TableRowData addRow() {
+
   public PersonTableRowData addRow() {
     return (TableRowData) super.addRow();
+
     return (PersonTableRowData) super.addRow();
 
   }
 
   }
  
 
   @Override
 
   @Override
   public TableRowData addRow(int rowState) {
+
   public PersonTableRowData addRow(int rowState) {
     return (TableRowData) super.addRow(rowState);
+
     return (PersonTableRowData) super.addRow(rowState);
 
   }
 
   }
  
 
   @Override
 
   @Override
   public TableRowData createRow() {
+
   public PersonTableRowData createRow() {
     return new TableRowData();
+
     return new PersonTableRowData();
 
   }
 
   }
  
 
   @Override
 
   @Override
 
   public Class<? extends AbstractTableRowData> getRowType() {
 
   public Class<? extends AbstractTableRowData> getRowType() {
     return TableRowData.class;
+
     return PersonTableRowData.class;
 
   }
 
   }
  
 
   @Override
 
   @Override
   public TableRowData[] getRows() {
+
   public PersonTableRowData[] getRows() {
     return (TableRowData[]) super.getRows();
+
     return (PersonTableRowData[]) super.getRows();
 
   }
 
   }
  
 
   @Override
 
   @Override
   public TableRowData rowAt(int index) {
+
   public PersonTableRowData rowAt(int index) {
     return (TableRowData) super.rowAt(index);
+
     return (PersonTableRowData) super.rowAt(index);
 
   }
 
   }
  
   public void setRows(TableRowData[] rows) {
+
   public void setRows(PersonTableRowData[] rows) {
 
     super.setRows(rows);
 
     super.setRows(rows);
 
   }
 
   }
  
   public static class TableRowData extends AbstractTableRowData {
+
   public static class PersonTableRowData extends AbstractTableRowData {
  
 
     private static final long serialVersionUID = 1L;
 
     private static final long serialVersionUID = 1L;
Line 210: Line 249:
 
}
 
}
 
</source>
 
</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>
 +
  
 
== See Also ==
 
== See Also ==

Revision as of 08:18, 25 May 2014

The Scout documentation has been moved to https://eclipsescout.github.io/. TableData is the data transfert object for The Scout documentation has been moved to https://eclipsescout.github.io/..

Description

TableData is contained in a The Scout documentation has been moved to https://eclipsescout.github.io/. or in a The Scout documentation has been moved to https://eclipsescout.github.io/. to hold the content of a The Scout documentation has been moved to https://eclipsescout.github.io/. (of a The Scout documentation has been moved to https://eclipsescout.github.io/. or of a The Scout documentation has been moved to https://eclipsescout.github.io/.).

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. 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).

Array based TableData

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

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 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;
    }
  }      
}

Bean based TableData

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


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

This kind of TableData is used by default in TablePageData. It is also possible to use it for a Table Field using the The Scout documentation has been moved to https://eclipsescout.github.io/. 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;
    }
  }
}


See Also

Back to the top