From 2b1ece1c48d1f7758b9e4cf878b5c1b4a1590a69 Mon Sep 17 00:00:00 2001 From: kske Date: Sat, 14 Dec 2019 08:44:03 +0100 Subject: [PATCH] Added user list serialization to LocalDB * Added user list to LocalDB * Removed client user from LocalDB constructor --- src/main/java/envoy/client/LocalDB.java | 108 +++++++++++++----- src/main/java/envoy/client/ui/ChatWindow.java | 2 +- src/main/java/envoy/client/ui/Startup.java | 3 +- 3 files changed, 80 insertions(+), 33 deletions(-) diff --git a/src/main/java/envoy/client/LocalDB.java b/src/main/java/envoy/client/LocalDB.java index f061b28..2ab33ed 100644 --- a/src/main/java/envoy/client/LocalDB.java +++ b/src/main/java/envoy/client/LocalDB.java @@ -34,9 +34,11 @@ import envoy.schema.User; */ public class LocalDB { - private File localDB; - private User sender; - private List chats = new ArrayList<>(); + private File localDBFile, usersFile; + private User user; + private List users = new ArrayList<>(); + private List chats = new ArrayList<>(); + private ObjectFactory objectFactory = new ObjectFactory(); private DatatypeFactory datatypeFactory; @@ -44,16 +46,15 @@ public class LocalDB { private Sync sync = objectFactory.createSync(); private Sync readMessages = objectFactory.createSync(); - private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); + private static final Logger logger = Logger.getLogger(LocalDB.class.getSimpleName()); /** - * Constructs an empty local database. + * Constructs an empty local database. To serialize any chats to the file + * system, call {@link LocalDB#initializeDBFile(File)}. * - * @param sender the user that is logged in with this client * @since Envoy v0.1-alpha */ - public LocalDB(User sender) { - this.sender = sender; + public LocalDB() { try { datatypeFactory = DatatypeFactory.newInstance(); } catch (DatatypeConfigurationException e) { @@ -70,10 +71,17 @@ public class LocalDB { * @since Envoy v0.1-alpha */ public void initializeDBFile(File localDBDir) throws EnvoyException { + if (user == null) throw new NullPointerException("Client user is null"); if (localDBDir.exists() && !localDBDir.isDirectory()) throw new EnvoyException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); - localDB = new File(localDBDir, sender.getID() + ".db"); - if (localDB.exists()) loadFromLocalDB(); + usersFile = new File(localDBDir, "users.db"); + localDBFile = new File(localDBDir, user.getID() + ".db"); + try { + loadUsers(); + loadChats(); + } catch (ClassNotFoundException | IOException e) { + throw new EnvoyException(e); + } } /** @@ -82,29 +90,46 @@ public class LocalDB { * @throws IOException if something went wrong during saving * @since Envoy v0.1-alpha */ - public void saveToLocalDB() throws IOException { - localDB.getParentFile().mkdirs(); - localDB.createNewFile(); - try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) { - out.writeObject(chats); - } catch (IOException ex) { - throw ex; - } + public void save() throws IOException { + // Save users + write(usersFile, users); + + // Save chats + write(localDBFile, chats); } + @SuppressWarnings("unchecked") + private void loadUsers() throws ClassNotFoundException, IOException { users = read(usersFile, ArrayList.class); } + /** * Loads all chats saved by Envoy for the client user. * - * @throws EnvoyException if something fails while loading. + * @throws EnvoyException if something fails while loading. + * @throws IOException + * @throws ClassNotFoundException * @since Envoy v0.1-alpha */ @SuppressWarnings("unchecked") - private void loadFromLocalDB() throws EnvoyException { - try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(localDB))) { - Object obj = in.readObject(); - if (obj instanceof ArrayList) chats = (ArrayList) obj; + private void loadChats() throws ClassNotFoundException, IOException { chats = read(localDBFile, ArrayList.class); } + + private T read(File file, Class serializedClass) throws ClassNotFoundException, IOException { + try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) { + return serializedClass.cast(in.readObject()); } catch (ClassNotFoundException | IOException e) { - throw new EnvoyException(e); + throw e; + } + } + + private void write(File file, T obj) throws IOException { + if (file == null) throw new NullPointerException("File is null"); + if (!file.exists()) { + file.getParentFile().mkdirs(); + file.createNewFile(); + } + try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) { + out.writeObject(obj); + } catch (IOException e) { + throw e; } } @@ -118,7 +143,7 @@ public class LocalDB { */ public Message createMessage(String textContent, long recipientID) { Message.Metadata metaData = objectFactory.createMessageMetadata(); - metaData.setSender(sender.getID()); + metaData.setSender(user.getID()); metaData.setRecipient(recipientID); metaData.setState(MessageState.WAITING); metaData.setDate(datatypeFactory.newXMLGregorianCalendar(Instant.now().toString())); @@ -134,13 +159,13 @@ public class LocalDB { return message; } - /** + /** * Creates a {@link Sync} object filled with the changes that occurred to the * local database since the last synchronization. * * @param userId the ID of the user that is synchronized by this client * @return {@link Sync} object filled with the current changes - * @since Envoy v0.1-alpha + * @since Envoy v0.1-alpha */ public Sync fillSync(long userId) { addWaitingMessagesToSync(); @@ -156,7 +181,7 @@ public class LocalDB { * Applies the changes carried by a {@link Sync} object to the local database * * @param returnSync the {@link Sync} object to apply - * @since Envoy v0.1-alpha + * @since Envoy v0.1-alpha */ public void applySync(Sync returnSync) { for (int i = 0; i < returnSync.getMessages().size(); i++) { @@ -280,6 +305,16 @@ public class LocalDB { */ public void clearUnreadMessagesSync() { unreadMessagesSync.getMessages().clear(); } + /** + * @return the users + */ + public List getUsers() { return users; } + + /** + * @param users the users to set + */ + public void setUsers(List users) { this.users = users; } + /** * @return all saved {@link Chat} objects that list the client user as the * sender @@ -288,8 +323,19 @@ public class LocalDB { public List getChats() { return chats; } /** - * @return the {@link User} who initialized the local database - * @since Envoy v0.1-alpha + * @param chats the chats to set */ - public User getUser() { return sender; } + public void setChats(List chats) { this.chats = chats; } + + /** + * @return the {@link User} who initialized the local database + * @since Envoy v0.2-alpha + */ + public User getUser() { return user; } + + /** + * @param user the user to set + * @since Envoy v0.2-alpha + */ + public void setUser(User user) { this.user = user; } } diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 6867f5e..ee32bf6 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -86,7 +86,7 @@ public class ChatWindow extends JFrame { @Override public void windowClosing(WindowEvent evt) { try { - localDB.saveToLocalDB(); + localDB.save(); Settings.getInstance().save(); } catch (IOException e1) { e1.printStackTrace(); diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 48ce399..f0cf53e 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -68,7 +68,8 @@ public class Startup { } // Load the local database - LocalDB localDB = new LocalDB(client.getSender()); + LocalDB localDB = new LocalDB(); + localDB.setUser(client.getSender()); try { localDB.initializeDBFile(config.getLocalDB()); } catch (EnvoyException e) {