diff --git a/pages/.idea/workspace.xml b/pages/.idea/workspace.xml index 21f5b5b..fb3a6b4 100644 --- a/pages/.idea/workspace.xml +++ b/pages/.idea/workspace.xml @@ -2,17 +2,10 @@ - - - + - - - - - @@ -36,8 +29,8 @@ - - + + @@ -56,8 +49,8 @@ - - + + @@ -80,8 +73,8 @@ - - + + @@ -138,12 +131,12 @@ @@ -258,7 +251,7 @@ - + @@ -369,12 +362,12 @@ 1469522327377 - + - @@ -515,14 +508,6 @@ - - - - - - - - @@ -541,18 +526,26 @@ + + + + + + + + - - + + - - + + diff --git a/pages/css/style.css b/pages/css/style.css index 564c1b1..67b0d11 100644 --- a/pages/css/style.css +++ b/pages/css/style.css @@ -1,7 +1,44 @@ body { - background-color: #EEE; + background-color: #EEE; } #errormsg, #successmsg { - display: none; + display: none; } + +#errormsg { + background-color: #8c0000; + border-color: #ff0000; + border-style: solid; + border-width: 2px; + color: #ff0000; +} + +#successmsg { + background-color: #188c00; + border-color: #00ff00; + border-style: solid; + border-width: 2px; + color: #00ff00; +} + +#sidebar { + float: right; + width: 300px; +} + +#loginregisterbox { + margin: auto; + width: 300px; +} + +#usercontent { + margin: auto; + width: 75%; +} + +#msginput { + width: 100%; + height: 30px; + max-width: 100%; +} \ No newline at end of file diff --git a/pages/index.html b/pages/index.html index 73b4019..a8da097 100644 --- a/pages/index.html +++ b/pages/index.html @@ -9,66 +9,78 @@ -

Index

-
- Logged in as - -
-
-
- - - - - - - - - - -
E-mail: - -
Password: - -
-
+ -
-
- - - - - - - - - - - - - - - - - - - - -
Name: - -
E-mail: - -
Password - -
Password confirm - -
- -
-
+
+
+

Login

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

Register

+
+ + + + + + + + + + + + + + + + + + + + +
Name: + +
E-mail: + +
Password + +
Password confirm + +
+ +
+
+
+
+
+
+
- diff --git a/pages/js/login.js b/pages/js/login.js index 55045fd..abc2912 100644 --- a/pages/js/login.js +++ b/pages/js/login.js @@ -1,7 +1,7 @@ /** * Created by Norbi on 2016-07-26. */ -function login(form) { +function login(form) { //TODO: Detect Enter var json = JSON.stringify(getFormData($(form))); $.ajax({ url: "/login", data: json, method: "POST", success: function (result) { diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index b3de86a..b6e80d4 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -3,6 +3,9 @@ package io.github.norbipeti.chat.server; import java.lang.reflect.Modifier; import java.net.InetAddress; import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; import java.util.Set; import org.apache.logging.log4j.Level; @@ -28,27 +31,47 @@ public class Main { try { // rt.jar Javadoc: // https://docs.oracle.com/javase/8/docs/jre/api/net/httpserver/spec/ // https://docs.oracle.com/javase/8/docs/api/ - System.out.println(System.getProperty("java.class.path")); //TODO: log4j + System.out.println(System.getProperty("java.class.path")); // TODO: + // log4j LoggerContext ctx = (LoggerContext) LogManager.getContext(false); Configuration config = ctx.getConfiguration(); LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); loggerConfig.setLevel(Level.WARN); - ctx.updateLoggers(); // This causes all Loggers to refetch information from their LoggerConfig. + ctx.updateLoggers(); // This causes all Loggers to refetch + // information from their LoggerConfig. System.out.println("Loading database..."); try (DataProvider provider = new DataProvider()) { User user = new User(); user.setName("asd"); user.setEmail("test@test.com"); - provider.addUser(user); User user2 = new User(); user2.setName("Teszt"); user2.setEmail("test2@test.com"); user2.getContacts().add(user); + provider.addUser(user); + List users = provider.getUsers(); + user = users.get(0); user.getContacts().add(user2); provider.addUser(user2); - System.out.println(provider.getUsers()); + users = provider.getUsers(); + user2 = users.get(1); + System.out.println(users); System.out.println("1st's contact: " + user.getContacts().get(0)); System.out.println("2nd's contact: " + user2.getContacts().get(0)); + Conversation convo = new Conversation(); + convo.getUsers().add(user); + convo.getUsers().add(user2); + Message msg = new Message(); + msg.setSender(user); + msg.setTime(new Date()); + msg.setMessage("Teszt 1"); + convo.getMesssages().add(msg); + Message msg2 = new Message(); + msg2.setSender(user2); + msg2.setTime(new Date()); + msg2.setMessage("Teszt 2"); + convo.getMesssages().add(msg2); + provider.addConversation(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/DataProvider.java b/src/io/github/norbipeti/chat/server/db/DataProvider.java index ddb518b..4a95175 100644 --- a/src/io/github/norbipeti/chat/server/db/DataProvider.java +++ b/src/io/github/norbipeti/chat/server/db/DataProvider.java @@ -19,6 +19,10 @@ public class DataProvider implements AutoCloseable { save(user); } + public void addConversation(Conversation convo) { + save(convo); + } + private void save(Object object) { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); 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 5e66106..c2f296a 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/Conversation.java +++ b/src/io/github/norbipeti/chat/server/db/domain/Conversation.java @@ -1,5 +1,6 @@ package io.github.norbipeti.chat.server.db.domain; +import java.util.ArrayList; import java.util.List; import javax.persistence.*; @@ -10,14 +11,19 @@ public class Conversation { @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false) private Long id; - @ElementCollection - @OneToMany + @ElementCollection(fetch=FetchType.EAGER) + @OneToMany(cascade = CascadeType.ALL) private List messsages; - @ElementCollection - @OneToMany + @ElementCollection(fetch = FetchType.EAGER) + @ManyToMany(cascade = CascadeType.ALL) private List users; + @Version + @GeneratedValue + private int Version; public List getMesssages() { + if (messsages == null) + messsages = new ArrayList<>(); return messsages; } @@ -26,6 +32,8 @@ public class Conversation { } public List getUsers() { + if (users == null) + users = new ArrayList<>(); return 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 5a8d2a4..889b01a 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/Message.java +++ b/src/io/github/norbipeti/chat/server/db/domain/Message.java @@ -10,12 +10,15 @@ public class Message { @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false) private Long id; - @ManyToOne + @ManyToOne(cascade=CascadeType.ALL) private User sender; private Date time; private String message; - @ManyToOne + @ManyToOne(cascade=CascadeType.ALL) private Conversation conversation; + @Version + @GeneratedValue + private int Version; public User getSender() { return sender; 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 48107c6..f11b3bf 100644 --- a/src/io/github/norbipeti/chat/server/db/domain/User.java +++ b/src/io/github/norbipeti/chat/server/db/domain/User.java @@ -21,7 +21,13 @@ public class User { private String salt; @Column(columnDefinition = "CHAR(16) FOR BIT DATA") private UUID sessionid; - + @Version + @GeneratedValue + private int Version; + @ElementCollection(fetch = FetchType.EAGER) + @ManyToMany(cascade = CascadeType.ALL) + public List conversations; + public List getContacts() { if (contacts == null) contacts = new ArrayList<>(); @@ -93,6 +99,16 @@ public class User { this.sessionid = sessionid; } + public List getConversations() { + if (conversations == null) + conversations = new ArrayList<>(); + return conversations; + } + + public void setConversations(List conversations) { + this.conversations = conversations; + } + public User() { } diff --git a/src/io/github/norbipeti/chat/server/page/IndexPage.java b/src/io/github/norbipeti/chat/server/page/IndexPage.java index 62493ec..35d3dea 100644 --- a/src/io/github/norbipeti/chat/server/page/IndexPage.java +++ b/src/io/github/norbipeti/chat/server/page/IndexPage.java @@ -5,28 +5,52 @@ import java.io.IOException; import org.jsoup.nodes.Element; import com.sun.net.httpserver.HttpExchange; +import com.sun.net.ssl.internal.ssl.Provider; 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; public class IndexPage extends Page { @Override public void handlePage(HttpExchange exchange) throws IOException { - User user = IOHelper.GetLoggedInUser(exchange); + // final User user = IOHelper.GetLoggedInUser(exchange); - TODO + 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(); + doc.getElementById("usercontent").remove(); return doc; }, exchange); else IOHelper.SendModifiedPage(200, this, (doc) -> { doc.getElementById("loginbox").remove(); + doc.getElementById("registerbox").remove(); Element userbox = doc.getElementById("userbox"); userbox.html(userbox.html().replace("", user.getName())); + Element channelmessages = doc.getElementById("channelmessages"); + try (DataProvider provider = new DataProvider()) { + Conversation convo = provider.getConversations().get(0); //TODO + for (Message message : convo.getMesssages()) { + Element msgelement = channelmessages.appendElement("div"); + Element header = msgelement.appendElement("p"); + header.text(message.getSender().getName() + " - " + message.getTime()); + Element body = msgelement.appendElement("p"); + body.text(message.getMessage()); + } + } return doc; }, exchange); - } // TODO: Validation at registration + } // TODO: + // Validation + // at + // registration @Override public String GetName() {