|
@@ -9,6 +9,7 @@ import java.util.logging.Logger;
|
|
|
|
|
|
import db.mapper.Mapper;
|
|
import db.mapper.Mapper;
|
|
|
|
|
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
import java.sql.Connection;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.DriverManager;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.PreparedStatement;
|
|
@@ -34,7 +35,7 @@ public class Database {
|
|
throw new IllegalStateException("Unable to connect to the database", e);
|
|
throw new IllegalStateException("Unable to connect to the database", e);
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
- db = DriverManager.getConnection("jdbc:derby:microfolie;create=true");
|
|
|
|
|
|
+ db = DriverManager.getConnection(DatabaseProperties.get("jdbc.url"));
|
|
} catch (SQLException e) {
|
|
} catch (SQLException e) {
|
|
LOGGER.warning(e.getMessage());
|
|
LOGGER.warning(e.getMessage());
|
|
throw new IllegalStateException("Unable to connect to the database", e);
|
|
throw new IllegalStateException("Unable to connect to the database", e);
|
|
@@ -58,12 +59,16 @@ public class Database {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
LOGGER.info("Creating table");
|
|
LOGGER.info("Creating table");
|
|
- ManageTable.create.forEach((table, sql) -> {
|
|
|
|
|
|
+ ManageDatabase md = getDatabaseManager();
|
|
|
|
+ md.create().forEach((table, sql) -> {
|
|
LOGGER.info("Creating table " + table);
|
|
LOGGER.info("Creating table " + table);
|
|
execute(sql);
|
|
execute(sql);
|
|
});
|
|
});
|
|
- LOGGER.info("Add mock");
|
|
|
|
- ManageTable.mock.forEach(req -> execute(req));
|
|
|
|
|
|
+ List<String> content = md.content();
|
|
|
|
+ if(content != null) {
|
|
|
|
+ LOGGER.info("Add content");
|
|
|
|
+ content.forEach(req -> execute(req));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
public static void unset() {
|
|
public static void unset() {
|
|
@@ -71,7 +76,8 @@ public class Database {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
LOGGER.info("Dropping table");
|
|
LOGGER.info("Dropping table");
|
|
- ManageTable.drop.forEach((table, sql) -> {
|
|
|
|
|
|
+ ManageDatabase md = getDatabaseManager();
|
|
|
|
+ md.drop().forEach((table, sql) -> {
|
|
LOGGER.info("Dropping table " + table);
|
|
LOGGER.info("Dropping table " + table);
|
|
execute(sql);
|
|
execute(sql);
|
|
});
|
|
});
|
|
@@ -172,5 +178,18 @@ public class Database {
|
|
public static Connection getDb() {
|
|
public static Connection getDb() {
|
|
return db;
|
|
return db;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private static ManageDatabase getDatabaseManager() {
|
|
|
|
+ try {
|
|
|
|
+ String className = DatabaseProperties.get("database.manager");
|
|
|
|
+ Class<?> clazz = Class.forName(className);
|
|
|
|
+ ManageDatabase md = (ManageDatabase) clazz.getMethod("getInstance").invoke(null);
|
|
|
|
+ return md;
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
+ throw new IllegalStateException("Unable to find Database Manager Class", e);
|
|
|
|
+ } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
|
|
|
|
+ throw new IllegalStateException("Database Manager Class is not a child of ManagaDatabase Class or no getInstance method found", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|