CSS, added temp fake login; trying to get the database to work

This commit is contained in:
Norbi Peti 2016-07-27 15:03:35 +02:00
parent 6ee360df0b
commit 5e57613bda
10 changed files with 225 additions and 105 deletions

View file

@ -2,17 +2,10 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="588ba1ca-220a-4913-8e8c-5657fa1c845f" name="Default" comment="">
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/js/register.js" />
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/js/utils.js" />
<change type="DELETED" beforePath="$PROJECT_DIR$/register.html" afterPath="" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/css/style.css" afterPath="$PROJECT_DIR$/css/style.css" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/index.html" afterPath="$PROJECT_DIR$/index.html" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/js/login.js" afterPath="$PROJECT_DIR$/js/login.js" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/IOHelper.java" afterPath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/IOHelper.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/Main.java" afterPath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/Main.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/db/DataProvider.java" afterPath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/db/DataProvider.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/page/LoginPage.java" afterPath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/page/LoginPage.java" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/page/RegisterPage.java" afterPath="$PROJECT_DIR$/../src/io/github/norbipeti/chat/server/page/RegisterPage.java" />
</list>
<ignored path="pages.iws" />
<ignored path=".idea/workspace.xml" />
@ -36,8 +29,8 @@
<file leaf-file-name="index.html" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="193">
<caret line="48" column="20" selection-start-line="48" selection-start-column="20" selection-end-line="48" selection-end-column="20" />
<state relative-caret-position="1043">
<caret line="82" column="30" selection-start-line="82" selection-start-column="30" selection-end-line="82" selection-end-column="30" />
<folding />
</state>
</provider>
@ -56,8 +49,8 @@
<file leaf-file-name="login.js" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/js/login.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
<caret line="13" column="38" selection-start-line="13" selection-start-column="38" selection-end-line="13" selection-end-column="38" />
<state relative-caret-position="153">
<caret line="11" column="13" selection-start-line="11" selection-start-column="13" selection-end-line="11" selection-end-column="13" />
<folding>
<element signature="n#!!doc;n#login#0" expanded="false" />
</folding>
@ -80,8 +73,8 @@
<file leaf-file-name="style.css" pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/css/style.css">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17">
<caret line="1" column="27" selection-start-line="1" selection-start-column="27" selection-end-line="1" selection-end-column="27" />
<state relative-caret-position="91">
<caret line="19" column="24" selection-start-line="19" selection-start-column="24" selection-end-line="19" selection-end-column="24" />
<folding />
</state>
</provider>
@ -138,12 +131,12 @@
<option value="$PROJECT_DIR$/userbox.html" />
<option value="$PROJECT_DIR$/js/userbox.js" />
<option value="$PROJECT_DIR$/js/message.js" />
<option value="$PROJECT_DIR$/css/style.css" />
<option value="$PROJECT_DIR$/register.html" />
<option value="$PROJECT_DIR$/js/utils.js" />
<option value="$PROJECT_DIR$/index.html" />
<option value="$PROJECT_DIR$/js/register.js" />
<option value="$PROJECT_DIR$/js/login.js" />
<option value="$PROJECT_DIR$/css/style.css" />
<option value="$PROJECT_DIR$/index.html" />
</list>
</option>
</component>
@ -258,7 +251,7 @@
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="HbShouldOpenHtmlAsHb" value="" />
<property name="settings.editor.selected.configurable" value="fileTemplates" />
<property name="settings.editor.selected.configurable" value="preferences.toDoOptions" />
<property name="settings.editor.splitter.proportion" value="0.2" />
<property name="FileHistory.git4idea.history.GitHistoryProvider_flatWidth0" value="72" />
<property name="FileHistory.git4idea.history.GitHistoryProvider_flatOrder0" value="0" />
@ -369,12 +362,12 @@
<updated>1469522327377</updated>
<workItem from="1469522328775" duration="6392000" />
<workItem from="1469535953166" duration="2597000" />
<workItem from="1469602650261" duration="7736000" />
<workItem from="1469602650261" duration="15397000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="16725000" />
<option name="totallyTimeSpent" value="24386000" />
</component>
<component name="ToolWindowManager">
<frame x="-8" y="-8" width="1936" height="1056" extended-state="6" />
@ -515,14 +508,6 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/css/style.css">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="17">
<caret line="1" column="27" selection-start-line="1" selection-start-column="27" selection-end-line="1" selection-end-column="27" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/utils.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
@ -541,18 +526,26 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/css/style.css">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="91">
<caret line="19" column="24" selection-start-line="19" selection-start-column="24" selection-end-line="19" selection-end-column="24" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/index.html">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="193">
<caret line="48" column="20" selection-start-line="48" selection-start-column="20" selection-end-line="48" selection-end-column="20" />
<state relative-caret-position="1043">
<caret line="82" column="30" selection-start-line="82" selection-start-column="30" selection-end-line="82" selection-end-column="30" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/js/login.js">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
<caret line="13" column="38" selection-start-line="13" selection-start-column="38" selection-end-line="13" selection-end-column="38" />
<state relative-caret-position="153">
<caret line="11" column="13" selection-start-line="11" selection-start-column="13" selection-end-line="11" selection-end-column="13" />
<folding>
<element signature="n#!!doc;n#login#0" expanded="false" />
</folding>

