From 095269a5859efd8ee9a501f5f2e32b6d35fd1ca2 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Fri, 29 Jul 2016 11:56:03 +0200 Subject: [PATCH] Sending messages works! Kind of... I give up on the database. I'm switching to JSON. --- pages/index.html | 172 +++++++++--------- pages/js/message.js | 22 +-- src/io/github/norbipeti/chat/server/Main.java | 13 ++ .../chat/server/db/domain/Conversation.java | 15 +- .../chat/server/db/domain/Message.java | 2 +- .../norbipeti/chat/server/db/domain/User.java | 18 +- .../norbipeti/chat/server/page/IndexPage.java | 10 +- .../chat/server/page/MessageAjaxPage.java | 38 +++- 8 files changed, 163 insertions(+), 127 deletions(-) diff --git a/pages/index.html b/pages/index.html index 0d3248f..f2a730a 100644 --- a/pages/index.html +++ b/pages/index.html @@ -1,86 +1,86 @@ - - - ChatServer - - - - - - - - -
-
- -
-
-

Login

-
- - - - - - - - - - - - -
E-mail: - -
Password: - -
- -
-
-
-
-

Register

-
- - - - - - - - - - - - - - - - - - - - -
Name: - -
E-mail: - -
Password - -
Password confirm - -
- -
-
-
-
-
-
- -
- + + + ChatServer + + + + + + + + +
+
+ +
+
+

Login

+
+ + + + + + + + + + + + +
E-mail: + +
Password: + +
+ +
+
+
+
+

Register

