From 0ace68267415f84b44372205418715563240c2e7 Mon Sep 17 00:00:00 2001 From: wrushton Date: Wed, 25 Mar 2026 16:54:05 +1300 Subject: [PATCH] Added crude writting of database config --- src/main/ConfigManager.java | 125 ++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/main/ConfigManager.java diff --git a/src/main/ConfigManager.java b/src/main/ConfigManager.java new file mode 100644 index 0000000..625aa4c --- /dev/null +++ b/src/main/ConfigManager.java @@ -0,0 +1,125 @@ +package src.main; + +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; + +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"); + + private static final Map.Entry DATABASE_CONFIG = new AbstractMap.SimpleEntry("database.json", + new String[]{ + "DatabaseAddress", + "DataBasePort", + "DataBaseUsername", + "DataBasePassword" + }); + + public ConfigManager () { + + makeBackups(CONFIGS_DIR, CONFIGS_BACKUP_DIR); + + if(!Files.exists(Path.of(CONFIGS_DIR.toString(), DATABASE_CONFIG.getKey()))) { + 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()); + } + + 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(); + } + + } + + private HashMap readConfig(Path configPath) { + + HashMap config = new HashMap(); + + try { + String configContent = new String(Files.readAllBytes(configPath)); + JSONObject jsonObject = new JSONObject(configContent); + System.out.println(jsonObject); + } catch (Exception error) { + error.printStackTrace(); + } + return config; + } + + 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() + ".bak"; + 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 "); + } + + } + +}