Search code examples
javadatabasegwtrpccelltable

GWT RPC match data for CellTable


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


Solution

  • 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();
        }
    };