+
+ + + + + + + + + + + + + + + + + + + + +
Name: + +
E-mail: + +
Password + +
Password confirm + +
+ +
+
+
+
+
+
+ +
+ diff --git a/pages/js/message.js b/pages/js/message.js index cbcd0cb..373e010 100644 --- a/pages/js/message.js +++ b/pages/js/message.js @@ -1,34 +1,28 @@ -var respfunc = function (result) { - if (result.responseText != "Success") { +var respfunc = function respfunc(result) { + if (result != "Success") { //on success result is string var errormsg = document.getElementById("errormsg"); errormsg.innerHTML = result.responseText; errormsg.style = "display: block"; } else location.reload(true); -} +}; -var sendmsgonenter = function (e) { //TODO: Detect Enter - console.log("A"); +var sendmsgonenter = function sendmsgonenter(e) { //TODO: Detect Enter var code = e.keyCode || e.which; if (code != 13) { //Enter keycode return; } - console.log("B"); var textarea = event.target; - window.convid = 0; + window.convid = 1; var json = JSON.stringify({"message": textarea.value, "conversation": window.convid}); $.ajax({ url: "/message", data: json, method: "POST", success: respfunc, error: respfunc }); }; -$(document).bind("ready", function () { - console.log("X"); - $('#msginput').keypress = sendmsgonenter; +$(document).ready(function () { + $('#msginput').on("keypress", sendmsgonenter); }); -console.log(respfunc); -console.log(sendmsgonenter); - -$('#msginput').bind("keypress", sendmsgonenter); +$('#msginput').on("keypress", sendmsgonenter); diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index 28220ae..8c5ce93 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -51,7 +51,10 @@ public class Main { System.out.println("2nd's contact: " + user2.getContacts().get(0)); Conversation convo = new Conversation(); convo.getUsers().add(user); + //user.getConversations().add(convo); convo.getUsers().add(user2); + // user2.getConversations().add(convo); - TODO: Fix duplicate + // key constraint Message msg = new Message(); msg.setSender(user); msg.setTime(new Date()); @@ -63,6 +66,16 @@ public class Main { msg2.setMessage("Teszt 2"); convo.getMesssages().add(msg2); provider.saveConversation(convo); + provider.saveUser(user); + provider.saveUser(user2); + User loggedinuser = new User(); + loggedinuser.setName("NorbiPeti"); + loggedinuser.setSessionid("2ed6e2cd-33ad-416e-92c2-7365510b8b31"); + loggedinuser.setEmail("sznp@asd.com"); + convo.getUsers().add(loggedinuser); + loggedinuser.getConversations().add(convo); + provider.saveUser(loggedinuser); + provider.saveConversation(convo); } System.out.println("Starting webserver..."); HttpServer server = HttpServer.create(new InetSocketAddress(InetAddress.getLocalHost(), 8080), 10); 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 6ecd880..53b7b40 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/Conversation.java +++ b/src/io/github/norbipeti/chat/server/db/domain/Conversation.java @@ -1,7 +1,9 @@ package io.github.norbipeti.chat.server.db.domain; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import javax.persistence.*; @@ -11,12 +13,13 @@ public class Conversation { @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false) private Long id; - @ElementCollection(fetch=FetchType.EAGER) - @OneToMany(cascade = CascadeType.ALL) + @ElementCollection(fetch = FetchType.EAGER) + @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "conversation") private List messsages; @ElementCollection(fetch = FetchType.EAGER) @ManyToMany(cascade = CascadeType.ALL) - private List users; + @JoinTable(name = "User_Conversation", joinColumns = @JoinColumn(name = "conversation_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id")) + private Set users; public List getMesssages() { if (messsages == null) @@ -28,13 +31,13 @@ public class Conversation { this.messsages = messsages; } - public List getUsers() { + public Set getUsers() { if (users == null) - users = new ArrayList<>(); + users = new HashSet<>(); return users; } - public void setUsers(List users) { + public void setUsers(Set users) { this.users = users; } 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 5e40261..a0acc27 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/Message.java +++ b/src/io/github/norbipeti/chat/server/db/domain/Message.java @@ -15,7 +15,7 @@ public class Message { private User sender; private Date time; private String message; - @ManyToOne(cascade=CascadeType.ALL) + @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER) //@JoinTable(name="conversation_message") private Conversation conversation; 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 1657d3b..52d43d6 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/User.java +++ b/src/io/github/norbipeti/chat/server/db/domain/User.java @@ -1,9 +1,10 @@ package io.github.norbipeti.chat.server.db.domain; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; -import javax.annotation.Generated; import javax.persistence.*; @Entity @@ -19,12 +20,13 @@ public class User { @ElementCollection(fetch = FetchType.EAGER) private List contacts; private String salt; - //@Column(columnDefinition = "CHAR(16) FOR BIT DATA") - @Column(columnDefinition="VARCHAR(64)") + // @Column(columnDefinition = "CHAR(16) FOR BIT DATA") + @Column(columnDefinition = "VARCHAR(64)") private String sessionid; @ElementCollection(fetch = FetchType.EAGER) - @ManyToMany(cascade = CascadeType.ALL) - public List conversations; + // @ManyToMany(fetch = FetchType.EAGER, mappedBy = "users") + @ManyToMany(mappedBy = "users", fetch = FetchType.EAGER) + private Set conversations; public List getContacts() { if (contacts == null) @@ -96,13 +98,13 @@ public class User { this.sessionid = sessionid; } - public List getConversations() { + public Set getConversations() { if (conversations == null) - conversations = new ArrayList<>(); + conversations = new HashSet<>(); return conversations; } - public void setConversations(List conversations) { + public void setConversations(Set conversations) { this.conversations = conversations; } diff --git a/src/io/github/norbipeti/chat/server/page/IndexPage.java b/src/io/github/norbipeti/chat/server/page/IndexPage.java index 5f11d5e..ab0ac92 100644 --- a/src/io/github/norbipeti/chat/server/page/IndexPage.java +++ b/src/io/github/norbipeti/chat/server/page/IndexPage.java @@ -16,10 +16,10 @@ public class IndexPage extends Page { @Override public void handlePage(HttpExchange exchange) throws IOException { final User user = IOHelper.GetLoggedInUser(exchange); - /*final User user = new User(); - user.setEmail("test@test.com"); - user.setName("Norbi"); - user.setId(3L);*/ + /* + * final User user = new User(); user.setEmail("test@test.com"); + * user.setName("Norbi"); user.setId(3L); + */ if (user == null) IOHelper.SendModifiedPage(200, this, (doc) -> { doc.getElementById("userbox").remove(); @@ -34,7 +34,9 @@ public class IndexPage extends Page { userbox.html(userbox.html().replace("", user.getName())); Element channelmessages = doc.getElementById("channelmessages"); try (DataProvider provider = new DataProvider()) { + System.out.println("Conversations: " + provider.getConversations().size()); Conversation convo = provider.getConversations().get(0); + System.out.println("Messages: " + convo.getMesssages().size()); for (Message message : convo.getMesssages()) { Element msgelement = channelmessages.appendElement("div"); Element header = msgelement.appendElement("p"); diff --git a/src/io/github/norbipeti/chat/server/page/MessageAjaxPage.java b/src/io/github/norbipeti/chat/server/page/MessageAjaxPage.java index f1ccab2..7cc4197 100644 --- a/src/io/github/norbipeti/chat/server/page/MessageAjaxPage.java +++ b/src/io/github/norbipeti/chat/server/page/MessageAjaxPage.java @@ -4,12 +4,14 @@ import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.List; +import java.util.Set; import org.json.JSONObject; import com.sun.net.httpserver.HttpExchange; import io.github.norbipeti.chat.server.IOHelper; +import io.github.norbipeti.chat.server.db.DataProvider; 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.User; @@ -26,7 +28,7 @@ public class MessageAjaxPage extends Page { User user = IOHelper.GetLoggedInUser(exchange); if (user == null) { IOHelper.SendResponse(403, "

Please log in to send messages

", exchange); - return; //TODO: Fix sending messages + return; // TODO: Fix sending messages } JSONObject obj = IOHelper.GetPOSTJSON(exchange); if (obj == null) { @@ -35,13 +37,33 @@ public class MessageAjaxPage extends Page { } String message = obj.getString("message"); int conversation = obj.getInt("conversation"); - List convos = user.getConversations(); - Conversation convo = convos.get(conversation); - Message msg = new Message(); - msg.setSender(user); - msg.setMessage(message); - msg.setTime(new Date()); - convo.getMesssages().add(msg); + Set convos = user.getConversations(); + Conversation conv = null; + System.out.println("Len: " + convos.size()); + for (Conversation con : convos) { + System.out.println(con.getId()); + if (con.getId() == conversation) { + conv = con; + break; + } + } + if (conv == null) { + IOHelper.SendResponse(400, "

400 Conversation not found

The conversation with the id " + + conversation + " is not found.

", exchange); + return; + } + try (DataProvider provider = new DataProvider()) { + Message msg = new Message(); + msg.setSender(user); + msg.setMessage(message); + msg.setTime(new Date()); + conv.getMesssages().add(msg); + provider.saveConversation(conv); + System.out.println(conv.getMesssages().size()); + } catch (Exception e) { + throw e; + } + IOHelper.SendResponse(200, "Success", exchange); }