[DEV] update some models

This commit is contained in:
Edouard DUPIN 2023-10-25 10:15:54 +02:00
parent 9730b89b15
commit 88b945285b
5 changed files with 108 additions and 42 deletions

View File

@ -17,7 +17,7 @@ public class DBEntry implements Closeable {
public Connection connection; public Connection connection;
private static List<DBEntry> stored = new ArrayList<>(); private static List<DBEntry> stored = new ArrayList<>();
private DBEntry(DBConfig config, boolean root) throws IOException { private DBEntry(final DBConfig config, final boolean root) throws IOException {
this.config = config; this.config = config;
if (root) { if (root) {
connectRoot(); connectRoot();
@ -26,13 +26,13 @@ public class DBEntry implements Closeable {
} }
} }
public static DBEntry createInterface(DBConfig config) throws IOException { public static DBEntry createInterface(final DBConfig config) throws IOException {
return createInterface(config, false); return createInterface(config, false);
} }
public static DBEntry createInterface(DBConfig config, boolean root) throws IOException { public static DBEntry createInterface(final DBConfig config, final boolean root) throws IOException {
if (config.getKeepConnected()) { if (config.getKeepConnected()) {
for (DBEntry elem : stored) { for (final DBEntry elem : stored) {
if (elem == null) { if (elem == null) {
continue; continue;
} }
@ -40,7 +40,7 @@ public class DBEntry implements Closeable {
return elem; return elem;
} }
} }
DBEntry tmp = new DBEntry(config, root); final DBEntry tmp = new DBEntry(config, root);
stored.add(tmp); stored.add(tmp);
return tmp; return tmp;
} else { } else {
@ -50,8 +50,8 @@ public class DBEntry implements Closeable {
public void connectRoot() throws IOException { public void connectRoot() throws IOException {
try { try {
connection = DriverManager.getConnection(config.getUrl(true), config.getLogin(), config.getPassword()); this.connection = DriverManager.getConnection(this.config.getUrl(true), this.config.getLogin(), this.config.getPassword());
} catch (SQLException ex) { } catch (final SQLException ex) {
throw new IOException("Connection db fail: " + ex.getMessage()); throw new IOException("Connection db fail: " + ex.getMessage());
} }
@ -59,8 +59,8 @@ public class DBEntry implements Closeable {
public void connect() throws IOException { public void connect() throws IOException {
try { try {
connection = DriverManager.getConnection(config.getUrl(), config.getLogin(), config.getPassword()); this.connection = DriverManager.getConnection(this.config.getUrl(), this.config.getLogin(), this.config.getPassword());
} catch (SQLException ex) { } catch (final SQLException ex) {
throw new IOException("Connection db fail: " + ex.getMessage()); throw new IOException("Connection db fail: " + ex.getMessage());
} }
@ -68,15 +68,24 @@ public class DBEntry implements Closeable {
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (config.getKeepConnected()) { if (this.config.getKeepConnected()) {
return; return;
} }
closeForce();
}
public void closeForce() throws IOException {
try { try {
//connection.commit(); //connection.commit();
connection.close(); this.connection.close();
} catch (SQLException ex) { } catch (final SQLException ex) {
throw new IOException("Dis-connection db fail: " + ex.getMessage()); throw new IOException("Dis-connection db fail: " + ex.getMessage());
} }
}
public static void closeAllForceMode() throws IOException {
for (final DBEntry entry : stored) {
entry.closeForce();
}
} }
} }

View File

@ -14,7 +14,6 @@ import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType; import jakarta.persistence.GenerationType;
import jakarta.persistence.Id; import jakarta.persistence.Id;
//@SQLWhere(clause = "deleted=false")
public class GenericTable { public class GenericTable {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)

View File

@ -0,0 +1,31 @@
package org.kar.archidata.model;
import java.sql.Timestamp;
import org.kar.archidata.annotation.CreationTimestamp;
import org.kar.archidata.annotation.SQLComment;
import org.kar.archidata.annotation.SQLNotRead;
import org.kar.archidata.annotation.UpdateTimestamp;
import jakarta.persistence.Column;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
public class GenericTableHardDelete {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false, unique = true)
@SQLComment("Primary key of the base")
public Long id = null;
@SQLNotRead
@CreationTimestamp
@Column(nullable = false)
@SQLComment("Create time of the object")
public Timestamp createdAt = null;
@SQLNotRead
@UpdateTimestamp
@Column(nullable = false)
@SQLComment("When update the object")
public Timestamp updatedAt = null;
}

View File

@ -1,19 +1,39 @@
package org.kar.archidata.util; package org.kar.archidata.util;
public class ConfigBaseVariable { public class ConfigBaseVariable {
static public String tmpDataFolder = System.getenv("DATA_TMP_FOLDER"); static public String tmpDataFolder;
static public String dataFolder = System.getenv("DATA_FOLDER"); static public String dataFolder;
static public String dbType = System.getenv("DB_TYPE"); static public String dbType;
static public String dbHost = System.getenv("DB_HOST"); static public String dbHost;
static public String dbPort = System.getenv("DB_PORT"); static public String dbPort;
static public String dbUser = System.getenv("DB_USER"); static public String dbUser;
static public String dbKeepConnected = System.getenv("DB_KEEP_CONNECTED"); static public String dbKeepConnected;
static public String dbPassword = System.getenv("DB_PASSWORD"); static public String dbPassword;
static public String bdDatabase = System.getenv("DB_DATABASE"); static public String bdDatabase;
static public String apiAdress = System.getenv("API_ADDRESS"); static public String apiAdress;
static public String ssoAdress = System.getenv("SSO_ADDRESS"); static public String ssoAdress;
static public String ssoToken = System.getenv("SSO_TOKEN"); static public String ssoToken;
// For test only
public static void clearAllValue() {
tmpDataFolder = System.getenv("DATA_TMP_FOLDER");
dataFolder = System.getenv("DATA_FOLDER");
dbType = System.getenv("DB_TYPE");
dbHost = System.getenv("DB_HOST");
dbPort = System.getenv("DB_PORT");
dbUser = System.getenv("DB_USER");
dbKeepConnected = System.getenv("DB_KEEP_CONNECTED");
dbPassword = System.getenv("DB_PASSWORD");
bdDatabase = System.getenv("DB_DATABASE");
apiAdress = System.getenv("API_ADDRESS");
ssoAdress = System.getenv("SSO_ADDRESS");
ssoToken = System.getenv("SSO_TOKEN");
}
static {
clearAllValue();
}
public static String getTmpDataFolder() { public static String getTmpDataFolder() {
if (tmpDataFolder == null) { if (tmpDataFolder == null) {
return "/application/data/tmp"; return "/application/data/tmp";

View File

@ -1,5 +1,7 @@
package test.kar.archidata; package test.kar.archidata;
import java.io.IOException;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
@ -7,35 +9,40 @@ import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestMethodOrder;
import org.kar.archidata.sqlWrapper.SqlWrapper; import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.RESTApi; import org.kar.archidata.util.ConfigBaseVariable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class TestBase { public class TestBase {
final static Logger logger = LoggerFactory.getLogger(TestBase.class); final static private Logger LOGGER = LoggerFactory.getLogger(TestBase.class);
static RESTApi api = null;
@BeforeAll @BeforeAll
public static void configureWebServer() throws Exception { public static void configureWebServer() throws Exception {
logger.info("Create DB"); /*LOGGER.info("Create DB");
final String dbName = "sdfsdfsdfsfsdfsfsfsfsdfsdfsd"; final String dbName = "sdfsdfsdfsfsdfsfsfsfsdfsdfsd";
boolean data = SqlWrapper.isDBExist(dbName); boolean data = SqlWrapper.isDBExist(dbName);
logger.error("exist: {}", data); LOGGER.error("exist: {}", data);
data = SqlWrapper.createDB(dbName); data = SqlWrapper.createDB(dbName);
logger.error("create: {}", data); LOGGER.error("create: {}", data);
data = SqlWrapper.isDBExist(dbName); data = SqlWrapper.isDBExist(dbName);
logger.error("exist: {}", data); LOGGER.error("exist: {}", data);
} */
ConfigBaseVariable.dbType = "sqlite";
@AfterAll ConfigBaseVariable.dbHost = "memory";
public static void stopWebServer() throws InterruptedException { // for test we need to connect all time the DB
logger.info("Kill the web server"); ConfigBaseVariable.dbKeepConnected = "true";
// TODO: do it better...
} }
@AfterAll
public static void removeDataBase() throws IOException {
LOGGER.info("Remove the test db");
DBEntry.closeAllForceMode();
ConfigBaseVariable.clearAllValue();
}
@Order(1) @Order(1)
@Test @Test
public void checkSimpleTestError() throws Exception { public void checkSimpleTestError() throws Exception {