continue refacto

This commit is contained in:
Edouard DUPIN 2023-10-02 16:25:45 +02:00
parent 293d80efd7
commit 99cca8bebf
12 changed files with 147 additions and 20 deletions

View File

@ -23,5 +23,22 @@
<attribute name="maven.pomderived" value="true"/> <attribute name="maven.pomderived" value="true"/>
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="src" path="out/maven/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="out/maven/test-classes" path="out/maven/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="out/maven/classes"/> <classpathentry kind="output" path="out/maven/classes"/>
</classpath> </classpath>

View File

@ -2,6 +2,7 @@ package org.kar.archidata;
import org.kar.archidata.db.DBEntry; import org.kar.archidata.db.DBEntry;
import org.kar.archidata.model.User; import org.kar.archidata.model.User;
import org.kar.archidata.sqlWrapper.SqlWrapper;
import java.io.IOException; import java.io.IOException;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;

View File

@ -11,10 +11,22 @@ import java.lang.annotation.Target;
@Target(ElementType.FIELD) @Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface SQLTableLinkGeneric { public @interface SQLTableLinkGeneric {
public static String AUTOMATIC ="__auto__";
public enum ModelLink { public enum ModelLink {
NONE, NONE,
INTERNAL, INTERNAL, // The list is serialized in a string ',' separated
EXTERNAL EXTERNAL // an external table is created and
}; };
// Permit to select the link mode.
ModelLink value() default ModelLink.EXTERNAL; ModelLink value() default ModelLink.EXTERNAL;
// If automatic table name, the table name is: parentTableName_externalTableName__link
String tableName() default AUTOMATIC;
// If automatic table name, the name of the foreign table is manage with the variable name.
String externalTableName() default AUTOMATIC;
// If the external link have a field to filter with a specific value (name of the field)
String filterField() default AUTOMATIC;
// If the external link have a field to filter with a specific value (value of the field)
String filterValue() default AUTOMATIC;
} }

View File

@ -4,7 +4,7 @@ import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam; import org.glassfish.jersey.media.multipart.FormDataParam;
import org.kar.archidata.filter.GenericContext; import org.kar.archidata.filter.GenericContext;
import org.kar.archidata.model.Data; import org.kar.archidata.model.Data;
import org.kar.archidata.SqlWrapper; import org.kar.archidata.sqlWrapper.SqlWrapper;
import org.kar.archidata.annotation.security.PermitTokenInURI; import org.kar.archidata.annotation.security.PermitTokenInURI;
import org.kar.archidata.annotation.security.RolesAllowed; import org.kar.archidata.annotation.security.RolesAllowed;
import org.kar.archidata.util.ConfigBaseVariable; import org.kar.archidata.util.ConfigBaseVariable;

View File

