[DEV] update the basic exception model
This commit is contained in:
parent
d1a866277d
commit
b48916be07
@ -10,16 +10,16 @@ import jakarta.ws.rs.ext.ExceptionMapper;
|
|||||||
|
|
||||||
public class FailException404API implements ExceptionMapper<ClientErrorException> {
|
public class FailException404API implements ExceptionMapper<ClientErrorException> {
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(FailException404API.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(FailException404API.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Response toResponse(ClientErrorException exception) {
|
public Response toResponse(final ClientErrorException exception) {
|
||||||
RestErrorResponse ret = build(exception);
|
final RestErrorResponse ret = build(exception);
|
||||||
LOGGER.error("Error UUID={}", ret.uuid);
|
LOGGER.error("Error UUID={}", ret.uuid);
|
||||||
return Response.status(exception.getResponse().getStatusInfo().toEnum()).entity(ret).type(MediaType.APPLICATION_JSON).build();
|
return Response.status(exception.getResponse().getStatusInfo().toEnum()).entity(ret).type(MediaType.APPLICATION_JSON).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private RestErrorResponse build(ClientErrorException exception) {
|
private RestErrorResponse build(final ClientErrorException exception) {
|
||||||
return new RestErrorResponse(exception.getResponse().getStatusInfo().toEnum(), "Catch system exception", exception.getMessage());
|
return new RestErrorResponse(exception.getResponse().getStatusInfo().toEnum(), "Catch system exception", exception.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,7 @@ import org.kar.archidata.dataAccess.addOn.AddOnManyToMany;
|
|||||||
import org.kar.archidata.dataAccess.addOn.AddOnManyToOne;
|
import org.kar.archidata.dataAccess.addOn.AddOnManyToOne;
|
||||||
import org.kar.archidata.dataAccess.addOn.AddOnSQLTableExternalForeinKeyAsList;
|
import org.kar.archidata.dataAccess.addOn.AddOnSQLTableExternalForeinKeyAsList;
|
||||||
import org.kar.archidata.db.DBEntry;
|
import org.kar.archidata.db.DBEntry;
|
||||||
|
import org.kar.archidata.exception.DataAccessException;
|
||||||
import org.kar.archidata.util.ConfigBaseVariable;
|
import org.kar.archidata.util.ConfigBaseVariable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -274,7 +275,7 @@ public class DataAccess {
|
|||||||
ps.setString(iii.value, tmp.toString());
|
ps.setString(iii.value, tmp.toString());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("Unknown Field Type");
|
throw new DataAccessException("Unknown Field Type");
|
||||||
}
|
}
|
||||||
iii.inc();
|
iii.inc();
|
||||||
}
|
}
|
||||||
@ -430,7 +431,7 @@ public class DataAccess {
|
|||||||
// TODO: maybe do something stupid if not exist ???
|
// TODO: maybe do something stupid if not exist ???
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("Unknown Field Type");
|
throw new DataAccessException("Unknown Field Type");
|
||||||
}
|
}
|
||||||
count.inc();
|
count.inc();
|
||||||
}
|
}
|
||||||
@ -448,6 +449,15 @@ public class DataAccess {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <T> List<T> insertMultiple(final List<T> data, final QueryOptions options) throws Exception {
|
||||||
|
final List<T> out = new ArrayList<>();
|
||||||
|
for (final T elem : data) {
|
||||||
|
final T tmp = insert(elem, options);
|
||||||
|
out.add(tmp);
|
||||||
|
}
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
public static <T> T insert(final T data) throws Exception {
|
public static <T> T insert(final T data) throws Exception {
|
||||||
return insert(data, null);
|
return insert(data, null);
|
||||||
}
|
}
|
||||||
@ -603,6 +613,20 @@ public class DataAccess {
|
|||||||
return insert(data);
|
return insert(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static <ID_TYPE> QueryCondition getTableIdCondition(final Class<?> clazz, final ID_TYPE idKey) throws Exception {
|
||||||
|
// Find the ID field type ....
|
||||||
|
final Field idField = AnnotationTools.getIdField(clazz);
|
||||||
|
if (idField == null) {
|
||||||
|
throw new DataAccessException("The class have no annotation @Id ==> can not determine the default type searching");
|
||||||
|
}
|
||||||
|
// check the compatibility of the id and the declared ID
|
||||||
|
final Class<?> typeClass = idField.getType();
|
||||||
|
if (idKey == typeClass) {
|
||||||
|
throw new DataAccessException("Request update with the wrong type ...");
|
||||||
|
}
|
||||||
|
return new QueryCondition(AnnotationTools.getFieldName(idField), "=", idKey);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update an object with the inserted json data
|
* Update an object with the inserted json data
|
||||||
*
|
*
|
||||||
@ -615,21 +639,10 @@ public class DataAccess {
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static <T, ID_TYPE> int updateWithJson(final Class<T> clazz, final ID_TYPE id, final String jsonData) throws Exception {
|
public static <T, ID_TYPE> int updateWithJson(final Class<T> clazz, final ID_TYPE id, final String jsonData) throws Exception {
|
||||||
// Find the ID field type ....
|
return updateWhereWithJson(clazz, getTableIdCondition(clazz, id), jsonData);
|
||||||
final Field idField = AnnotationTools.getIdField(clazz);
|
|
||||||
if (idField == null) {
|
|
||||||
throw new Exception("The class have no annotation @Id ==> can not determine the default type searching");
|
|
||||||
}
|
|
||||||
// check the compatibility of the id and the declared ID
|
|
||||||
final Class<?> typeClass = idField.getType();
|
|
||||||
if (id == typeClass) {
|
|
||||||
throw new Exception("Request update with the wrong type ...");
|
|
||||||
}
|
|
||||||
// Udpade Json Value
|
|
||||||
return updateWithJson(clazz, new QueryCondition(AnnotationTools.getFieldName(idField), "=", id), jsonData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> int updateWithJson(final Class<T> clazz, final QueryItem condition, final String jsonData) throws Exception {
|
public static <T> int updateWhereWithJson(final Class<T> clazz, final QueryItem condition, final String jsonData) throws Exception {
|
||||||
final ObjectMapper mapper = new ObjectMapper();
|
final ObjectMapper mapper = new ObjectMapper();
|
||||||
// parse the object to be sure the data are valid:
|
// parse the object to be sure the data are valid:
|
||||||
final T data = mapper.readValue(jsonData, clazz);
|
final T data = mapper.readValue(jsonData, clazz);
|
||||||
@ -659,17 +672,7 @@ public class DataAccess {
|
|||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static <T, ID_TYPE> int update(final T data, final ID_TYPE id, final List<String> filterValue) throws Exception {
|
public static <T, ID_TYPE> int update(final T data, final ID_TYPE id, final List<String> filterValue) throws Exception {
|
||||||
// Find the ID field type ....
|
return updateWhere(data, getTableIdCondition(data.getClass(), id), null, filterValue);
|
||||||
final Field idField = AnnotationTools.getIdField(data.getClass());
|
|
||||||
if (idField == null) {
|
|
||||||
throw new Exception("The class have no annotation @Id ==> can not determine the default type searching");
|
|
||||||
}
|
|
||||||
// check the compatibility of the id and the declared ID
|
|
||||||
final Class<?> typeClass = idField.getType();
|
|
||||||
if (id == typeClass) {
|
|
||||||
throw new Exception("Request update with the wriong type ...");
|
|
||||||
}
|
|
||||||
return updateWhere(data, new QueryCondition(AnnotationTools.getFieldName(idField), "=", id), null, filterValue);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> int updateWhere(final T data, final QueryItem condition, final QueryOptions options, final List<String> filterValue) throws Exception {
|
public static <T> int updateWhere(final T data, final QueryItem condition, final QueryOptions options, final List<String> filterValue) throws Exception {
|
||||||
@ -799,7 +802,7 @@ public class DataAccess {
|
|||||||
} else if (value.getClass().isEnum()) {
|
} else if (value.getClass().isEnum()) {
|
||||||
ps.setString(iii.value, value.toString());
|
ps.setString(iii.value, value.toString());
|
||||||
} else {
|
} else {
|
||||||
throw new Exception("Not manage type ==> need to add it ...");
|
throw new DataAccessException("Not manage type ==> need to add it ...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1014,25 +1017,12 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO : detect the @Id
|
// TODO : detect the @Id
|
||||||
public static <T> T get(final Class<T> clazz, final long id) throws Exception {
|
public static <T, ID_TYPE> T get(final Class<T> clazz, final ID_TYPE id) throws Exception {
|
||||||
return get(clazz, id, null);
|
return get(clazz, id, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static <T> T get(final Class<T> clazz, final long id, final QueryOptions options) throws Exception {
|
public static <T, ID_TYPE> T get(final Class<T> clazz, final ID_TYPE id, final QueryOptions options) throws Exception {
|
||||||
Field primaryKeyField = null;
|
return DataAccess.getWhere(clazz, getTableIdCondition(clazz, id), options);
|
||||||
for (final Field elem : clazz.getFields()) {
|
|
||||||
// static field is only for internal global declaration ==> remove it ..
|
|
||||||
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (AnnotationTools.isPrimaryKey(elem)) {
|
|
||||||
primaryKeyField = elem;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (primaryKeyField != null) {
|
|
||||||
return DataAccess.getWhere(clazz, new QueryCondition(AnnotationTools.getFieldName(primaryKeyField), "=", id), options);
|
|
||||||
}
|
|
||||||
throw new Exception("Missing primary Key...");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getCurrentTimeStamp() {
|
public static String getCurrentTimeStamp() {
|
||||||
@ -1048,11 +1038,11 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO : detect the @Id
|
// TODO : detect the @Id
|
||||||
public static int delete(final Class<?> clazz, final long id) throws Exception {
|
public static <ID_TYPE> int delete(final Class<?> clazz, final ID_TYPE id) throws Exception {
|
||||||
return delete(clazz, id, null);
|
return delete(clazz, id, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int delete(final Class<?> clazz, final long id, final QueryOptions options) throws Exception {
|
public static <ID_TYPE> int delete(final Class<?> clazz, final ID_TYPE id, final QueryOptions options) throws Exception {
|
||||||
final String hasDeletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
final String hasDeletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||||
if (hasDeletedFieldName != null) {
|
if (hasDeletedFieldName != null) {
|
||||||
return deleteSoft(clazz, id, options);
|
return deleteSoft(clazz, id, options);
|
||||||
@ -1070,8 +1060,8 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int deleteHard(final Class<?> clazz, final long id, final QueryOptions options) throws Exception {
|
public static <ID_TYPE> int deleteHard(final Class<?> clazz, final ID_TYPE id, final QueryOptions options) throws Exception {
|
||||||
return deleteHardWhere(clazz, new QueryCondition("id", "=", id), options);
|
return deleteHardWhere(clazz, getTableIdCondition(clazz, id), options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int deleteHardWhere(final Class<?> clazz, final QueryItem condition, final QueryOptions options) throws Exception {
|
public static int deleteHardWhere(final Class<?> clazz, final QueryItem condition, final QueryOptions options) throws Exception {
|
||||||
@ -1097,8 +1087,8 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int deleteSoft(final Class<?> clazz, final long id, final QueryOptions options) throws Exception {
|
private static <ID_TYPE> int deleteSoft(final Class<?> clazz, final ID_TYPE id, final QueryOptions options) throws Exception {
|
||||||
return deleteSoftWhere(clazz, new QueryCondition("id", "=", id), options);
|
return deleteSoftWhere(clazz, getTableIdCondition(clazz, id), options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getDBNow() {
|
public static String getDBNow() {
|
||||||
@ -1148,19 +1138,19 @@ public class DataAccess {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int unsetDelete(final Class<?> clazz, final long id) throws Exception {
|
public static <ID_TYPE> int unsetDelete(final Class<?> clazz, final ID_TYPE id) throws Exception {
|
||||||
return unsetDeleteWhere(clazz, new QueryCondition("id", "=", id), null);
|
return unsetDeleteWhere(clazz, getTableIdCondition(clazz, id), null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int unsetDelete(final Class<?> clazz, final long id, final QueryOptions options) throws Exception {
|
public static <ID_TYPE> int unsetDelete(final Class<?> clazz, final ID_TYPE id, final QueryOptions options) throws Exception {
|
||||||
return unsetDeleteWhere(clazz, new QueryCondition("id", "=", id), options);
|
return unsetDeleteWhere(clazz, getTableIdCondition(clazz, id), options);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int unsetDeleteWhere(final Class<?> clazz, final QueryItem condition, final QueryOptions options) throws Exception {
|
public static int unsetDeleteWhere(final Class<?> clazz, final QueryItem condition, final QueryOptions options) throws Exception {
|
||||||
final String tableName = AnnotationTools.getTableName(clazz, options);
|
final String tableName = AnnotationTools.getTableName(clazz, options);
|
||||||
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
final String deletedFieldName = AnnotationTools.getDeletedFieldName(clazz);
|
||||||
if (deletedFieldName == null) {
|
if (deletedFieldName == null) {
|
||||||
throw new Exception("The class " + clazz.getCanonicalName() + " has no deleted field");
|
throw new DataAccessException("The class " + clazz.getCanonicalName() + " has no deleted field");
|
||||||
}
|
}
|
||||||
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
DBEntry entry = DBEntry.createInterface(GlobalConfiguration.dbConfig);
|
||||||
final StringBuilder query = new StringBuilder();
|
final StringBuilder query = new StringBuilder();
|
||||||
|
@ -12,6 +12,7 @@ import org.kar.archidata.annotation.AnnotationTools;
|
|||||||
import org.kar.archidata.annotation.CreationTimestamp;
|
import org.kar.archidata.annotation.CreationTimestamp;
|
||||||
import org.kar.archidata.annotation.DataIfNotExists;
|
import org.kar.archidata.annotation.DataIfNotExists;
|
||||||
import org.kar.archidata.annotation.UpdateTimestamp;
|
import org.kar.archidata.annotation.UpdateTimestamp;
|
||||||
|
import org.kar.archidata.exception.DataAccessException;
|
||||||
import org.kar.archidata.util.ConfigBaseVariable;
|
import org.kar.archidata.util.ConfigBaseVariable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -22,7 +23,7 @@ import jakarta.persistence.GenerationType;
|
|||||||
|
|
||||||
public class DataFactory {
|
public class DataFactory {
|
||||||
static final Logger LOGGER = LoggerFactory.getLogger(DataFactory.class);
|
static final Logger LOGGER = LoggerFactory.getLogger(DataFactory.class);
|
||||||
|
|
||||||
public static String convertTypeInSQL(final Class<?> type, final String fieldName) throws Exception {
|
public static String convertTypeInSQL(final Class<?> type, final String fieldName) throws Exception {
|
||||||
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||||
if (type == Long.class || type == long.class) {
|
if (type == Long.class || type == long.class) {
|
||||||
@ -123,23 +124,23 @@ public class DataFactory {
|
|||||||
return out.toString();
|
return out.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw new Exception("Imcompatible type of element in object for: " + type.getCanonicalName());
|
throw new DataAccessException("Imcompatible type of element in object for: " + type.getCanonicalName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void createTablesSpecificType(final String tableName, final Field elem, final StringBuilder mainTableBuilder, final List<String> preOtherTables, final List<String> postOtherTables,
|
public static void createTablesSpecificType(final String tableName, final Field elem, final StringBuilder mainTableBuilder, final List<String> preOtherTables, final List<String> postOtherTables,
|
||||||
final boolean createIfNotExist, final boolean createDrop, final int fieldId, final Class<?> classModel) throws Exception {
|
final boolean createIfNotExist, final boolean createDrop, final int fieldId, final Class<?> classModel) throws Exception {
|
||||||
final String name = AnnotationTools.getFieldName(elem);
|
final String name = AnnotationTools.getFieldName(elem);
|
||||||
final Integer limitSize = AnnotationTools.getLimitSize(elem);
|
final Integer limitSize = AnnotationTools.getLimitSize(elem);
|
||||||
final boolean notNull = AnnotationTools.getNotNull(elem);
|
final boolean notNull = AnnotationTools.getNotNull(elem);
|
||||||
|
|
||||||
final boolean primaryKey = AnnotationTools.isPrimaryKey(elem);
|
final boolean primaryKey = AnnotationTools.isPrimaryKey(elem);
|
||||||
final GenerationType strategy = AnnotationTools.getStrategy(elem);
|
final GenerationType strategy = AnnotationTools.getStrategy(elem);
|
||||||
|
|
||||||
final boolean createTime = elem.getDeclaredAnnotationsByType(CreationTimestamp.class).length != 0;
|
final boolean createTime = elem.getDeclaredAnnotationsByType(CreationTimestamp.class).length != 0;
|
||||||
final boolean updateTime = elem.getDeclaredAnnotationsByType(UpdateTimestamp.class).length != 0;
|
final boolean updateTime = elem.getDeclaredAnnotationsByType(UpdateTimestamp.class).length != 0;
|
||||||
final String comment = AnnotationTools.getComment(elem);
|
final String comment = AnnotationTools.getComment(elem);
|
||||||
final String defaultValue = AnnotationTools.getDefault(elem);
|
final String defaultValue = AnnotationTools.getDefault(elem);
|
||||||
|
|
||||||
if (fieldId == 0) {
|
if (fieldId == 0) {
|
||||||
mainTableBuilder.append("\n\t\t`");
|
mainTableBuilder.append("\n\t\t`");
|
||||||
} else {
|
} else {
|
||||||
@ -199,10 +200,10 @@ public class DataFactory {
|
|||||||
triggerBuilder.append(name);
|
triggerBuilder.append(name);
|
||||||
triggerBuilder.append(" = datetime('now') WHERE id = NEW.id; \n");
|
triggerBuilder.append(" = datetime('now') WHERE id = NEW.id; \n");
|
||||||
triggerBuilder.append("END;");
|
triggerBuilder.append("END;");
|
||||||
|
|
||||||
postOtherTables.add(triggerBuilder.toString());
|
postOtherTables.add(triggerBuilder.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
mainTableBuilder.append(" ");
|
mainTableBuilder.append(" ");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -237,11 +238,11 @@ public class DataFactory {
|
|||||||
mainTableBuilder.append("DEFAULT ");
|
mainTableBuilder.append("DEFAULT ");
|
||||||
mainTableBuilder.append(defaultValue);
|
mainTableBuilder.append(defaultValue);
|
||||||
mainTableBuilder.append(" ");
|
mainTableBuilder.append(" ");
|
||||||
|
|
||||||
}
|
}
|
||||||
if (primaryKey && "sqlite".equals(ConfigBaseVariable.getDBType())) {
|
if (primaryKey && "sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||||
mainTableBuilder.append("PRIMARY KEY ");
|
mainTableBuilder.append("PRIMARY KEY ");
|
||||||
|
|
||||||
}
|
}
|
||||||
if (strategy == GenerationType.IDENTITY) {
|
if (strategy == GenerationType.IDENTITY) {
|
||||||
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
if (!"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||||
@ -250,16 +251,16 @@ public class DataFactory {
|
|||||||
mainTableBuilder.append("AUTOINCREMENT ");
|
mainTableBuilder.append("AUTOINCREMENT ");
|
||||||
}
|
}
|
||||||
} else if (strategy != null) {
|
} else if (strategy != null) {
|
||||||
throw new Exception("Can not generate a stategy different of IDENTITY");
|
throw new DataAccessException("Can not generate a stategy different of IDENTITY");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (comment != null && !"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
if (comment != null && !"sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||||
mainTableBuilder.append("COMMENT '");
|
mainTableBuilder.append("COMMENT '");
|
||||||
mainTableBuilder.append(comment.replace('\'', '\''));
|
mainTableBuilder.append(comment.replace('\'', '\''));
|
||||||
mainTableBuilder.append("' ");
|
mainTableBuilder.append("' ");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static boolean isFieldFromSuperClass(final Class<?> model, final String filedName) {
|
private static boolean isFieldFromSuperClass(final Class<?> model, final String filedName) {
|
||||||
final Class<?> superClass = model.getSuperclass();
|
final Class<?> superClass = model.getSuperclass();
|
||||||
if (superClass == null) {
|
if (superClass == null) {
|
||||||
@ -279,14 +280,14 @@ public class DataFactory {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> createTable(final Class<?> clazz) throws Exception {
|
public static List<String> createTable(final Class<?> clazz) throws Exception {
|
||||||
return createTable(clazz, null);
|
return createTable(clazz, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<String> createTable(final Class<?> clazz, final QueryOptions options) throws Exception {
|
public static List<String> createTable(final Class<?> clazz, final QueryOptions options) throws Exception {
|
||||||
final String tableName = AnnotationTools.getTableName(clazz, options);
|
final String tableName = AnnotationTools.getTableName(clazz, options);
|
||||||
|
|
||||||
boolean createDrop = false;
|
boolean createDrop = false;
|
||||||
if (options != null) {
|
if (options != null) {
|
||||||
final Object data = options.get(QueryOptions.CREATE_DROP_TABLE);
|
final Object data = options.get(QueryOptions.CREATE_DROP_TABLE);
|
||||||
@ -296,7 +297,7 @@ public class DataFactory {
|
|||||||
LOGGER.error("'{}' ==> has not a Boolean value: {}", QueryOptions.CREATE_DROP_TABLE, data);
|
LOGGER.error("'{}' ==> has not a Boolean value: {}", QueryOptions.CREATE_DROP_TABLE, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(DataIfNotExists.class).length != 0;
|
final boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(DataIfNotExists.class).length != 0;
|
||||||
final List<String> preActionList = new ArrayList<>();
|
final List<String> preActionList = new ArrayList<>();
|
||||||
final List<String> postActionList = new ArrayList<>();
|
final List<String> postActionList = new ArrayList<>();
|
||||||
@ -316,7 +317,7 @@ public class DataFactory {
|
|||||||
int fieldId = 0;
|
int fieldId = 0;
|
||||||
LOGGER.debug("===> TABLE `{}`", tableName);
|
LOGGER.debug("===> TABLE `{}`", tableName);
|
||||||
final List<String> primaryKeys = new ArrayList<>();
|
final List<String> primaryKeys = new ArrayList<>();
|
||||||
|
|
||||||
for (final Field elem : clazz.getFields()) {
|
for (final Field elem : clazz.getFields()) {
|
||||||
// DEtect the primary key (support only one primary key right now...
|
// DEtect the primary key (support only one primary key right now...
|
||||||
if (AnnotationTools.isPrimaryKey(elem)) {
|
if (AnnotationTools.isPrimaryKey(elem)) {
|
||||||
@ -353,7 +354,7 @@ public class DataFactory {
|
|||||||
if (addOn != null) {
|
if (addOn != null) {
|
||||||
addOn.createTables(tableName, elem, tmpOut, preActionList, postActionList, createIfNotExist, createDrop, fieldId);
|
addOn.createTables(tableName, elem, tmpOut, preActionList, postActionList, createIfNotExist, createDrop, fieldId);
|
||||||
} else {
|
} else {
|
||||||
throw new Exception(
|
throw new DataAccessException(
|
||||||
"Element matked as add-on but add-on does not loaded: table:" + tableName + " field name=" + AnnotationTools.getFieldName(elem) + " type=" + elem.getType());
|
"Element matked as add-on but add-on does not loaded: table:" + tableName + " field name=" + AnnotationTools.getFieldName(elem) + " type=" + elem.getType());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -397,5 +398,5 @@ public class DataFactory {
|
|||||||
preActionList.addAll(postActionList);
|
preActionList.addAll(postActionList);
|
||||||
return preActionList;
|
return preActionList;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -20,6 +20,7 @@ import org.kar.archidata.dataAccess.QueryItem;
|
|||||||
import org.kar.archidata.dataAccess.QueryOptions;
|
import org.kar.archidata.dataAccess.QueryOptions;
|
||||||
import org.kar.archidata.dataAccess.QueryOr;
|
import org.kar.archidata.dataAccess.QueryOr;
|
||||||
import org.kar.archidata.dataAccess.addOn.model.LinkTable;
|
import org.kar.archidata.dataAccess.addOn.model.LinkTable;
|
||||||
|
import org.kar.archidata.exception.DataAccessException;
|
||||||
import org.kar.archidata.util.ConfigBaseVariable;
|
import org.kar.archidata.util.ConfigBaseVariable;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -31,43 +32,43 @@ import jakarta.validation.constraints.NotNull;
|
|||||||
public class AddOnManyToMany implements DataAccessAddOn {
|
public class AddOnManyToMany implements DataAccessAddOn {
|
||||||
static final Logger LOGGER = LoggerFactory.getLogger(AddOnManyToMany.class);
|
static final Logger LOGGER = LoggerFactory.getLogger(AddOnManyToMany.class);
|
||||||
static final String SEPARATOR = "-";
|
static final String SEPARATOR = "-";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> getAnnotationClass() {
|
public Class<?> getAnnotationClass() {
|
||||||
return ManyToMany.class;
|
return ManyToMany.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSQLFieldType(final Field elem) {
|
public String getSQLFieldType(final Field elem) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCompatibleField(final Field elem) {
|
public boolean isCompatibleField(final Field elem) {
|
||||||
final ManyToMany decorators = elem.getDeclaredAnnotation(ManyToMany.class);
|
final ManyToMany decorators = elem.getDeclaredAnnotation(ManyToMany.class);
|
||||||
return decorators != null;
|
return decorators != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii) throws SQLException, IllegalArgumentException, IllegalAccessException {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canInsert(final Field field) {
|
public boolean canInsert(final Field field) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRetrieve(final Field field) {
|
public boolean canRetrieve(final Field field) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String generateLinkTableNameField(final String tableName, final Field field) throws Exception {
|
public static String generateLinkTableNameField(final String tableName, final Field field) throws Exception {
|
||||||
final String name = AnnotationTools.getFieldName(field);
|
final String name = AnnotationTools.getFieldName(field);
|
||||||
return generateLinkTableName(tableName, name);
|
return generateLinkTableName(tableName, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String generateLinkTableName(final String tableName, final String name) {
|
public static String generateLinkTableName(final String tableName, final String name) {
|
||||||
String localName = name;
|
String localName = name;
|
||||||
if (name.endsWith("s")) {
|
if (name.endsWith("s")) {
|
||||||
@ -75,10 +76,10 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
return tableName + "_link_" + localName;
|
return tableName + "_link_" + localName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateConcatQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry,
|
public void generateConcatQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry,
|
||||||
@NotNull final String name, @NotNull final CountInOut elemCount, final QueryOptions options) {
|
@NotNull final String name, @NotNull final CountInOut elemCount, final QueryOptions options) {
|
||||||
|
|
||||||
final String linkTableName = generateLinkTableName(tableName, name);
|
final String linkTableName = generateLinkTableName(tableName, name);
|
||||||
final String tmpVariable = "tmp_" + Integer.toString(elemCount.value);
|
final String tmpVariable = "tmp_" + Integer.toString(elemCount.value);
|
||||||
querrySelect.append(" (SELECT GROUP_CONCAT(");
|
querrySelect.append(" (SELECT GROUP_CONCAT(");
|
||||||
@ -120,7 +121,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
|||||||
*/
|
*/
|
||||||
elemCount.inc();
|
elemCount.inc();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name,
|
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name,
|
||||||
@NotNull final CountInOut elemCount, final QueryOptions options) throws Exception {
|
@NotNull final CountInOut elemCount, final QueryOptions options) throws Exception {
|
||||||
@ -137,13 +138,13 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
if (objectClass == decorators.targetEntity()) {
|
if (objectClass == decorators.targetEntity()) {
|
||||||
if (decorators.fetch() == FetchType.EAGER) {
|
if (decorators.fetch() == FetchType.EAGER) {
|
||||||
throw new Exception("EAGER is not supported for list of element...");
|
throw new DataAccessException("EAGER is not supported for list of element...");
|
||||||
} else {
|
} else {
|
||||||
generateConcatQuerry(tableName, field, querrySelect, querry, name, elemCount, options);
|
generateConcatQuerry(tableName, field, querrySelect, querry, name, elemCount, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fillFromQuerry(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) throws Exception {
|
public void fillFromQuerry(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) throws Exception {
|
||||||
if (field.getType() != List.class) {
|
if (field.getType() != List.class) {
|
||||||
@ -161,7 +162,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
if (objectClass == decorators.targetEntity()) {
|
if (objectClass == decorators.targetEntity()) {
|
||||||
if (decorators.fetch() == FetchType.EAGER) {
|
if (decorators.fetch() == FetchType.EAGER) {
|
||||||
throw new Exception("EAGER is not supported for list of element...");
|
throw new DataAccessException("EAGER is not supported for list of element...");
|
||||||
} else {
|
} else {
|
||||||
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR);
|
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR);
|
||||||
//field.set(data, idList);
|
//field.set(data, idList);
|
||||||
@ -186,16 +187,16 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addLink(final Class<?> clazz, final long localKey, final String column, final long remoteKey) throws Exception {
|
public static void addLink(final Class<?> clazz, final long localKey, final String column, final long remoteKey) throws Exception {
|
||||||
final String tableName = AnnotationTools.getTableName(clazz);
|
final String tableName = AnnotationTools.getTableName(clazz);
|
||||||
final String linkTableName = generateLinkTableName(tableName, column);
|
final String linkTableName = generateLinkTableName(tableName, column);
|
||||||
final LinkTable insertElement = new LinkTable(localKey, remoteKey);
|
final LinkTable insertElement = new LinkTable(localKey, remoteKey);
|
||||||
final QueryOptions options = new QueryOptions(QueryOptions.OVERRIDE_TABLE_NAME, linkTableName);
|
final QueryOptions options = new QueryOptions(QueryOptions.OVERRIDE_TABLE_NAME, linkTableName);
|
||||||
DataAccess.insert(insertElement, options);
|
DataAccess.insert(insertElement, options);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int removeLink(final Class<?> clazz, final long localKey, final String column, final long remoteKey) throws Exception {
|
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 tableName = AnnotationTools.getTableName(clazz);
|
||||||
final String linkTableName = generateLinkTableName(tableName, column);
|
final String linkTableName = generateLinkTableName(tableName, column);
|
||||||
@ -203,7 +204,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
|||||||
final QueryAnd condition = new QueryAnd(new QueryCondition("object1Id", "=", localKey), new QueryCondition("object2Id", "=", remoteKey));
|
final QueryAnd condition = new QueryAnd(new QueryCondition("object1Id", "=", localKey), new QueryCondition("object2Id", "=", remoteKey));
|
||||||
return DataAccess.deleteWhere(LinkTable.class, condition, options);
|
return DataAccess.deleteWhere(LinkTable.class, condition, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void createTables(final String tableName, final Field field, final StringBuilder mainTableBuilder, final List<String> preActionList, final List<String> postActionList,
|
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 boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception {
|
||||||
|
@ -13,6 +13,7 @@ import org.kar.archidata.dataAccess.DataAccessAddOn;
|
|||||||
import org.kar.archidata.dataAccess.DataFactory;
|
import org.kar.archidata.dataAccess.DataFactory;
|
||||||
import org.kar.archidata.dataAccess.LazyGetter;
|
import org.kar.archidata.dataAccess.LazyGetter;
|
||||||
import org.kar.archidata.dataAccess.QueryOptions;
|
import org.kar.archidata.dataAccess.QueryOptions;
|
||||||
|
import org.kar.archidata.exception.DataAccessException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -22,12 +23,12 @@ import jakarta.validation.constraints.NotNull;
|
|||||||
|
|
||||||
public class AddOnManyToOne implements DataAccessAddOn {
|
public class AddOnManyToOne implements DataAccessAddOn {
|
||||||
static final Logger LOGGER = LoggerFactory.getLogger(AddOnManyToMany.class);
|
static final Logger LOGGER = LoggerFactory.getLogger(AddOnManyToMany.class);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> getAnnotationClass() {
|
public Class<?> getAnnotationClass() {
|
||||||
return ManyToOne.class;
|
return ManyToOne.class;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getSQLFieldType(final Field elem) throws Exception {
|
public String getSQLFieldType(final Field elem) throws Exception {
|
||||||
final String fieldName = AnnotationTools.getFieldName(elem);
|
final String fieldName = AnnotationTools.getFieldName(elem);
|
||||||
@ -39,13 +40,13 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isCompatibleField(final Field elem) {
|
public boolean isCompatibleField(final Field elem) {
|
||||||
final ManyToOne decorators = elem.getDeclaredAnnotation(ManyToOne.class);
|
final ManyToOne decorators = elem.getDeclaredAnnotation(ManyToOne.class);
|
||||||
return decorators != null;
|
return decorators != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii) throws Exception {
|
public void insertData(final PreparedStatement ps, final Field field, final Object rootObject, final CountInOut iii) throws Exception {
|
||||||
final Object data = field.get(rootObject);
|
final Object data = field.get(rootObject);
|
||||||
@ -59,7 +60,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
|||||||
final Object uid = idField.get(data);
|
final Object uid = idField.get(data);
|
||||||
if (uid == null) {
|
if (uid == null) {
|
||||||
ps.setNull(iii.value, Types.BIGINT);
|
ps.setNull(iii.value, Types.BIGINT);
|
||||||
throw new Exception("Not implemented adding subClasses ==> add it manualy before...");
|
throw new DataAccessException("Not implemented adding subClasses ==> add it manualy before...");
|
||||||
} else {
|
} else {
|
||||||
final Long dataLong = (Long) uid;
|
final Long dataLong = (Long) uid;
|
||||||
ps.setLong(iii.value, dataLong);
|
ps.setLong(iii.value, dataLong);
|
||||||
@ -67,7 +68,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
iii.inc();
|
iii.inc();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canInsert(final Field field) {
|
public boolean canInsert(final Field field) {
|
||||||
if (field.getType() == Long.class) {
|
if (field.getType() == Long.class) {
|
||||||
@ -79,7 +80,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canRetrieve(final Field field) {
|
public boolean canRetrieve(final Field field) {
|
||||||
if (field.getType() == Long.class) {
|
if (field.getType() == Long.class) {
|
||||||
@ -91,7 +92,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name,
|
public void generateQuerry(@NotNull final String tableName, @NotNull final Field field, @NotNull final StringBuilder querrySelect, @NotNull final StringBuilder querry, @NotNull final String name,
|
||||||
@NotNull final CountInOut elemCount, final QueryOptions options) throws Exception {
|
@NotNull final CountInOut elemCount, final QueryOptions options) throws Exception {
|
||||||
@ -130,14 +131,14 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
SELECT k.id, r.id
|
SELECT k.id, r.id
|
||||||
FROM `right` k
|
FROM `right` k
|
||||||
LEFT OUTER JOIN `rightDescription` r ON k.rightDescriptionId=r.id
|
LEFT OUTER JOIN `rightDescription` r ON k.rightDescriptionId=r.id
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fillFromQuerry(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) throws Exception {
|
public void fillFromQuerry(final ResultSet rs, final Field field, final Object data, final CountInOut count, final QueryOptions options, final List<LazyGetter> lazyCall) throws Exception {
|
||||||
if (field.getType() == Long.class) {
|
if (field.getType() == Long.class) {
|
||||||
@ -179,7 +180,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO : refacto this table to manage a generic table with dynamic name to be serialisable with the default system
|
// TODO : refacto this table to manage a generic table with dynamic name to be serialisable with the default system
|
||||||
@Override
|
@Override
|
||||||
public void createTables(final String tableName, final Field field, final StringBuilder mainTableBuilder, final List<String> preActionList, final List<String> postActionList,
|
public void createTables(final String tableName, final Field field, final StringBuilder mainTableBuilder, final List<String> preActionList, final List<String> postActionList,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user