Search code examples
javagwtgxt

How to get data from another EntityProxy in GXT ColumnConfig?


Here is the thing: I have an application to manage user's tickets. I have 2 basic classes : Ticket and User. Using GXT I have some ColumnConfig class like this:

ColumnConfig<TicketProxy, String> dateColumn = new ColumnConfig<TicketProxy, String>(
        new ValueProvider<TicketProxy, String>() {
            public String getValue(TicketProxy object) {
                Date initialDate = object.getInitialDate();
                String date = "";
                if (initialDate != null) {
                    date = dtFormat.format(initialDate);
                }
                return date;
            }

            public void setValue(TicketProxy object, String initialDate) {
                if (object instanceof TicketProxy) {
                    object.setInitialDate(dtFormat.parse(initialDate));
                }
            }

            public String getPath() {
                return "initialDate";
            }
        }, 70, "Date");
columnsChamado.add(dateColumn);

but I want to get some data from UserProxy class, some like this:

ColumnConfig<UserProxy, String> userRoomColumn = new ColumnConfig<UserProxy, String>(
                new ValueProvider<UserProxy, String>() {
                    public String getValue(UserProxy object) {
                        String userRoom = object.getUserRoom();
                        String room = "";
                        if (userRoom != null) {
                            room = userRoom;
                        }
                        return room;
                    }

                    public void setValue(UserProxy object, String userRoom) {
                        if (object instanceof UserProxy) {
                            object.setUserRoom(userRoom);
                        }
                    }

                    public String getPath() {
                        return "userRoom";
                    }
                }, 70, "User's Room");
        columnsChamado.add(userRoomColumn);

But GWT doesn't allow me to change the "Proxy" parameter to another class in the same ColumnConfig.

How can I get data from other Proxy class in this ColumnConfig?

I use GXT 3.0 (Sencha) + Hibernate.

Proxy classes: BaseEntityProxy:

package com.acme.ccc.shared;

import com.acme.ccc.server.locator.CCCLocator;
import com.acme.db.base.DatabaseObject;

import com.google.web.bindery.requestfactory.shared.EntityProxy;
import com.google.web.bindery.requestfactory.shared.ProxyFor;
import com.google.web.bindery.requestfactory.shared.SkipInterfaceValidation;

@SkipInterfaceValidation
@ProxyFor(value = DatabaseObject.class, locator = CCCLocator.class)
public interface BaseEntityProxy extends EntityProxy {
    Long getId();
    Long getVersion();
    void setId(Long id);
    void setVersion(Long version);
}

TicketProxy:

package com.acme.ccc.shared.entityproxy;

import java.util.Date;
import java.util.List;

import com.acme.ccc.db.Ticket;
import com.acme.ccc.server.locator.CCCLocator;
import com.acme.ccc.shared.BaseEntityProxy;

import com.google.web.bindery.requestfactory.shared.ProxyFor;


@ProxyFor(value = Ticket.class, locator = CCCLocator.class)
public interface TicketProxy extends BaseEntityProxy {
    Date getPrazo();
    void setPrazo(Date prazo);

    TicketTipoProxy getTicketTipo();
    void setTicketTipo(TicketTipoProxy chamadoTipo);

    CanalOrigemProxy getCanalOrigem();
    void setCanalOrigem(CanalOrigemProxy canalOrigem);

    UserProxy getUser();
    void setUser(UserProxy user);

    CategoriaProxy getPedidoTipo();
    void setPedidoTipo(CategoriaProxy pedidoTipo);

    Date getInitialDate();
    void setInitialDate(Date dt);

    Long getTotal();
    void setTotal(Long total);

}

UserProxy:

package com.acme.ccc.shared.entityproxy;

import java.util.List;

import com.acme.ccc.db.User;
import com.acme.ccc.server.locator.CCCLocator;
import com.acme.ccc.shared.BaseEntityProxy;

import com.google.web.bindery.requestfactory.shared.ProxyFor;

@ProxyFor(value = User.class, locator = CCCLocator.class)
public interface UserProxy extends BaseEntityProxy {
    String getName();
    String getUserRoom();
    Long getTotal();

    void setName(String name);
    void setUserRoom(Sting room)
    void setTotal(Long total);
}

