96 lines
2.7 KiB
Java
96 lines
2.7 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 who logs in
|
|
*@since Envoy v0.1-alpha
|
|
**/
|
|
public LocalDB(User sender) { this.sender = sender; }
|
|
|
|
/**
|
|
*Initialises the local database and fills it with values
|
|
*if the user already sent/ received a message.<br>
|
|
*
|
|
*@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 user.<br>
|
|
*
|
|
*@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 chats that the user has saved
|
|
*@since Envoy v0.1-alpha
|
|
**/
|
|
public List<Chat> getChats() { return chats; }
|
|
}
|