[DEV] better management of UUID as remove elemnt

This commit is contained in:
Edouard DUPIN 2024-04-20 21:58:31 +02:00
parent 3309abe053
commit 0605ef0824
14 changed files with 502 additions and 86 deletions

View File

@ -257,6 +257,19 @@ public class AnnotationTools {
return true; return true;
} }
public static Field getPrimaryKeyField(final Class<?> clazz) throws Exception {
for (final Field field : clazz.getFields()) {
// static field is only for internal global declaration ==> remove it ..
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
continue;
}
if (AnnotationTools.isPrimaryKey(field)) {
return field;
}
}
return null;
}
public static boolean isPrimaryKey(final Field element) throws Exception { public static boolean isPrimaryKey(final Field element) throws Exception {
final Annotation[] annotation = element.getDeclaredAnnotationsByType(Id.class); final Annotation[] annotation = element.getDeclaredAnnotationsByType(Id.class);
if (annotation.length == 0) { if (annotation.length == 0) {

View File

@ -174,7 +174,7 @@ public class DataAccess {
throw new InternalServerErrorException("Can Not manage the DB-access"); throw new InternalServerErrorException("Can Not manage the DB-access");
} }
/** extract a list of "-" separated element from a SQL input data. /** Extract a list of Long with "-" separated element from a SQL input data.
* @param rs Result Set of the BDD * @param rs Result Set of the BDD
* @param iii Id in the result set * @param iii Id in the result set
* @return The list of Long value * @return The list of Long value
@ -185,7 +185,7 @@ public class DataAccess {
return null; return null;
} }
final List<Long> out = new ArrayList<>(); final List<Long> out = new ArrayList<>();
final String[] elements = trackString.split("-"); final String[] elements = trackString.split(separator);
for (final String elem : elements) { for (final String elem : elements) {
final Long tmp = Long.parseLong(elem); final Long tmp = Long.parseLong(elem);
out.add(tmp); out.add(tmp);
@ -193,6 +193,25 @@ public class DataAccess {
return out; return out;
} }
/** Extract a list of UUID with "-" separated element from a SQL input data.
* @param rs Result Set of the BDD
* @param iii Id in the result set
* @return The list of Long value
* @throws SQLException if an error is generated in the SQL request. */
public static List<UUID> getListOfUUIDs(final ResultSet rs, final int iii, final String separator) throws SQLException {
final String trackString = rs.getString(iii);
if (rs.wasNull()) {
return null;
}
final List<UUID> out = new ArrayList<>();
final String[] elements = trackString.split(separator);
for (final String elem : elements) {
final UUID tmp = UUID.fromString(elem);
out.add(tmp);
}
return out;
}
protected static <T> void setValuedb(final Class<?> type, final T data, final CountInOut iii, final Field field, final PreparedStatement ps) throws Exception { protected static <T> void setValuedb(final Class<?> type, final T data, final CountInOut iii, final Field field, final PreparedStatement ps) throws Exception {
if (type == UUID.class) { if (type == UUID.class) {
final Object tmp = field.get(data); final Object tmp = field.get(data);
@ -930,7 +949,11 @@ public class DataAccess {
final List<LazyGetter> asyncActions = new ArrayList<>(); final List<LazyGetter> asyncActions = new ArrayList<>();
for (final Field field : asyncFieldUpdate) { for (final Field field : asyncFieldUpdate) {
final DataAccessAddOn addOn = findAddOnforField(field); final DataAccessAddOn addOn = findAddOnforField(field);
if (uniqueSQLID != null) {
addOn.asyncInsert(tableName, uniqueSQLID, field, field.get(data), asyncActions); addOn.asyncInsert(tableName, uniqueSQLID, field, field.get(data), asyncActions);
} else if (uniqueSQLUUID != null) {
addOn.asyncInsert(tableName, uniqueSQLUUID, field, field.get(data), asyncActions);
}
} }
for (final LazyGetter action : asyncActions) { for (final LazyGetter action : asyncActions) {
action.doRequest(); action.doRequest();

View File

@ -61,8 +61,17 @@ public interface DataAccessAddOn {
* @param createDrop * @param createDrop
* @param fieldId * @param fieldId
* @throws Exception */ * @throws Exception */
void createTables(String tableName, Field field, StringBuilder mainTableBuilder, List<String> preActionList, List<String> postActionList, boolean createIfNotExist, boolean createDrop, int fieldId) void createTables(//
throws Exception; String tableName, //
final Field primaryField, //
Field field, //
StringBuilder mainTableBuilder, //
List<String> preActionList, //
List<String> postActionList, //
boolean createIfNotExist, //
boolean createDrop, //
int fieldId //
) throws Exception;
/** Some action must be done asynchronously for update or remove element /** Some action must be done asynchronously for update or remove element
* @param field * @param field

View File

@ -339,6 +339,7 @@ public class DataFactory {
LOGGER.debug("===> TABLE `{}`", tableName); LOGGER.debug("===> TABLE `{}`", tableName);
final List<String> primaryKeys = new ArrayList<>(); final List<String> primaryKeys = new ArrayList<>();
final Field primaryField = AnnotationTools.getPrimaryKeyField(clazz);
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)) {
@ -373,7 +374,7 @@ public class DataFactory {
final DataAccessAddOn addOn = DataAccess.findAddOnforField(elem); final DataAccessAddOn addOn = DataAccess.findAddOnforField(elem);
LOGGER.trace("Create type for: {} ==> {} (ADD-ON)", AnnotationTools.getFieldName(elem), elem.getType()); LOGGER.trace("Create type for: {} ==> {} (ADD-ON)", AnnotationTools.getFieldName(elem), elem.getType());
if (addOn != null) { if (addOn != null) {
addOn.createTables(tableName, elem, tmpOut, preActionList, postActionList, createIfNotExist, createDrop, fieldId); addOn.createTables(tableName, primaryField, elem, tmpOut, preActionList, postActionList, createIfNotExist, createDrop, fieldId);
} else { } else {
throw new DataAccessException( 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());

View File

@ -141,8 +141,17 @@ public class AddOnDataJson implements DataAccessAddOn {
} }
@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 boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception { final String tableName, //
final Field primaryField, //
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 {
DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, JsonValue.class); DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, JsonValue.class);
} }

View File

@ -7,6 +7,7 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
import org.kar.archidata.annotation.AnnotationTools; import org.kar.archidata.annotation.AnnotationTools;
import org.kar.archidata.dataAccess.CountInOut; import org.kar.archidata.dataAccess.CountInOut;
@ -18,7 +19,10 @@ import org.kar.archidata.dataAccess.QueryAnd;
import org.kar.archidata.dataAccess.QueryCondition; import org.kar.archidata.dataAccess.QueryCondition;
import org.kar.archidata.dataAccess.QueryInList; import org.kar.archidata.dataAccess.QueryInList;
import org.kar.archidata.dataAccess.QueryOptions; import org.kar.archidata.dataAccess.QueryOptions;
import org.kar.archidata.dataAccess.addOn.model.LinkTable; import org.kar.archidata.dataAccess.addOn.model.LinkTableLongLong;
import org.kar.archidata.dataAccess.addOn.model.LinkTableLongUUID;
import org.kar.archidata.dataAccess.addOn.model.LinkTableUUIDLong;
import org.kar.archidata.dataAccess.addOn.model.LinkTableUUIDUUID;
import org.kar.archidata.dataAccess.options.Condition; import org.kar.archidata.dataAccess.options.Condition;
import org.kar.archidata.dataAccess.options.OverrideTableName; import org.kar.archidata.dataAccess.options.OverrideTableName;
import org.kar.archidata.exception.DataAccessException; import org.kar.archidata.exception.DataAccessException;
@ -32,7 +36,8 @@ 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_LONG = "-";
static final String SEPARATOR_UUID = "_";
@Override @Override
public Class<?> getAnnotationClass() { public Class<?> getAnnotationClass() {
@ -62,7 +67,15 @@ public class AddOnManyToMany implements DataAccessAddOn {
@Override @Override
public boolean canRetrieve(final Field field) { public boolean canRetrieve(final Field field) {
if (field.getType() != List.class) {
return false;
}
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
if (objectClass == Long.class || objectClass == UUID.class) {
return true; return true;
} else {
return false;
}
} }
public static String generateLinkTableNameField(final String tableName, final Field field) throws Exception { public static String generateLinkTableNameField(final String tableName, final Field field) throws Exception {
@ -78,21 +91,39 @@ 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 name, @NotNull final CountInOut elemCount, final QueryOptions options) { @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//
) {
final String linkTableName = generateLinkTableName(tableName, name); final String linkTableName = generateLinkTableName(tableName, name);
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
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(");
if (objectClass == Long.class) {
querrySelect.append(tmpVariable); querrySelect.append(tmpVariable);
querrySelect.append(".object2Id "); querrySelect.append(".object2Id ");
} else {
querrySelect.append("BIN_TO_UUID(");
querrySelect.append(tmpVariable);
querrySelect.append(".object2Id) ");
}
if ("sqlite".equals(ConfigBaseVariable.getDBType())) { if ("sqlite".equals(ConfigBaseVariable.getDBType())) {
querrySelect.append(", "); querrySelect.append(", ");
} else { } else {
querrySelect.append("SEPARATOR "); querrySelect.append("SEPARATOR ");
} }
querrySelect.append("'"); querrySelect.append("'");
querrySelect.append(SEPARATOR); if (objectClass == Long.class) {
querrySelect.append(SEPARATOR_LONG);
} else {
querrySelect.append(SEPARATOR_UUID);
}
querrySelect.append("') FROM "); querrySelect.append("') FROM ");
querrySelect.append(linkTableName); querrySelect.append(linkTableName);
querrySelect.append(" "); querrySelect.append(" ");
@ -118,13 +149,20 @@ public class AddOnManyToMany implements DataAccessAddOn {
} }
@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 CountInOut elemCount, final QueryOptions options) throws Exception { @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 {
if (field.getType() != List.class) { if (field.getType() != List.class) {
return; return;
} }
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
if (objectClass == Long.class) { if (objectClass == Long.class || objectClass == UUID.class) {
generateConcatQuerry(tableName, field, querrySelect, querry, name, elemCount, options); generateConcatQuerry(tableName, field, querrySelect, querry, name, elemCount, options);
} }
final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class); final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
@ -141,20 +179,29 @@ public class AddOnManyToMany implements DataAccessAddOn {
} }
@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) {
LOGGER.error("Can not ManyToMany with other than List Model: {}", field.getType().getCanonicalName()); LOGGER.error("Can not ManyToMany with other than List Model: {}", field.getType().getCanonicalName());
return; return;
} }
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
if (objectClass == Long.class) { if (objectClass == Long.class) {
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR); final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR_LONG);
field.set(data, idList);
count.inc();
return;
} else if (objectClass == UUID.class) {
final List<UUID> idList = DataAccess.getListOfUUIDs(rs, count.value, SEPARATOR_UUID);
field.set(data, idList); field.set(data, idList);
count.inc(); count.inc();
return; return;
// } else {
// LOGGER.error("Can not ManyToMany with other than List<Long> Model: List<{}>", objectClass.getCanonicalName());
// return;
} }
final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class); final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
if (decorators == null) { if (decorators == null) {
@ -163,8 +210,8 @@ 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 DataAccessException("EAGER is not supported for list of element..."); throw new DataAccessException("EAGER is not supported for list of element...");
} else { } else if (objectClass == Long.class) {
final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR); final List<Long> idList = DataAccess.getListOfIds(rs, count.value, SEPARATOR_LONG);
// field.set(data, idList); // field.set(data, idList);
count.inc(); count.inc();
if (idList != null && idList.size() > 0) { if (idList != null && idList.size() > 0) {
@ -182,6 +229,25 @@ public class AddOnManyToMany implements DataAccessAddOn {
}; };
lazyCall.add(lambda); lazyCall.add(lambda);
} }
} else if (objectClass == UUID.class) {
final List<UUID> idList = DataAccess.getListOfUUIDs(rs, count.value, SEPARATOR_UUID);
// field.set(data, idList);
count.inc();
if (idList != null && idList.size() > 0) {
final String idField = AnnotationTools.getFieldName(AnnotationTools.getIdField(objectClass));
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
final LazyGetter lambda = () -> {
final List<UUID> childs = new ArrayList<>(idList);
// TODO: update to have get with abstract types ....
@SuppressWarnings("unchecked")
final Object foreignData = DataAccess.getsWhere(decorators.targetEntity(), new Condition(new QueryInList<>(idField, childs)));
if (foreignData == null) {
return;
}
field.set(data, foreignData);
};
lazyCall.add(lambda);
}
} }
} }
} }
@ -197,12 +263,38 @@ public class AddOnManyToMany implements DataAccessAddOn {
LOGGER.error("Can not ManyToMany with other than List Model: {}", field.getType().getCanonicalName()); LOGGER.error("Can not ManyToMany with other than List Model: {}", field.getType().getCanonicalName());
return; return;
} }
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<Long> or List<UUID> Model: List<" + objectClass.getCanonicalName() + ">");
}
final String columnName = AnnotationTools.getFieldName(field); final String columnName = AnnotationTools.getFieldName(field);
final String linkTableName = generateLinkTableName(tableName, columnName); final String linkTableName = generateLinkTableName(tableName, columnName);
if (localKey instanceof final Long localKeyLong) {
if (objectClass == Long.class) {
actions.add(() -> { actions.add(() -> {
DataAccess.deleteWhere(LinkTable.class, new OverrideTableName(linkTableName), new Condition(new QueryCondition("object1Id", "=", localKey))); DataAccess.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName), new Condition(new QueryCondition("object1Id", "=", localKeyLong)));
}); });
asyncInsert(tableName, localKey, field, data, actions); asyncInsert(tableName, localKey, field, data, actions);
} else {
actions.add(() -> {
DataAccess.deleteWhere(LinkTableLongUUID.class, new OverrideTableName(linkTableName), new Condition(new QueryCondition("object1Id", "=", localKeyLong)));
});
asyncInsert(tableName, localKey, field, data, actions);
}
} else if (localKey instanceof final UUID localKeyUUID) {
if (objectClass == Long.class) {
actions.add(() -> {
DataAccess.deleteWhere(LinkTableUUIDLong.class, new OverrideTableName(linkTableName), new Condition(new QueryCondition("object1Id", "=", localKeyUUID)));
});
asyncInsert(tableName, localKey, field, data, actions);
} else {
actions.add(() -> {
DataAccess.deleteWhere(LinkTableUUIDUUID.class, new OverrideTableName(linkTableName), new Condition(new QueryCondition("object1Id", "=", localKeyUUID)));
});
asyncInsert(tableName, localKey, field, data, actions);
}
}
} }
@Override @Override
@ -219,28 +311,51 @@ public class AddOnManyToMany implements DataAccessAddOn {
LOGGER.error("Can not ManyToMany with other than List Model: {}", field.getType().getCanonicalName()); LOGGER.error("Can not ManyToMany with other than List Model: {}", field.getType().getCanonicalName());
return; return;
} }
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<Long> or List<UUID> Model: List<" + objectClass.getCanonicalName() + ">");
}
final String columnName = AnnotationTools.getFieldName(field); final String columnName = AnnotationTools.getFieldName(field);
final String linkTableName = generateLinkTableName(tableName, columnName); final String linkTableName = generateLinkTableName(tableName, columnName);
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; if (localKey instanceof final Long localKeyLong) {
if (objectClass != Long.class) { if (objectClass == Long.class) {
LOGGER.error("Can not ManyToMany with other than List<Long> Model: List<{}>", objectClass.getCanonicalName()); // ========================================================
return; // == Link a "Long" primary Key with List<Long>
} // ========================================================
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final List<Long> dataCasted = (List<Long>) data; final List<Long> dataCasted = (List<Long>) data;
if (dataCasted.size() == 0) { if (dataCasted.size() == 0) {
return; return;
} }
final List<LinkTable> insertElements = new ArrayList<>(); final List<LinkTableLongLong> insertElements = new ArrayList<>();
for (final Long remoteKey : dataCasted) { for (final Long remoteKey : dataCasted) {
if (remoteKey == null) { if (remoteKey == null) {
throw new DataAccessException("Try to insert remote key with null value"); throw new DataAccessException("Try to insert remote key with null value");
} }
if (localKey instanceof final Long localKeyLong) { insertElements.add(new LinkTableLongLong(localKeyLong, remoteKey));
insertElements.add(new LinkTable(localKeyLong, remoteKey));
} else {
throw new DataAccessException("Not manage access of remte key like ManyToMany other than Long: " + localKey.getClass().getCanonicalName());
} }
if (insertElements.size() == 0) {
LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted);
return;
}
actions.add(() -> {
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
});
} else {
// ========================================================
// == Link a "Long" primary Key with List<UUID>
// ========================================================
@SuppressWarnings("unchecked")
final List<UUID> dataCasted = (List<UUID>) data;
if (dataCasted.size() == 0) {
return;
}
final List<LinkTableLongUUID> 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));
} }
if (insertElements.size() == 0) { if (insertElements.size() == 0) {
LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted); LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted);
@ -250,25 +365,88 @@ public class AddOnManyToMany implements DataAccessAddOn {
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName)); DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
}); });
} }
} else if (localKey instanceof final UUID localKeyUUID) {
if (objectClass == Long.class) {
// ========================================================
// == Link a "UUID" primary Key with List<Long>
// ========================================================
@SuppressWarnings("unchecked")
final List<Long> dataCasted = (List<Long>) data;
if (dataCasted.size() == 0) {
return;
}
final List<LinkTableUUIDLong> 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));
}
if (insertElements.size() == 0) {
LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted);
return;
}
actions.add(() -> {
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
});
} else {
// ========================================================
// == Link a "UUID" primary Key with List<UUID>
// ========================================================
@SuppressWarnings("unchecked")
final List<UUID> dataCasted = (List<UUID>) data;
if (dataCasted.size() == 0) {
return;
}
final List<LinkTableUUIDUUID> 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));
}
if (insertElements.size() == 0) {
LOGGER.warn("Insert multiple link without any value (may have null in the list): {}", dataCasted);
return;
}
actions.add(() -> {
DataAccess.insertMultiple(insertElements, new OverrideTableName(linkTableName));
});
}
} else {
throw new DataAccessException("Not manage access of remte key like ManyToMany other than Long or UUID: " + localKey.getClass().getCanonicalName());
}
}
@Override @Override
public void drop(final String tableName, final Field field) throws Exception { public void drop(final String tableName, final Field field) throws Exception {
final String columnName = AnnotationTools.getFieldName(field); final String columnName = AnnotationTools.getFieldName(field);
final String linkTableName = generateLinkTableName(tableName, columnName); final String linkTableName = generateLinkTableName(tableName, columnName);
DataAccess.drop(LinkTable.class, new OverrideTableName(linkTableName)); 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<Long> or List<UUID> Model: List<" + objectClass.getCanonicalName() + ">");
}
DataAccess.drop(LinkTableLongLong.class, new OverrideTableName(linkTableName));
} }
@Override @Override
public void cleanAll(final String tableName, final Field field) throws Exception { public void cleanAll(final String tableName, final Field field) throws Exception {
final String columnName = AnnotationTools.getFieldName(field); final String columnName = AnnotationTools.getFieldName(field);
final String linkTableName = generateLinkTableName(tableName, columnName); final String linkTableName = generateLinkTableName(tableName, columnName);
DataAccess.cleanAll(LinkTable.class, new OverrideTableName(linkTableName)); 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<Long> or List<UUID> Model: List<" + objectClass.getCanonicalName() + ">");
}
DataAccess.cleanAll(LinkTableLongLong.class, new OverrideTableName(linkTableName));
} }
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 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<Long> or List<UUID> Model: List<" + objectClass.getCanonicalName() + ">"); } */
final LinkTableLongLong insertElement = new LinkTableLongLong(localKey, remoteKey);
DataAccess.insert(insertElement, new OverrideTableName(linkTableName)); DataAccess.insert(insertElement, new OverrideTableName(linkTableName));
} }
@ -276,16 +454,48 @@ public class AddOnManyToMany implements DataAccessAddOn {
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);
return DataAccess.deleteWhere(LinkTable.class, new OverrideTableName(linkTableName), return DataAccess.deleteWhere(LinkTableLongLong.class, new OverrideTableName(linkTableName),
new Condition(new QueryAnd(new QueryCondition("object1Id", "=", localKey), new QueryCondition("object2Id", "=", remoteKey)))); new Condition(new QueryAnd(new QueryCondition("object1Id", "=", localKey), new QueryCondition("object2Id", "=", remoteKey))));
} }
@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 boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception { final String tableName, //
final Field primaryField, //
final Field field, //
final StringBuilder mainTableBuilder, //
final List<String> preActionList, //
final List<String> postActionList, //
final boolean createIfNotExist, //
final boolean createDrop, //
final int fieldId//
) throws Exception {
final String linkTableName = generateLinkTableNameField(tableName, field); final String linkTableName = generateLinkTableNameField(tableName, field);
final QueryOptions options = new QueryOptions(new OverrideTableName(linkTableName)); final QueryOptions options = new QueryOptions(new OverrideTableName(linkTableName));
final List<String> sqlCommand = DataFactory.createTable(LinkTable.class, options); 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<Long> or List<UUID> Model: List<" + objectClass.getCanonicalName() + ">");
}
final Class<?> primaryType = primaryField.getType();
if (primaryType == Long.class) {
if (objectClass == Long.class) {
final List<String> sqlCommand = DataFactory.createTable(LinkTableLongLong.class, options);
postActionList.addAll(sqlCommand);
} else {
final List<String> sqlCommand = DataFactory.createTable(LinkTableLongUUID.class, options);
postActionList.addAll(sqlCommand); postActionList.addAll(sqlCommand);
} }
} else if (primaryType == UUID.class) {
if (objectClass == Long.class) {
final List<String> sqlCommand = DataFactory.createTable(LinkTableUUIDLong.class, options);
postActionList.addAll(sqlCommand);
} else {
final List<String> sqlCommand = DataFactory.createTable(LinkTableUUIDUUID.class, options);
postActionList.addAll(sqlCommand);
}
} else {
throw new DataAccessException("Can not ManyToMany with other than primary key type Long or UUID Model: " + primaryType.getCanonicalName());
}
}
} }

View File

@ -32,12 +32,11 @@ public class AddOnManyToOne implements DataAccessAddOn {
} }
@Override @Override
public String getSQLFieldType(final Field elem) throws Exception { public String getSQLFieldType(final Field field) throws Exception {
final String fieldName = AnnotationTools.getFieldName(elem); final String fieldName = AnnotationTools.getFieldName(field);
try { try {
return DataFactory.convertTypeInSQL(Long.class, fieldName); return DataFactory.convertTypeInSQL(field.getType(), fieldName);
} catch (final Exception e) { } catch (final Exception e) {
// TODO Auto-generated catch block
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
@ -45,8 +44,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
@Override @Override
public boolean isCompatibleField(final Field elem) { public boolean isCompatibleField(final Field elem) {
final ManyToOne decorators = elem.getDeclaredAnnotation(ManyToOne.class); return elem.getDeclaredAnnotation(ManyToOne.class) != null;
return decorators != null;
} }
@Override @Override
@ -78,6 +76,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
ps.setString(iii.value, dataTyped); ps.setString(iii.value, dataTyped);
} else if (field.getType() == UUID.class) { } else if (field.getType() == UUID.class) {
final UUID dataTyped = (UUID) data; final UUID dataTyped = (UUID) data;
LOGGER.info("Generate UUTD for DB: {}", dataTyped);
final byte[] dataByte = UuidUtils.asBytes(dataTyped); final byte[] dataByte = UuidUtils.asBytes(dataTyped);
ps.setBytes(iii.value, dataByte); ps.setBytes(iii.value, dataByte);
} else { } else {
@ -96,7 +95,11 @@ public class AddOnManyToOne implements DataAccessAddOn {
@Override @Override
public boolean canInsert(final Field field) { public boolean canInsert(final Field field) {
if (field.getType() == Long.class || field.getType() == Integer.class || field.getType() == Short.class || field.getType() == String.class || field.getType() == UUID.class) { if (field.getType() == Long.class //
|| field.getType() == Integer.class //
|| field.getType() == Short.class //
|| field.getType() == String.class //
|| field.getType() == UUID.class) {
return true; return true;
} }
final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class); final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class);
@ -113,7 +116,12 @@ public class AddOnManyToOne implements DataAccessAddOn {
@Override @Override
public boolean canRetrieve(final Field field) { public boolean canRetrieve(final Field field) {
if (field.getType() == Long.class || field.getType() == Integer.class || field.getType() == Short.class || field.getType() == String.class || field.getType() == UUID.class) { final Class<?> classType = field.getType();
if (classType == Long.class //
|| classType == Integer.class //
|| classType == Short.class //
|| classType == String.class //
|| classType == UUID.class) {
return true; return true;
} }
final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class); final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class);
@ -124,9 +132,20 @@ public class AddOnManyToOne implements DataAccessAddOn {
} }
@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 CountInOut elemCount, final QueryOptions options) throws Exception { @NotNull final String tableName, //
if (field.getType() == Long.class || field.getType() == Integer.class || field.getType() == Short.class || field.getType() == String.class || field.getType() == UUID.class) { @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 {
if (field.getType() == Long.class //
|| field.getType() == Integer.class //
|| field.getType() == Short.class //
|| field.getType() == String.class //
|| field.getType() == UUID.class) {
querrySelect.append(" "); querrySelect.append(" ");
querrySelect.append(tableName); querrySelect.append(tableName);
querrySelect.append("."); querrySelect.append(".");
@ -242,8 +261,27 @@ 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 boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception { final String tableName, //
final Field primaryField, //
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 Class<?> classType = field.getType();
if (classType == Long.class //
|| classType == Integer.class //
|| classType == Short.class //
|| classType == String.class //
|| classType == UUID.class) {
DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, classType);
} else {
LOGGER.error("Support only the Long remote field of ecternal primary keys...");
DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, Long.class); DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, Long.class);
} }
} }
}

View File

@ -125,8 +125,17 @@ public class AddOnOneToMany implements DataAccessAddOn {
// TODO : refacto this table to manage a generic table with dynamic name to be serializable with the default system // TODO : refacto this table to manage a generic table with dynamic name to be serializable 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 boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception { final String tableName, //
final Field primaryField, //
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 {
DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, Long.class); DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, Long.class);
} }
} }

View File

@ -105,8 +105,17 @@ public class AddOnSQLTableExternalForeinKeyAsList implements DataAccessAddOn {
} }
@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 boolean createIfNotExist, final boolean createDrop, final int fieldId) throws Exception { final String tableName, //
final Field primaryField, //
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 {
// TODO Auto-generated method stub // TODO Auto-generated method stub
DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, String.class); DataFactory.createTablesSpecificType(tableName, field, mainTableBuilder, preActionList, postActionList, createIfNotExist, createDrop, fieldId, String.class);

View File

@ -5,12 +5,12 @@ import org.kar.archidata.model.GenericData;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column; import jakarta.persistence.Column;
public class LinkTable extends GenericData { public class LinkTableLongLong extends GenericData {
public LinkTable() { public LinkTableLongLong() {
// nothing to do... // nothing to do...
} }
public LinkTable(final long object1Id, final long object2Id) { public LinkTableLongLong(final long object1Id, final long object2Id) {
this.object1Id = object1Id; this.object1Id = object1Id;
this.object2Id = object2Id; this.object2Id = object2Id;
} }

View File

@ -0,0 +1,27 @@
package org.kar.archidata.dataAccess.addOn.model;
import java.util.UUID;
import org.kar.archidata.model.GenericData;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
public class LinkTableLongUUID extends GenericData {
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;
}

View File

@ -0,0 +1,27 @@
package org.kar.archidata.dataAccess.addOn.model;
import java.util.UUID;
import org.kar.archidata.model.GenericData;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
public class LinkTableUUIDLong extends GenericData {
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;
}

View File

@ -0,0 +1,27 @@
package org.kar.archidata.dataAccess.addOn.model;
import java.util.UUID;
import org.kar.archidata.model.GenericData;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
public class LinkTableUUIDUUID extends GenericData {
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;
}

View File

@ -9,6 +9,7 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.kar.archidata.annotation.AnnotationTools; import org.kar.archidata.annotation.AnnotationTools;
@ -125,7 +126,6 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
} else if (type == Integer.class || type == int.class) { } else if (type == Integer.class || type == int.class) {
final Long maxValueRoot = AnnotationTools.getConstraintsMax(field); final Long maxValueRoot = AnnotationTools.getConstraintsMax(field);
if (maxValueRoot != null) { if (maxValueRoot != null) {
final int maxValue = maxValueRoot.intValue(); final int maxValue = maxValueRoot.intValue();
@ -167,6 +167,20 @@ public class CheckJPA<T> implements CheckFunctionInterface {
} }
}); });
} }
} else if (type == UUID.class) {
final ManyToOne annotationManyToOne = AnnotationTools.getManyToOne(field);
if (annotationManyToOne != null && annotationManyToOne.targetEntity() != null) {
add(fieldName, (final String baseName, final T data) -> {
final Object elem = field.get(data);
if (elem == null) {
return;
}
final long count = DataAccess.count(annotationManyToOne.targetEntity(), elem);
if (count == 0) {
throw new InputException(baseName + fieldName, "Foreign element does not exist in the DB:" + elem);
}
});
}
} else if (type == Boolean.class || type == boolean.class) { } else if (type == Boolean.class || type == boolean.class) {
} else if (type == Float.class || type == float.class) { } else if (type == Float.class || type == float.class) {