package src.main; import java.time.LocalDate; import java.util.AbstractMap; import java.util.HashMap; import java.util.List; import java.io.IOException; import java.lang.Object; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; 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[] { "address", "port", "username", "password", "ssl", "defaultTable" }); // 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())) || firstTimeSetup) { writeConfig(Paths.get(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey()), DATABASE_CONFIG.getValue()); } } // 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(); for (String entry : configContent) { getUserInputConfig(configData, inputScanner, entry); } JSONObject json = new JSONObject(configData); try { Files.writeString(configPath, json.toString(), StandardCharsets.UTF_8); } catch (IOException error) { error.printStackTrace(); } } // Reads out the config based on the config file Path public HashMap readConfig(Path configPath) { 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; } // 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) { // Ensure the source path exists. if (!Files.exists(source) || !Files.isDirectory(source)) { 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 { // Create destination directory. Files.createDirectories(destination); } catch (IOException error) { error.printStackTrace(); System.err.println("Failed to create destination address \"" + destination.toString() + "\""); 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 List fileList = pathStream .filter(Files::isRegularFile) .collect(Collectors.toList()); // Copy the file to the backup directory, replacing existing ones for(Path filePath : fileList) { String newFileName = filePath.getFileName().toString() + LocalDate.now(); System.out.println(newFileName); System.out.println(Path.of(destination.toString(), newFileName).toString()); Files.copy(filePath, Path.of(destination.toString(), newFileName), StandardCopyOption.REPLACE_EXISTING); } } catch (IOException e) { e.printStackTrace(); System.err.println("Failed to copy file "); } } private void getUserInputConfig(HashMap config, Scanner scanner, String configEntry) { System.out.println("Please enter " + configEntry); config.put(configEntry, scanner.nextLine()); } }