1

I have a GWT 2.4 project using a CellTable. It has columns like this (actually more):

LastName --- FirstName --- Departments
Smith        Tom           Research, Management

The names I get from a "User" object which is created on the server from my Database.

The DB looks like this:

users:
  userID
  firstName
  lastName

departments:
  departmentID
  departmentName

user_in_department:
  userID
  departmentID

So what is the best way to get my departments show up in the table?

At the moment I fetch the User-list and the Department-list from the server using a RPC.

I thought about a 3rd RPC to get me the user-department relation and then match the names to the users on the client. What would be a good way to match that btw?

But even if I had the departments matched to my users, how would I add that info to the table?

For the names I can just do that:

    TextColumn<User> firstNameColumn = new TextColumn<User>() {

        @Override
        public String getValue(User object) {
            return object.getFirstName();
        }
    };

But as the departments aren't stored in the "User" object, I have no idea how to get them in the correct column and row.

I hope I've explained my issue good enough for you to understand :)

Cassio
  • 147
  • 1
  • 8

2 Answers2

0

Assuming that your User object has a list of departments like so:

public ArrayList<Department> getDepartments() {
  // ...
}

You can create a column to list the departments like so:

TextColumn<User> departmentsColumn = new TextColumn<User>() {

    @Override
    public String getValue(User object) {
        StringBuilder departments = new StringBuilder();
        for(int i=0; i < object.getDepartments().size(); i++) {
            if (i>0) {
              departments.append(",");
            }
            departments.append(object.getDepartments().get(i).getDepartmentName());
        }
        return departments.toString();
    }
};
Strelok
  • 50,229
  • 9
  • 102
  • 115
  • Ok so you are also suggesting to put the department list into the User object to solve this issue? Wouldn't the User objects become more and more "heavy" when adding such info? – Cassio Mar 14 '12 at 16:46
  • Yes I am suggesting that. When retrieving tabular data like that you should strive to include the information required to display your table in one shot and not make numerous ajax calls to retrive parts of the objects you want to display. Especially when the user doesn't need to perform an action to view that data. – Strelok Mar 14 '12 at 21:21
0

In addition to the first answer: since a CellTable is parameterized to display a list of objects of a given type in its rows that object should include the user name as well as a list of departments. You'll have to somehow make that data available through the User object, or create a new one (e.g. UserDepartmentDTO) as the elements in the underlying model.