From 2f1f72b1fc999a8a71d02ca3ffdf199fa5ca6ab1 Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 5 Mar 2022 16:51:12 +0100 Subject: [PATCH] =?UTF-8?q?Syst=C3=A8me=20de=20synchronisation=20de=20l'in?= =?UTF-8?q?formation=20entre=20FileFrontEnd<>StorBackEnd=20->=20impl=C3=A9?= =?UTF-8?q?mentation=20de=20AppData=20+=20ajout=20de=20la=20biblioth=C3=A8?= =?UTF-8?q?que=20GSON=20=C3=A0=20build.gradle?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle | 2 + .../java/lightcontainer/storage/AppData.java | 132 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 app/src/main/java/lightcontainer/storage/AppData.java diff --git a/app/build.gradle b/app/build.gradle index 2a11f36..5345a80 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -22,6 +22,8 @@ dependencies { // This dependency is used by the application. implementation 'com.google.guava:guava:30.1-jre' + // Use gson to serialize/deserialize json files + implementation 'com.google.code.gson:gson:2.9.0' } application { diff --git a/app/src/main/java/lightcontainer/storage/AppData.java b/app/src/main/java/lightcontainer/storage/AppData.java new file mode 100644 index 0000000..ba10605 --- /dev/null +++ b/app/src/main/java/lightcontainer/storage/AppData.java @@ -0,0 +1,132 @@ +package lightcontainer.storage; + +import java.util.HashMap; +import java.util.Map; + +/** + * AppData represents the database of the FileFrontEnd program. + * It contains an AppConfig instance and a collection of Users. + * + * @author Maximilien LEDOUX + * @version 1.0 + * @see AppConfig + * @see User + * @since 1.0 + */ +public class AppData { + + private static AppData instance = null; + private AppConfig appConfig; + private final Map users; + + + /** + * Constructs a new instance of AppData. + * Sets appConfig to null and creates a new Hashmap of users. + */ + private AppData() { + this.appConfig = null; + this.users = new HashMap<>(); + } + + /** + * @return An instance of this class. Always returns the same instance. + */ + public static AppData getInstance() { + if (instance == null) { + instance = new AppData(); + } + return instance; + } + + /** + * @return The AppConfig + */ + public AppConfig getAppConfig() { + return appConfig; + } + + /** + * Sets the AppConfig. This method sets the AppConfig for once and for all. + * It is locked after first call. + * + * @param appConfig The network configuration of the program. + */ + public void setAppConfig(AppConfig appConfig) { + if (this.appConfig == null) { + this.appConfig = appConfig; + } + } + + /** + * @param userName The name of the user. + * @return The user corresponding to userName, null otherwise. + */ + public User getUser(String userName) { + return this.users.get(userName); + } + + /** + * Use this method when a user signs up. + * + * @param user The user to add. + * @return True if the user was added. False if a user with the same name already exists. + */ + public boolean addUser(User user) { + if (this.users.containsKey(user.getName())) { + return false; + } else { + this.users.put(user.getName(), user); + return true; + } + } + + /** + * @param fileName The name of the file + * @param user The user + * @return The file corresponding to the given name and belonging to the user. Null if the user cannot be found or the file cannot be found + * @deprecated Maybe not useful. DO NOT USE FOR THE TIME BEING + */ + public File getFileOf(String fileName, User user) { + return this.users.get(user.getName()).getFile(fileName); + } + + /** + * Call this method after receiving SAVEFILE_OK from the StorBackEnd. + * Do NOT call when receiving SAVEFILE_ERROR, or it will break the system's synchronization. + *

+ * Adds the file of for a specific user. + * True indicates the success of the operation. + * False indicates the failure of the operation. + * + * @param file The file to add + * @param user The user who wants to add the file + * @return True if the user is found and a file with the same name doesn't already exist for this user. False otherwise. + */ + public boolean addFileFor(File file, User user) { + if (!this.users.containsKey(user.getName())) { + return false; + } else { + return this.users.get(user.getName()).addFile(file); + } + } + + /** + * Call this method after receiving REMOVEFILE_OK from the StorBackEnd. + * Do NOT call when receiving REMOVEFILE_ERROR, or it will break the system's synchronization. + * Deletes the file of for a specific user. + * True indicates the success of the operation. + * False indicates the failure of the operation. + * + * @param fileName The name of the file to delete + * @param user The user who wants to delete the file + * @return True if the user is found and the file was deleted. False otherwise. + */ + public boolean deleteFileOf(String fileName, User user) { + if (!this.users.containsKey(user.getName())) { + return false; + } else { + return this.users.get(user.getName()).deleteFile(fileName); + } + } +}