Solution

  • An gxt grid is able to show the data only of one data type. If you put a single TicketProxy row, how do you expect to access a user object?

    If you want to display both Tickets and Users independently (so a row is either a Ticket OR a User), you have to use BaseEntityProxy in your Grid: Grid<BaseEntityProxy>. Then you can define your columns as ColumnConfig<BaseEntityProxy, ?> and check the type within your getters and setter:

    List<ColumnConfig<BaseEntityProxy, ?>> columnsChamado = new ArrayList<ColumnConfig<BaseEntityProxy, ?>>();
    ColumnConfig<BaseEntityProxy, String> dateColumn = new ColumnConfig<BaseEntityProxy, String>(
            new ValueProvider<BaseEntityProxy, String>() {
    
                private final DateTimeFormat dtFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_FULL);
    
                public String getValue(BaseEntityProxy object) {
                    Date initialDate =  ((TicketProxy) object).getInitialDate();
                    String date = "";
                    if (initialDate != null) {
                        date = dtFormat.format(initialDate);
                    }
                    return date;
                }
    
                public void setValue(BaseEntityProxy object, String initialDate) {
                    if (object instanceof TicketProxy) {
                        ((TicketProxy) object).setInitialDate(dtFormat.parse(initialDate));
                    }
                }
    
                public String getPath() {
                    return "initialDate";
                }
            }, 70, "Date");
    
    columnsChamado.add(dateColumn);
    
    ColumnConfig<BaseEntityProxy, String> userRoomColumn = new ColumnConfig<BaseEntityProxy, String>(
            new ValueProvider<BaseEntityProxy, String>() {
                public String getValue(BaseEntityProxy object) {
                    String userRoom = ((UserProxy)object).getUserRoom();
                    String room = "";
                    if (userRoom != null) {
                        room = userRoom;
                    }
                    return room;
                }
    
                public void setValue(BaseEntityProxy object, String userRoom) {
                    if (object instanceof UserProxy) {
                        ((UserProxy)object).setUserRoom(userRoom);
                    }
                }
    
                public String getPath() {
                    return "userRoom";
                }
            }, 70, "User's Room");
    columnsChamado.add(userRoomColumn);
    
    ColumnModel<BaseEntityProxy> cm = new ColumnModel<BaseEntityProxy>(columnsChamado);
    

    If, on the other hand, you want one grid row to display a User AND a Ticket, you have to use a wrapper class:

    class TicketWithUserProxy extends BaseEntityProxy{
    
        private UserProxy userProxy;
        private TicketProxy ticketProxy;
    
        public UserProxy getUserProxy() {
            return userProxy;
        }
    
        public void setUserProxy(UserProxy userProxy) {
            this.userProxy = userProxy;
        }
    
        public TicketProxy getTicketProxy() {
            return ticketProxy;
        }
    
        public void setTicketProxy(TicketProxy ticketProxy) {
            this.ticketProxy = ticketProxy;
        }
    }
    

    and setup your grid (Grid<TicketWithUserProxy>) accordingly:

    List<ColumnConfig<TicketWithUserProxy, ?>> columnsChamado = new ArrayList<ColumnConfig<TicketWithUserProxy, ?>>();
    
    ColumnConfig<TicketWithUserProxy, String> dateColumn = new ColumnConfig<TicketWithUserProxy, String>(
            new ValueProvider<TicketWithUserProxy, String>() {
    
                private final DateTimeFormat dtFormat = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_FULL);
    
                public String getValue(TicketWithUserProxy object) {
                    Date initialDate =  object.getTicketProxy().getInitialDate();
                    String date = "";
                    if (initialDate != null) {
                        date = dtFormat.format(initialDate);
                    }
                    return date;
                }
    
                public void setValue(TicketWithUserProxy object, String initialDate) {
                    object.getTicketProxy().setInitialDate(dtFormat.parse(initialDate));
                }
    
                public String getPath() {
                    return "initialDate";
                }
            }, 70, "Date");
    
    columnsChamado.add(dateColumn);
    
    ColumnConfig<TicketWithUserProxy, String> userRoomColumn = new ColumnConfig<TicketWithUserProxy, String>(
            new ValueProvider<TicketWithUserProxy, String>() {
                public String getValue(TicketWithUserProxy object) {
                    String userRoom = object.getUserProxy().getUserRoom();
                    String room = "";
                    if (userRoom != null) {
                        room = userRoom;
                    }
                    return room;
                }
    
                public void setValue(TicketWithUserProxy object, String userRoom) {
                    object.getUserProxy().setUserRoom(userRoom);
                }
    
                public String getPath() {
                    return "userRoom";
                }
            }, 70, "User's Room");
    columnsChamado.add(userRoomColumn);
    ColumnModel<TicketWithUserProxy> cm = new ColumnModel<TicketWithUserProxy>(columnsChamado);