[FIX] dynamc modification type for DataJson

This commit is contained in:
Edouard DUPIN 2025-01-02 23:06:27 +01:00
parent 6b12d26a8b
commit ee831283d3
4 changed files with 157 additions and 7 deletions

View File

@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.List;
import org.kar.archidata.annotation.AnnotationTools;
import org.kar.archidata.annotation.AnnotationTools.FieldName;
import org.kar.archidata.dataAccess.options.Condition;
import org.kar.archidata.dataAccess.options.FilterValue;
import org.kar.archidata.dataAccess.options.Limit;
@ -97,11 +98,11 @@ public abstract class DBAccess implements Closeable {
if (idKey == null) {
throw new DataAccessException("Try to identify the ID type and object was null.");
}
final String fieldName = AnnotationTools.getFieldName(idField, options).inTable();
final FieldName fieldName = AnnotationTools.getFieldName(idField, options);
final List<OptionSpecifyType> specificTypes = options.get(OptionSpecifyType.class);
if (typeClass == Object.class) {
for (final OptionSpecifyType specify : specificTypes) {
if (specify.name.equals(fieldName)) {
if (specify.name.equals(fieldName.inStruct())) {
typeClass = specify.clazz;
LOGGER.trace("Detect overwrite of typing ... '{}' => '{}'", typeClass.getCanonicalName(),
specify.clazz.getCanonicalName());
@ -116,7 +117,7 @@ public abstract class DBAccess implements Closeable {
}
throw new DataAccessException("Request update with the wrong type ...");
}
return new QueryCondition(fieldName, "=", idKey);
return new QueryCondition(fieldName.inTable(), "=", idKey);
}
// TODO: manage insert batch...

View File

@ -10,6 +10,7 @@ import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.bson.types.ObjectId;
import org.kar.archidata.annotation.AnnotationTools;
import org.kar.archidata.annotation.AnnotationTools.FieldName;
import org.kar.archidata.annotation.DataJson;
@ -25,6 +26,7 @@ import org.kar.archidata.dataAccess.options.OptionRenameColumn;
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.ContextGenericTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -117,13 +119,24 @@ public class AddOnDataJson implements DataAccessAddOn {
final CountInOut count,
final QueryOptions options,
final List<LazyGetter> lazyCall) throws Exception {
final List<OptionSpecifyType> specificTypes = options.get(OptionSpecifyType.class);
final String jsonData = rs.getString(count.value);
count.inc();
if (!rs.wasNull()) {
final ObjectMapper objectMapper = ContextGenericTools.createObjectMapper();
if (field.getType() == List.class) {
final ParameterizedType listType = (ParameterizedType) field.getGenericType();
final Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];
Class<?> listClass = (Class<?>) listType.getActualTypeArguments()[0];
if (listClass == Object.class) {
for (final OptionSpecifyType specify : specificTypes) {
if (specify.name.equals(field.getName())) {
listClass = specify.clazz;
LOGGER.trace("Detect overwrite of typing var={} ... '{}' => '{}'", field.getName(),
listClass.getCanonicalName(), specify.clazz.getCanonicalName());
break;
}
}
}
if (listClass == Long.class) {
final Object dataParsed = objectMapper.readValue(jsonData, new TypeReference<List<Long>>() {});
field.set(data, dataParsed);
@ -159,12 +172,32 @@ public class AddOnDataJson implements DataAccessAddOn {
field.set(data, dataParsed);
return;
}
if (listClass == ObjectId.class) {
final Object dataParsed = objectMapper.readValue(jsonData, new TypeReference<List<ObjectId>>() {});
field.set(data, dataParsed);
return;
}
LOGGER.warn("Maybe fail to translate Model in datajson list: List<{}>", listClass.getCanonicalName());
}
final TypeFactory typeFactory = objectMapper.getTypeFactory();
final JavaType fieldType = typeFactory.constructType(field.getGenericType());
final Object dataParsed = objectMapper.readValue(jsonData, fieldType);
field.set(data, dataParsed);
Class<?> listClass = field.getType();
if (listClass == Object.class) {
for (final OptionSpecifyType specify : specificTypes) {
if (specify.name.equals(field.getName())) {
listClass = specify.clazz;
LOGGER.trace("Detect overwrite of typing var={} ... '{}' => '{}'", field.getName(),
listClass.getCanonicalName(), specify.clazz.getCanonicalName());
break;
}
}
final JavaType javaType = typeFactory.constructType(listClass);
final Object dataParsed = objectMapper.readValue(jsonData, javaType);
field.set(data, dataParsed);
} else {
final JavaType fieldType = typeFactory.constructType(field.getGenericType());
final Object dataParsed = objectMapper.readValue(jsonData, fieldType);
field.set(data, dataParsed);
}
}
}

View File

@ -4,6 +4,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.bson.types.ObjectId;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
@ -21,6 +22,7 @@ import org.slf4j.LoggerFactory;
import test.kar.archidata.ConfigureDb;
import test.kar.archidata.StepwiseExtension;
import test.kar.archidata.dataAccess.model.SerializeListAsJson;
import test.kar.archidata.dataAccess.model.SerializeListAsJsonObjectId;
@ExtendWith(StepwiseExtension.class)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@ -138,4 +140,101 @@ public class TestListJson {
Assertions.assertEquals(firstDataInserted3, retrieve.data.get(1));
}
@Order(101)
@Test
public void testOIDTableInsertAndRetrieve() throws Exception {
final List<String> sqlCommand = DataFactory.createTable(SerializeListAsJsonObjectId.class);
if (ConfigureDb.da instanceof final DBAccessSQL daSQL) {
for (final String elem : sqlCommand) {
LOGGER.debug("request: '{}'", elem);
daSQL.executeSimpleQuery(elem);
}
}
}
@Order(102)
@Test
public void testOIDIO() throws Exception {
final SerializeListAsJsonObjectId test = new SerializeListAsJsonObjectId();
test.data = new ArrayList<>();
test.data.add(new ObjectId());
test.data.add(new ObjectId());
test.data.add(new ObjectId());
test.data.add(new ObjectId());
test.data.add(new ObjectId());
final SerializeListAsJsonObjectId insertedData = ConfigureDb.da.insert(test);
Assertions.assertNotNull(insertedData);
Assertions.assertNotNull(insertedData._id);
Assertions.assertNotNull(insertedData.data);
Assertions.assertEquals(5, insertedData.data.size());
Assertions.assertEquals(test.data.get(0), insertedData.data.get(0));
Assertions.assertEquals(test.data.get(1), insertedData.data.get(1));
Assertions.assertEquals(test.data.get(2), insertedData.data.get(2));
Assertions.assertEquals(test.data.get(3), insertedData.data.get(3));
Assertions.assertEquals(test.data.get(4), insertedData.data.get(4));
// Try to retrieve all the data:
final SerializeListAsJsonObjectId retrieve = ConfigureDb.da.get(SerializeListAsJsonObjectId.class,
insertedData._id);
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve._id);
Assertions.assertNotNull(retrieve.data);
Assertions.assertEquals(5, retrieve.data.size());
Assertions.assertEquals(test.data.get(0), retrieve.data.get(0));
Assertions.assertEquals(test.data.get(1), retrieve.data.get(1));
Assertions.assertEquals(test.data.get(2), retrieve.data.get(2));
Assertions.assertEquals(test.data.get(3), retrieve.data.get(3));
Assertions.assertEquals(test.data.get(4), retrieve.data.get(4));
}
@Order(103)
@Test
public void testOIDToolInsert() throws Exception {
final SerializeListAsJsonObjectId test = new SerializeListAsJsonObjectId();
test.data = new ArrayList<>();
final SerializeListAsJsonObjectId insertedData = ConfigureDb.da.insert(test);
Assertions.assertNotNull(insertedData);
Assertions.assertNotNull(insertedData._id);
Assertions.assertNotNull(insertedData.data);
Assertions.assertEquals(0, insertedData.data.size());
final ObjectId firstDataInserted1 = new ObjectId();
final ObjectId firstDataInserted2 = new ObjectId();
final ObjectId firstDataInserted3 = new ObjectId();
AddOnDataJson.addLink(ConfigureDb.da, SerializeListAsJsonObjectId.class, "_id", insertedData._id, "data",
firstDataInserted1);
AddOnDataJson.addLink(ConfigureDb.da, SerializeListAsJsonObjectId.class, "_id", insertedData._id, "data",
firstDataInserted2);
AddOnDataJson.addLink(ConfigureDb.da, SerializeListAsJsonObjectId.class, "_id", insertedData._id, "data",
firstDataInserted3);
// Try to retrieve all the data:
SerializeListAsJsonObjectId retrieve = ConfigureDb.da.get(SerializeListAsJsonObjectId.class, insertedData._id);
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve._id);
Assertions.assertNotNull(retrieve.data);
Assertions.assertEquals(3, retrieve.data.size());
Assertions.assertEquals(firstDataInserted1, retrieve.data.get(0));
Assertions.assertEquals(firstDataInserted2, retrieve.data.get(1));
Assertions.assertEquals(firstDataInserted3, retrieve.data.get(2));
AddOnDataJson.removeLink(ConfigureDb.da, SerializeListAsJsonObjectId.class, "_id", insertedData._id, "data",
firstDataInserted2);
// Try to retrieve all the data:
retrieve = ConfigureDb.da.get(SerializeListAsJsonObjectId.class, insertedData._id);
Assertions.assertNotNull(retrieve);
Assertions.assertNotNull(retrieve._id);
Assertions.assertNotNull(retrieve.data);
Assertions.assertEquals(2, retrieve.data.size());
Assertions.assertEquals(firstDataInserted1, retrieve.data.get(0));
Assertions.assertEquals(firstDataInserted3, retrieve.data.get(1));
}
}

View File

@ -0,0 +1,17 @@
package test.kar.archidata.dataAccess.model;
import java.util.List;
import org.bson.types.ObjectId;
import org.kar.archidata.annotation.DataJson;
import org.kar.archidata.model.OIDGenericData;
import dev.morphia.annotations.Entity;
@Entity
public class SerializeListAsJsonObjectId extends OIDGenericData {
@DataJson
public List<ObjectId> data;
}