197 lines
5.7 KiB
Java
197 lines
5.7 KiB
Java
package lightcontainer.storage;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
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 <m.ledoux@student.helmo.be>
|
|
* @version 1.0
|
|
* @see AppConfig
|
|
* @see User
|
|
* @since 1.0
|
|
*/
|
|
public class AppData {
|
|
|
|
private static AppData instance = null;
|
|
private AppConfig appConfig;
|
|
private final Map<String, User> 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);
|
|
}
|
|
|
|
public List<String> getStringifiedFilesOf(String login) {
|
|
User user = users.get(login);
|
|
if (user != null) {
|
|
return user.getFilesWithSize();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
|
|
/**
|
|
* Use this method when a user signs up.
|
|
*
|
|
* @return True if the user was added. False if a user with the same name already exists.
|
|
*/
|
|
public boolean addUser(String login, String password, String key, String passwordSalt) {
|
|
User user = new User(login, password, key, passwordSalt, new HashMap<>());
|
|
if (this.users.containsKey(user.getName())) {
|
|
return false;
|
|
} else {
|
|
this.users.put(user.getName(), user);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Use this method when a user signs up.
|
|
*
|
|
* @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;
|
|
}
|
|
}
|
|
|
|
public boolean canAddFile(String login) {
|
|
if (this.users.containsKey(login)) {
|
|
return this.users.get(login).canAddFile();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public Iterator<User> usersIterator() {
|
|
return users.values().iterator();
|
|
}
|
|
|
|
|
|
/**
|
|
* 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.
|
|
* <p>
|
|
* 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 userName The name of 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, String userName) {
|
|
if (!this.users.containsKey(userName)) {
|
|
return false;
|
|
} else {
|
|
return this.users.get(userName).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 userName The name of 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, String userName) {
|
|
if (!this.users.containsKey(userName)) {
|
|
return false;
|
|
} else {
|
|
return this.users.get(userName).deleteFile(fileName);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param userName The name of the user who wants to add a storage for their file
|
|
* @param file The file that needs a new storage
|
|
* @param storage The storage to add
|
|
* @return True if the storage was added. False otherwise.
|
|
*/
|
|
public boolean addStorage(String userName, File file, String storage) {
|
|
if (!this.users.containsKey(userName)) {
|
|
return false;
|
|
} else {
|
|
return this.users.get(userName).addStorage(file, storage);
|
|
}
|
|
}
|
|
|
|
public boolean verifyUser(String login, String password) {
|
|
User user = getUser(login);
|
|
return user != null && user.verifyPassword(password);
|
|
}
|
|
|
|
public String getUserPasswordSalt(String login) {
|
|
User user = getUser(login);
|
|
return user == null ? null : user.getPasswordSalt();
|
|
}
|
|
|
|
|
|
/**
|
|
* Méthode permettant de récupérer la clé AES d'un utilisateur
|
|
*
|
|
* @param login Login de l'utilisateur
|
|
* @return Clé AES
|
|
*/
|
|
public String getUserAesKey(String login) {
|
|
User user = getUser(login);
|
|
return user == null ? null : user.getAesKey();
|
|
}
|
|
|
|
}
|