@ -1,6 +1,6 @@
package org.kar.archidata.db; package org.kar.archidata.db;
import org.kar.archidata.SqlWrapper; import org.kar.archidata.sqlWrapper.SqlWrapper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@ -5,9 +5,9 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.kar.archidata.SqlWrapper;
import org.kar.archidata.db.DBConfig; import org.kar.archidata.db.DBConfig;
import org.kar.archidata.db.DBEntry; import org.kar.archidata.db.DBEntry;
import org.kar.archidata.sqlWrapper.SqlWrapper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@ -5,8 +5,8 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.kar.archidata.SqlWrapper;
import org.kar.archidata.db.DBEntry; import org.kar.archidata.db.DBEntry;
import org.kar.archidata.sqlWrapper.SqlWrapper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@ -1,4 +1,4 @@
package org.kar.archidata; package org.kar.archidata.sqlWrapper;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
@ -11,6 +11,7 @@ import java.sql.*;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import org.kar.archidata.GlobalConfiguration;
import org.kar.archidata.annotation.SQLAutoIncrement; import org.kar.archidata.annotation.SQLAutoIncrement;
import org.kar.archidata.annotation.SQLComment; import org.kar.archidata.annotation.SQLComment;
import org.kar.archidata.annotation.SQLIfNotExists; import org.kar.archidata.annotation.SQLIfNotExists;
@ -38,6 +39,11 @@ import org.kar.archidata.annotation.SQLDeleted;
public class SqlWrapper { public class SqlWrapper {
static final Logger LOGGER = LoggerFactory.getLogger(SqlWrapper.class); static final Logger LOGGER = LoggerFactory.getLogger(SqlWrapper.class);
static final List<SqlWrapperAddOn> addOn = new ArrayList<>();
public static void addAddOn(SqlWrapperAddOn addOn) {
SqlWrapper.addOn.add(addOn);
};
public static class ExceptionDBInterface extends Exception { public static class ExceptionDBInterface extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -395,6 +401,10 @@ public class SqlWrapper {
boolean firstField = true; boolean firstField = true;
int count = 0; int count = 0;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0; boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0;
if (primaryKey) { if (primaryKey) {
continue; continue;
@ -445,6 +455,10 @@ public class SqlWrapper {
Field primaryKeyField = null; Field primaryKeyField = null;
int iii = 1; int iii = 1;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0; boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0;
if (primaryKey) { if (primaryKey) {
primaryKeyField = elem; primaryKeyField = elem;
@ -567,6 +581,10 @@ public class SqlWrapper {
boolean firstField = true; boolean firstField = true;
Field primaryKeyField = null; Field primaryKeyField = null;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0; boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0;
if (primaryKey) { if (primaryKey) {
primaryKeyField = elem; primaryKeyField = elem;
@ -615,6 +633,10 @@ public class SqlWrapper {
PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS); PreparedStatement ps = entry.connection.prepareStatement(query.toString(), Statement.RETURN_GENERATED_KEYS);
int iii = 1; int iii = 1;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0; boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0;
if (primaryKey) { if (primaryKey) {
continue; continue;
@ -822,6 +844,10 @@ public class SqlWrapper {
int count = 0; int count = 0;
boolean hasDeleted = false; boolean hasDeleted = false;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
ModelLink linkGeneric = getLinkMode(elem); ModelLink linkGeneric = getLinkMode(elem);
if (linkGeneric != ModelLink.NONE) { if (linkGeneric != ModelLink.NONE) {
continue; continue;
@ -879,6 +905,10 @@ public class SqlWrapper {
Object data = clazz.getConstructors()[0].newInstance(); Object data = clazz.getConstructors()[0].newInstance();
count = 1; count = 1;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
ModelLink linkGeneric = getLinkMode(elem); ModelLink linkGeneric = getLinkMode(elem);
if (linkGeneric != ModelLink.NONE) { if (linkGeneric != ModelLink.NONE) {
continue; continue;
@ -913,6 +943,10 @@ public class SqlWrapper {
public static <T> T get(Class<T> clazz, long id) throws Exception { public static <T> T get(Class<T> clazz, long id) throws Exception {
Field primaryKeyField = null; Field primaryKeyField = null;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0; boolean primaryKey = elem.getDeclaredAnnotationsByType(SQLPrimaryKey.class).length != 0;
if (primaryKey) { if (primaryKey) {
primaryKeyField = elem; primaryKeyField = elem;
@ -948,13 +982,20 @@ public class SqlWrapper {
int count = 0; int count = 0;
StateLoad[] autoClasify = new StateLoad[clazz.getFields().length]; StateLoad[] autoClasify = new StateLoad[clazz.getFields().length];
int indexAutoClasify = 0; int indexAutoClasify = 0;
boolean hasDeleted = false;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
boolean notRead = elem.getDeclaredAnnotationsByType(SQLNotRead.class).length != 0; boolean notRead = elem.getDeclaredAnnotationsByType(SQLNotRead.class).length != 0;
if (!full && notRead) { if (!full && notRead) {
autoClasify[indexAutoClasify++] = StateLoad.DISABLE; autoClasify[indexAutoClasify++] = StateLoad.DISABLE;
continue; continue;
} }
if (!hasDeleted) {
hasDeleted = elem.getDeclaredAnnotationsByType(SQLDeleted.class).length != 0;
}
String name = elem.getName(); String name = elem.getName();
count++; count++;
if (firstField) { if (firstField) {
@ -962,6 +1003,8 @@ public class SqlWrapper {
} else { } else {
query.append(","); query.append(",");
} }
ModelLink linkGeneric = getLinkMode(elem); ModelLink linkGeneric = getLinkMode(elem);
if (linkGeneric == ModelLink.EXTERNAL) { if (linkGeneric == ModelLink.EXTERNAL) {
autoClasify[indexAutoClasify++] = StateLoad.ARRAY; autoClasify[indexAutoClasify++] = StateLoad.ARRAY;
@ -1017,14 +1060,16 @@ public class SqlWrapper {
query.append(" FROM `"); query.append(" FROM `");
query.append(tableName); query.append(tableName);
query.append("` "); query.append("` ");
query.append(" WHERE "); if (hasDeleted) {
//query.append(tableName); query.append(" WHERE ");
//query.append("."); //query.append(tableName);
//query.append(primaryKeyField.getName()); //query.append(".");
//query.append(" = ?"); //query.append(primaryKeyField.getName());
//query.append(" AND "); //query.append(" = ?");
query.append(tableName); //query.append(" AND ");
query.append(".deleted = false "); query.append(tableName);
query.append(".deleted = false ");
}
firstField = true; firstField = true;
LOGGER.debug("generate the querry: '{}'", query.toString()); LOGGER.debug("generate the querry: '{}'", query.toString());
LOGGER.debug("request get {} prepare @{}", clazz.getCanonicalName(), getCurrentTimeStamp()); LOGGER.debug("request get {} prepare @{}", clazz.getCanonicalName(), getCurrentTimeStamp());
@ -1041,6 +1086,9 @@ public class SqlWrapper {
Object data = clazz.getConstructors()[0].newInstance(); Object data = clazz.getConstructors()[0].newInstance();
count = 1; count = 1;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
/* /*
boolean notRead = elem.getDeclaredAnnotationsByType(SQLNotRead.class).length != 0; boolean notRead = elem.getDeclaredAnnotationsByType(SQLNotRead.class).length != 0;
*/ */
@ -1255,6 +1303,10 @@ public class SqlWrapper {
LOGGER.debug("===> TABLE `{}`", tableName); LOGGER.debug("===> TABLE `{}`", tableName);
String primaryKeyValue = null; String primaryKeyValue = null;
for (Field elem : clazz.getFields()) { for (Field elem : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
continue;
}
String name = elem.getName(); String name = elem.getName();
Integer limitSize = getLimitSize(elem); Integer limitSize = getLimitSize(elem);
@ -1373,8 +1425,8 @@ public class SqlWrapper {
out.append(" "); out.append(" ");
} }
if (updateTime) { if (updateTime) {
out.append("ON UPDATE CURRENT_TIMESTAMP");
if (!ConfigBaseVariable.getDBType().equals("sqlite")) { if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append("ON UPDATE CURRENT_TIMESTAMP");
out.append("(3)"); out.append("(3)");
} }
out.append(" "); out.append(" ");
@ -1388,8 +1440,8 @@ public class SqlWrapper {
} }
out.append(" "); out.append(" ");
if (updateTime) { if (updateTime) {
out.append("ON UPDATE CURRENT_TIMESTAMP");
if (!ConfigBaseVariable.getDBType().equals("sqlite")) { if (!ConfigBaseVariable.getDBType().equals("sqlite")) {
out.append("ON UPDATE CURRENT_TIMESTAMP");
out.append("(3)"); out.append("(3)");
} }
out.append(" "); out.append(" ");

View File

@ -0,0 +1,24 @@
package org.kar.archidata.sqlWrapper;
import java.lang.reflect.Field;
public interface SqlWrapperAddOn {
/**
* Get the Class of the declaration annotation
* @return The annotation class
*/
Class<?> getAnnotationClass();
/**
* Get the SQL type that is needed to declare for the specific Field Type.
* @param elem Field to declare.
* @return SQL type to create.
*/
String getSQLFieldType(Field elem);
/**
* Check if the field is manage by the local add-on
* @param elem Field to inspect.
* @return True of the field is manage by the current Add-on.
*/
boolean isCompatibleField(Field elem);
}

View File

@ -1,4 +1,4 @@
package org.kar.archidata; package org.kar.archidata.sqlWrapper;
public record WhereCondition( public record WhereCondition(
String key, String key,

View File

@ -0,0 +1,21 @@
package org.kar.archidata.sqlWrapper.addOn;
import java.lang.reflect.Field;
import org.kar.archidata.annotation.SQLTableLinkGeneric;
import org.kar.archidata.sqlWrapper.SqlWrapperAddOn;
public class ExternalLink implements SqlWrapperAddOn {
@Override
public Class<?> getAnnotationClass() {
return SQLTableLinkGeneric.class;
}
public String getSQLFieldType(Field elem) {
return "STRING";
}
public boolean isCompatibleField(Field elem) {
SQLTableLinkGeneric decorators = elem.getDeclaredAnnotation(SQLTableLinkGeneric.class);
return decorators != null;
}
}

View File

@ -15,8 +15,8 @@ import java.sql.SQLException;
import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.Response;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition; import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.kar.archidata.SqlWrapper;
import org.kar.archidata.model.Data; import org.kar.archidata.model.Data;
import org.kar.archidata.sqlWrapper.SqlWrapper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;