97 lines
2.9 KiB
Java
97 lines
2.9 KiB
Java
package envoy.client;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.ObjectInputStream;
|
|
import java.io.ObjectOutputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import envoy.exception.EnvoyException;
|
|
import envoy.schema.User;
|
|
|
|
/**
|
|
* Project: <strong>envoy-client</strong><br>
|
|
* File: <strong>LocalDB.java</strong><br>
|
|
* Created: <strong>27.10.2019</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy v0.1-alpha
|
|
*/
|
|
public class LocalDB {
|
|
|
|
private File localDB;
|
|
private User sender;
|
|
private List<Chat> chats = new ArrayList<>();
|
|
|
|
/**
|
|
* Constructs an empty local database.
|
|
*
|
|
* @param sender the user that is logged in with this client
|
|
* @since Envoy v0.1-alpha
|
|
**/
|
|
public LocalDB(User sender) { this.sender = sender; }
|
|
|
|
/**
|
|
* Initializes the local database and fills it with values
|
|
* if the user has already sent or received messages.
|
|
*
|
|
* @param localDBDir the directory where we wish to save/load the database from.
|
|
* @throws EnvoyException if the directory selected is not an actual directory.
|
|
* @since Envoy v0.1-alpha
|
|
**/
|
|
public void initializeDBFile(File localDBDir) throws EnvoyException {
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* Saves the database into a file for future use.
|
|
*
|
|
* @throws IOException if something went wrong during saving
|
|
* @since Envoy v0.1-alpha
|
|
**/
|
|
public void saveToLocalDB() {
|
|
try {
|
|
localDB.getParentFile().mkdirs();
|
|
localDB.createNewFile();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
System.err.println("unable to save the messages");
|
|
}
|
|
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(localDB))) {
|
|
out.writeObject(chats);
|
|
} catch (IOException ex) {
|
|
ex.printStackTrace();
|
|
System.err.println("unable to save the messages");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Loads all chats saved by Envoy for the client user.
|
|
*
|
|
* @throws EnvoyException if something fails while loading.
|
|
* @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;
|
|
} catch (ClassNotFoundException | IOException e) {
|
|
throw new EnvoyException(e);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return all saves {@link Chat} objects that list the client user as the
|
|
* sender
|
|
* @since Envoy v0.1-alpha
|
|
**/
|
|
public List<Chat> getChats() { return chats; }
|
|
}
|