Fixed serialization

This commit is contained in:
Norbi Peti 2016-08-04 13:10:46 +02:00
parent b01b1324eb
commit d6441d90e1
8 changed files with 100 additions and 79 deletions

View file

@ -33,55 +33,56 @@ public class Main {
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(new TypeToken<LoaderCollection<Conversation>>() {
}.getType(), new LoaderCollectionSerializer<Conversation>());
gsonBuilder.registerTypeAdapter(new TypeToken<LoaderCollection<Message>>() {
}.getType(), new LoaderCollectionSerializer<Message>());
gsonBuilder.registerTypeAdapter(new TypeToken<LoaderCollection<MessageChunk>>() {
}.getType(), new LoaderCollectionSerializer<MessageChunk>());
gsonBuilder.registerTypeAdapter(new TypeToken<LoaderCollection<User>>() {
}.getType(), new LoaderCollectionSerializer<User>());
gsonBuilder.registerTypeAdapter(new TypeToken<LoaderRef<Conversation>>() {
}.getType(), new LoaderRefSerializer<Conversation>());
gsonBuilder.registerTypeAdapter(new TypeToken<LoaderRef<MessageChunk>>() {
}.getType(), new LoaderRefSerializer<MessageChunk>());
gsonBuilder.registerTypeAdapter(new TypeToken<LoaderRef<User>>() {
}.getType(), new LoaderRefSerializer<User>());
gson = gsonBuilder.create();
/*User user = new User();
user.setName("asd");
user.setEmail("test@test.com");
User user2 = new User();
user2.setName("Teszt");
user2.setEmail("test2@test.com");
// user = provider.save(user);
// user2 = provider.save(user2);
user.getContacts().add(user2);
user2.getContacts().add(user);
LogManager.getLogger().log(Level.DEBUG, "1st's contact: " + user.getContacts().get(0));
LogManager.getLogger().log(Level.DEBUG, "2nd's contact: " + user2.getContacts().get(0));
Conversation conversation = new Conversation();
conversation.getUsers().add(user);
user.getConversations().add(conversation);
LogManager.getLogger().debug("User: " + user);
conversation.getUsers().add(user2);
LogManager.getLogger().debug("User2: " + user2);
user2.getConversations().add(conversation);
Message msg = new Message();
msg.setSender(user);
msg.setTime(new Date());
msg.setMessage("Teszt 1");
conversation.getMesssages().add(msg);
Message msg2 = new Message();
msg2.setSender(user2);
msg2.setTime(new Date());
msg2.setMessage("Teszt 2");
conversation.getMesssages().add(msg2);
// provider.save(user);
// provider.save(user2);s
User loggedinuser = new User();
loggedinuser.setName("NorbiPeti");
loggedinuser.setSessionid("093b1395-8c31-4f3b-ba67-828a755af92e");
loggedinuser.setEmail("sznp@asd.com");
loggedinuser.getContacts().add(user2);
conversation.getUsers().add(loggedinuser);
loggedinuser.getConversations().add(conversation);
DataManager.save(user);
DataManager.save(user2);
DataManager.save(loggedinuser);
DataManager.save(conversation);*/
/*
* User user = new User(); user.setName("asd");
* user.setEmail("test@test.com"); User user2 = new User();
* user2.setName("Teszt"); user2.setEmail("test2@test.com"); // user
* = provider.save(user); // user2 = provider.save(user2);
* user.getContacts().add(user2); user2.getContacts().add(user);
* LogManager.getLogger().log(Level.DEBUG, "1st's contact: " +
* user.getContacts().get(0));
* LogManager.getLogger().log(Level.DEBUG, "2nd's contact: " +
* user2.getContacts().get(0)); Conversation conversation = new
* Conversation(); conversation.getUsers().add(user);
* user.getConversations().add(conversation);
* LogManager.getLogger().debug("User: " + user);
* conversation.getUsers().add(user2);
* LogManager.getLogger().debug("User2: " + user2);
* user2.getConversations().add(conversation); Message msg = new
* Message(); msg.setSender(user); msg.setTime(new Date());
* msg.setMessage("Teszt 1"); conversation.getMesssages().add(msg);
* Message msg2 = new Message(); msg2.setSender(user2);
* msg2.setTime(new Date()); msg2.setMessage("Teszt 2");
* conversation.getMesssages().add(msg2); // provider.save(user); //
* provider.save(user2);s User loggedinuser = new User();
* loggedinuser.setName("NorbiPeti");
* loggedinuser.setSessionid("093b1395-8c31-4f3b-ba67-828a755af92e")
* ; loggedinuser.setEmail("sznp@asd.com");
* loggedinuser.getContacts().add(user2);
* conversation.getUsers().add(loggedinuser);
* loggedinuser.getConversations().add(conversation);
* DataManager.save(user); DataManager.save(user2);
* DataManager.save(loggedinuser); DataManager.save(conversation);
*/
LogManager.getLogger().log(Level.INFO, "Starting webserver..."); //TODO: Separate IDs for conversations and users
LogManager.getLogger().log(Level.INFO, "Starting webserver..."); // TODO:
// Separate
// IDs
// for
// conversations
// and
// users
HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10);
Reflections rf = new Reflections(
new ConfigurationBuilder().setUrls(ClasspathHelper.forClassLoader(Page.class.getClassLoader()))

View file

@ -12,10 +12,10 @@ import com.google.gson.stream.JsonWriter;
import io.github.norbipeti.chat.server.db.domain.SavedData;
//@SuppressWarnings("rawtypes")
public class LoaderCollectionSerializer extends TypeAdapter<LoaderCollection<? extends SavedData>> {
public class LoaderCollectionSerializer<T extends SavedData> extends TypeAdapter<LoaderCollection<T>> {
@Override
public void write(JsonWriter out, LoaderCollection<? extends SavedData> value) throws IOException {
public void write(JsonWriter out, LoaderCollection<T> value) throws IOException {
out.beginObject();
out.name("items");
new Gson().toJson(value.idlist, new TypeToken<List<Long>>() {
@ -24,10 +24,9 @@ public class LoaderCollectionSerializer extends TypeAdapter<LoaderCollection<? e
out.endObject();
}
// @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked")
@Override
public LoaderCollection<? extends SavedData> read(JsonReader in) throws IOException {
public LoaderCollection<T> read(JsonReader in) throws IOException {
in.beginObject();
in.nextName();
List<Long> list = new Gson().fromJson(in, new TypeToken<List<Long>>() {
@ -36,15 +35,14 @@ public class LoaderCollectionSerializer extends TypeAdapter<LoaderCollection<? e
new Exception("Error: Next isn't \"class\"").printStackTrace();
return null;
}
Class<? extends SavedData> cl;
Class<T> cl;
try {
cl = (Class<? extends SavedData>) Class.forName(in.nextString());
cl = (Class<T>) Class.forName(in.nextString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
LoaderCollection<? extends SavedData> col = new LoaderCollection<SavedData>(
(Class<SavedData>) cl); // TODO
LoaderCollection<T> col = new LoaderCollection<T>(cl); // TODO
col.idlist.addAll(list);
in.endObject();
return col;

View file

@ -1,5 +1,7 @@
package io.github.norbipeti.chat.server.data;
import java.io.Serializable;
import io.github.norbipeti.chat.server.db.domain.SavedData;
/**
@ -16,7 +18,8 @@ import io.github.norbipeti.chat.server.db.domain.SavedData;
* @param <T>
* The type of the stored object
*/
public class LoaderRef<T extends SavedData> {
public class LoaderRef<T extends SavedData> implements Serializable {
private static final long serialVersionUID = 8458570738734235320L;
Class<T> cl;
Long id;

View file

@ -12,14 +12,13 @@ import com.google.gson.stream.JsonWriter;
import io.github.norbipeti.chat.server.db.domain.SavedData;
//@SuppressWarnings("rawtypes")
public class LoaderRefSerializer extends TypeAdapter<LoaderRef<? extends SavedData>> {
public class LoaderRefSerializer<T extends SavedData> extends TypeAdapter<LoaderRef<T>> {
@Override
public void write(JsonWriter out, LoaderRef<? extends SavedData> value) throws IOException {
public void write(JsonWriter out, LoaderRef<T> value) throws IOException {
out.beginObject();
out.name("items");
new Gson().toJson(value.id, new TypeToken<List<Long>>() {
}.getType(), out);
out.name("id");
out.value(value.id);
out.name("class").value(value.cl.getName());
out.endObject();
}
@ -27,24 +26,22 @@ public class LoaderRefSerializer extends TypeAdapter<LoaderRef<? extends SavedDa
// @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked")
@Override
public LoaderRef<? extends SavedData> read(JsonReader in) throws IOException {
public LoaderRef<T> read(JsonReader in) throws IOException {
in.beginObject();
in.nextName();
Long id = new Gson().fromJson(in, new TypeToken<Long>() {
}.getType());
long id = in.nextLong();
if (!in.nextName().equals("class")) {
new Exception("Error: Next isn't \"class\"").printStackTrace();
return null;
}
Class<? extends SavedData> cl;
Class<T> cl;
try {
cl = (Class<? extends SavedData>) Class.forName(in.nextString());
cl = (Class<T>) Class.forName(in.nextString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
LoaderRef<? extends SavedData> col = new LoaderRef<SavedData>(
(Class<SavedData>) cl, id); // TODO
LoaderRef<T> col = new LoaderRef<T>(cl, id); // TODO
in.endObject();
return col;
}

View file

@ -17,21 +17,25 @@ public class Message implements Serializable {
// private Long id;
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER)
// @JoinTable(name="user_message")
private User sender;
private LoaderRef<User> sender;
private Date time;
private String message;
@ManyToOne(fetch = FetchType.EAGER)
// @JoinTable(name="conversation_message")
private LoaderRef<MessageChunk> messagechunk;
public User getSender() {
public LoaderRef<User> getSender() {
return sender;
}
public void setSender(User sender) {
public void setSender(LoaderRef<User> sender) {
this.sender = sender;
}
public void setSender(User sender) {
this.sender = new LoaderRef<User>(sender);
}
public Date getTime() {
return time;
}
@ -56,6 +60,10 @@ public class Message implements Serializable {
this.messagechunk = messagechunk;
}
public void setMessageChunk(MessageChunk messagechunk) {
this.messagechunk = new LoaderRef<MessageChunk>(messagechunk);
}
/*
* public Long getId() { return id; }
*

View file

@ -26,4 +26,8 @@ public class MessageChunk extends SavedData {
public void setConversation(LoaderRef<Conversation> conversation) {
this.conversation = conversation;
}
public void setConversation(Conversation conversation) {
this.conversation = new LoaderRef<Conversation>(conversation);
}
}

View file

@ -11,6 +11,7 @@ import io.github.norbipeti.chat.server.IOHelper;
import io.github.norbipeti.chat.server.data.DataManager;
import io.github.norbipeti.chat.server.db.domain.Conversation;
import io.github.norbipeti.chat.server.db.domain.Message;
import io.github.norbipeti.chat.server.db.domain.MessageChunk;
import io.github.norbipeti.chat.server.db.domain.User;
public class IndexPage extends Page {
@ -46,12 +47,15 @@ public class IndexPage extends Page {
cide.attr("id", "convidp");
cide.text(Long.toString(conv.getId()));
LogManager.getLogger().log(Level.INFO, "Messages: " + conv.getMesssageChunks().size());
for (Message message : conv.getMesssageChunks()) {
Element msgelement = channelmessages.appendElement("div"); //TODO: Save messages in conversation files
Element header = msgelement.appendElement("p");
header.text(message.getSender().getName() + " - " + message.getTime());
Element body = msgelement.appendElement("p");
body.text(message.getMessage());
for (MessageChunk chunk : conv.getMesssageChunks()) { // TODO:
// Reverse
for (Message message : chunk.getMessages()) {
Element msgelement = channelmessages.appendElement("div");
Element header = msgelement.appendElement("p");
header.text(message.getSender().get().getName() + " - " + message.getTime());
Element body = msgelement.appendElement("p");
body.text(message.getMessage());
}
}
return doc;
}, exchange);

View file

@ -12,6 +12,7 @@ import io.github.norbipeti.chat.server.data.DataManager;
import io.github.norbipeti.chat.server.data.LoaderCollection;
import io.github.norbipeti.chat.server.db.domain.Conversation;
import io.github.norbipeti.chat.server.db.domain.Message;
import io.github.norbipeti.chat.server.db.domain.MessageChunk;
import io.github.norbipeti.chat.server.db.domain.User;
public class MessageAjaxPage extends Page {
@ -66,15 +67,20 @@ public class MessageAjaxPage extends Page {
+ conversation + " is not found.</p>", exchange);
return;
}
MessageChunk chunk = new MessageChunk(); // TODO: Automatize
chunk.setConversation(conv);
Message msg = new Message();
msg.setSender(user);
msg.setMessage(message);
msg.setTime(new Date());
msg.setConversation(conv); // TODO: Store relations at one side or both
DataManager.save(msg);
conv.getMesssageChunks().add(msg);
msg.setMessageChunk(chunk); // TODO: Store relations at one side or
// both);
chunk.getMessages().add(msg);
//DataManager.save(chunk); - TODO
conv.getMesssageChunks().add(chunk);
DataManager.save(conv);
LogManager.getLogger().log(Level.DEBUG, "Added conversation's message count: " + conv.getMesssageChunks().size());
LogManager.getLogger().log(Level.DEBUG,
"Added conversation's message count: " + conv.getMesssageChunks().size());
IOHelper.SendResponse(200, "Success", exchange);
}