mirror of
https://git.theprivateserver.de/cbu615/ChatStatistikTool.git
synced 2026-02-15 13:10:41 +00:00
init
This commit is contained in:
105
src/util/Config.java
Normal file
105
src/util/Config.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package util;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.*;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Globale Konfiguration des Schulungsstatistik-Tools.
|
||||
* - Erstellt automatisch ~/.schulungsstatistik/config.properties
|
||||
* - Lädt vorhandene Werte oder setzt Standardwerte
|
||||
* - Speichert Änderungen automatisch
|
||||
*/
|
||||
public class Config {
|
||||
|
||||
private static final Properties props = new Properties();
|
||||
|
||||
// Standardpfad für die Datenbank
|
||||
private static final String dbPath_default = "Y:\\Gruppen\\Bibliothek-Oeffentliches\\14. Information\\14.1 Informations- und Schulungsstatistik\\system\\schulungsstatistik.db";
|
||||
private static String dbPath = dbPath_default;
|
||||
|
||||
// Speicherort der Config-Datei
|
||||
private static final Path CONFIG_DIR =
|
||||
Paths.get(System.getProperty("user.home"), ".schulungsstatistik");
|
||||
private static final Path CONFIG_FILE = CONFIG_DIR.resolve("config.properties");
|
||||
|
||||
// Wird beim Laden der Klasse ausgeführt
|
||||
static {
|
||||
load();
|
||||
}
|
||||
|
||||
/** Setzt den Pfad zur Datenbank und speichert ihn sofort */
|
||||
public static void setDbPath(String path) {
|
||||
if (path != null && !path.isBlank()) {
|
||||
dbPath = path;
|
||||
props.setProperty("db.path", path);
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
/** Wird von der Config-UI verwendet, um neuen Pfad zu speichern */
|
||||
public static void saveDbPathFromUI(String newPath) {
|
||||
if (newPath == null || newPath.isBlank()) return;
|
||||
setDbPath(newPath);
|
||||
System.out.println("Datenbankpfad aktualisiert: " + newPath);
|
||||
}
|
||||
|
||||
public static String getDbPath() {
|
||||
if (dbPath == null || dbPath.isBlank() || !Files.exists(Paths.get(dbPath))) {
|
||||
System.err.println("Warnung: Ungültiger Datenbankpfad, Standardwert wird verwendet.");
|
||||
dbPath = dbPath_default;
|
||||
props.setProperty("db.path", dbPath_default);
|
||||
save();
|
||||
}
|
||||
return dbPath;
|
||||
}
|
||||
|
||||
public static void load() {
|
||||
try {
|
||||
if (!Files.exists(CONFIG_DIR)) {
|
||||
Files.createDirectories(CONFIG_DIR);
|
||||
}
|
||||
|
||||
if (Files.exists(CONFIG_FILE)) {
|
||||
try (InputStream in = Files.newInputStream(CONFIG_FILE)) {
|
||||
props.load(in);
|
||||
String loaded = props.getProperty("db.path");
|
||||
if (loaded == null || loaded.isBlank() || !Files.exists(Paths.get(loaded))) {
|
||||
dbPath = dbPath_default;
|
||||
props.setProperty("db.path", dbPath_default);
|
||||
save();
|
||||
} else {
|
||||
dbPath = loaded;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
dbPath = dbPath_default;
|
||||
props.setProperty("db.path", dbPath_default);
|
||||
save();
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Fehler beim Laden der Konfiguration: " + e.getMessage());
|
||||
dbPath = dbPath_default; // Fallback
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Speichert aktuelle Werte in die Config-Datei */
|
||||
public static void save() {
|
||||
try {
|
||||
if (!Files.exists(CONFIG_DIR)) {
|
||||
Files.createDirectories(CONFIG_DIR);
|
||||
}
|
||||
|
||||
try (OutputStream out = Files.newOutputStream(CONFIG_FILE)) {
|
||||
props.store(out, "Schulungsstatistik Tool Konfiguration");
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
System.err.println("Fehler beim Speichern der Konfiguration: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user