Added user list serialization to LocalDB

* Added user list to LocalDB
* Removed client user from LocalDB constructor
This commit is contained in:
Kai S. K. Engelbart 2019-12-14 08:44:03 +01:00
parent daca66d729
commit f983b6ffe7
3 changed files with 80 additions and 33 deletions

View File

@ -34,9 +34,11 @@ import envoy.schema.User;
*/
public class LocalDB {
private File localDB;
private User sender;
private List<Chat> chats = new ArrayList<>();
private File localDBFile, usersFile;
private User user;
private List<User> users = new ArrayList<>();
private List<Chat> 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<Chat>) obj;
private void loadChats() throws ClassNotFoundException, IOException { chats = read(localDBFile, ArrayList.class); }
private <T> T read(File file, Class<T> 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 <T> 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<User> getUsers() { return users; }
/**
* @param users the users to set
*/
public void setUsers(List<User> 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<Chat> 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<Chat> 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; }
}

View File

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

View File

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