1
0
Fork 0

Update configuration system

This commit is contained in:
Ethanell 2021-01-12 15:01:02 +01:00
parent c58cfaf148
commit b2fb461fd4
3 changed files with 87 additions and 29 deletions

View file

@ -0,0 +1,76 @@
package Config;
import Exceptions.InvalidConfig;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.Map;
public class Config {
static private final JSONObject template = new JSONObject(
Map.ofEntries(
Map.entry("db",
new JSONObject(Map.ofEntries(Map.entry("host", ""),
Map.entry("database", ""),
Map.entry("user", ""),
Map.entry("password", "")))
),
Map.entry("startDate", ""),
Map.entry("endDate", "")
)
);
static private void createConfig() {
File f = new File("./config.json");
try {
FileWriter fw = new FileWriter(f);
fw.write(template.toJSONString());
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
static private boolean checkConfig(JSONObject jsonObject, JSONObject template) {
for (Object k : template.keySet().toArray())
if (!jsonObject.containsKey(k) ||
(template.get(k).getClass() == JSONObject.class &&
!checkConfig((JSONObject) jsonObject.get(k), (JSONObject) template.get(k))))
return false;
return true;
}
static private JSONObject getConfig() throws InvalidConfig {
try {
File f = new File("./config.json");
if (!f.exists())
createConfig();
JSONObject config = (JSONObject) new JSONParser().parse(new FileReader(f));
if (checkConfig(config, template))
return config;
else
throw new InvalidConfig();
} catch (ParseException | IOException e) {
e.printStackTrace();
}
return null;
}
static public JSONObject getDBAuth() throws InvalidConfig {
return (JSONObject) Config.getConfig().get("db");
}
static public LocalDate getStartDate() throws InvalidConfig {
return (LocalDate) Config.getConfig().get("startDate");
}
static public LocalDate getEndDate() throws InvalidConfig {
return (LocalDate) Config.getConfig().get("endDate");
}
}

View file

@ -1,13 +1,12 @@
package DB; package DB;
import Config.Config;
import Exceptions.AlreadyOnTable; import Exceptions.AlreadyOnTable;
import Exceptions.InvalidConfig;
import Exceptions.NotFoundInTable; import Exceptions.NotFoundInTable;
import lombok.Getter; import lombok.Getter;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.*;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.sql.*; import java.sql.*;
import java.sql.Date; import java.sql.Date;
@ -16,39 +15,15 @@ import java.util.*;
public class DB { public class DB {
@Getter static private Connection connection; @Getter static private Connection connection;
static private JSONObject getAuth() {
try {
File f = new File("./config.json");
if (!f.exists()) {
FileWriter fw = new FileWriter(f);
fw.write(
new JSONObject(
Map.ofEntries(
Map.entry("host", ""),
Map.entry("database", ""),
Map.entry("user", ""),
Map.entry("password", "")
)
).toJSONString()
);
fw.close();
}
return (JSONObject) new JSONParser().parse(new FileReader(f));
} catch (ParseException | IOException e) {
e.printStackTrace();
return null;
}
}
static public boolean connect() { static public boolean connect() {
try { try {
if (connection == null || connection.isClosed()) { if (connection == null || connection.isClosed()) {
JSONObject auth = getAuth(); JSONObject auth = Config.getDBAuth();
if (auth != null) if (auth != null)
connection = DriverManager.getConnection("jdbc:mariadb://"+auth.get("host")+"/"+auth.get("database")+"?user="+auth.get("user")+"&password="+auth.get("password")); connection = DriverManager.getConnection("jdbc:mariadb://"+auth.get("host")+"/"+auth.get("database")+"?user="+auth.get("user")+"&password="+auth.get("password"));
return true; return true;
} }
} catch (SQLException e) { } catch (SQLException | InvalidConfig e) {
return false; return false;
} }
return false; return false;

View file

@ -0,0 +1,7 @@
package Exceptions;
public class InvalidConfig extends Exception {
public InvalidConfig() {
super("Configuration file is invalid");
}
}