From bbc8121968bf5bf6aeeaf8cab2bf1fb5405effda Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Thu, 11 Aug 2016 12:28:36 +0200 Subject: [PATCH] Message listener in progress (along with a refactoring) --- pages/index.html | 3 +- pages/js/index.js | 1 - pages/js/receivemessage.js | 7 ++++ pages/js/sendmessage.js | 4 +-- pages/js/utils.js | 33 +++++++++++-------- .../chat/server/db/domain/Conversation.java | 7 ++-- .../chat/server/db/domain/Message.java | 31 ++++++++++++++--- .../chat/server/db/domain/MessageChunk.java | 6 ++-- .../chat/server/db/domain/SavedData.java | 5 ++- .../norbipeti/chat/server/db/domain/User.java | 7 ++-- .../norbipeti/chat/server/io/IOHelper.java | 2 +- .../norbipeti/chat/server/page/IndexPage.java | 2 +- .../server/page/ReceiveMessageAjaxPage.java | 15 ++------- 13 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 pages/js/receivemessage.js diff --git a/pages/index.html b/pages/index.html index be4c675..9a7bbc5 100644 --- a/pages/index.html +++ b/pages/index.html @@ -5,7 +5,8 @@ - + + diff --git a/pages/js/index.js b/pages/js/index.js index 8621e31..0a1d36a 100644 --- a/pages/js/index.js +++ b/pages/js/index.js @@ -18,7 +18,6 @@ $(document).ready(function () { console.log(ctime.innerText); if (ctime != null) ctime.innerText = moment(ctime.innerText, "YYYY-MM-DDTHH:mm:ssZ").fromNow(); //.format("lll"); - //ctime.innerText = new Date(ctime.innerText * 1).toDateString(); } cmsgs.lastElementChild.scrollIntoView(false); } diff --git a/pages/js/receivemessage.js b/pages/js/receivemessage.js new file mode 100644 index 0000000..39b6e3c --- /dev/null +++ b/pages/js/receivemessage.js @@ -0,0 +1,7 @@ +(function poll() { + setTimeout(function() { + $.ajax({ url: "/receivemessage", success: function(data) { + + }, dataType: "json", complete: poll }); + }, 100); +})(); diff --git a/pages/js/sendmessage.js b/pages/js/sendmessage.js index 6083833..d9165cb 100644 --- a/pages/js/sendmessage.js +++ b/pages/js/sendmessage.js @@ -15,9 +15,7 @@ var respfunc = function respfunc(result) { sendmsg(msginput); } else { - var errormsg = document.getElementById("errormsg"); - errormsg.innerHTML = result.responseText; - errormsg.style = "display: block"; //TODO: Hide errormsg after a while (index.js) + showError. msginput.disabled = false; } } diff --git a/pages/js/utils.js b/pages/js/utils.js index 7739879..761362c 100644 --- a/pages/js/utils.js +++ b/pages/js/utils.js @@ -1,13 +1,20 @@ -/** - * Created by Norbi on 2016-07-27. - */ -function getFormData($form) { - var unindexed_array = $form.serializeArray(); - var indexed_array = {}; - - $.map(unindexed_array, function (n, i) { - indexed_array[n['name']] = n['value']; - }); - - return indexed_array; -} +/** + * Created by Norbi on 2016-07-27. + */ +function getFormData($form) { + var unindexed_array = $form.serializeArray(); + var indexed_array = {}; + + $.map(unindexed_array, function (n, i) { + indexed_array[n['name']] = n['value']; + }); + + return indexed_array; +} + +function showError(message) { + var errormsg = document.getElementById("errormsg"); + errormsg.innerHTML = result.responseText; + errormsg.style = "display: block"; + setTimeout(function(){errormsg.style.display="none";}, 2000); +} \ No newline at end of file diff --git a/src/io/github/norbipeti/chat/server/db/domain/Conversation.java b/src/io/github/norbipeti/chat/server/db/domain/Conversation.java index 312cfc5..c728d9f 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/Conversation.java +++ b/src/io/github/norbipeti/chat/server/db/domain/Conversation.java @@ -8,11 +8,10 @@ import io.github.norbipeti.chat.server.data.LoaderCollection; @Table(name = "CONVERSATION") public class Conversation extends SavedData { private static final long serialVersionUID = 5058682475353799722L; - private static Long nextid = 0L; // @Id // @GeneratedValue(strategy = GenerationType.IDENTITY) // @Column(name = "ID", unique = true, nullable = false) - private Long id = nextid++; + private Long id; @ElementCollection(fetch = FetchType.EAGER) @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "conversation") private LoaderCollection messsagechunks = new LoaderCollection<>(MessageChunk.class); @@ -53,4 +52,8 @@ public class Conversation extends SavedData { private Conversation() { } + + @Override + protected void init() { + } } diff --git a/src/io/github/norbipeti/chat/server/db/domain/Message.java b/src/io/github/norbipeti/chat/server/db/domain/Message.java index 4a3611e..1ea971b 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/Message.java +++ b/src/io/github/norbipeti/chat/server/db/domain/Message.java @@ -10,11 +10,13 @@ import io.github.norbipeti.chat.server.data.LoaderRef; @Entity @Table(name = "MESSAGE") public class Message implements Serializable { + private static final int MESSAGE_LIMIT_PER_CHUNK = 50; private static final long serialVersionUID = 6345941601716826570L; + private static Long nextid = 0L; // @Id // @GeneratedValue(strategy = GenerationType.IDENTITY) // @Column(name = "ID", unique = true, nullable = false) - // private Long id; + private Long id = nextid++; @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) // @JoinTable(name="user_message") private LoaderRef sender; @@ -64,7 +66,28 @@ public class Message implements Serializable { this.messagechunk = new LoaderRef(messagechunk); } - /* - * public Long getId() { return id; } public void setId(Long id) { this.id = id; } - */ + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + private Message() { + } + + public static Message create(Conversation parent) { + Message msg = new Message(); + int size = parent.getMesssageChunks().size(); + MessageChunk chunk; + if (size == 0 || parent.getMesssageChunks().get(size - 1).getMessages().size() >= MESSAGE_LIMIT_PER_CHUNK) { + chunk = SavedData.create(MessageChunk.class); + chunk.setConversation(parent); + parent.getMesssageChunks().add(chunk); + } else + chunk = parent.getMesssageChunks().get(size - 1); + msg.setMessageChunk(chunk); + return msg; + } } diff --git a/src/io/github/norbipeti/chat/server/db/domain/MessageChunk.java b/src/io/github/norbipeti/chat/server/db/domain/MessageChunk.java index f669501..887edf0 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/MessageChunk.java +++ b/src/io/github/norbipeti/chat/server/db/domain/MessageChunk.java @@ -7,11 +7,11 @@ import io.github.norbipeti.chat.server.data.LoaderRef; public class MessageChunk extends SavedData { private static final long serialVersionUID = -1665300779209348467L; - private static Long nextid = 0L; - private Long id = nextid++; - + + private Long id; private List messages = new ArrayList<>(); private LoaderRef conversation; + private Long nextmsgid = 0L; public List getMessages() { return messages; diff --git a/src/io/github/norbipeti/chat/server/db/domain/SavedData.java b/src/io/github/norbipeti/chat/server/db/domain/SavedData.java index 75a7e10..1e30a2b 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/SavedData.java +++ b/src/io/github/norbipeti/chat/server/db/domain/SavedData.java @@ -11,10 +11,12 @@ public abstract class SavedData implements Serializable { public abstract void setId(long id); + protected abstract void init(); + protected SavedData() { } - public static T create(Class cl) { + static T create(Class cl) { T obj; try { Constructor constructor = cl.getDeclaredConstructor(new Class[0]); @@ -27,6 +29,7 @@ public abstract class SavedData implements Serializable { } obj.setId(DataManager.getNextIDs().getOrDefault(obj.getClass(), 0L)); DataManager.setNextID(obj.getClass(), obj.getId() + 1); + obj.init(); DataManager.save(obj); return obj; } diff --git a/src/io/github/norbipeti/chat/server/db/domain/User.java b/src/io/github/norbipeti/chat/server/db/domain/User.java index c7a80ff..706bb9d 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/User.java +++ b/src/io/github/norbipeti/chat/server/db/domain/User.java @@ -11,8 +11,7 @@ import io.github.norbipeti.chat.server.data.LoaderCollection; @Table(name = "\"USER\"") public class User extends SavedData { private static final long serialVersionUID = 2862762084164225666L; - private static Long nextid = 0L; - private Long id = nextid++; + private Long id; private String name; private String email; private String password; @@ -109,4 +108,8 @@ public class User extends SavedData { public void setId(long id) { this.id = id; } + + public static User createUser() { + return SavedData.create(User.class); + } } diff --git a/src/io/github/norbipeti/chat/server/io/IOHelper.java b/src/io/github/norbipeti/chat/server/io/IOHelper.java index f825aa6..ead0113 100644 --- a/src/io/github/norbipeti/chat/server/io/IOHelper.java +++ b/src/io/github/norbipeti/chat/server/io/IOHelper.java @@ -62,7 +62,7 @@ public class IOHelper { } public static String ReadFile(File file) throws FileNotFoundException, IOException { - String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8); // TODO: FIx UTF-8 file reading + String content = FileUtils.readFileToString(file, StandardCharsets.UTF_8); return content; } diff --git a/src/io/github/norbipeti/chat/server/page/IndexPage.java b/src/io/github/norbipeti/chat/server/page/IndexPage.java index f48a471..9cda427 100644 --- a/src/io/github/norbipeti/chat/server/page/IndexPage.java +++ b/src/io/github/norbipeti/chat/server/page/IndexPage.java @@ -45,7 +45,7 @@ public class IndexPage extends Page { if (user.getConversations().size() == 0) { LoaderCollection convs = DataManager.getAll(Conversation.class); if (convs.size() == 0) { - Conversation c = SavedData.create(Conversation.class); + Conversation c = Conversation.create(); convs.add(c); // TODO: Handle no conversation open } user.getConversations().add(convs.get(0)); diff --git a/src/io/github/norbipeti/chat/server/page/ReceiveMessageAjaxPage.java b/src/io/github/norbipeti/chat/server/page/ReceiveMessageAjaxPage.java index 394962b..c886a31 100644 --- a/src/io/github/norbipeti/chat/server/page/ReceiveMessageAjaxPage.java +++ b/src/io/github/norbipeti/chat/server/page/ReceiveMessageAjaxPage.java @@ -27,21 +27,10 @@ public class ReceiveMessageAjaxPage extends Page { public void handlePage(HttpExchange exchange) throws IOException { User user = IOHelper.GetLoggedInUser(exchange); if (user == null) { - IOHelper.SendResponse(403, "

Please log in to send messages

", exchange); - return; - } - JsonObject obj = IOHelper.GetPOSTJSON(exchange); - if (obj == null) { - IOHelper.SendResponse(400, "JSONERROR: " + IOHelper.GetPOST(exchange), exchange); - return; - } - if (!obj.has("message") || !obj.has("conversation")) { - IOHelper.SendResponse(400, - "

400 Bad request

Message or conversation not found in JSON response.

" - + IOHelper.GetPOST(exchange) + "

", - exchange); + IOHelper.SendResponse(403, "

Please log in to receive messages

", exchange); return; } + JsonObject obj = new JsonObject(); String message = obj.get("message").getAsString().trim(); int conversation = obj.get("conversation").getAsInt(); if (message.trim().length() == 0) {