简体   繁体   中英

bean array eclipse scout sample

I am learning Eclipse Scout... I have connected to Sql server, fetching data using Object[][]...now, I want to fetch data using beans, beanarray holder... I dont know the process...

I have created bean Users!
I have populated bean using service, using this example: http://www.eclipse.org/forums/index.php/t/310526/

So can someone explain how to use beans in scout, to populate table, or form...

  1. Make a bean example: users
  2. Fill the bean in service example: get user data from users table
  3. populate table using that bean...

tnx

Java POJO (bean)

If you are working with plain old java object (POJO) like this:

public class User {
    private String firstName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

You can populate an array of those POJO like this:

public User[] loadAll() throws ProcessingException {
  BeanArrayHolder<User> beansArray = new BeanArrayHolder<User>(User.class);

  SQL.selectInto(" select first_name, last_name " +
      " from users " +
      " into :{FirstName}, :{LastName} ", beansArray);

  return beansArray.getBeans();
}

To populate your table, you need to do it by hand. For example on the client side:

for (User user : beansArray.getBeans()) {
  ITableRow row = getTable().createRow();
  getTable().getNameColumn().setValue(row, user.getLastName());
  getTable().getFirstNameColumn().setValue(row, user.getFirstName());
  getTable().addRow(row, true);
}

A mapping server side is also possible. But in this case, you should definitively consider to use table data (see the next section)


Table data

You should ensure that you are using bean based TableData. Read this answer to know how you can differentiate table based TableData and bean based TableData .

Assuming you have a UserTableField like this in your Form:

@Order(10.0)
@FormData(sdkCommand = FormData.SdkCommand.USE, value = AbstractTableFieldBeanData.class, defaultSubtypeSdkCommand = FormData.DefaultSubtypeSdkCommand.CREATE)
public class UserTableField extends AbstractTableField<UserTableField.Table> {

  @Order(10.0)
  public class Table extends AbstractExtensibleTable {

    public LastNameColumn getLastNameColumn() {
      return getColumnSet().getColumnByClass(LastNameColumn.class);
    }

    public FirstNameColumn getFirstNameColumn() {
      return getColumnSet().getColumnByClass(FirstNameColumn.class);
    }

    @Order(10.0)
    public class FirstNameColumn extends AbstractStringColumn {

      @Override
      protected String getConfiguredHeaderText() {
        return TEXTS.get("FirstName");
      }
    }

    @Order(20.0)
    public class LastNameColumn extends AbstractStringColumn {

      @Override
      protected String getConfiguredHeaderText() {
        return TEXTS.get("LastName");
      }
    }
  }
}

You should be able to do something like that in your service:

UserTableRowData rowData = formData.getUserTable().addRow();
rowData.setFirstName("John");
rowData.setLastName("Smith");

Instead of adding the rows manualy, if you want to have a SQL query to populate the table, you can do something like that:

BeanArrayHolder<User> beansArray = new BeanArrayHolder<User>(User.class);

SQL.selectInto(" select first_name, last_name " +
      " from users " +
      " into :{UserTable.FirstName}, :{UserTable.LastName} ", formData);

It works the same way for TablePageData, see an example in our tutorial:

MiniCrm Tutorial > Write the first page > Load data on the server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM