From 71f69fb7cf9b8337756a73995d9432aa973dbe24 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Wed, 1 Jan 2025 18:06:00 +0100 Subject: [PATCH] [FEAT] Simplify interface of the ManyToMany (internal) add capability to overwrite the type of an object --- .../archidata/annotation/AnnotationTools.java | 12 ++- .../kar/archidata/dataAccess/DBAccessSQL.java | 38 ++++++-- .../kar/archidata/dataAccess/DataFactory.java | 39 ++++++-- .../addOnMongo/AddOnManyToMany.java | 94 ++++++++++++------- .../dataAccess/addOnSQL/AddOnManyToMany.java | 94 ++++++++++++------- ...bleLongLong.java => LinkTableGeneric.java} | 12 +-- .../addOnSQL/model/LinkTableLongUUID.java | 27 ------ .../addOnSQL/model/LinkTableUUIDLong.java | 27 ------ .../addOnSQL/model/LinkTableUUIDUUID.java | 27 ------ .../dataAccess/options/OptionSpecifyType.java | 12 +++ src/org/kar/archidata/model/GenericData.java | 4 - .../kar/archidata/model/OIDGenericData.java | 1 + 12 files changed, 210 insertions(+), 177 deletions(-) rename src/org/kar/archidata/dataAccess/addOnSQL/model/{LinkTableLongLong.java => LinkTableGeneric.java} (57%) delete mode 100644 src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableLongUUID.java delete mode 100644 src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableUUIDLong.java delete mode 100644 src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableUUIDUUID.java create mode 100644 src/org/kar/archidata/dataAccess/options/OptionSpecifyType.java diff --git a/src/org/kar/archidata/annotation/AnnotationTools.java b/src/org/kar/archidata/annotation/AnnotationTools.java index e655737..d7363ef 100644 --- a/src/org/kar/archidata/annotation/AnnotationTools.java +++ b/src/org/kar/archidata/annotation/AnnotationTools.java @@ -286,11 +286,15 @@ public class AnnotationTools { } public static boolean isPrimaryKey(final Field element) { - final Annotation[] annotation = element.getDeclaredAnnotationsByType(Id.class); - if (annotation.length == 0) { - return false; + final Annotation[] annotationSQL = element.getDeclaredAnnotationsByType(Id.class); + if (annotationSQL.length > 0) { + return true; } - return true; + final Annotation[] annotationMongo = element.getDeclaredAnnotationsByType(dev.morphia.annotations.Id.class); + if (annotationMongo.length > 0) { + return true; + } + return false; } public static boolean isUnique(final Field element) { diff --git a/src/org/kar/archidata/dataAccess/DBAccessSQL.java b/src/org/kar/archidata/dataAccess/DBAccessSQL.java index d24e731..8f230eb 100644 --- a/src/org/kar/archidata/dataAccess/DBAccessSQL.java +++ b/src/org/kar/archidata/dataAccess/DBAccessSQL.java @@ -35,6 +35,7 @@ import org.kar.archidata.dataAccess.options.DBInterfaceRoot; import org.kar.archidata.dataAccess.options.FilterValue; import org.kar.archidata.dataAccess.options.GroupBy; import org.kar.archidata.dataAccess.options.Limit; +import org.kar.archidata.dataAccess.options.OptionSpecifyType; import org.kar.archidata.dataAccess.options.OrderBy; import org.kar.archidata.dataAccess.options.QueryOption; import org.kar.archidata.dataAccess.options.TransmitKey; @@ -296,7 +297,7 @@ public class DBAccessSQL extends DBAccess { return new ObjectId(elem); } - protected void setValuedb( + protected void setValueToDb( final Class type, final T data, final CountInOut iii, @@ -417,7 +418,7 @@ public class DBAccessSQL extends DBAccess { ps.setString(iii.value, tmp.toString()); } } else { - throw new DataAccessException("Unknown Field Type"); + throw new DataAccessException("Unknown Field Type: " + type.getCanonicalName()); } iii.inc(); } @@ -611,7 +612,7 @@ public class DBAccessSQL extends DBAccess { } } } else { - throw new DataAccessException("Unknown Field Type"); + throw new DataAccessException("Unknown Field Type: " + type.getCanonicalName()); } count.inc(); } @@ -874,6 +875,7 @@ public class DBAccessSQL extends DBAccess { Field primaryKeyField = null; boolean generateUUID = false; boolean generateOID = false; + final List specificTypes = options.get(OptionSpecifyType.class); // real add in the BDD: try { // boolean createIfNotExist = clazz.getDeclaredAnnotationsByType(SQLIfNotExists.class).length != 0; @@ -1013,14 +1015,24 @@ public class DBAccessSQL extends DBAccess { addOn.insertData(this, ps, elem, data, iii); } else { // Generic class insertion... - final Class type = elem.getType(); + Class type = elem.getType(); if (!type.isPrimitive()) { final Object tmp = elem.get(data); if (tmp == null && elem.getDeclaredAnnotationsByType(DefaultValue.class).length != 0) { continue; } } - setValuedb(type, data, iii, elem, ps); + if (type == Object.class) { + for (final OptionSpecifyType specify : specificTypes) { + if (specify.name.equals(elem.getName())) { + type = specify.clazz; + LOGGER.trace("Detect overwrite of typing ... '{}' => '{}'", + elem.getType().getCanonicalName(), specify.clazz.getCanonicalName()); + break; + } + } + } + setValueToDb(type, data, iii, elem, ps); } count++; } @@ -1207,7 +1219,7 @@ public class DBAccessSQL extends DBAccess { continue; } } - setValuedb(type, data, iii, field, ps); + setValueToDb(type, data, iii, field, ps); } else { addOn.insertData(this, ps, field, data, iii); } @@ -1415,6 +1427,7 @@ public class DBAccessSQL extends DBAccess { final QueryOptions options, final List lazyCall) throws Exception { final boolean readAllfields = QueryOptions.readAllColomn(options); + final List specificTypes = options.get(OptionSpecifyType.class); // TODO: manage class that is defined inside a class ==> Not manage for now... Object data = null; for (final Constructor contructor : clazz.getConstructors()) { @@ -1442,7 +1455,18 @@ public class DBAccessSQL extends DBAccess { if (addOn != null) { addOn.fillFromQuery(this, rs, elem, data, count, options, lazyCall); } else { - setValueFromDb(elem.getType(), data, count, elem, rs, countNotNull); + Class type = elem.getType(); + if (type == Object.class) { + for (final OptionSpecifyType specify : specificTypes) { + if (specify.name.equals(elem.getName())) { + type = specify.clazz; + LOGGER.trace("Detect overwrite of typing ... '{}' => '{}'", + elem.getType().getCanonicalName(), specify.clazz.getCanonicalName()); + break; + } + } + } + setValueFromDb(type, data, count, elem, rs, countNotNull); } } return data; diff --git a/src/org/kar/archidata/dataAccess/DataFactory.java b/src/org/kar/archidata/dataAccess/DataFactory.java index fc9db8e..a07f105 100644 --- a/src/org/kar/archidata/dataAccess/DataFactory.java +++ b/src/org/kar/archidata/dataAccess/DataFactory.java @@ -10,12 +10,14 @@ import java.util.Date; import java.util.List; import java.util.UUID; +import org.bson.types.ObjectId; 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.addOnSQL.DataAccessAddOn; import org.kar.archidata.dataAccess.options.CreateDropTable; +import org.kar.archidata.dataAccess.options.OptionSpecifyType; import org.kar.archidata.exception.DataAccessException; import org.kar.archidata.tools.ConfigBaseVariable; import org.slf4j.Logger; @@ -34,6 +36,10 @@ public class DataFactory { if (type == UUID.class) { return "binary(16)"; } + if (type == ObjectId.class) { + return "varchar(24)"; + // return "binary(12)"; + } if (type == Long.class || type == long.class) { return "bigint"; } @@ -88,6 +94,10 @@ public class DataFactory { if (type == UUID.class) { return "BINARY(16)"; } + if (type == ObjectId.class) { + return "text"; + //return "BINARY(12)"; + } if (type == Long.class || type == long.class) { return "INTEGER"; } @@ -387,23 +397,38 @@ public class DataFactory { continue; } alreadyAdded.add(dataName); + + List specificTypes = new ArrayList<>(); + if (options != null) { + specificTypes = options.get(OptionSpecifyType.class); + } + Class basicType = elem.getType(); + if (basicType == Object.class) { + for (final OptionSpecifyType specify : specificTypes) { + if (specify.name.equals(elem.getName())) { + basicType = specify.clazz; + LOGGER.trace("Detect overwrite of typing ... '{}' => '{}'", + elem.getType().getCanonicalName(), specify.clazz.getCanonicalName()); + break; + } + } + } LOGGER.trace(" + '{}'", elem.getName()); if (DBAccessSQL.isAddOnField(elem)) { final DataAccessAddOn addOn = DBAccessSQL.findAddOnforField(elem); - LOGGER.trace("Create type for: {} ==> {} (ADD-ON)", AnnotationTools.getFieldName(elem), - elem.getType()); + LOGGER.trace("Create type for: {} ==> {} (ADD-ON)", AnnotationTools.getFieldName(elem), basicType); if (addOn != null) { addOn.createTables(tableName, primaryField, elem, tmpOut, preActionList, postActionList, createIfNotExist, createDrop, fieldId); } else { - throw new DataAccessException("Element matked as add-on but add-on does not loaded: table:" - + tableName + " field name=" + AnnotationTools.getFieldName(elem) + " type=" - + elem.getType()); + throw new DataAccessException( + "Element matked as add-on but add-on does not loaded: table:" + tableName + + " field name=" + AnnotationTools.getFieldName(elem) + " type=" + basicType); } } else { - LOGGER.trace("Create type for: {} ==> {}", AnnotationTools.getFieldName(elem), elem.getType()); + LOGGER.trace("Create type for: {} ==> {}", AnnotationTools.getFieldName(elem), basicType); DataFactory.createTablesSpecificType(tableName, tablePrimaryKeyField, elem, tmpOut, preActionList, - postActionList, createIfNotExist, createDrop, fieldId, elem.getType()); + postActionList, createIfNotExist, createDrop, fieldId, basicType); } fieldId++; } diff --git a/src/org/kar/archidata/dataAccess/addOnMongo/AddOnManyToMany.java b/src/org/kar/archidata/dataAccess/addOnMongo/AddOnManyToMany.java index 3050be2..ae7802a 100644 --- a/src/org/kar/archidata/dataAccess/addOnMongo/AddOnManyToMany.java +++ b/src/org/kar/archidata/dataAccess/addOnMongo/AddOnManyToMany.java @@ -16,11 +16,9 @@ import org.kar.archidata.dataAccess.LazyGetter; import org.kar.archidata.dataAccess.QueryAnd; import org.kar.archidata.dataAccess.QueryCondition; import org.kar.archidata.dataAccess.QueryOptions; -import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableLongLong; -import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableLongUUID; -import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableUUIDLong; -import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableUUIDUUID; +import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableGeneric; import org.kar.archidata.dataAccess.options.Condition; +import org.kar.archidata.dataAccess.options.OptionSpecifyType; import org.kar.archidata.dataAccess.options.OverrideTableName; import org.kar.archidata.exception.DataAccessException; import org.kar.archidata.tools.ConfigBaseVariable; @@ -315,28 +313,36 @@ public class AddOnManyToMany implements DataAccessAddOn { if (localKey instanceof final Long localKeyLong) { if (objectClass == Long.class) { actions.add(() -> { - ioDb.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName), - new Condition(new QueryCondition("object1Id", "=", localKeyLong))); + ioDb.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new Condition(new QueryCondition("object1Id", "=", localKeyLong)), + new OptionSpecifyType("object1Id", Long.class), + new OptionSpecifyType("object2Id", Long.class)); }); asyncInsert(ioDb, tableName, localKey, field, data, actions); } else { actions.add(() -> { - ioDb.deleteWhere(LinkTableLongUUID.class, new OverrideTableName(linkTableName), - new Condition(new QueryCondition("object1Id", "=", localKeyLong))); + ioDb.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new Condition(new QueryCondition("object1Id", "=", localKeyLong)), + new OptionSpecifyType("object1Id", Long.class), + new OptionSpecifyType("object2Id", UUID.class)); }); asyncInsert(ioDb, tableName, localKey, field, data, actions); } } else if (localKey instanceof final UUID localKeyUUID) { if (objectClass == Long.class) { actions.add(() -> { - ioDb.deleteWhere(LinkTableUUIDLong.class, new OverrideTableName(linkTableName), - new Condition(new QueryCondition("object1Id", "=", localKeyUUID))); + ioDb.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new Condition(new QueryCondition("object1Id", "=", localKeyUUID)), + new OptionSpecifyType("object1Id", UUID.class), + new OptionSpecifyType("object2Id", Long.class)); }); asyncInsert(ioDb, tableName, localKey, field, data, actions); } else { actions.add(() -> { - ioDb.deleteWhere(LinkTableUUIDUUID.class, new OverrideTableName(linkTableName), - new Condition(new QueryCondition("object1Id", "=", localKeyUUID))); + ioDb.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new Condition(new QueryCondition("object1Id", "=", localKeyUUID)), + new OptionSpecifyType("object1Id", UUID.class), + new OptionSpecifyType("object2Id", UUID.class)); }); asyncInsert(ioDb, tableName, localKey, field, data, actions); } @@ -381,19 +387,21 @@ public class AddOnManyToMany implements DataAccessAddOn { if (dataCasted.size() == 0) { return; } - final List insertElements = new ArrayList<>(); + final List insertElements = new ArrayList<>(); for (final Long remoteKey : dataCasted) { if (remoteKey == null) { throw new DataAccessException("Try to insert remote key with null value"); } - insertElements.add(new LinkTableLongLong(localKeyLong, remoteKey)); + insertElements.add(new LinkTableGeneric(localKeyLong, remoteKey)); } if (insertElements.size() == 0) { LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted); return; } actions.add(() -> { - ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName)); + ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), + new OptionSpecifyType("object2Id", Long.class)); }); } else { // ======================================================== @@ -404,19 +412,21 @@ public class AddOnManyToMany implements DataAccessAddOn { if (dataCasted.size() == 0) { return; } - final List insertElements = new ArrayList<>(); + final List insertElements = new ArrayList<>(); for (final UUID remoteKey : dataCasted) { if (remoteKey == null) { throw new DataAccessException("Try to insert remote key with null value"); } - insertElements.add(new LinkTableLongUUID(localKeyLong, remoteKey)); + insertElements.add(new LinkTableGeneric(localKeyLong, remoteKey)); } if (insertElements.size() == 0) { LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted); return; } actions.add(() -> { - ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName)); + ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), + new OptionSpecifyType("object2Id", UUID.class)); }); } } else if (localKey instanceof final UUID localKeyUUID) { @@ -429,19 +439,21 @@ public class AddOnManyToMany implements DataAccessAddOn { if (dataCasted.size() == 0) { return; } - final List insertElements = new ArrayList<>(); + final List insertElements = new ArrayList<>(); for (final Long remoteKey : dataCasted) { if (remoteKey == null) { throw new DataAccessException("Try to insert remote key with null value"); } - insertElements.add(new LinkTableUUIDLong(localKeyUUID, remoteKey)); + insertElements.add(new LinkTableGeneric(localKeyUUID, remoteKey)); } if (insertElements.size() == 0) { LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted); return; } actions.add(() -> { - ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName)); + ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", UUID.class), + new OptionSpecifyType("object2Id", Long.class)); }); } else { // ======================================================== @@ -452,19 +464,21 @@ public class AddOnManyToMany implements DataAccessAddOn { if (dataCasted.size() == 0) { return; } - final List insertElements = new ArrayList<>(); + final List insertElements = new ArrayList<>(); for (final UUID remoteKey : dataCasted) { if (remoteKey == null) { throw new DataAccessException("Try to insert remote key with null value"); } - insertElements.add(new LinkTableUUIDUUID(localKeyUUID, remoteKey)); + insertElements.add(new LinkTableGeneric(localKeyUUID, remoteKey)); } if (insertElements.size() == 0) { LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted); return; } actions.add(() -> { - ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName)); + ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", UUID.class), + new OptionSpecifyType("object2Id", UUID.class)); }); } } else { @@ -484,7 +498,8 @@ public class AddOnManyToMany implements DataAccessAddOn { throw new DataAccessException("Can not ManyToMany with other than List or List Model: List<" + objectClass.getCanonicalName() + ">"); } - ioDb.drop(LinkTableLongLong.class, new OverrideTableName(linkTableName)); + ioDb.drop(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), new OptionSpecifyType("object2Id", Long.class)); } @Override @@ -497,7 +512,8 @@ public class AddOnManyToMany implements DataAccessAddOn { throw new DataAccessException("Can not ManyToMany with other than List or List Model: List<" + objectClass.getCanonicalName() + ">"); } - ioDb.cleanAll(LinkTableLongLong.class, new OverrideTableName(linkTableName)); + ioDb.cleanAll(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), new OptionSpecifyType("object2Id", Long.class)); } public static void addLink( @@ -511,8 +527,9 @@ public class AddOnManyToMany implements DataAccessAddOn { final String linkTableName = generateLinkTableName(tableName, column); /* final Class objectClass = (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; if (objectClass != Long.class && objectClass != UUID.class) { throw new * DataAccessException("Can not ManyToMany with other than List or List Model: List<" + objectClass.getCanonicalName() + ">"); } */ - final LinkTableLongLong insertElement = new LinkTableLongLong(localKey, remoteKey); - daSQL.insert(insertElement, new OverrideTableName(linkTableName)); + final LinkTableGeneric insertElement = new LinkTableGeneric(localKey, remoteKey); + daSQL.insert(insertElement, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), new OptionSpecifyType("object2Id", Long.class)); } else if (ioDb instanceof final DBAccessMorphia dam) { } else { @@ -530,9 +547,10 @@ public class AddOnManyToMany implements DataAccessAddOn { if (ioDb instanceof final DBAccessMorphia daSQL) { final String tableName = AnnotationTools.getTableName(clazz); final String linkTableName = generateLinkTableName(tableName, column); - return daSQL.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName), + return daSQL.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), new Condition(new QueryAnd(new QueryCondition("object1Id", "=", localKey), - new QueryCondition("object2Id", "=", remoteKey)))); + new QueryCondition("object2Id", "=", remoteKey))), + new OptionSpecifyType("object1Id", Long.class), new OptionSpecifyType("object2Id", Long.class)); } else if (ioDb instanceof final DBAccessMorphia dam) { return 0L; } else { @@ -568,18 +586,26 @@ public class AddOnManyToMany implements DataAccessAddOn { final Class primaryType = primaryField.getType(); if (primaryType == Long.class) { if (objectClass == Long.class) { - final List sqlCommand = DataFactory.createTable(LinkTableLongLong.class, options); + options.add(new OptionSpecifyType("object1Id", Long.class)); + options.add(new OptionSpecifyType("object2Id", Long.class)); + final List sqlCommand = DataFactory.createTable(LinkTableGeneric.class, options); postActionList.addAll(sqlCommand); } else { - final List sqlCommand = DataFactory.createTable(LinkTableLongUUID.class, options); + options.add(new OptionSpecifyType("object1Id", Long.class)); + options.add(new OptionSpecifyType("object2Id", UUID.class)); + final List sqlCommand = DataFactory.createTable(LinkTableGeneric.class, options); postActionList.addAll(sqlCommand); } } else if (primaryType == UUID.class) { if (objectClass == Long.class) { - final List sqlCommand = DataFactory.createTable(LinkTableUUIDLong.class, options); + options.add(new OptionSpecifyType("object1Id", UUID.class)); + options.add(new OptionSpecifyType("object2Id", Long.class)); + final List sqlCommand = DataFactory.createTable(LinkTableGeneric.class, options); postActionList.addAll(sqlCommand); } else { - final List sqlCommand = DataFactory.createTable(LinkTableUUIDUUID.class, options); + options.add(new OptionSpecifyType("object1Id", UUID.class)); + options.add(new OptionSpecifyType("object2Id", UUID.class)); + final List sqlCommand = DataFactory.createTable(LinkTableGeneric.class, options); postActionList.addAll(sqlCommand); } } else { diff --git a/src/org/kar/archidata/dataAccess/addOnSQL/AddOnManyToMany.java b/src/org/kar/archidata/dataAccess/addOnSQL/AddOnManyToMany.java index 2f5b51d..33b116a 100644 --- a/src/org/kar/archidata/dataAccess/addOnSQL/AddOnManyToMany.java +++ b/src/org/kar/archidata/dataAccess/addOnSQL/AddOnManyToMany.java @@ -20,11 +20,9 @@ import org.kar.archidata.dataAccess.QueryAnd; import org.kar.archidata.dataAccess.QueryCondition; import org.kar.archidata.dataAccess.QueryInList; import org.kar.archidata.dataAccess.QueryOptions; -import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableLongLong; -import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableLongUUID; -import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableUUIDLong; -import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableUUIDUUID; +import org.kar.archidata.dataAccess.addOnSQL.model.LinkTableGeneric; import org.kar.archidata.dataAccess.options.Condition; +import org.kar.archidata.dataAccess.options.OptionSpecifyType; import org.kar.archidata.dataAccess.options.OverrideTableName; import org.kar.archidata.exception.DataAccessException; import org.kar.archidata.tools.ConfigBaseVariable; @@ -319,28 +317,36 @@ public class AddOnManyToMany implements DataAccessAddOn { if (localKey instanceof final Long localKeyLong) { if (objectClass == Long.class) { actions.add(() -> { - ioDb.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName), - new Condition(new QueryCondition("object1Id", "=", localKeyLong))); + ioDb.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new Condition(new QueryCondition("object1Id", "=", localKeyLong)), + new OptionSpecifyType("object1Id", Long.class), + new OptionSpecifyType("object2Id", Long.class)); }); asyncInsert(ioDb, tableName, localKey, field, data, actions); } else { actions.add(() -> { - ioDb.deleteWhere(LinkTableLongUUID.class, new OverrideTableName(linkTableName), - new Condition(new QueryCondition("object1Id", "=", localKeyLong))); + ioDb.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new Condition(new QueryCondition("object1Id", "=", localKeyLong)), + new OptionSpecifyType("object1Id", Long.class), + new OptionSpecifyType("object2Id", UUID.class)); }); asyncInsert(ioDb, tableName, localKey, field, data, actions); } } else if (localKey instanceof final UUID localKeyUUID) { if (objectClass == Long.class) { actions.add(() -> { - ioDb.deleteWhere(LinkTableUUIDLong.class, new OverrideTableName(linkTableName), - new Condition(new QueryCondition("object1Id", "=", localKeyUUID))); + ioDb.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new Condition(new QueryCondition("object1Id", "=", localKeyUUID)), + new OptionSpecifyType("object1Id", UUID.class), + new OptionSpecifyType("object2Id", Long.class)); }); asyncInsert(ioDb, tableName, localKey, field, data, actions); } else { actions.add(() -> { - ioDb.deleteWhere(LinkTableUUIDUUID.class, new OverrideTableName(linkTableName), - new Condition(new QueryCondition("object1Id", "=", localKeyUUID))); + ioDb.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new Condition(new QueryCondition("object1Id", "=", localKeyUUID)), + new OptionSpecifyType("object1Id", UUID.class), + new OptionSpecifyType("object2Id", UUID.class)); }); asyncInsert(ioDb, tableName, localKey, field, data, actions); } @@ -385,19 +391,21 @@ public class AddOnManyToMany implements DataAccessAddOn { if (dataCasted.size() == 0) { return; } - final List insertElements = new ArrayList<>(); + final List insertElements = new ArrayList<>(); for (final Long remoteKey : dataCasted) { if (remoteKey == null) { throw new DataAccessException("Try to insert remote key with null value"); } - insertElements.add(new LinkTableLongLong(localKeyLong, remoteKey)); + insertElements.add(new LinkTableGeneric(localKeyLong, remoteKey)); } if (insertElements.size() == 0) { LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted); return; } actions.add(() -> { - ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName)); + ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), + new OptionSpecifyType("object2Id", Long.class)); }); } else { // ======================================================== @@ -408,19 +416,21 @@ public class AddOnManyToMany implements DataAccessAddOn { if (dataCasted.size() == 0) { return; } - final List insertElements = new ArrayList<>(); + final List insertElements = new ArrayList<>(); for (final UUID remoteKey : dataCasted) { if (remoteKey == null) { throw new DataAccessException("Try to insert remote key with null value"); } - insertElements.add(new LinkTableLongUUID(localKeyLong, remoteKey)); + insertElements.add(new LinkTableGeneric(localKeyLong, remoteKey)); } if (insertElements.size() == 0) { LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted); return; } actions.add(() -> { - ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName)); + ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), + new OptionSpecifyType("object2Id", UUID.class)); }); } } else if (localKey instanceof final UUID localKeyUUID) { @@ -433,19 +443,21 @@ public class AddOnManyToMany implements DataAccessAddOn { if (dataCasted.size() == 0) { return; } - final List insertElements = new ArrayList<>(); + final List insertElements = new ArrayList<>(); for (final Long remoteKey : dataCasted) { if (remoteKey == null) { throw new DataAccessException("Try to insert remote key with null value"); } - insertElements.add(new LinkTableUUIDLong(localKeyUUID, remoteKey)); + insertElements.add(new LinkTableGeneric(localKeyUUID, remoteKey)); } if (insertElements.size() == 0) { LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted); return; } actions.add(() -> { - ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName)); + ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", UUID.class), + new OptionSpecifyType("object2Id", Long.class)); }); } else { // ======================================================== @@ -456,19 +468,21 @@ public class AddOnManyToMany implements DataAccessAddOn { if (dataCasted.size() == 0) { return; } - final List insertElements = new ArrayList<>(); + final List insertElements = new ArrayList<>(); for (final UUID remoteKey : dataCasted) { if (remoteKey == null) { throw new DataAccessException("Try to insert remote key with null value"); } - insertElements.add(new LinkTableUUIDUUID(localKeyUUID, remoteKey)); + insertElements.add(new LinkTableGeneric(localKeyUUID, remoteKey)); } if (insertElements.size() == 0) { LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted); return; } actions.add(() -> { - ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName)); + ioDb.insertMultiple(insertElements, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", UUID.class), + new OptionSpecifyType("object2Id", UUID.class)); }); } } else { @@ -488,7 +502,8 @@ public class AddOnManyToMany implements DataAccessAddOn { throw new DataAccessException("Can not ManyToMany with other than List or List Model: List<" + objectClass.getCanonicalName() + ">"); } - ioDb.drop(LinkTableLongLong.class, new OverrideTableName(linkTableName)); + ioDb.drop(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), new OptionSpecifyType("object2Id", Long.class)); } @Override @@ -501,7 +516,8 @@ public class AddOnManyToMany implements DataAccessAddOn { throw new DataAccessException("Can not ManyToMany with other than List or List Model: List<" + objectClass.getCanonicalName() + ">"); } - ioDb.cleanAll(LinkTableLongLong.class, new OverrideTableName(linkTableName)); + ioDb.cleanAll(LinkTableGeneric.class, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), new OptionSpecifyType("object2Id", Long.class)); } public static void addLink( @@ -515,8 +531,9 @@ public class AddOnManyToMany implements DataAccessAddOn { final String linkTableName = generateLinkTableName(tableName, column); /* final Class objectClass = (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; if (objectClass != Long.class && objectClass != UUID.class) { throw new * DataAccessException("Can not ManyToMany with other than List or List Model: List<" + objectClass.getCanonicalName() + ">"); } */ - final LinkTableLongLong insertElement = new LinkTableLongLong(localKey, remoteKey); - daSQL.insert(insertElement, new OverrideTableName(linkTableName)); + final LinkTableGeneric insertElement = new LinkTableGeneric(localKey, remoteKey); + daSQL.insert(insertElement, new OverrideTableName(linkTableName), + new OptionSpecifyType("object1Id", Long.class), new OptionSpecifyType("object2Id", Long.class)); } else if (ioDb instanceof final DBAccessMorphia dam) { } else { @@ -534,9 +551,10 @@ public class AddOnManyToMany implements DataAccessAddOn { if (ioDb instanceof final DBAccessSQL daSQL) { final String tableName = AnnotationTools.getTableName(clazz); final String linkTableName = generateLinkTableName(tableName, column); - return daSQL.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName), + return daSQL.deleteWhere(LinkTableGeneric.class, new OverrideTableName(linkTableName), new Condition(new QueryAnd(new QueryCondition("object1Id", "=", localKey), - new QueryCondition("object2Id", "=", remoteKey)))); + new QueryCondition("object2Id", "=", remoteKey))), + new OptionSpecifyType("object1Id", Long.class), new OptionSpecifyType("object2Id", Long.class)); } else if (ioDb instanceof final DBAccessMorphia dam) { return 0L; } else { @@ -572,18 +590,26 @@ public class AddOnManyToMany implements DataAccessAddOn { final Class primaryType = primaryField.getType(); if (primaryType == Long.class) { if (objectClass == Long.class) { - final List sqlCommand = DataFactory.createTable(LinkTableLongLong.class, options); + options.add(new OptionSpecifyType("object1Id", Long.class)); + options.add(new OptionSpecifyType("object2Id", Long.class)); + final List sqlCommand = DataFactory.createTable(LinkTableGeneric.class, options); postActionList.addAll(sqlCommand); } else { - final List sqlCommand = DataFactory.createTable(LinkTableLongUUID.class, options); + options.add(new OptionSpecifyType("object1Id", Long.class)); + options.add(new OptionSpecifyType("object2Id", UUID.class)); + final List sqlCommand = DataFactory.createTable(LinkTableGeneric.class, options); postActionList.addAll(sqlCommand); } } else if (primaryType == UUID.class) { if (objectClass == Long.class) { - final List sqlCommand = DataFactory.createTable(LinkTableUUIDLong.class, options); + options.add(new OptionSpecifyType("object1Id", UUID.class)); + options.add(new OptionSpecifyType("object2Id", Long.class)); + final List sqlCommand = DataFactory.createTable(LinkTableGeneric.class, options); postActionList.addAll(sqlCommand); } else { - final List sqlCommand = DataFactory.createTable(LinkTableUUIDUUID.class, options); + options.add(new OptionSpecifyType("object1Id", UUID.class)); + options.add(new OptionSpecifyType("object2Id", UUID.class)); + final List sqlCommand = DataFactory.createTable(LinkTableGeneric.class, options); postActionList.addAll(sqlCommand); } } else { diff --git a/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableLongLong.java b/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableGeneric.java similarity index 57% rename from src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableLongLong.java rename to src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableGeneric.java index e7c002e..57bcd84 100644 --- a/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableLongLong.java +++ b/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableGeneric.java @@ -1,25 +1,25 @@ package org.kar.archidata.dataAccess.addOnSQL.model; -import org.kar.archidata.model.UUIDGenericDataSoftDelete; +import org.kar.archidata.model.OIDGenericDataSoftDelete; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.persistence.Column; -public class LinkTableLongLong extends UUIDGenericDataSoftDelete { - public LinkTableLongLong() { +public class LinkTableGeneric extends OIDGenericDataSoftDelete { + public LinkTableGeneric() { // nothing to do... } - public LinkTableLongLong(final long object1Id, final long object2Id) { + public LinkTableGeneric(final Object object1Id, final Object object2Id) { this.object1Id = object1Id; this.object2Id = object2Id; } @Schema(description = "Object reference 1") @Column(nullable = false) - public Long object1Id; + public Object object1Id; @Schema(description = "Object reference 2") @Column(nullable = false) - public Long object2Id; + public Object object2Id; } diff --git a/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableLongUUID.java b/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableLongUUID.java deleted file mode 100644 index 8db1ecd..0000000 --- a/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableLongUUID.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.kar.archidata.dataAccess.addOnSQL.model; - -import java.util.UUID; - -import org.kar.archidata.model.UUIDGenericDataSoftDelete; - -import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.persistence.Column; - -public class LinkTableLongUUID extends UUIDGenericDataSoftDelete { - public LinkTableLongUUID() { - // nothing to do... - } - - public LinkTableLongUUID(final long object1Id, final UUID object2Id) { - this.object1Id = object1Id; - this.object2Id = object2Id; - } - - @Schema(description = "Object reference 1") - @Column(nullable = false) - public Long object1Id; - @Schema(description = "Object reference 2") - @Column(nullable = false) - public UUID object2Id; - -} diff --git a/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableUUIDLong.java b/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableUUIDLong.java deleted file mode 100644 index 1314b4b..0000000 --- a/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableUUIDLong.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.kar.archidata.dataAccess.addOnSQL.model; - -import java.util.UUID; - -import org.kar.archidata.model.UUIDGenericDataSoftDelete; - -import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.persistence.Column; - -public class LinkTableUUIDLong extends UUIDGenericDataSoftDelete { - public LinkTableUUIDLong() { - // nothing to do... - } - - public LinkTableUUIDLong(final UUID object1Id, final long object2Id) { - this.object1Id = object1Id; - this.object2Id = object2Id; - } - - @Schema(description = "Object reference 1") - @Column(nullable = false) - public UUID object1Id; - @Schema(description = "Object reference 2") - @Column(nullable = false) - public Long object2Id; - -} diff --git a/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableUUIDUUID.java b/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableUUIDUUID.java deleted file mode 100644 index 0316d6b..0000000 --- a/src/org/kar/archidata/dataAccess/addOnSQL/model/LinkTableUUIDUUID.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.kar.archidata.dataAccess.addOnSQL.model; - -import java.util.UUID; - -import org.kar.archidata.model.UUIDGenericDataSoftDelete; - -import io.swagger.v3.oas.annotations.media.Schema; -import jakarta.persistence.Column; - -public class LinkTableUUIDUUID extends UUIDGenericDataSoftDelete { - public LinkTableUUIDUUID() { - // nothing to do... - } - - public LinkTableUUIDUUID(final UUID object1Id, final UUID object2Id) { - this.object1Id = object1Id; - this.object2Id = object2Id; - } - - @Schema(description = "Object reference 1") - @Column(nullable = false) - public UUID object1Id; - @Schema(description = "Object reference 2") - @Column(nullable = false) - public UUID object2Id; - -} diff --git a/src/org/kar/archidata/dataAccess/options/OptionSpecifyType.java b/src/org/kar/archidata/dataAccess/options/OptionSpecifyType.java new file mode 100644 index 0000000..a8efdb8 --- /dev/null +++ b/src/org/kar/archidata/dataAccess/options/OptionSpecifyType.java @@ -0,0 +1,12 @@ +package org.kar.archidata.dataAccess.options; + +public class OptionSpecifyType extends QueryOption { + public final String name; + public final Class clazz; + + // To specify the type of an element if the model is a Object. + public OptionSpecifyType(final String name, final Class clazz) { + this.clazz = clazz; + this.name = name; + } +} diff --git a/src/org/kar/archidata/model/GenericData.java b/src/org/kar/archidata/model/GenericData.java index 4749023..dacedb0 100644 --- a/src/org/kar/archidata/model/GenericData.java +++ b/src/org/kar/archidata/model/GenericData.java @@ -1,7 +1,5 @@ package org.kar.archidata.model; -import org.bson.types.ObjectId; - import io.swagger.v3.oas.annotations.media.Schema; import jakarta.persistence.Column; import jakarta.persistence.GeneratedValue; @@ -9,8 +7,6 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; public class GenericData extends GenericTiming { - @dev.morphia.annotations.Id - private ObjectId _id; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(nullable = false, unique = true) diff --git a/src/org/kar/archidata/model/OIDGenericData.java b/src/org/kar/archidata/model/OIDGenericData.java index a1c26cd..b1d76c3 100644 --- a/src/org/kar/archidata/model/OIDGenericData.java +++ b/src/org/kar/archidata/model/OIDGenericData.java @@ -9,6 +9,7 @@ import jakarta.validation.constraints.NotNull; public class OIDGenericData extends GenericTiming { @Id + //@jakarta.persistence.Id @Column(nullable = false, unique = true) @Schema(description = "Unique ObjectID of the object", required = false, readOnly = true, example = "65161616841351") @NotNull