From b41d1c4b4cd7ce47deef31f614fb13212f0fe3b7 Mon Sep 17 00:00:00 2001 From: vylion Date: Sun, 9 Oct 2016 19:25:24 +0200 Subject: [PATCH] Created a chat name index. Fixed list name parsing. --- src/elements/Chat.java | 18 +++++++++--------- src/elements/Persistence.java | 34 +++++++++++++++++++++++++++------- src/main/Geiserbot.java | 30 +++++++++++++++++++++++++----- 3 files changed, 61 insertions(+), 21 deletions(-) diff --git a/src/elements/Chat.java b/src/elements/Chat.java index 52128c4..cebc8ca 100644 --- a/src/elements/Chat.java +++ b/src/elements/Chat.java @@ -31,6 +31,10 @@ public class Chat { name = n; } + public String getName() { + return name; + } + public boolean exists(String list) { return lists.containsKey(list); } @@ -83,15 +87,6 @@ public class Chat { Iterator it = lists.entrySet().iterator(); - if(name != null) { - lines = new ArrayList(); - - lines.add("#Chat name:"); - lines.add("#" + name); - - listDocs.add(lines); - } - while(it.hasNext()) { lines = new ArrayList(); @@ -99,6 +94,11 @@ public class Chat { lines.add("#List name:"); lines.add("#" + entry.getKey()); + if(name != null) { + lines.add("#Chat name:"); + lines.add("#" + name); + } + ArrayList list = entry.getValue(); for(int i = 0; i < list.size(); i++) { diff --git a/src/elements/Persistence.java b/src/elements/Persistence.java index 1c3b84b..3017b9c 100644 --- a/src/elements/Persistence.java +++ b/src/elements/Persistence.java @@ -30,19 +30,41 @@ public class Persistence { } + public void saveChatNames(List lines) throws SaveErrorChatException { + try { + Path file; + + file = Paths.get("geiserFiles/chat_names.txt"); + if (!Files.exists(file.getParent())) + Files.createDirectories(file.getParent()); + Files.write(file, lines, Charset.forName("UTF-8")); + } catch (IOException e) { + throw new SaveErrorChatException(e); + } + } + public void saveChat(long chat_id, Chat c) throws SaveErrorChatException { try { + List lines = new ArrayList(); + Path file; + + //Save chat stats + lines.add("#Chat name:"); + lines.add("#" + c.getName()); + + file = Paths.get("geiserFiles/" + chat_id + "/chat_names.txt"); + if (!Files.exists(file.getParent())) + Files.createDirectories(file.getParent()); + Files.write(file, lines, Charset.forName("UTF-8")); + + //Save chat lists ArrayList> lists = c.saveLists(); - List lines; for(int i = 0; i < lists.size(); i++) { lines = lists.get(i); String filename = lines.get(1).substring("#".length()); - if(lines.get(0).equals("#Chat name:")) { - filename = "chat_name"; - } - Path file = Paths.get("geiserFiles/" + chat_id + "/lists/" + filename + ".txt"); + file = Paths.get("geiserFiles/" + chat_id + "/lists/" + filename + ".txt"); if (!Files.exists(file.getParent())) Files.createDirectories(file.getParent()); Files.write(file, lines, Charset.forName("UTF-8")); @@ -50,8 +72,6 @@ public class Persistence { } catch (IOException e) { throw new SaveErrorChatException(e); } - - return; } /* diff --git a/src/main/Geiserbot.java b/src/main/Geiserbot.java index 523de9b..54dbe4d 100644 --- a/src/main/Geiserbot.java +++ b/src/main/Geiserbot.java @@ -318,7 +318,7 @@ public class Geiserbot { "Debes poner un espacio entre el comando /list y el nombre de la lista."); return; } - String list = reading[0]; + String list = reading[1]; if(!exists(m.getCid())) chats.put(m.getCid(), new Chat(m.getChatName())); @@ -335,16 +335,23 @@ public class Geiserbot { "Debes poner un espacio entre el comando /list y el nombre de la lista."); return; } - String list = reading[0]; + String list = reading[1]; - if(!exists(m.getCid())) - chats.put(m.getCid(), new Chat(m.getChatName())); + if(!exists(m.getCid())) { + try { + chats.put(m.getCid(), new Chat(m.getChatName())); + saveChatNames(); + } catch (SaveErrorChatException e) { + sendMessage(m.getCid(), "Ha habido un error guardando los datos de este chat."); + return; + } + } if(!exists(m.getCid(), list)) { try { chats.get(m.getCid()).add(list); saveChat(m.getCid()); } catch (SaveErrorChatException e) { - sendMessage(m.getCid(), "Ha habido un error guardando la lista."); + sendMessage(m.getCid(), "Ha habido un error creando la lista."); return; } } @@ -364,6 +371,19 @@ public class Geiserbot { Persistence.getInstance().saveChat(chat_id, chats.get(chat_id)); } + private void saveChatNames() throws SaveErrorChatException { + ArrayList lines = new ArrayList(); + + Iterator> it = chats.entrySet().iterator(); + while(it.hasNext()) { + Map.Entry entry = it.next(); + + lines.add(entry.getKey() + " = " + entry.getValue().getName()); + } + + Persistence.getInstance().saveChatNames(lines); + } + private String getWarning(int warning_id) { switch (warning_id) { case WARNING_UNKNOWN_ERROR: