Error handling on save, send convid, FIXED CSS...

This commit is contained in:
Norbi Peti 2016-08-03 13:59:14 +02:00
parent 3a1f2a65e1
commit 7d7e681107
7 changed files with 71 additions and 27 deletions

View file

@ -34,7 +34,9 @@ body {
#usercontent {
margin: auto;
width: 75%;
width: -moz-calc(100% - 600px);
width: -webkit-calc(100% - 600px);
width: calc(100% - 600px);
height: 100%;
}

View file

@ -18,7 +18,7 @@ var sendmsgonenter = function sendmsgonenter(e) { //TODO: Detect Enter
if (textarea.value.trim().length == 0)
return;
textarea.disabled = true;
window.convid = 1;
window.convid = document.getElementById("convidp").innerText * 1;
var json = JSON.stringify({"message": textarea.value, "conversation": window.convid});
$.ajax({
url: "/message", data: json, method: "POST", success: respfunc, error: respfunc

View file

@ -4,7 +4,6 @@ import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Date;
import java.util.List;
import java.util.Set;
import org.apache.logging.log4j.Level;
@ -19,9 +18,7 @@ import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.sun.net.httpserver.HttpServer;
import io.github.norbipeti.chat.server.data.DataManager;
import io.github.norbipeti.chat.server.data.LoaderCollection;
import io.github.norbipeti.chat.server.data.LoaderCollectionSerializer;
import io.github.norbipeti.chat.server.data.*;
import io.github.norbipeti.chat.server.db.domain.*;
import io.github.norbipeti.chat.server.page.*;
@ -57,13 +54,9 @@ public class Main {
conversation.getUsers().add(user);
user.getConversations().add(conversation);
LogManager.getLogger().debug("User: " + user);
// conversation.getUsers().add(user2); - TODO
LogManager.getLogger().debug("User2: " + user2); // TODO: Switch
// to JSON
// files?
// user2.getConversations().add(conversation); // TODO: Fix
// duplicate
// key constraint
conversation.getUsers().add(user2);
LogManager.getLogger().debug("User2: " + user2);
user2.getConversations().add(conversation);
Message msg = new Message();
msg.setSender(user);
msg.setTime(new Date());

View file

@ -5,6 +5,9 @@ import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import com.google.common.io.Files;
import io.github.norbipeti.chat.server.Main;
import io.github.norbipeti.chat.server.db.domain.ChatDatabaseEntity;
@ -13,9 +16,13 @@ public final class DataManager {
private DataManager() {
}
public static <T extends ChatDatabaseEntity> void save(T object) throws IOException {
Files.write(Main.gson.toJson(object), new File(object.getClass().getName() + "-" + object.getId()),
StandardCharsets.UTF_8);
public static <T extends ChatDatabaseEntity> void save(T object) {
try {
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) {
@ -37,16 +44,24 @@ public final class DataManager {
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) {
try {
if (!file.exists())
return cl.newInstance();
if (cache.containsKey(file))
return (T) cache.get(file);
BufferedReader reader = Files.newReader(file, StandardCharsets.UTF_8);
String objstr = "";
String line;
while ((line = reader.readLine()) != null)
objstr += line;
return Main.gson.fromJson(objstr, cl);
T obj = Main.gson.fromJson(objstr, cl);
cache.put(file, obj);
return obj;
} catch (Exception e) {
e.printStackTrace();
}
@ -54,6 +69,12 @@ public final class DataManager {
}
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();
}
public static <T extends ChatDatabaseEntity> boolean remove(Class<T> cl, Long id) {
return new File(cl.getName() + "-" + id).delete();
}
}

View file

@ -44,28 +44,38 @@ public class LoaderCollection<T extends ChatDatabaseEntity> implements List<T>,
@Override
public boolean add(T e) {
DataManager.save(e);
return idlist.add(e.getId());
}
@Override
public void add(int index, T element) {
DataManager.save(element);
idlist.add(index, element.getId());
}
@Override
public boolean addAll(Collection<? extends T> c) {
return idlist
.addAll(c.stream().map((data) -> ((ChatDatabaseEntity) data).getId()).collect(Collectors.toList()));
return idlist.addAll(c.stream().map((data) -> {
ChatDatabaseEntity cde = ((ChatDatabaseEntity) data);
DataManager.save(cde);
return cde.getId();
}).collect(Collectors.toList()));
}
@Override
public boolean addAll(int index, Collection<? extends T> c) {
return idlist.addAll(index,
c.stream().map((data) -> ((ChatDatabaseEntity) data).getId()).collect(Collectors.toList()));
return idlist.addAll(index, c.stream().map((data) -> {
ChatDatabaseEntity cde = ((ChatDatabaseEntity) data);
DataManager.save(cde);
return cde.getId();
}).collect(Collectors.toList()));
}
@Override
public void clear() {
for (Long id : idlist)
DataManager.remove(cl, id); //TODO: Move out to a main list
idlist.clear();
}
@ -117,8 +127,12 @@ public class LoaderCollection<T extends ChatDatabaseEntity> implements List<T>,
*/
@Override
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());
}
if (Long.class.isAssignableFrom(o.getClass()))
DataManager.remove(cl, (Long) o);
return idlist.remove(o);
}

View file

@ -37,9 +37,16 @@ public class IndexPage extends Page {
Element channelmessages = doc.getElementById("channelmessages");
LogManager.getLogger().log(Level.INFO, "Conversations: " + DataManager.load(Conversation.class).size());
LogManager.getLogger().log(Level.INFO, "User conversations: " + user.getConversations().size());
Conversation convo = user.getConversations().get(0);
LogManager.getLogger().log(Level.INFO, "Messages: " + convo.getMesssages().size());
for (Message message : convo.getMesssages()) {
LogManager.getLogger().log(Level.INFO, "Username: " + user.getName());
if (user.getConversations().size() == 0)
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 header = msgelement.appendElement("p");
header.text(message.getSender().getName() + " - " + message.getTime());
@ -48,7 +55,14 @@ public class IndexPage extends Page {
}
return doc;
}, exchange);
} // TODO: Validation at registration (no special chars, etc.)
} // TODO:
// Validation
// at
// registration
// (no
// special
// chars,
// etc.)
@Override
public String GetName() {

View file

@ -45,7 +45,7 @@ public class MessageAjaxPage extends Page {
String message = obj.get("message").getAsString();
int conversation = obj.get("conversation").getAsInt();
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;
}
LoaderCollection<Conversation> convos = user.getConversations();