diff --git a/src/main/ConfigManager.java b/src/main/ConfigManager.java index f0a8e1d..ea42cbd 100644 --- a/src/main/ConfigManager.java +++ b/src/main/ConfigManager.java @@ -18,40 +18,47 @@ import org.json.JSONObject; import java.util.Scanner; import java.util.stream.Collectors; import java.util.stream.Stream; + import java.util.Map; +import src.main.utilities.JsonUtils; + + public class ConfigManager { private static final Path CONFIGS_DIR = Paths.get("src/resources/configs"); private static final Path CONFIGS_BACKUP_DIR = Paths.get("src/resources/configs/backups"); + // Map entry of database config contents and file path private static final Map.Entry DATABASE_CONFIG = new AbstractMap.SimpleEntry("database.json", - new String[]{ - "DatabaseAddress", - "DataBasePort", - "DataBaseUsername", - "DataBasePassword" + new String[] { + "address", + "port", + "username", + "password", + "ssl", + "defaultTable" }); - public ConfigManager () { + // Default constructor + public ConfigManager() { + + } + + // Boolean constructor for instanciating first time setup + public ConfigManager(boolean firstTimeSetup) { makeBackups(CONFIGS_DIR, CONFIGS_BACKUP_DIR); - if(!Files.exists(Path.of(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey()))) { + if(!Files.exists(Path.of(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey())) || firstTimeSetup) { writeConfig(Paths.get(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey()), DATABASE_CONFIG.getValue()); } - - //HashMap databaseConfig; - //databaseConfig = readConfig(Paths.get(CONFIGS_DIR.toString(), "database.json")); - //System.out.println(databaseConfig); - } - - private void getUserInputConfig(HashMap config, Scanner scanner, String configEntry) { - System.out.println("Please enter " + configEntry); - config.put(configEntry, scanner.nextLine()); + } + // Writes the config content to the config path private void writeConfig(Path configPath, String[] configContent) { + Scanner inputScanner = new Scanner(System.in); HashMap configData = new HashMap(); @@ -69,18 +76,42 @@ public class ConfigManager { } - private HashMap readConfig(Path configPath) { + // Reads out the config based on the config file Path + public HashMap readConfig(Path configPath) { - HashMap config = new HashMap(); + HashMap configContents = new HashMap(); try { String configContent = new String(Files.readAllBytes(configPath)); JSONObject jsonObject = new JSONObject(configContent); - System.out.println(jsonObject); + configContents = JsonUtils.jsonToHashMap(jsonObject); } catch (Exception error) { error.printStackTrace(); } - return config; + + return configContents; + } + + // Reads out the config based on the config file String + public HashMap readConfig(String config) { + + // Checks if the string passed is of type .json + if (config.length() > 5 && config.substring(config.length() - 4) == ".json") { + System.out.println("Getting config "); + } + + Path configPath = Path.of(CONFIGS_DIR.toString(), config); + HashMap configContents = new HashMap(); + + try { + String configContent = new String(Files.readAllBytes(configPath)); + JSONObject jsonObject = new JSONObject(configContent); + configContents = JsonUtils.jsonToHashMap(jsonObject); + } catch (Exception error) { + error.printStackTrace(); + } + + return configContents; } private void makeBackups(Path source, Path destination) { @@ -89,7 +120,6 @@ public class ConfigManager { System.err.println("Source directory does not exist or is not a directory."); return; } - // Ensure the destination exists, if not create it if (!Files.exists(destination)) { try { @@ -101,7 +131,6 @@ public class ConfigManager { return; } } - // Attempt to copy files from source to destination try (Stream pathStream = Files.list(source)) { // Get a list of files, filter out non-file types @@ -124,4 +153,11 @@ public class ConfigManager { } + private void getUserInputConfig(HashMap config, Scanner scanner, String configEntry) { + + System.out.println("Please enter " + configEntry); + config.put(configEntry, scanner.nextLine()); + + } + } diff --git a/src/main/utilities/Configs.java b/src/main/utilities/Configs.java new file mode 100644 index 0000000..552b20d --- /dev/null +++ b/src/main/utilities/Configs.java @@ -0,0 +1,16 @@ +package src.main.utilities; + +public enum Configs { + + database("database.json"); + + private final String fileName; + + private Configs(String fileName) { + this.fileName = fileName; + } + + public String getFileName() { + return fileName; + } +} diff --git a/src/main/utilities/JsonUtils.java b/src/main/utilities/JsonUtils.java new file mode 100644 index 0000000..2e3da84 --- /dev/null +++ b/src/main/utilities/JsonUtils.java @@ -0,0 +1,68 @@ +package src.main.utilities; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +public class JsonUtils { + + public static HashMap jsonToHashMap(JSONObject json) throws JSONException { + if (json == null || json == JSONObject.NULL) { + return new HashMap<>(); + } + return new HashMap<>(toMap(json)); + } + + public static Map jsonToMap(JSONObject json) throws JSONException { + if (json == null || json == JSONObject.NULL) { + return new HashMap<>(); + } + return toMap(json); + } + + private static Map toMap(JSONObject object) throws JSONException { + Map map = new HashMap<>(); + + Iterator keysItr = object.keys(); + while (keysItr.hasNext()) { + String key = keysItr.next(); + Object value = object.get(key); + + if (value == JSONObject.NULL) { + value = null; + } else if (value instanceof JSONArray) { + value = toList((JSONArray) value); + } else if (value instanceof JSONObject) { + value = toMap((JSONObject) value); + } + + map.put(key, value); + } + return map; + } + + private static List toList(JSONArray array) throws JSONException { + List list = new ArrayList<>(); + + for (int i = 0; i < array.length(); i++) { + Object value = array.get(i); + + if (value == JSONObject.NULL) { + value = null; + } else if (value instanceof JSONArray) { + value = toList((JSONArray) value); + } else if (value instanceof JSONObject) { + value = toMap((JSONObject) value); + } + + list.add(value); + } + return list; + } +} \ No newline at end of file