[DEV] update options and where condition, some normalisation

This commit is contained in:
Edouard DUPIN 2023-11-25 14:13:14 +01:00
parent ed3bfa0604
commit c3d2eff5be
46 changed files with 527 additions and 292 deletions

View File

@ -1,7 +1,7 @@
package org.kar.archidata;
import org.kar.archidata.db.DBConfig;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
public class GlobalConfiguration {
public static DBConfig dbConfig = null;

View File

@ -1,7 +1,7 @@
package org.kar.archidata;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.util.JWTWrapper;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.kar.archidata.tools.JWTWrapper;
public class UpdateJwtPublicKey extends Thread {
boolean kill = false;

View File

@ -6,6 +6,7 @@ import java.util.ArrayList;
import java.util.List;
import org.kar.archidata.dataAccess.QueryOptions;
import org.kar.archidata.dataAccess.options.OverrideTableName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -20,11 +21,9 @@ public class AnnotationTools {
public static String getTableName(final Class<?> clazz, final QueryOptions options) throws Exception {
if (options != null) {
final Object data = options.get(QueryOptions.OVERRIDE_TABLE_NAME);
if (data instanceof final String optionString) {
return optionString;
} else if (data != null) {
LOGGER.error("'{}' ==> has not a String value: {}", QueryOptions.SQL_DELETED_DISABLE, data);
final OverrideTableName data = options.get(OverrideTableName.class);
if (data != null) {
return data.getName();
}
}
return AnnotationTools.getTableName(clazz);

View File

@ -26,7 +26,7 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.QueryCondition;
import org.kar.archidata.filter.GenericContext;
import org.kar.archidata.model.Data;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -1,16 +1,20 @@
package org.kar.archidata.dataAccess;
/** Java does not permit to set return data (eg: integer) in the function parameter. This class permit to update a value as in/out function parameters. */
public class CountInOut {
// internal value of the stream
public int value = 0;
public CountInOut() {
// TODO Auto-generated constructor stub
}
/** Default constructor */
public CountInOut() {}
/** Constructor with the initial value.
* @param i Initial Value */
public CountInOut(final int i) {
this.value = i;
}
/** Increment by one the value. */
public void inc() {
this.value++;
}

View File

@ -25,9 +25,13 @@ import org.kar.archidata.dataAccess.addOn.AddOnDataJson;
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
import org.kar.archidata.dataAccess.addOn.AddOnManyToOne;
import org.kar.archidata.dataAccess.addOn.AddOnSQLTableExternalForeinKeyAsList;
import org.kar.archidata.dataAccess.options.AccessDeletedItems;
import org.kar.archidata.dataAccess.options.CheckFunction;
import org.kar.archidata.dataAccess.options.Limit;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.exception.DataAccessException;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.kar.archidata.tools.DateTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -166,7 +170,7 @@ public class DataAccess {
* @param rs Result Set of the BDD
* @param iii Id in the result set
* @return The list of Long value
* @throws SQLException if an error is generated in the sql request. */
* @throws SQLException if an error is generated in the SQL request. */
public static List<Long> getListOfIds(final ResultSet rs, final int iii, final String separator) throws SQLException {
final String trackString = rs.getString(iii);
if (rs.wasNull()) {
@ -278,14 +282,12 @@ public class DataAccess {
iii.inc();
}
// TODO: maybe wrap this if the use of sqlite ==> maybe some problems came with sqlite ...
protected static <T> void setValueFromDb(final Class<?> type, final Object data, final CountInOut count, final Field field, final ResultSet rs, final CountInOut countNotNull) throws Exception {
if (type == Long.class) {
final Long tmp = rs.getLong(count.value);
if (rs.wasNull()) {
field.set(data, null);
} else {
// logger.debug(" ==> {}", tmp);
field.set(data, tmp);
countNotNull.inc();
}
@ -381,11 +383,12 @@ public class DataAccess {
} catch (final SQLException ex) {
final String tmp = rs.getString(count.value);
LOGGER.error("Fail to parse the SQL time !!! {}", tmp);
LOGGER.error("Fail to parse the SQL time !!! {}", Date.parse(tmp));
if (rs.wasNull()) {
field.set(data, null);
} else {
field.set(data, Date.parse(tmp));
final Date date = DateTools.parseDate(tmp);
LOGGER.error("Fail to parse the SQL time !!! {}", date);
field.set(data, date);
countNotNull.inc();
}
}
@ -418,15 +421,19 @@ public class DataAccess {
if (rs.wasNull()) {
field.set(data, null);
} else {
boolean find = false;
final Object[] arr = type.getEnumConstants();
for (final Object elem : arr) {
if (elem.toString().equals(tmp)) {
field.set(data, elem);
countNotNull.inc();
find = true;
break;
}
}
// TODO: maybe do something stupid if not exist ???
if (!find) {
throw new DataAccessException("Enum value does not exist in the Model: '" + tmp + "'");
}
}
} else {
throw new DataAccessException("Unknown Field Type");
@ -463,6 +470,14 @@ public class DataAccess {
public static <T> T insert(final T data, final QueryOptions options) throws Exception {
final Class<?> clazz = data.getClass();
// External checker of data:
if (options != null) {
final CheckFunction check = options.get(CheckFunction.class);
if (check != null) {
check.getChecker().check(data, null);
}
}
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
// real add in the BDD:
try {
@ -672,6 +687,14 @@ public class DataAccess {
final Class<?> clazz = data.getClass();
// public static NodeSmall createNode(String typeInNode, String name, String description, Long parentId) {
// External checker of data:
if (options != null) {
final CheckFunction check = options.get(CheckFunction.class);
if (check != null) {
check.getChecker().check(data, filterValue);
}
}
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
// real add in the BDD:
try {
@ -803,12 +826,7 @@ public class DataAccess {
throws ExceptionDBInterface {
boolean exclude_deleted = true;
if (options != null) {
final Object data = options.get(QueryOptions.SQL_DELETED_DISABLE);
if (data instanceof final Boolean elem) {
exclude_deleted = !elem;
} else if (data != null) {
LOGGER.error("'{}' ==> has not a boolean value: {}", QueryOptions.SQL_DELETED_DISABLE, data);
}
exclude_deleted = !options.exist(AccessDeletedItems.class);
}
// Check if we have a condition to generate
if (condition == null) {
@ -865,8 +883,12 @@ public class DataAccess {
return getWhere(clazz, condition, null);
}
public static <T> T getWhere(final Class<T> clazz, final QueryItem condition, final QueryOptions options) throws Exception {
final List<T> values = getsWhere(clazz, condition, options, 1);
public static <T> T getWhere(final Class<T> clazz, final QueryItem condition, QueryOptions options) throws Exception {
if (options == null) {
options = new QueryOptions();
}
options.add(new Limit(1));
final List<T> values = getsWhere(clazz, condition, options);
if (values.size() == 0) {
return null;
}
@ -874,19 +896,15 @@ public class DataAccess {
}
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition) throws Exception {
return getsWhere(clazz, condition, null, null, null);
return getsWhere(clazz, condition, null, null);
}
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition, final QueryOptions options) throws Exception {
return getsWhere(clazz, condition, null, options, null);
}
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition, final QueryOptions options, final Integer linit) throws Exception {
return getsWhere(clazz, condition, null, options, linit);
return getsWhere(clazz, condition, null, options);
}
public static void generateSelectField(final StringBuilder querySelect, final StringBuilder query, final Class<?> clazz, final QueryOptions options, final CountInOut count) throws Exception {
final boolean readAllfields = QueryOptions.readAllFields(options);
final boolean readAllfields = QueryOptions.readAllColomn(options);
final String tableName = AnnotationTools.getTableName(clazz, options);
boolean firstField = true;
@ -899,7 +917,6 @@ public class DataAccess {
if (addOn != null && !addOn.canRetrieve(elem)) {
continue;
}
// TODO: Manage it with AddOn
final boolean notRead = AnnotationTools.isdefaultNotRead(elem);
if (!readAllfields && notRead) {
continue;
@ -922,9 +939,8 @@ public class DataAccess {
}
}
// TODO: set limit as an query Option...
@SuppressWarnings("unchecked")
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition, final String orderBy, final QueryOptions options, final Integer linit) throws Exception {
public static <T> List<T> getsWhere(final Class<T> clazz, final QueryItem condition, final String orderBy, final QueryOptions options) throws Exception {
final List<LazyGetter> lazyCall = new ArrayList<>();
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
@ -948,10 +964,20 @@ public class DataAccess {
query.append(" ORDER BY ");
query.append(orderBy);
}
if (linit != null && linit >= 1) {
query.append(" LIMIT " + linit);
if (options != null) {
final Limit limit = options.get(Limit.class);
if (limit != null) {
if (limit.getLimit() >= 1) {
query.append(" LIMIT " + limit.getLimit());
} else {
LOGGER.warn("Limit is equal @ {}", limit.getLimit());
entry.close();
entry = null;
return outs;
}
LOGGER.debug("generate the query: '{}'", query.toString());
}
}
LOGGER.warn("generate the query: '{}'", query.toString());
// prepare the request:
final PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
final CountInOut iii = new CountInOut(1);
@ -983,7 +1009,7 @@ public class DataAccess {
public static Object createObjectFromSQLRequest(final ResultSet rs, final Class<?> clazz, final CountInOut count, final CountInOut countNotNull, final QueryOptions options,
final List<LazyGetter> lazyCall) throws Exception {
final boolean readAllfields = QueryOptions.readAllFields(options);
final boolean readAllfields = QueryOptions.readAllColomn(options);
// TODO: manage class that is defined inside a class ==> Not manage for now...
final Object data = clazz.getConstructors()[0].newInstance();
for (final Field elem : clazz.getFields()) {
@ -995,7 +1021,6 @@ public class DataAccess {
if (addOn != null && !addOn.canRetrieve(elem)) {
continue;
}
// TODO: Manage it with AddOn
final boolean notRead = AnnotationTools.isdefaultNotRead(elem);
if (!readAllfields && notRead) {
continue;
@ -1009,7 +1034,6 @@ public class DataAccess {
return data;
}
// TODO : detect the @Id
public static <T, ID_TYPE> T get(final Class<T> clazz, final ID_TYPE id) throws Exception {
return get(clazz, id, null);
}
@ -1030,7 +1054,6 @@ public class DataAccess {
return getsWhere(clazz, null, options);
}
// TODO : detect the @Id
public static <ID_TYPE> int delete(final Class<?> clazz, final ID_TYPE id) throws Exception {
return delete(clazz, id, null);
}
@ -1142,7 +1165,7 @@ public class DataAccess {
query.append("`=false ");
/* is is needed only for SQLite ??? query.append("`modify_date`="); query.append(getDBNow()); query.append(", "); */
// need to disable the deleted false because the model must be unselected to be updated.
options.put(QueryOptions.SQL_DELETED_DISABLE, true);
options.add(QueryOptions.ACCESS_DELETED_ITEMS);
whereAppendQuery(query, tableName, condition, options, deletedFieldName);
try {
final PreparedStatement ps = entry.connection.prepareStatement(query.toString());

View File

@ -12,8 +12,9 @@ import org.kar.archidata.annotation.AnnotationTools;
import org.kar.archidata.annotation.CreationTimestamp;
import org.kar.archidata.annotation.DataIfNotExists;
import org.kar.archidata.annotation.UpdateTimestamp;
import org.kar.archidata.dataAccess.options.CreateDropTable;
import org.kar.archidata.exception.DataAccessException;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -193,7 +194,7 @@ public class DataFactory {
triggerBuilder.append(tableName);
triggerBuilder.append(" SET ");
triggerBuilder.append(name);
//triggerBuilder.append(" = datetime('now') WHERE id = NEW.id; \n");
// triggerBuilder.append(" = datetime('now') WHERE id = NEW.id; \n");
triggerBuilder.append(" = strftime('%Y-%m-%d %H:%M:%f', 'now') WHERE id = NEW.id; \n");
triggerBuilder.append("END;");
@ -286,12 +287,7 @@ public class DataFactory {
boolean createDrop = false;
if (options != null) {
final Object data = options.get(QueryOptions.CREATE_DROP_TABLE);
if (data instanceof final Boolean optionBoolean) {
createDrop = optionBoolean;
} else if (data != null) {
LOGGER.error("'{}' ==> has not a Boolean value: {}", QueryOptions.CREATE_DROP_TABLE, data);
}
createDrop = options.exist(CreateDropTable.class);
}
final boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(DataIfNotExists.class).length != 0;

View File

@ -0,0 +1,47 @@
package org.kar.archidata.dataAccess;
import java.sql.PreparedStatement;
import java.util.List;
public class QueryInList<T> implements QueryItem {
protected final String key;
protected final String comparator;
protected final List<T> value;
protected QueryInList(final String key, final String comparator, final List<T> value) {
this.key = key;
this.comparator = comparator;
this.value = value;
}
public QueryInList(final String key, final List<T> value) {
this(key, "IN", value);
}
@Override
public void generateQuerry(final StringBuilder querry, final String tableName) {
querry.append(tableName);
querry.append(".");
querry.append(this.key);
querry.append(" ");
querry.append(this.comparator);
querry.append(" (");
for (int iii = 0; iii < this.value.size(); iii++) {
if (iii != 0) {
querry.append(",?");
} else {
querry.append("?");
}
}
querry.append(")");
}
@Override
public void injectQuerry(final PreparedStatement ps, final CountInOut iii) throws Exception {
for (final Object elem : this.value) {
DataAccess.addElement(ps, elem, iii);
iii.inc();
}
}
}

View File

@ -0,0 +1,9 @@
package org.kar.archidata.dataAccess;
import java.util.List;
public class QueryNoInList<T> extends QueryInList<T> {
public QueryNoInList(final String key, final List<T> value) {
super(key, "NOT IN", value);
}
}

View File

@ -0,0 +1,3 @@
package org.kar.archidata.dataAccess;
public class QueryOption {}

View File

@ -1,57 +1,61 @@
package org.kar.archidata.dataAccess;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.kar.archidata.dataAccess.options.AccessDeletedItems;
import org.kar.archidata.dataAccess.options.CreateDropTable;
import org.kar.archidata.dataAccess.options.ReadAllColumn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class QueryOptions {
static final Logger LOGGER = LoggerFactory.getLogger(QueryOptions.class);
public static final String SQL_NOT_READ_DISABLE = "SQLNotRead_disable";
public static final String SQL_DELETED_DISABLE = "SQLDeleted_disable";
public static final String OVERRIDE_TABLE_NAME = "SQL_OVERRIDE_TABLE_NAME";
public static final String CREATE_DROP_TABLE = "CREATE_DROP_TABLE";
public static final ReadAllColumn READ_ALL_COLOMN = new ReadAllColumn();
public static final AccessDeletedItems ACCESS_DELETED_ITEMS = new AccessDeletedItems();
public static final CreateDropTable CREATE_DROP_TABLE = new CreateDropTable();
private final Map<String, Object> options = new HashMap<>();
public QueryOptions() {
private final List<QueryOption> options = new ArrayList<>();
public QueryOptions(final QueryOption... elems) {
Collections.addAll(this.options, elems);
}
public QueryOptions(final String key, final Object value) {
this.options.put(key, value);
public QueryOptions() {}
public void add(final QueryOption option) {
this.options.add(option);
}
public QueryOptions(final String key, final Object value, final String key2, final Object value2) {
this.options.put(key, value);
this.options.put(key2, value2);
public List<QueryOption> getAll() {
return this.options;
}
public QueryOptions(final String key, final Object value, final String key2, final Object value2, final String key3, final Object value3) {
this.options.put(key, value);
this.options.put(key2, value2);
this.options.put(key3, value3);
@SuppressWarnings("unchecked")
public <T> T get(final Class<T> type) {
for (final QueryOption elem : this.options) {
if (elem.getClass() == type) {
return (T) elem;
}
}
return null;
}
public void put(final String key, final Object value) {
this.options.put(key, value);
}
public Object get(final String value) {
return this.options.get(value);
}
public static boolean readAllFields(final QueryOptions options) {
if (options != null) {
final Object data = options.get(QueryOptions.SQL_NOT_READ_DISABLE);
if (data instanceof final Boolean elem) {
return elem;
} else if (data != null) {
LOGGER.error("'{}' ==> has not a boolean value: {}", QueryOptions.SQL_NOT_READ_DISABLE, data);
public boolean exist(final Class<?> type) {
for (final QueryOption elem : this.options) {
if (elem.getClass() == type) {
return true;
}
}
return false;
}
public static boolean readAllColomn(final QueryOptions options) {
if (options != null) {
return options.exist(ReadAllColumn.class);
}
return false;
}
}

View File

@ -16,12 +16,12 @@ import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.dataAccess.LazyGetter;
import org.kar.archidata.dataAccess.QueryAnd;
import org.kar.archidata.dataAccess.QueryCondition;
import org.kar.archidata.dataAccess.QueryItem;
import org.kar.archidata.dataAccess.QueryInList;
import org.kar.archidata.dataAccess.QueryOptions;
import org.kar.archidata.dataAccess.QueryOr;
import org.kar.archidata.dataAccess.addOn.model.LinkTable;
import org.kar.archidata.dataAccess.options.OverrideTableName;
import org.kar.archidata.exception.DataAccessException;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -166,12 +166,10 @@ public class AddOnManyToMany implements DataAccessAddOn {
final String idField = AnnotationTools.getFieldName(AnnotationTools.getIdField(objectClass));
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
final LazyGetter lambda = () -> {
final List<QueryItem> childs = new ArrayList<>();
for (final Long elem : idList) {
childs.add(new QueryCondition(idField, "=", elem));
}
final List<Long> childs = new ArrayList<>(idList);
// TODO: update to have get with abstract types ....
final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(), new QueryOr(childs), options);
@SuppressWarnings("unchecked")
final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(), new QueryInList<>(idField, childs), null);
if (foreignData == null) {
return;
}
@ -187,7 +185,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
final String tableName = AnnotationTools.getTableName(clazz);
final String linkTableName = generateLinkTableName(tableName, column);
final LinkTable insertElement = new LinkTable(localKey, remoteKey);
final QueryOptions options = new QueryOptions(QueryOptions.OVERRIDE_TABLE_NAME, linkTableName);
final QueryOptions options = new QueryOptions(new OverrideTableName(linkTableName));
DataAccess.insert(insertElement, options);
}
@ -195,7 +193,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
public static int removeLink(final Class<?> clazz, final long localKey, final String column, final long remoteKey) throws Exception {
final String tableName = AnnotationTools.getTableName(clazz);
final String linkTableName = generateLinkTableName(tableName, column);
final QueryOptions options = new QueryOptions(QueryOptions.OVERRIDE_TABLE_NAME, linkTableName);
final QueryOptions options = new QueryOptions(new OverrideTableName(linkTableName));
final QueryAnd condition = new QueryAnd(new QueryCondition("object1Id", "=", localKey), new QueryCondition("object2Id", "=", remoteKey));
return DataAccess.deleteWhere(LinkTable.class, condition, options);
}
@ -204,7 +202,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
public void createTables(final String tableName, final Field field, final StringBuilder mainTableBuilder, final List<String> preActionList, final List<String> postActionList,
final boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception {
final String linkTableName = generateLinkTableNameField(tableName, field);
final QueryOptions options = new QueryOptions(QueryOptions.OVERRIDE_TABLE_NAME, linkTableName);
final QueryOptions options = new QueryOptions(new OverrideTableName(linkTableName));
final List<String> sqlCommand = DataFactory.createTable(LinkTable.class, options);
postActionList.addAll(sqlCommand);
}

View File

@ -0,0 +1,8 @@
package org.kar.archidata.dataAccess.options;
import org.kar.archidata.dataAccess.QueryOption;
/** This option permit to access on deleted items of a table */
public class AccessDeletedItems extends QueryOption {
public AccessDeletedItems() {}
}

View File

@ -0,0 +1,16 @@
package org.kar.archidata.dataAccess.options;
import org.kar.archidata.dataAccess.QueryOption;
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
public class CheckFunction extends QueryOption {
private final CheckFunctionInterface checker;
public CheckFunction(final CheckFunctionInterface checker) {
this.checker = checker;
}
public CheckFunctionInterface getChecker() {
return this.checker;
}
}

View File

@ -0,0 +1,13 @@
package org.kar.archidata.dataAccess.options;
import java.util.List;
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
public interface CheckFunctionInterface {
/** This function implementation is design to check if the updated class is valid of not for insertion
* @param data The object that might be injected.
* @param filterValue List of fields that might be check. If null, then all column must be checked.
* @throws Exception Exception is generate if the data are incorrect. */
void check(Object data, List<String> filterValue) throws Exception;
}

View File

@ -0,0 +1,8 @@
package org.kar.archidata.dataAccess.options;
import org.kar.archidata.dataAccess.QueryOption;
/** This option permit to create the DROP `Table` IF EXIST in the generation of structure. */
public class CreateDropTable extends QueryOption {
public CreateDropTable() {}
}

View File

@ -0,0 +1,17 @@
package org.kar.archidata.dataAccess.options;
import org.kar.archidata.dataAccess.QueryOption;
/** Option that permit to access to a table structure with an other name that is define in the structure. Note: Internal use for link tables (see:
* org.kar.archidata.dataAccess.addOn.model.LinkTable). */
public class Limit extends QueryOption {
private final int limit;
public Limit(final int limit) {
this.limit = limit;
}
public int getLimit() {
return this.limit;
}
}

View File

@ -0,0 +1,17 @@
package org.kar.archidata.dataAccess.options;
import org.kar.archidata.dataAccess.QueryOption;
/** Option that permit to access to a table structure with an other name that is define in the structure. Note: Internal use for link tables (see:
* org.kar.archidata.dataAccess.addOn.model.LinkTable). */
public class OverrideTableName extends QueryOption {
private final String name;
public OverrideTableName(final String name) {
this.name = name;
}
public String getName() {
return this.name;
}
}

View File

@ -0,0 +1,8 @@
package org.kar.archidata.dataAccess.options;
import org.kar.archidata.dataAccess.QueryOption;
/** By default some element are not read like createAt and UpdatedAt. This option permit to read it. */
public class ReadAllColumn extends QueryOption {
public ReadAllColumn() {}
}

View File

@ -13,7 +13,7 @@ import java.util.Map.Entry;
import org.kar.archidata.annotation.security.PermitTokenInURI;
import org.kar.archidata.catcher.RestErrorResponse;
import org.kar.archidata.model.UserByToken;
import org.kar.archidata.util.JWTWrapper;
import org.kar.archidata.tools.JWTWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -54,7 +54,7 @@ public class MigrationEngine {
return null;
}
try {
final List<Migration> data = DataAccess.gets(Migration.class, new QueryOptions("SQLNotRead_disable", true));
final List<Migration> data = DataAccess.gets(Migration.class, new QueryOptions(QueryOptions.READ_ALL_COLOMN));
if (data == null) {
LOGGER.error("Can not collect the migration table in the DB:{}");
return null;

View File

@ -9,13 +9,11 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.migration.model.Migration;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
record Action(
String action,
List<String> filterDB) {
record Action(String action, List<String> filterDB) {
public Action(final String action) {
this(action, List.of());
}

View File

@ -1,4 +1,4 @@
package org.kar.archidata.util;
package org.kar.archidata.tools;
public class ConfigBaseVariable {
static public String tmpDataFolder;

View File

@ -1,4 +1,4 @@
package org.kar.archidata.util;
package org.kar.archidata.tools;
import java.io.File;
import java.io.FileOutputStream;

View File

@ -0,0 +1,53 @@
package org.kar.archidata.tools;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class DateTools {
static private List<SimpleDateFormat> knownPatterns = new ArrayList<>();
{
// SYSTEM mode
DateTools.knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"));
DateTools.knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm.ss.SSS'Z'"));
DateTools.knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));
DateTools.knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"));
// Human mode
DateTools.knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss"));
DateTools.knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss.SSS"));
// date mode
DateTools.knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd"));
// time mode
DateTools.knownPatterns.add(new SimpleDateFormat("HH:mm:ss"));
DateTools.knownPatterns.add(new SimpleDateFormat("HH:mm:ss.SSS"));
}
public static Date parseDate(final String inputDate, final String pattern) throws ParseException {
final SimpleDateFormat format = new SimpleDateFormat(pattern);
return format.parse(inputDate);
}
public static Date parseDate(final String inputDate) throws ParseException {
for (final SimpleDateFormat pattern : DateTools.knownPatterns) {
try {
return pattern.parse(inputDate);
} catch (final ParseException e) {
continue;
}
}
throw new ParseException("Can not parse the date-time format: '" + inputDate + "'", 0);
}
public static String formatDate(final Date date, final String requiredDateFormat) {
final SimpleDateFormat df = new SimpleDateFormat(requiredDateFormat);
final String outputDateFormatted = df.format(date);
return outputDateFormatted;
}
public static String formatDate(final Date date) {
return formatDate(date, "yyyy-MM-dd'T'HH:mm.ss.SSS'Z'");
}
}

View File

@ -1,4 +1,4 @@
package org.kar.archidata.util;
package org.kar.archidata.tools;
import java.io.BufferedReader;
import java.io.IOException;

View File

@ -1,4 +1,4 @@
package org.kar.archidata.util;
package org.kar.archidata.tools;
public class PublicKey {
public String key;

View File

@ -1,4 +1,4 @@
package org.kar.archidata.util;
package org.kar.archidata.tools;
import java.io.IOException;
import java.net.URI;

View File

@ -15,7 +15,7 @@ import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,11 +29,12 @@ public class TestJson {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();

View File

@ -16,7 +16,7 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -31,11 +31,12 @@ public class TestManyToMany {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();

View File

@ -15,7 +15,7 @@ import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -30,11 +30,12 @@ public class TestManyToOne {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();

View File

@ -14,7 +14,7 @@ import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -27,11 +27,12 @@ public class TestOneToMany {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();

View File

@ -19,7 +19,7 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.dataAccess.QueryOptions;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -36,11 +36,12 @@ public class TestSimpleTable {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Clear the static test:
idOfTheObject = null;
startAction = null;
@ -90,7 +91,7 @@ public class TestSimpleTable {
@Test
public void testReadAllValuesUnreadable() throws Exception {
// check the full values
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject, new QueryOptions(QueryOptions.SQL_NOT_READ_DISABLE, true));
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject, new QueryOptions(QueryOptions.READ_ALL_COLOMN));
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve.id);
@ -113,7 +114,7 @@ public class TestSimpleTable {
final SimpleTable test = new SimpleTable();
test.data = TestSimpleTable.DATA_INJECTED_2;
DataAccess.update(test, TestSimpleTable.idOfTheObject, List.of("data"));
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject, new QueryOptions(QueryOptions.SQL_NOT_READ_DISABLE, true));
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject, new QueryOptions(QueryOptions.READ_ALL_COLOMN));
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve.id);
Assertions.assertEquals(TestSimpleTable.idOfTheObject, retrieve.id);
@ -138,7 +139,7 @@ public class TestSimpleTable {
public void testReadDeletedObject() throws Exception {
// check if we set get deleted element
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject, new QueryOptions(QueryOptions.SQL_DELETED_DISABLE, true));
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject, new QueryOptions(QueryOptions.ACCESS_DELETED_ITEMS));
Assertions.assertNull(retrieve);
}
@ -147,8 +148,7 @@ public class TestSimpleTable {
@Test
public void testReadAllValuesUnreadableOfDeletedObject() throws Exception {
// check if we set get deleted element with all data
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject,
new QueryOptions(QueryOptions.SQL_DELETED_DISABLE, true, QueryOptions.SQL_NOT_READ_DISABLE, true));
final SimpleTable retrieve = DataAccess.get(SimpleTable.class, TestSimpleTable.idOfTheObject, new QueryOptions(QueryOptions.ACCESS_DELETED_ITEMS, QueryOptions.READ_ALL_COLOMN));
Assertions.assertNull(retrieve);
}

View File

@ -19,7 +19,7 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.dataAccess.QueryOptions;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -36,11 +36,12 @@ public class TestSimpleTableSoftDelete {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Clear the static test:
idOfTheObject = null;
startAction = null;
@ -91,7 +92,7 @@ public class TestSimpleTableSoftDelete {
@Test
public void testReadAllValuesUnreadable() throws Exception {
// check the full values
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class, TestSimpleTableSoftDelete.idOfTheObject, new QueryOptions(QueryOptions.SQL_NOT_READ_DISABLE, true));
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class, TestSimpleTableSoftDelete.idOfTheObject, new QueryOptions(QueryOptions.READ_ALL_COLOMN));
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve.id);
@ -118,7 +119,7 @@ public class TestSimpleTableSoftDelete {
test.data = TestSimpleTableSoftDelete.DATA_INJECTED_2;
DataAccess.update(test, TestSimpleTableSoftDelete.idOfTheObject, List.of("data"));
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class, TestSimpleTableSoftDelete.idOfTheObject,
new QueryOptions(QueryOptions.SQL_DELETED_DISABLE, true, QueryOptions.SQL_NOT_READ_DISABLE, true));
new QueryOptions(QueryOptions.ACCESS_DELETED_ITEMS, QueryOptions.READ_ALL_COLOMN));
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve.id);
Assertions.assertEquals(TestSimpleTableSoftDelete.idOfTheObject, retrieve.id);
@ -150,7 +151,7 @@ public class TestSimpleTableSoftDelete {
public void testReadDeletedObject() throws Exception {
// check if we set get deleted element
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class, TestSimpleTableSoftDelete.idOfTheObject, new QueryOptions(QueryOptions.SQL_DELETED_DISABLE, true));
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class, TestSimpleTableSoftDelete.idOfTheObject, new QueryOptions(QueryOptions.ACCESS_DELETED_ITEMS));
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve.id);
Assertions.assertEquals(TestSimpleTableSoftDelete.idOfTheObject, retrieve.id);
@ -166,7 +167,7 @@ public class TestSimpleTableSoftDelete {
public void testReadAllValuesUnreadableOfDeletedObject() throws Exception {
// check if we set get deleted element with all data
final SimpleTableSoftDelete retrieve = DataAccess.get(SimpleTableSoftDelete.class, TestSimpleTableSoftDelete.idOfTheObject,
new QueryOptions(QueryOptions.SQL_DELETED_DISABLE, true, QueryOptions.SQL_NOT_READ_DISABLE, true));
new QueryOptions(QueryOptions.ACCESS_DELETED_ITEMS, QueryOptions.READ_ALL_COLOMN));
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve.id);
Assertions.assertEquals(TestSimpleTableSoftDelete.idOfTheObject, retrieve.id);

View File

@ -15,7 +15,7 @@ import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,11 +29,12 @@ public class TestTypeEnum1 {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();

View File

@ -15,7 +15,7 @@ import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,11 +29,12 @@ public class TestTypeEnum2 {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();

View File

@ -20,7 +20,7 @@ import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.dataAccess.DataFactory;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,11 +33,12 @@ public class TestTypes {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();

View File

@ -15,7 +15,7 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.migration.MigrationEngine;
import org.kar.archidata.migration.MigrationException;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,10 +29,12 @@ public class TestMigrationFail {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();

View File

@ -14,7 +14,7 @@ import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.migration.MigrationEngine;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -29,10 +29,12 @@ public class TestMigrationFirstInit {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();

View File

@ -16,7 +16,7 @@ import org.kar.archidata.dataAccess.DataAccess;
import org.kar.archidata.db.DBEntry;
import org.kar.archidata.migration.MigrationEngine;
import org.kar.archidata.migration.model.Migration;
import org.kar.archidata.util.ConfigBaseVariable;
import org.kar.archidata.tools.ConfigBaseVariable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -30,10 +30,12 @@ public class TestMigrationFirstInitWithMigration {
@BeforeAll
public static void configureWebServer() throws Exception {
if (!"true".equalsIgnoreCase(System.getenv("TEST_E2E_MODE"))) {
ConfigBaseVariable.dbType = "sqlite";
ConfigBaseVariable.dbHost = "memory";
// for test we need to connect all time the DB
ConfigBaseVariable.dbKeepConnected = "true";
}
// Connect the dataBase...
final DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
entry.connect();