Error handling on save, send convid, FIXED CSS...
This commit is contained in:
parent
3a1f2a65e1
commit
7d7e681107
7 changed files with 71 additions and 27 deletions
|
@ -34,7 +34,9 @@ body {
|
||||||
|
|
||||||
#usercontent {
|
#usercontent {
|
||||||
margin: auto;
|
margin: auto;
|
||||||
width: 75%;
|
width: -moz-calc(100% - 600px);
|
||||||
|
width: -webkit-calc(100% - 600px);
|
||||||
|
width: calc(100% - 600px);
|
||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,7 @@ var sendmsgonenter = function sendmsgonenter(e) { //TODO: Detect Enter
|
||||||
if (textarea.value.trim().length == 0)
|
if (textarea.value.trim().length == 0)
|
||||||
return;
|
return;
|
||||||
textarea.disabled = true;
|
textarea.disabled = true;
|
||||||
window.convid = 1;
|
window.convid = document.getElementById("convidp").innerText * 1;
|
||||||
var json = JSON.stringify({"message": textarea.value, "conversation": window.convid});
|
var json = JSON.stringify({"message": textarea.value, "conversation": window.convid});
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: "/message", data: json, method: "POST", success: respfunc, error: respfunc
|
url: "/message", data: json, method: "POST", success: respfunc, error: respfunc
|
||||||
|
|
|
@ -4,7 +4,6 @@ import java.lang.reflect.Modifier;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.apache.logging.log4j.Level;
|
import org.apache.logging.log4j.Level;
|
||||||
|
@ -19,9 +18,7 @@ import com.google.gson.GsonBuilder;
|
||||||
import com.google.gson.reflect.TypeToken;
|
import com.google.gson.reflect.TypeToken;
|
||||||
import com.sun.net.httpserver.HttpServer;
|
import com.sun.net.httpserver.HttpServer;
|
||||||
|
|
||||||
import io.github.norbipeti.chat.server.data.DataManager;
|
import io.github.norbipeti.chat.server.data.*;
|
||||||
import io.github.norbipeti.chat.server.data.LoaderCollection;
|
|
||||||
import io.github.norbipeti.chat.server.data.LoaderCollectionSerializer;
|
|
||||||
import io.github.norbipeti.chat.server.db.domain.*;
|
import io.github.norbipeti.chat.server.db.domain.*;
|
||||||
import io.github.norbipeti.chat.server.page.*;
|
import io.github.norbipeti.chat.server.page.*;
|
||||||
|
|
||||||
|
@ -57,13 +54,9 @@ public class Main {
|
||||||
conversation.getUsers().add(user);
|
conversation.getUsers().add(user);
|
||||||
user.getConversations().add(conversation);
|
user.getConversations().add(conversation);
|
||||||
LogManager.getLogger().debug("User: " + user);
|
LogManager.getLogger().debug("User: " + user);
|
||||||
// conversation.getUsers().add(user2); - TODO
|
conversation.getUsers().add(user2);
|
||||||
LogManager.getLogger().debug("User2: " + user2); // TODO: Switch
|
LogManager.getLogger().debug("User2: " + user2);
|
||||||
// to JSON
|
user2.getConversations().add(conversation);
|
||||||
// files?
|
|
||||||
// user2.getConversations().add(conversation); // TODO: Fix
|
|
||||||
// duplicate
|
|
||||||
// key constraint
|
|
||||||
Message msg = new Message();
|
Message msg = new Message();
|
||||||
msg.setSender(user);
|
msg.setSender(user);
|
||||||
msg.setTime(new Date());
|
msg.setTime(new Date());
|
||||||
|
|
|
@ -5,6 +5,9 @@ import java.io.File;
|
||||||
import java.io.FilenameFilter;
|
import java.io.FilenameFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import com.google.common.io.Files;
|
import com.google.common.io.Files;
|
||||||
import io.github.norbipeti.chat.server.Main;
|
import io.github.norbipeti.chat.server.Main;
|
||||||
import io.github.norbipeti.chat.server.db.domain.ChatDatabaseEntity;
|
import io.github.norbipeti.chat.server.db.domain.ChatDatabaseEntity;
|
||||||
|
@ -13,9 +16,13 @@ public final class DataManager {
|
||||||
private DataManager() {
|
private DataManager() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends ChatDatabaseEntity> void save(T object) throws IOException {
|
public static <T extends ChatDatabaseEntity> void save(T object) {
|
||||||
Files.write(Main.gson.toJson(object), new File(object.getClass().getName() + "-" + object.getId()),
|
try {
|
||||||
StandardCharsets.UTF_8);
|
Files.write(Main.gson.toJson(object), new File(object.getClass().getName() + "-" + object.getId()),
|
||||||
|
StandardCharsets.UTF_8);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends ChatDatabaseEntity> T load(Class<T> cl, long id) {
|
public static <T extends ChatDatabaseEntity> T load(Class<T> cl, long id) {
|
||||||
|
@ -37,16 +44,24 @@ public final class DataManager {
|
||||||
return rets;
|
return rets;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static Map<File, Object> cache = new HashMap<>();
|
||||||
|
// TODO: Remove objects from the cache over time
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
private static <T extends ChatDatabaseEntity> T loadFromFile(File file, Class<T> cl) {
|
private static <T extends ChatDatabaseEntity> T loadFromFile(File file, Class<T> cl) {
|
||||||
try {
|
try {
|
||||||
if (!file.exists())
|
if (!file.exists())
|
||||||
return cl.newInstance();
|
return cl.newInstance();
|
||||||
|
if (cache.containsKey(file))
|
||||||
|
return (T) cache.get(file);
|
||||||
BufferedReader reader = Files.newReader(file, StandardCharsets.UTF_8);
|
BufferedReader reader = Files.newReader(file, StandardCharsets.UTF_8);
|
||||||
String objstr = "";
|
String objstr = "";
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null)
|
while ((line = reader.readLine()) != null)
|
||||||
objstr += line;
|
objstr += line;
|
||||||
return Main.gson.fromJson(objstr, cl);
|
T obj = Main.gson.fromJson(objstr, cl);
|
||||||
|
cache.put(file, obj);
|
||||||
|
return obj;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -54,6 +69,12 @@ public final class DataManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T extends ChatDatabaseEntity> boolean remove(T obj) {
|
public static <T extends ChatDatabaseEntity> boolean remove(T obj) {
|
||||||
|
if (cache.containsValue(obj))
|
||||||
|
cache.values().remove(obj);
|
||||||
return new File(obj.getClass().getName() + "-" + obj.getId()).delete();
|
return new File(obj.getClass().getName() + "-" + obj.getId()).delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T extends ChatDatabaseEntity> boolean remove(Class<T> cl, Long id) {
|
||||||
|
return new File(cl.getName() + "-" + id).delete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,28 +44,38 @@ public class LoaderCollection<T extends ChatDatabaseEntity> implements List<T>,
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean add(T e) {
|
public boolean add(T e) {
|
||||||
|
DataManager.save(e);
|
||||||
return idlist.add(e.getId());
|
return idlist.add(e.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void add(int index, T element) {
|
public void add(int index, T element) {
|
||||||
|
DataManager.save(element);
|
||||||
idlist.add(index, element.getId());
|
idlist.add(index, element.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addAll(Collection<? extends T> c) {
|
public boolean addAll(Collection<? extends T> c) {
|
||||||
return idlist
|
return idlist.addAll(c.stream().map((data) -> {
|
||||||
.addAll(c.stream().map((data) -> ((ChatDatabaseEntity) data).getId()).collect(Collectors.toList()));
|
ChatDatabaseEntity cde = ((ChatDatabaseEntity) data);
|
||||||
|
DataManager.save(cde);
|
||||||
|
return cde.getId();
|
||||||
|
}).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean addAll(int index, Collection<? extends T> c) {
|
public boolean addAll(int index, Collection<? extends T> c) {
|
||||||
return idlist.addAll(index,
|
return idlist.addAll(index, c.stream().map((data) -> {
|
||||||
c.stream().map((data) -> ((ChatDatabaseEntity) data).getId()).collect(Collectors.toList()));
|
ChatDatabaseEntity cde = ((ChatDatabaseEntity) data);
|
||||||
|
DataManager.save(cde);
|
||||||
|
return cde.getId();
|
||||||
|
}).collect(Collectors.toList()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public void clear() {
|
||||||
|
for (Long id : idlist)
|
||||||
|
DataManager.remove(cl, id); //TODO: Move out to a main list
|
||||||
idlist.clear();
|
idlist.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,8 +127,12 @@ public class LoaderCollection<T extends ChatDatabaseEntity> implements List<T>,
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean remove(Object o) {
|
public boolean remove(Object o) {
|
||||||
if (ChatDatabaseEntity.class.isAssignableFrom(o.getClass()))
|
if (ChatDatabaseEntity.class.isAssignableFrom(o.getClass())) {
|
||||||
|
DataManager.remove((ChatDatabaseEntity) o);
|
||||||
return idlist.remove(((ChatDatabaseEntity) o).getId());
|
return idlist.remove(((ChatDatabaseEntity) o).getId());
|
||||||
|
}
|
||||||
|
if (Long.class.isAssignableFrom(o.getClass()))
|
||||||
|
DataManager.remove(cl, (Long) o);
|
||||||
return idlist.remove(o);
|
return idlist.remove(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,9 +37,16 @@ public class IndexPage extends Page {
|
||||||
Element channelmessages = doc.getElementById("channelmessages");
|
Element channelmessages = doc.getElementById("channelmessages");
|
||||||
LogManager.getLogger().log(Level.INFO, "Conversations: " + DataManager.load(Conversation.class).size());
|
LogManager.getLogger().log(Level.INFO, "Conversations: " + DataManager.load(Conversation.class).size());
|
||||||
LogManager.getLogger().log(Level.INFO, "User conversations: " + user.getConversations().size());
|
LogManager.getLogger().log(Level.INFO, "User conversations: " + user.getConversations().size());
|
||||||
Conversation convo = user.getConversations().get(0);
|
LogManager.getLogger().log(Level.INFO, "Username: " + user.getName());
|
||||||
LogManager.getLogger().log(Level.INFO, "Messages: " + convo.getMesssages().size());
|
if (user.getConversations().size() == 0)
|
||||||
for (Message message : convo.getMesssages()) {
|
user.getConversations().add(DataManager.load(Conversation.class).get(0));
|
||||||
|
Conversation conv = user.getConversations().get(0);
|
||||||
|
Element cide = channelmessages.appendElement("p");
|
||||||
|
cide.attr("style", "display: none");
|
||||||
|
cide.attr("id", "convidp");
|
||||||
|
cide.text(Long.toString(conv.getId()));
|
||||||
|
LogManager.getLogger().log(Level.INFO, "Messages: " + conv.getMesssages().size());
|
||||||
|
for (Message message : conv.getMesssages()) {
|
||||||
Element msgelement = channelmessages.appendElement("div");
|
Element msgelement = channelmessages.appendElement("div");
|
||||||
Element header = msgelement.appendElement("p");
|
Element header = msgelement.appendElement("p");
|
||||||
header.text(message.getSender().getName() + " - " + message.getTime());
|
header.text(message.getSender().getName() + " - " + message.getTime());
|
||||||
|
@ -48,7 +55,14 @@ public class IndexPage extends Page {
|
||||||
}
|
}
|
||||||
return doc;
|
return doc;
|
||||||
}, exchange);
|
}, exchange);
|
||||||
} // TODO: Validation at registration (no special chars, etc.)
|
} // TODO:
|
||||||
|
// Validation
|
||||||
|
// at
|
||||||
|
// registration
|
||||||
|
// (no
|
||||||
|
// special
|
||||||
|
// chars,
|
||||||
|
// etc.)
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String GetName() {
|
public String GetName() {
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class MessageAjaxPage extends Page {
|
||||||
String message = obj.get("message").getAsString();
|
String message = obj.get("message").getAsString();
|
||||||
int conversation = obj.get("conversation").getAsInt();
|
int conversation = obj.get("conversation").getAsInt();
|
||||||
if (message.trim().length() == 0) {
|
if (message.trim().length() == 0) {
|
||||||
IOHelper.SendResponse(400, "<h1>400 Bad request</h1><p>The message cannot be empty,</p>", exchange);
|
IOHelper.SendResponse(400, "<h1>400 Bad request</h1><p>The message cannot be empty.</p>", exchange);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LoaderCollection<Conversation> convos = user.getConversations();
|
LoaderCollection<Conversation> convos = user.getConversations();
|
||||||
|
|
Loading…
Reference in a new issue