N
N
Nicholas Unknown2020-02-17 20:45:07
Java
Nicholas Unknown, 2020-02-17 20:45:07

How to transfer the model from the server to the client if they are different for the client and server?

Good day to all!

I am writing a messenger as a thesis in Java EE.

The server has a ChatRoom model

public class ChatRoom {

    private long id;
    private String name;
    private Map<String, Session> clients;
    private List<User> members;
    MessageEncoder encoder;
    OnlineUsersStorage onlineUsersStorage = OnlineUsersStorage.getInstance();

    public ChatRoom(long id, String name, List<User> members) {
        clients = new HashMap<>();
        encoder = new MessageEncoder();
        this.id = id;
        this.name = name;
        this.members = members;
    }

    //Setters
    public void setId(long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMembers(List<User> members) {
        this.members = members;
    }

    public void setClients(Map<String, Session> clients) {
        this.clients = clients;
    }


    //Getters
    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<User> getMembers() {
        return members;
    }

    public Map<String, Session> getClients() {
        return clients;
    }


    //Methods
    public void sendMessage(Message message){

        try {
            String encodedMessage = encoder.encode(message);
            List<Session> sessions = onlineUsersStorage.getUserSession(members);
            for (Session s : sessions) s.getBasicRemote().sendText(encodedMessage);

        }catch (IOException | EncodeException e){
            e.printStackTrace();
        }

    }

}


And the client also has ChatRoom

public class ChatRoom {

    private long id;
    private String name;
    private List<User> members;
    private List<Message> messages;

    public ChatRoom(long id, String name, List<User> members, List<Message> messages) {
        this.id = id;
        this.name = name;
        this.members = members;
        this.messages = messages;
    }

    //Getters
    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<User> getMembers() {
        return members;
    }

    public List<Message> getMessages() {
        return messages;
    }

    //Setters
    public void setId(long id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setMembers(List<User> members) {
        this.members = members;
    }

    public void setMessages(List<Message> messages) {
        this.messages = messages;
    }

    //Methods
    public Message getLastMessage(){
        return messages.isEmpty() ? null : messages.get(messages.size() - 1);
    }
}


The trouble is that they have different implementations.

The client does not need any fields like
Map<String, Session> clients;
 MessageEncoder encoder;
OnlineUsersStorage onlineUsersStorage


And the server does not need to have ChatRoom inside the
List<Message> messages;

Client during the program operation sends a request to the server with a request to provide it with an ArrayList with the client's ChatRooms objects.

How can this be done on the server? Producing two kinds of ChatRoom models ala ClientChatRoom/ServerChatRoom on the server seems like a stupid decision.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmtm, 2020-02-18
@OMGcoder

the internal objects of the server and the client are not connected at all, even if the fields completely match the
special DTO objects (data transfer) on the client, and this is not the same as the entities from the client or server models

N
nApoBo3, 2020-02-18
@nApoBo3

Use an intermediate model. Generate local objects from it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question