View file

@ -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%;
}

View file

@ -9,66 +9,78 @@
<link rel="stylesheet" href="css/style.css"/>
</head>
<body>
<h1>Index</h1>
<div id="userbox">
Logged in as
<username/>
</div>
<div id="errormsg">
</div>
<div id="loginbox">
<form>
<table>
<tr>
<td>E-mail:</td>
<td>
<input type="email" name="email">
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type="password" name="pass">
</td>
</tr>
<input type="button" value="Login" onclick="login(this.form)">
</table>
</form>
<div id="sidebar">
<div id="userbox">
Logged in as
<username/>
</div>
</div>
<div id="registerbox">
<form>
<table>
<tr>
<td>Name:</td>
<td>
<input type="text" name="name"/>
</td>
</tr>
<tr>
<td>E-mail:</td>
<td>
<input type="email" name="email"/>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="pass"/>
</td>
</tr>
<tr>
<td>Password confirm</td>
<td>
<input type="password" name="pass2"/>
</td>
</tr>
<tr>
<td>
<input type="button" value="Register" onclick="register(this.form)"/>
</td>
</tr>
</table>
</form>
<div id="loginregisterbox">
<div id="loginbox">
<h2>Login</h2>
<form>
<table>
<tr>
<td>E-mail:</td>
<td>
<input type="email" name="email">
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type="password" name="pass">
</td>
</tr>
<tr>
<td>
<input type="button" value="Login" onclick="login(this.form)">
</td>
</tr>
</table>
</form>
</div>
<div id="registerbox">
<h2>Register</h2>
<form>
<table>
<tr>
<td>Name:</td>
<td>
<input type="text" name="name"/>
</td>
</tr>
<tr>
<td>E-mail:</td>
<td>
<input type="email" name="email"/>
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" name="pass"/>
</td>
</tr>
<tr>
<td>Password confirm</td>
<td>
<input type="password" name="pass2"/>
</td>
</tr>
<tr>
<td>
<input type="button" value="Register" onclick="register(this.form)"/>
</td>
</tr>
</table>
</form>
</div>
</div>
<div id="usercontent">
<div id="channelmessages"></div>
<textarea id="msginput"></textarea>
</div>
<input id="msginput" value=""/>
</body>

View file

@ -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) {

View file

@ -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<User> 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);

View file

@ -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();

View file

@ -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<Message> messsages;
@ElementCollection
@OneToMany
@ElementCollection(fetch = FetchType.EAGER)
@ManyToMany(cascade = CascadeType.ALL)
private List<User> users;
@Version
@GeneratedValue
private int Version;
public List<Message> getMesssages() {
if (messsages == null)
messsages = new ArrayList<>();
return messsages;
}
@ -26,6 +32,8 @@ public class Conversation {
}
public List<User> getUsers() {
if (users == null)
users = new ArrayList<>();
return users;
}

View file

@ -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;

View file

@ -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<Conversation> conversations;
public List<User> getContacts() {
if (contacts == null)
contacts = new ArrayList<>();
@ -93,6 +99,16 @@ public class User {
this.sessionid = sessionid;
}
public List<Conversation> getConversations() {
if (conversations == null)
conversations = new ArrayList<>();
return conversations;
}
public void setConversations(List<Conversation> conversations) {
this.conversations = conversations;
}
public User() {
}

View file

@ -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("<username />", 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() {