Compare commits
15 Commits
c7338c7877
...
a15be78e10
Author | SHA1 | Date | |
---|---|---|---|
a15be78e10 | |||
abff1ada45 | |||
1c769827cf | |||
e09de7cc7a | |||
a5b9b60294 | |||
288e1f8293 | |||
d9a5f1ece2 | |||
f77c6ce13e | |||
85754f20f8 | |||
969bf78576 | |||
f0cf1acf8a | |||
7208db5bdf | |||
5b88401d48 | |||
d53a0719b5 | |||
3e7a1a5473 |
6
pom.xml
6
pom.xml
@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>kangaroo-and-rabbit</groupId>
|
||||
<artifactId>archidata</artifactId>
|
||||
<version>0.25.4</version>
|
||||
<version>0.25.6</version>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>gitea</id>
|
||||
@ -192,7 +192,9 @@
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver-sync</artifactId>
|
||||
<version>5.4.0-alpha0</version>
|
||||
<!--<version>5.3.1</version>-->
|
||||
<!--Morphia 2.4.x does not support version upper than 4.x-->
|
||||
<version>4.11.5</version>
|
||||
</dependency>
|
||||
<!-- Bean Validation (JSR 303 / 380) -->
|
||||
<dependency>
|
||||
|
@ -18,6 +18,7 @@ import org.bson.Document;
|
||||
import org.bson.conversions.Bson;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.annotation.AnnotationTools.FieldName;
|
||||
import org.kar.archidata.annotation.CreationTimestamp;
|
||||
import org.kar.archidata.annotation.UpdateTimestamp;
|
||||
import org.kar.archidata.dataAccess.addOnMongo.AddOnManyToOne;
|
||||
@ -27,6 +28,7 @@ import org.kar.archidata.dataAccess.options.CheckFunction;
|
||||
import org.kar.archidata.dataAccess.options.Condition;
|
||||
import org.kar.archidata.dataAccess.options.FilterValue;
|
||||
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.db.DbIoMorphia;
|
||||
@ -128,7 +130,7 @@ public class DBAccessMorphia extends DBAccess {
|
||||
return groups;
|
||||
}
|
||||
|
||||
protected <T> void setValuedb(
|
||||
protected <T> void setValueToDb(
|
||||
final Class<?> type,
|
||||
final T data,
|
||||
final Field field,
|
||||
@ -136,7 +138,9 @@ public class DBAccessMorphia extends DBAccess {
|
||||
final Document docSet,
|
||||
final Document docUnSet) throws Exception {
|
||||
if (field.get(data) == null) {
|
||||
docUnSet.append(fieldName, "");
|
||||
if (docUnSet != null) {
|
||||
docUnSet.append(fieldName, "");
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (type == long.class) {
|
||||
@ -151,7 +155,7 @@ public class DBAccessMorphia extends DBAccess {
|
||||
docSet.append(fieldName, field.getFloat(data));
|
||||
return;
|
||||
}
|
||||
if (type == Double.class) {
|
||||
if (type == double.class) {
|
||||
docSet.append(fieldName, field.getDouble(data));
|
||||
return;
|
||||
}
|
||||
@ -196,56 +200,40 @@ public class DBAccessMorphia extends DBAccess {
|
||||
docSet.append(fieldName, tmp);
|
||||
return;
|
||||
}
|
||||
if (type == ObjectId.class) {
|
||||
docSet.append(fieldName, tmp);
|
||||
return;
|
||||
}
|
||||
if (type == UUID.class) {
|
||||
docSet.append(fieldName, tmp);
|
||||
return;
|
||||
}
|
||||
if (type == Date.class) {
|
||||
// TODO ...
|
||||
/*
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
final Timestamp sqlDate = java.sql.Timestamp.from(((Date) tmp).toInstant());
|
||||
ps.setTimestamp(iii.value, sqlDate);
|
||||
}*/
|
||||
*/
|
||||
}
|
||||
if (type == Instant.class) {
|
||||
/*
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
final String sqlDate = ((Instant) tmp).toString();
|
||||
ps.setString(iii.value, sqlDate);
|
||||
}
|
||||
*/
|
||||
}
|
||||
if (type == LocalDate.class) {
|
||||
/*
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
final java.sql.Date sqlDate = java.sql.Date.valueOf((LocalDate) tmp);
|
||||
ps.setDate(iii.value, sqlDate);
|
||||
}
|
||||
*/
|
||||
}
|
||||
if (type == LocalTime.class) {
|
||||
/*
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
final java.sql.Time sqlDate = java.sql.Time.valueOf((LocalTime) tmp);
|
||||
ps.setTime(iii.value, sqlDate);
|
||||
}
|
||||
*/
|
||||
}
|
||||
throw new DataAccessException("Unknown Field Type");
|
||||
|
||||
docSet.append(fieldName, tmp);
|
||||
//throw new DataAccessException("Unknown Field Type");
|
||||
}
|
||||
|
||||
public <T> void setValueFromDoc(
|
||||
@ -265,6 +253,11 @@ public class DBAccessMorphia extends DBAccess {
|
||||
field.set(data, value);
|
||||
return;
|
||||
}
|
||||
if (type == ObjectId.class) {
|
||||
final ObjectId value = doc.get(fieldName, ObjectId.class);
|
||||
field.set(data, value);
|
||||
return;
|
||||
}
|
||||
if (type == Long.class || type == long.class) {
|
||||
final Long value = doc.getLong(fieldName);
|
||||
field.set(data, value);
|
||||
@ -349,8 +342,8 @@ public class DBAccessMorphia extends DBAccess {
|
||||
final Object value = doc.get(fieldName, field.getType());
|
||||
field.set(data, value);
|
||||
} else {
|
||||
final Object value = createObjectFromDocument(doc.get(fieldName, Document.class), field.getType(), null,
|
||||
lazyCall);
|
||||
final Object value = createObjectFromDocument(doc.get(fieldName, Document.class), field.getType(),
|
||||
new QueryOptions(), lazyCall);
|
||||
field.set(data, value);
|
||||
}
|
||||
|
||||
@ -459,32 +452,45 @@ public class DBAccessMorphia extends DBAccess {
|
||||
Object uniqueId = null;
|
||||
// real add in the BDD:
|
||||
ObjectId insertedId = null;
|
||||
final List<OptionSpecifyType> specificTypes = options.get(OptionSpecifyType.class);
|
||||
try {
|
||||
final MongoCollection<Document> collection = this.db.getDatastore().getDatabase()
|
||||
.getCollection(collectionName);
|
||||
final Document doc = new Document();
|
||||
final Document docSet = new Document();
|
||||
final Document docUnSet = new Document();
|
||||
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;
|
||||
}
|
||||
final String tableFieldName = AnnotationTools.getFieldName(field, options).inTable();
|
||||
final FieldName tableFieldName = AnnotationTools.getFieldName(field, options);
|
||||
Object currentInsertValue = field.get(data);
|
||||
if (AnnotationTools.isPrimaryKey(field)) {
|
||||
primaryKeyField = field;
|
||||
if (primaryKeyField.getType() == UUID.class) {
|
||||
final UUID uuid = UuidUtils.nextUUID();
|
||||
uniqueId = uuid;
|
||||
doc.append(tableFieldName, uuid);
|
||||
docSet.append(tableFieldName.inTable(), uuid);
|
||||
continue;
|
||||
} else if (primaryKeyField.getType() == Long.class || primaryKeyField.getType() == long.class) {
|
||||
// By default the MongoDB does not manage the
|
||||
final long id = getNextSequenceLongValue(collectionName, tableFieldName);
|
||||
final long id = getNextSequenceLongValue(collectionName, tableFieldName.inTable());
|
||||
uniqueId = id;
|
||||
doc.append(tableFieldName, id);
|
||||
docSet.append(tableFieldName.inTable(), id);
|
||||
continue;
|
||||
}
|
||||
LOGGER.error("TODO: Manage the ID primary key for type: ");
|
||||
LOGGER.error("TODO: Manage the ID primary key for type: {}=>{}", clazz.getCanonicalName(),
|
||||
primaryKeyField.getType());
|
||||
continue;
|
||||
}
|
||||
final boolean createTime = field.getDeclaredAnnotationsByType(CreationTimestamp.class).length != 0;
|
||||
if (createTime) {
|
||||
docSet.append(tableFieldName.inTable(), Date.from(Instant.now()));
|
||||
continue;
|
||||
}
|
||||
final boolean updateTime = field.getDeclaredAnnotationsByType(UpdateTimestamp.class).length != 0;
|
||||
if (updateTime) {
|
||||
docSet.append(tableFieldName.inTable(), Date.from(Instant.now()));
|
||||
continue;
|
||||
}
|
||||
final DataAccessAddOn addOn = findAddOnforField(field);
|
||||
@ -495,19 +501,8 @@ public class DBAccessMorphia extends DBAccess {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
final boolean createTime = field.getDeclaredAnnotationsByType(CreationTimestamp.class).length != 0;
|
||||
if (createTime) {
|
||||
doc.append(tableFieldName, Date.from(Instant.now()));
|
||||
continue;
|
||||
}
|
||||
final boolean updateTime = field.getDeclaredAnnotationsByType(UpdateTimestamp.class).length != 0;
|
||||
if (updateTime) {
|
||||
doc.append(tableFieldName, Date.from(Instant.now()));
|
||||
continue;
|
||||
}
|
||||
if (currentInsertValue == null && !field.getClass().isPrimitive()) {
|
||||
final DefaultValue[] defaultValue = field.getDeclaredAnnotationsByType(DefaultValue.class);
|
||||
LOGGER.error("TODO: convert default value in the correct value for the DB...");
|
||||
if (defaultValue.length == 0) {
|
||||
continue;
|
||||
} else {
|
||||
@ -518,9 +513,24 @@ public class DBAccessMorphia extends DBAccess {
|
||||
currentInsertValue = convertDefaultField(value, field);
|
||||
}
|
||||
}
|
||||
doc.append(tableFieldName, currentInsertValue);
|
||||
// conversion table ...
|
||||
//doc.append(tableFieldName, currentInsertValue);
|
||||
if (addOn != null) {
|
||||
addOn.insertData(this, field, data, options, docSet, docUnSet);
|
||||
} else {
|
||||
final Class<?> type = field.getType();
|
||||
if (!type.isPrimitive()) {
|
||||
if (field.get(data) == null) {
|
||||
if (currentInsertValue != null) {
|
||||
docSet.append(tableFieldName.inTable(), currentInsertValue);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
}
|
||||
setValueToDb(type, data, field, tableFieldName.inTable(), docSet, null);
|
||||
}
|
||||
}
|
||||
final InsertOneResult result = collection.insertOne(doc);
|
||||
final InsertOneResult result = collection.insertOne(docSet);
|
||||
// Get the Object of inserted object:
|
||||
insertedId = result.getInsertedId().asObjectId().getValue();
|
||||
LOGGER.info("Document inserted with ID: " + insertedId);
|
||||
@ -587,14 +597,14 @@ public class DBAccessMorphia extends DBAccess {
|
||||
if (java.lang.reflect.Modifier.isStatic(field.getModifiers())) {
|
||||
continue;
|
||||
}
|
||||
final String fieldName = AnnotationTools.getFieldName(field, options).inTable();
|
||||
final FieldName fieldName = AnnotationTools.getFieldName(field, options);
|
||||
// update field is not conditioned by filter:
|
||||
final boolean updateTime = field.getDeclaredAnnotationsByType(UpdateTimestamp.class).length != 0;
|
||||
if (updateTime) {
|
||||
docSet.append(fieldName, Date.from(Instant.now()));
|
||||
docSet.append(fieldName.inTable(), Date.from(Instant.now()));
|
||||
continue;
|
||||
}
|
||||
if (!filterKey.getValues().contains(fieldName)) {
|
||||
if (!filterKey.getValues().contains(fieldName.inStruct())) {
|
||||
continue;
|
||||
} else if (AnnotationTools.isGenericField(field)) {
|
||||
continue;
|
||||
@ -624,12 +634,10 @@ public class DBAccessMorphia extends DBAccess {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
setValuedb(type, data, field, fieldName, docSet, docUnSet);
|
||||
setValueToDb(type, data, field, fieldName.inTable(), docSet, docUnSet);
|
||||
}
|
||||
|
||||
}
|
||||
// Do the query ...
|
||||
|
||||
final MongoCollection<Document> collection = this.db.getDatastore().getDatabase()
|
||||
.getCollection(collectionName);
|
||||
final Document actions = new Document();
|
||||
@ -652,7 +660,6 @@ public class DBAccessMorphia extends DBAccess {
|
||||
}
|
||||
|
||||
public List<String> generateSelectField(final Class<?> clazz, final QueryOptions options) throws Exception {
|
||||
// TODO: list of user select fields.
|
||||
final boolean readAllfields = QueryOptions.readAllColomn(options);
|
||||
final List<String> fieldsName = new ArrayList<>();
|
||||
|
||||
@ -776,6 +783,8 @@ public class DBAccessMorphia extends DBAccess {
|
||||
final Class<?> clazz,
|
||||
final QueryOptions options,
|
||||
final List<LazyGetter> lazyCall) throws Exception {
|
||||
final List<OptionSpecifyType> specificTypes = options.get(OptionSpecifyType.class);
|
||||
LOGGER.info("createObjectFromDocument: {}", clazz.getCanonicalName());
|
||||
final boolean readAllfields = QueryOptions.readAllColomn(options);
|
||||
// TODO: manage class that is defined inside a class ==> Not manage for now...
|
||||
Object data = null;
|
||||
@ -790,23 +799,37 @@ public class DBAccessMorphia extends DBAccess {
|
||||
"Can not find the default constructor for the class: " + clazz.getCanonicalName());
|
||||
}
|
||||
for (final Field elem : clazz.getFields()) {
|
||||
LOGGER.info(" Inspect field: name='{}' type='{}'", elem.getName(), elem.getType().getCanonicalName());
|
||||
// static field is only for internal global declaration ==> remove it ..
|
||||
if (java.lang.reflect.Modifier.isStatic(elem.getModifiers())) {
|
||||
LOGGER.info(" ==> static");
|
||||
continue;
|
||||
}
|
||||
final DataAccessAddOn addOn = findAddOnforField(elem);
|
||||
if (addOn != null && !addOn.canRetrieve(elem)) {
|
||||
LOGGER.info(" ==> Can not retreive this field");
|
||||
continue;
|
||||
}
|
||||
final boolean notRead = AnnotationTools.isDefaultNotRead(elem);
|
||||
if (!readAllfields && notRead) {
|
||||
LOGGER.info(" ==> Not read this element");
|
||||
continue;
|
||||
}
|
||||
if (addOn != null) {
|
||||
LOGGER.error("TODO: Add on not managed .6. ");
|
||||
addOn.fillFromDoc(this, doc, elem, data, options, lazyCall);
|
||||
} else {
|
||||
setValueFromDoc(elem.getType(), data, elem, doc, lazyCall, options);
|
||||
Class<?> type = elem.getType();
|
||||
if (type == Object.class) {
|
||||
for (final OptionSpecifyType specify : specificTypes) {
|
||||
if (specify.name.equals(elem.getName())) {
|
||||
type = specify.clazz;
|
||||
LOGGER.info("Detect overwrite of typing var={} ... '{}' => '{}'", elem.getName(),
|
||||
elem.getType().getCanonicalName(), specify.clazz.getCanonicalName());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
setValueFromDoc(type, data, elem, doc, lazyCall, options);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
|
@ -278,8 +278,81 @@ public class DBAccessSQL extends DBAccess {
|
||||
final CountInOut iii,
|
||||
final Field field,
|
||||
final PreparedStatement ps) throws Exception {
|
||||
if (type == ObjectId.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (type == long.class) {
|
||||
ps.setLong(iii.value, field.getLong(data));
|
||||
iii.inc();
|
||||
return;
|
||||
}
|
||||
if (type == int.class) {
|
||||
ps.setInt(iii.value, field.getInt(data));
|
||||
iii.inc();
|
||||
return;
|
||||
}
|
||||
if (type == float.class) {
|
||||
ps.setFloat(iii.value, field.getFloat(data));
|
||||
iii.inc();
|
||||
return;
|
||||
}
|
||||
if (type == double.class) {
|
||||
ps.setDouble(iii.value, field.getDouble(data));
|
||||
iii.inc();
|
||||
return;
|
||||
}
|
||||
if (type == boolean.class) {
|
||||
ps.setBoolean(iii.value, field.getBoolean(data));
|
||||
iii.inc();
|
||||
return;
|
||||
}
|
||||
final Object tmp = field.get(data);
|
||||
if (type.isEnum()) {
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.VARCHAR);
|
||||
} else {
|
||||
ps.setString(iii.value, tmp.toString());
|
||||
}
|
||||
} else if (type == Long.class) {
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.BIGINT);
|
||||
} else {
|
||||
ps.setLong(iii.value, (Long) tmp);
|
||||
}
|
||||
} else if (type == Integer.class) {
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
ps.setInt(iii.value, (Integer) tmp);
|
||||
}
|
||||
} else if (type == Float.class) {
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.FLOAT);
|
||||
} else {
|
||||
ps.setFloat(iii.value, (Float) tmp);
|
||||
}
|
||||
} else if (type == Double.class) {
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.DOUBLE);
|
||||
} else {
|
||||
ps.setDouble(iii.value, (Double) tmp);
|
||||
}
|
||||
} else if (type == Boolean.class) {
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
ps.setBoolean(iii.value, (Boolean) tmp);
|
||||
}
|
||||
} else if (type == String.class) {
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.VARCHAR);
|
||||
} else {
|
||||
ps.setString(iii.value, (String) tmp);
|
||||
}
|
||||
} else if (type == Timestamp.class) {
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
ps.setTimestamp(iii.value, (Timestamp) tmp);
|
||||
}
|
||||
} else if (type == ObjectId.class) {
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.BINARY);
|
||||
} else {
|
||||
@ -287,67 +360,13 @@ public class DBAccessSQL extends DBAccess {
|
||||
ps.setBytes(iii.value, dataByte);
|
||||
}
|
||||
} else if (type == UUID.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.BINARY);
|
||||
} else {
|
||||
final byte[] dataByte = UuidUtils.asBytes((UUID) tmp);
|
||||
ps.setBytes(iii.value, dataByte);
|
||||
}
|
||||
} else if (type == Long.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.BIGINT);
|
||||
} else {
|
||||
ps.setLong(iii.value, (Long) tmp);
|
||||
}
|
||||
} else if (type == long.class) {
|
||||
ps.setLong(iii.value, field.getLong(data));
|
||||
} else if (type == Integer.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
ps.setInt(iii.value, (Integer) tmp);
|
||||
}
|
||||
} else if (type == int.class) {
|
||||
ps.setInt(iii.value, field.getInt(data));
|
||||
} else if (type == Float.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.FLOAT);
|
||||
} else {
|
||||
ps.setFloat(iii.value, (Float) tmp);
|
||||
}
|
||||
} else if (type == float.class) {
|
||||
ps.setFloat(iii.value, field.getFloat(data));
|
||||
} else if (type == Double.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.DOUBLE);
|
||||
} else {
|
||||
ps.setDouble(iii.value, (Double) tmp);
|
||||
}
|
||||
} else if (type == Double.class) {
|
||||
ps.setDouble(iii.value, field.getDouble(data));
|
||||
} else if (type == Boolean.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
ps.setBoolean(iii.value, (Boolean) tmp);
|
||||
}
|
||||
} else if (type == boolean.class) {
|
||||
ps.setBoolean(iii.value, field.getBoolean(data));
|
||||
} else if (type == Timestamp.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
ps.setTimestamp(iii.value, (Timestamp) tmp);
|
||||
}
|
||||
} else if (type == Date.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
@ -355,7 +374,6 @@ public class DBAccessSQL extends DBAccess {
|
||||
ps.setTimestamp(iii.value, sqlDate);
|
||||
}
|
||||
} else if (type == Instant.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
@ -363,7 +381,6 @@ public class DBAccessSQL extends DBAccess {
|
||||
ps.setString(iii.value, sqlDate);
|
||||
}
|
||||
} else if (type == LocalDate.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
@ -371,27 +388,12 @@ public class DBAccessSQL extends DBAccess {
|
||||
ps.setDate(iii.value, sqlDate);
|
||||
}
|
||||
} else if (type == LocalTime.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.INTEGER);
|
||||
} else {
|
||||
final java.sql.Time sqlDate = java.sql.Time.valueOf((LocalTime) tmp);
|
||||
ps.setTime(iii.value, sqlDate);
|
||||
}
|
||||
} else if (type == String.class) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.VARCHAR);
|
||||
} else {
|
||||
ps.setString(iii.value, (String) tmp);
|
||||
}
|
||||
} else if (type.isEnum()) {
|
||||
final Object tmp = field.get(data);
|
||||
if (tmp == null) {
|
||||
ps.setNull(iii.value, Types.VARCHAR);
|
||||
} else {
|
||||
ps.setString(iii.value, tmp.toString());
|
||||
}
|
||||
} else {
|
||||
throw new DataAccessException("Unknown Field Type: " + type.getCanonicalName());
|
||||
}
|
||||
|
@ -1,174 +0,0 @@
|
||||
package org.kar.archidata.dataAccess.addOnMongo;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.annotation.DataJson;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DBAccessMorphia;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.LazyGetter;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.kar.archidata.exception.DataAccessException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonValue;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class AddOnDataJson implements DataAccessAddOn {
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(AddOnDataJson.class);
|
||||
|
||||
@Override
|
||||
public Class<?> getAnnotationClass() {
|
||||
return DataJson.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSQLFieldType(final Field elem, final QueryOptions options) throws DataAccessException {
|
||||
final String fieldName = AnnotationTools.getFieldName(elem, options).inTable();
|
||||
return DataFactory.convertTypeInSQL(String.class, fieldName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCompatibleField(final Field elem) {
|
||||
final DataJson decorators = elem.getDeclaredAnnotation(DataJson.class);
|
||||
return decorators != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertData(
|
||||
final DBAccessMorphia ioDb,
|
||||
final Field field,
|
||||
final Object rootObject,
|
||||
final QueryOptions options,
|
||||
final Document docSet,
|
||||
final Document docUnSet) throws Exception {
|
||||
/*
|
||||
final Object data = field.get(rootObject);
|
||||
if (data == null) {
|
||||
ps.setNull(iii.value, Types.VARCHAR);
|
||||
}
|
||||
final ObjectMapper objectMapper = ContextGenericTools.createObjectMapper();
|
||||
final String dataString = objectMapper.writeValueAsString(data);
|
||||
ps.setString(iii.value, dataString);
|
||||
iii.inc();
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canInsert(final Field field) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isInsertAsync(final Field field) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRetrieve(final Field field) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateQuery(
|
||||
@NotNull final String tableName,
|
||||
@NotNull final String primaryKey,
|
||||
@NotNull final Field field,
|
||||
@NotNull final StringBuilder querySelect,
|
||||
@NotNull final StringBuilder query,
|
||||
@NotNull final String name,
|
||||
@NotNull final CountInOut count,
|
||||
final QueryOptions options) throws Exception {
|
||||
querySelect.append(" ");
|
||||
querySelect.append(tableName);
|
||||
querySelect.append(".");
|
||||
querySelect.append(name);
|
||||
count.inc();
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillFromDoc(
|
||||
final DBAccessMorphia ioDb,
|
||||
final Document doc,
|
||||
final Field field,
|
||||
final Object data,
|
||||
final QueryOptions options,
|
||||
final List<LazyGetter> lazyCall) throws Exception {
|
||||
/*
|
||||
final String fieldName = AnnotationTools.getFieldName(field);
|
||||
if (!doc.containsKey(fieldName)) {
|
||||
field.set(data, null);
|
||||
return;
|
||||
}
|
||||
final String jsonData = rs.getString(count.value);
|
||||
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];
|
||||
if (listClass == Long.class) {
|
||||
final Object dataParsed = objectMapper.readValue(jsonData, new TypeReference<List<Long>>() {});// field.getType());
|
||||
field.set(data, dataParsed);
|
||||
return;
|
||||
}
|
||||
if (listClass == Float.class) {
|
||||
final Object dataParsed = objectMapper.readValue(jsonData, new TypeReference<List<Float>>() {});// field.getType());
|
||||
field.set(data, dataParsed);
|
||||
return;
|
||||
}
|
||||
if (listClass == Double.class) {
|
||||
final Object dataParsed = objectMapper.readValue(jsonData, new TypeReference<List<Double>>() {});// field.getType());
|
||||
field.set(data, dataParsed);
|
||||
return;
|
||||
}
|
||||
if (listClass == Integer.class) {
|
||||
final Object dataParsed = objectMapper.readValue(jsonData, new TypeReference<List<Integer>>() {});// field.getType());
|
||||
field.set(data, dataParsed);
|
||||
return;
|
||||
}
|
||||
if (listClass == Short.class) {
|
||||
final Object dataParsed = objectMapper.readValue(jsonData, new TypeReference<List<Short>>() {});// field.getType());
|
||||
field.set(data, dataParsed);
|
||||
return;
|
||||
}
|
||||
if (listClass == String.class) {
|
||||
final Object dataParsed = objectMapper.readValue(jsonData, new TypeReference<List<String>>() {});// field.getType());
|
||||
field.set(data, dataParsed);
|
||||
return;
|
||||
}
|
||||
if (listClass == UUID.class) {
|
||||
final Object dataParsed = objectMapper.readValue(jsonData, new TypeReference<List<UUID>>() {});// field.getType());
|
||||
field.set(data, dataParsed);
|
||||
return;
|
||||
}
|
||||
LOGGER.warn("Maybe fail to translate Model in datajson list: List<{}>", listClass.getCanonicalName());
|
||||
}
|
||||
final Object dataParsed = objectMapper.readValue(jsonData, field.getType());
|
||||
field.set(data, dataParsed);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public void createTables(
|
||||
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,
|
||||
final QueryOptions options) throws Exception {
|
||||
DataFactory.createTablesSpecificType(tableName, primaryField, field, mainTableBuilder, preActionList,
|
||||
postActionList, createIfNotExist, createDrop, fieldId, JsonValue.class, options);
|
||||
}
|
||||
|
||||
}
|
@ -173,34 +173,6 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
count.inc();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateQuery(
|
||||
@NotNull final String tableName,
|
||||
@NotNull final String primaryKey,
|
||||
@NotNull final Field field,
|
||||
@NotNull final StringBuilder querySelect,
|
||||
@NotNull final StringBuilder query,
|
||||
@NotNull final String name,
|
||||
@NotNull final CountInOut count,
|
||||
final QueryOptions options) throws Exception {
|
||||
if (field.getType() != List.class) {
|
||||
return;
|
||||
}
|
||||
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType())
|
||||
.getActualTypeArguments()[0];
|
||||
if (objectClass == Long.class || objectClass == UUID.class) {
|
||||
generateConcatQuery(tableName, primaryKey, field, querySelect, query, name, count, options);
|
||||
}
|
||||
final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
|
||||
if (decorators == null) {
|
||||
return;
|
||||
}
|
||||
if (objectClass == decorators.targetEntity()) {
|
||||
generateConcatQuery(tableName, primaryKey, field, querySelect, query, name, count, options);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillFromDoc(
|
||||
final DBAccessMorphia ioDb,
|
||||
|
@ -5,9 +5,9 @@ import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.annotation.AnnotationTools.FieldName;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DBAccessMorphia;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.LazyGetter;
|
||||
@ -16,7 +16,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class AddOnManyToOne implements DataAccessAddOn {
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(AddOnManyToMany.class);
|
||||
@ -52,7 +51,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
final Document docUnSet) throws Exception {
|
||||
final FieldName fieldName = AnnotationTools.getFieldName(field, options);
|
||||
final Object data = field.get(rootObject);
|
||||
if (field.get(data) == null) {
|
||||
if (data == null) {
|
||||
docUnSet.append(fieldName.inTable(), "");
|
||||
return;
|
||||
} else if (field.getType() == Long.class) {
|
||||
@ -70,6 +69,9 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
} else if (field.getType() == UUID.class) {
|
||||
final UUID dataTyped = (UUID) data;
|
||||
docSet.append(fieldName.inTable(), dataTyped);
|
||||
} else if (field.getType() == ObjectId.class) {
|
||||
final ObjectId dataTyped = (ObjectId) data;
|
||||
docSet.append(fieldName.inTable(), dataTyped);
|
||||
} else {
|
||||
final Field idField = AnnotationTools.getFieldOfId(field.getType());
|
||||
final Object uid = idField.get(data);
|
||||
@ -84,7 +86,8 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
@Override
|
||||
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) {
|
||||
|| field.getType() == String.class || field.getType() == UUID.class
|
||||
|| field.getType() == ObjectId.class) {
|
||||
return true;
|
||||
}
|
||||
final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class);
|
||||
@ -103,7 +106,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
public boolean canRetrieve(final Field field) {
|
||||
final Class<?> classType = field.getType();
|
||||
if (classType == Long.class || classType == Integer.class || classType == Short.class
|
||||
|| classType == String.class || classType == UUID.class) {
|
||||
|| classType == String.class || classType == UUID.class || classType == ObjectId.class) {
|
||||
return true;
|
||||
}
|
||||
final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class);
|
||||
@ -113,36 +116,6 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateQuery(
|
||||
@NotNull final String tableName,
|
||||
@NotNull final String primaryKey,
|
||||
@NotNull final Field field,
|
||||
@NotNull final StringBuilder querySelect,
|
||||
@NotNull final StringBuilder query,
|
||||
@NotNull final String name,
|
||||
@NotNull final CountInOut count,
|
||||
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) {
|
||||
querySelect.append(" ");
|
||||
querySelect.append(tableName);
|
||||
querySelect.append(".");
|
||||
querySelect.append(name);
|
||||
count.inc();
|
||||
return;
|
||||
}
|
||||
final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class);
|
||||
if (field.getType() == decorators.targetEntity()) {
|
||||
// no eager possible for no sql
|
||||
querySelect.append(" ");
|
||||
querySelect.append(tableName);
|
||||
querySelect.append(".");
|
||||
querySelect.append(name);
|
||||
count.inc();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillFromDoc(
|
||||
final DBAccessMorphia ioDb,
|
||||
@ -159,7 +132,8 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
}
|
||||
// local field to manage no remote object to retrieve.
|
||||
if (field.getType() == Long.class || field.getType() == Integer.class || field.getType() == Short.class
|
||||
|| field.getType() == String.class || field.getType() == UUID.class) {
|
||||
|| field.getType() == String.class || field.getType() == UUID.class
|
||||
|| field.getType() == ObjectId.class) {
|
||||
ioDb.setValueFromDoc(field.getType(), data, field, doc, lazyCall, options);
|
||||
return;
|
||||
}
|
||||
@ -174,9 +148,8 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
final Class<?> remotePrimaryKeyType = remotePrimaryKeyField.getType();
|
||||
if (remotePrimaryKeyType == Long.class) {
|
||||
// here we have the field, the data and the the remote value ==> can create callback that generate the update of the value ...
|
||||
final Long foreignKey = doc.getLong(fieldName);
|
||||
final Long foreignKey = doc.getLong(fieldName.inTable());
|
||||
if (foreignKey != null) {
|
||||
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
|
||||
final LazyGetter lambda = () -> {
|
||||
// TODO: update to have get with abstract types ....
|
||||
final Object foreignData = ioDb.get(decorators.targetEntity(), foreignKey);
|
||||
@ -189,9 +162,22 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
}
|
||||
} else if (remotePrimaryKeyType == UUID.class) {
|
||||
// here we have the field, the data and the the remote value ==> can create callback that generate the update of the value ...
|
||||
final UUID foreignKey = doc.get(fieldName, UUID.class);
|
||||
final UUID foreignKey = doc.get(fieldName.inTable(), UUID.class);
|
||||
if (foreignKey != null) {
|
||||
final LazyGetter lambda = () -> {
|
||||
// TODO: update to have get with abstract types ....
|
||||
final Object foreignData = ioDb.get(decorators.targetEntity(), foreignKey);
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
}
|
||||
field.set(data, foreignData);
|
||||
};
|
||||
lazyCall.add(lambda);
|
||||
}
|
||||
} else if (remotePrimaryKeyType == ObjectId.class) {
|
||||
// here we have the field, the data and the the remote value ==> can create callback that generate the update of the value ...
|
||||
final ObjectId foreignKey = doc.get(fieldName.inTable(), ObjectId.class);
|
||||
if (foreignKey != null) {
|
||||
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
|
||||
final LazyGetter lambda = () -> {
|
||||
// TODO: update to have get with abstract types ....
|
||||
final Object foreignData = ioDb.get(decorators.targetEntity(), foreignKey);
|
||||
@ -221,7 +207,7 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
final QueryOptions options) throws Exception {
|
||||
final Class<?> classType = field.getType();
|
||||
if (classType == Long.class || classType == Integer.class || classType == Short.class
|
||||
|| classType == String.class || classType == UUID.class) {
|
||||
|| classType == String.class || classType == UUID.class || classType == ObjectId.class) {
|
||||
DataFactory.createTablesSpecificType(tableName, primaryField, field, mainTableBuilder, preActionList,
|
||||
postActionList, createIfNotExist, createDrop, fieldId, classType, options);
|
||||
} else {
|
||||
|
@ -2,62 +2,28 @@ package org.kar.archidata.dataAccess.addOnMongo;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.annotation.AnnotationTools.FieldName;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DBAccessMorphia;
|
||||
import org.kar.archidata.dataAccess.LazyGetter;
|
||||
import org.kar.archidata.dataAccess.QueryCondition;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.kar.archidata.dataAccess.options.Condition;
|
||||
import org.kar.archidata.exception.DataAccessException;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class AddOnOneToMany implements DataAccessAddOn {
|
||||
static final Logger LOGGER = LoggerFactory.getLogger(AddOnOneToMany.class);
|
||||
static final String SEPARATOR_LONG = "-";
|
||||
|
||||
/** Convert the list if external id in a string '-' separated
|
||||
* @param ids List of value (null are removed)
|
||||
* @return '-' string separated */
|
||||
protected static String getStringOfIds(final List<Long> ids) {
|
||||
final List<Long> tmp = new ArrayList<>(ids);
|
||||
return tmp.stream().map(String::valueOf).collect(Collectors.joining("-"));
|
||||
}
|
||||
|
||||
/** extract a list of "-" 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. */
|
||||
protected static List<Long> getListOfIds(final ResultSet rs, final int iii) throws SQLException {
|
||||
final String trackString = rs.getString(iii);
|
||||
if (rs.wasNull()) {
|
||||
return null;
|
||||
}
|
||||
final List<Long> out = new ArrayList<>();
|
||||
final String[] elements = trackString.split("-");
|
||||
for (final String elem : elements) {
|
||||
final Long tmp = Long.parseLong(elem);
|
||||
out.add(tmp);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?> getAnnotationClass() {
|
||||
return OneToMany.class;
|
||||
@ -98,7 +64,7 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
}
|
||||
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType())
|
||||
.getActualTypeArguments()[0];
|
||||
if (objectClass == Long.class || objectClass == UUID.class) {
|
||||
if (objectClass == Long.class || objectClass == UUID.class || objectClass == ObjectId.class) {
|
||||
return true;
|
||||
}
|
||||
final OneToMany decorators = field.getDeclaredAnnotation(OneToMany.class);
|
||||
@ -111,104 +77,7 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void generateConcatQuery(
|
||||
@NotNull final String tableName,
|
||||
@NotNull final String primaryKey,
|
||||
@NotNull final Field field,
|
||||
@NotNull final StringBuilder querySelect,
|
||||
@NotNull final StringBuilder query,
|
||||
@NotNull final String name,
|
||||
@NotNull final CountInOut count,
|
||||
final QueryOptions options,
|
||||
final Class<?> targetEntity,
|
||||
final String mappedBy) throws Exception {
|
||||
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType())
|
||||
.getActualTypeArguments()[0];
|
||||
final String remoteTableName = AnnotationTools.getTableName(targetEntity);
|
||||
final FieldName remoteTablePrimaryKeyName = AnnotationTools
|
||||
.getFieldName(AnnotationTools.getPrimaryKeyField(targetEntity), options);
|
||||
final String tmpRemoteVariable = "tmp_" + Integer.toString(count.value);
|
||||
final String remoteDeletedFieldName = AnnotationTools.getDeletedFieldName(targetEntity);
|
||||
|
||||
querySelect.append(" (SELECT GROUP_CONCAT(");
|
||||
querySelect.append(tmpRemoteVariable);
|
||||
querySelect.append(".");
|
||||
querySelect.append(remoteTablePrimaryKeyName.inTable());
|
||||
querySelect.append(" ");
|
||||
if ("sqlite".equals(ConfigBaseVariable.getDBType())) {
|
||||
querySelect.append(", ");
|
||||
} else {
|
||||
querySelect.append("SEPARATOR ");
|
||||
}
|
||||
querySelect.append("'");
|
||||
if (objectClass == Long.class) {
|
||||
querySelect.append(SEPARATOR_LONG);
|
||||
}
|
||||
querySelect.append("') FROM ");
|
||||
querySelect.append(remoteTableName);
|
||||
querySelect.append(" ");
|
||||
querySelect.append(tmpRemoteVariable);
|
||||
querySelect.append(" WHERE ");
|
||||
if (remoteDeletedFieldName != null) {
|
||||
querySelect.append(tmpRemoteVariable);
|
||||
querySelect.append(".");
|
||||
querySelect.append(remoteDeletedFieldName);
|
||||
querySelect.append(" = false");
|
||||
querySelect.append(" AND ");
|
||||
}
|
||||
querySelect.append(tableName);
|
||||
querySelect.append(".");
|
||||
querySelect.append(primaryKey);
|
||||
querySelect.append(" = ");
|
||||
querySelect.append(tmpRemoteVariable);
|
||||
querySelect.append(".");
|
||||
querySelect.append(mappedBy);
|
||||
querySelect.append(" ");
|
||||
querySelect.append(") AS ");
|
||||
querySelect.append(name);
|
||||
querySelect.append(" ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void generateQuery(
|
||||
@NotNull final String tableName,
|
||||
@NotNull final String primaryKey,
|
||||
@NotNull final Field field,
|
||||
@NotNull final StringBuilder querySelect,
|
||||
@NotNull final StringBuilder query,
|
||||
@NotNull final String name,
|
||||
@NotNull final CountInOut count,
|
||||
final QueryOptions options) throws Exception {
|
||||
if (field.getType() != List.class) {
|
||||
return;
|
||||
}
|
||||
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType())
|
||||
.getActualTypeArguments()[0];
|
||||
final OneToMany decorators = field.getDeclaredAnnotation(OneToMany.class);
|
||||
if (decorators == null) {
|
||||
return;
|
||||
}
|
||||
// TODO: manage better the eager and lazy !!
|
||||
if (objectClass == Long.class || objectClass == UUID.class) {
|
||||
generateConcatQuery(tableName, primaryKey, field, querySelect, query, name, count, options,
|
||||
decorators.targetEntity(), decorators.mappedBy());
|
||||
return;
|
||||
}
|
||||
if (objectClass == decorators.targetEntity()) {
|
||||
if (decorators.fetch() == FetchType.EAGER) {
|
||||
throw new DataAccessException("EAGER is not supported for list of element...");
|
||||
} else {
|
||||
// Force a copy of the primaryKey to permit the async retrieve of the data
|
||||
querySelect.append(" ");
|
||||
querySelect.append(tableName);
|
||||
querySelect.append(".");
|
||||
querySelect.append(primaryKey);
|
||||
querySelect.append(" AS tmp_");
|
||||
querySelect.append(Integer.toString(count.value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// in first implementation we did not keep the data in the 2 Objects, bun we will do it after to have a faster table interactions.
|
||||
@Override
|
||||
public void fillFromDoc(
|
||||
final DBAccessMorphia ioDb,
|
||||
@ -222,11 +91,12 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
return;
|
||||
}
|
||||
|
||||
final String fieldName = AnnotationTools.getFieldName(field, options).inTable();
|
||||
if (!doc.containsKey(fieldName)) {
|
||||
field.set(data, null);
|
||||
return;
|
||||
}
|
||||
final FieldName fieldName = AnnotationTools.getFieldName(field, options);
|
||||
// in step 1 the fields are not stored in the local element
|
||||
// if (!doc.containsKey(fieldName.inTable())) {
|
||||
// field.set(data, null);
|
||||
// return;
|
||||
// }
|
||||
|
||||
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType())
|
||||
.getActualTypeArguments()[0];
|
||||
@ -234,62 +104,46 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
if (decorators == null) {
|
||||
return;
|
||||
}
|
||||
if (objectClass == Long.class || objectClass == UUID.class) {
|
||||
final Object value = doc.get(fieldName, field.getType());
|
||||
field.set(data, value);
|
||||
if (objectClass == Long.class || objectClass == UUID.class || objectClass == ObjectId.class) {
|
||||
if (true) {
|
||||
// DEVELOPMENT step 1 we search all the element in the list:
|
||||
// get the curentObject primary key
|
||||
final Field primaryField = AnnotationTools.getPrimaryKeyField(data.getClass());
|
||||
final String primaryKeyName = AnnotationTools.getFieldNameRaw(primaryField);
|
||||
final Object primaryKey = doc.get(primaryKeyName, primaryField.getType());
|
||||
// get the remotes objects
|
||||
final List<?> returnValue = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryCondition(decorators.mappedBy(), "=", primaryKey)));
|
||||
// extract the primary key of the remote objects
|
||||
final Field remotePrimaryField = AnnotationTools.getPrimaryKeyField(decorators.targetEntity());
|
||||
final String remotePrimaryKeyName = AnnotationTools.getFieldNameRaw(remotePrimaryField);
|
||||
final List<Object> listOfRemoteKeys = new ArrayList<>();
|
||||
for (final var item : returnValue) {
|
||||
listOfRemoteKeys.add(remotePrimaryField.get(item));
|
||||
}
|
||||
// inject in the current data field
|
||||
if (listOfRemoteKeys.size() != 0) {
|
||||
field.set(data, listOfRemoteKeys);
|
||||
}
|
||||
} else {
|
||||
// DEVELOPMENT In step 2 this will work well:
|
||||
final Object value = doc.get(fieldName.inTable(), field.getType());
|
||||
field.set(data, value);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (objectClass == decorators.targetEntity()) {
|
||||
|
||||
Long parentIdTmp = null;
|
||||
UUID parendUuidTmp = null;
|
||||
try {
|
||||
final Object value = doc.get(fieldName);
|
||||
if (value instanceof final Long valueCasted) {
|
||||
parentIdTmp = valueCasted;
|
||||
} else if (value instanceof final UUID valueCasted) {
|
||||
parendUuidTmp = valueCasted;
|
||||
}
|
||||
} catch (final Exception ex) {
|
||||
LOGGER.error("fail to find the correct type... {}", ex.getMessage());
|
||||
}
|
||||
final Long parentId = parentIdTmp;
|
||||
final UUID parendUuid = parendUuidTmp;
|
||||
final String mappingKey = decorators.mappedBy();
|
||||
// We get the parent ID ... ==> need to request the list of elements
|
||||
if (objectClass == Long.class) {
|
||||
LOGGER.error("Need to retreive all primary key of all elements");
|
||||
//field.set(data, idList);
|
||||
return;
|
||||
} else if (objectClass == UUID.class) {
|
||||
LOGGER.error("Need to retreive all primary key of all elements");
|
||||
//field.set(data, idList);
|
||||
return;
|
||||
}
|
||||
if (objectClass == decorators.targetEntity()) {
|
||||
if (parentId != null) {
|
||||
final LazyGetter lambda = () -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
final Object foreignData = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryCondition(mappingKey, "=", parentId)));
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
}
|
||||
field.set(data, foreignData);
|
||||
};
|
||||
lazyCall.add(lambda);
|
||||
} else if (parendUuid != null) {
|
||||
final LazyGetter lambda = () -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
final Object foreignData = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryCondition(mappingKey, "=", parendUuid)));
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
}
|
||||
field.set(data, foreignData);
|
||||
};
|
||||
lazyCall.add(lambda);
|
||||
}
|
||||
// Maybe in a second step we do not like this but this way is efficient too.
|
||||
// get the curentObject primary key
|
||||
final Field primaryField = AnnotationTools.getPrimaryKeyField(data.getClass());
|
||||
final String primaryKeyName = AnnotationTools.getFieldNameRaw(primaryField);
|
||||
final Object primaryKey = doc.get(primaryKeyName, primaryField.getType());
|
||||
// get the remotes objects
|
||||
final List<?> returnValue = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryCondition(decorators.mappedBy(), "=", primaryKey)));
|
||||
// inject in the current data field
|
||||
if (returnValue.size() != 0) {
|
||||
field.set(data, returnValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,13 +5,10 @@ import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.Document;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DBAccessMorphia;
|
||||
import org.kar.archidata.dataAccess.LazyGetter;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public interface DataAccessAddOn {
|
||||
/** Get the Class of the declaration annotation
|
||||
* @return The annotation class */
|
||||
@ -57,16 +54,6 @@ public interface DataAccessAddOn {
|
||||
return false;
|
||||
}
|
||||
|
||||
void generateQuery(
|
||||
@NotNull String tableName,
|
||||
@NotNull final String primaryKey,
|
||||
@NotNull Field field,
|
||||
@NotNull final StringBuilder querySelect,
|
||||
@NotNull final StringBuilder query,
|
||||
@NotNull String name,
|
||||
@NotNull CountInOut count,
|
||||
QueryOptions options) throws Exception;
|
||||
|
||||
// Return the number of colomn read
|
||||
void fillFromDoc(
|
||||
final DBAccessMorphia ioDb,
|
||||
|
@ -16,7 +16,6 @@ import org.kar.archidata.annotation.AnnotationTools.FieldName;
|
||||
import org.kar.archidata.annotation.DataJson;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
import org.kar.archidata.dataAccess.DBAccess;
|
||||
import org.kar.archidata.dataAccess.DBAccessMorphia;
|
||||
import org.kar.archidata.dataAccess.DBAccessSQL;
|
||||
import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.LazyGetter;
|
||||
@ -220,68 +219,62 @@ public class AddOnDataJson implements DataAccessAddOn {
|
||||
public static void addLink(
|
||||
final DBAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final String columnId,
|
||||
final Object id,
|
||||
final String columnList,
|
||||
final Object remoteKey) throws Exception {
|
||||
final String clazzPrimaryKeyName,
|
||||
final Object clazzPrimaryKeyValue,
|
||||
final String fieldNameToUpdate,
|
||||
final Object valueToAdd) throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final QueryOptions options = new QueryOptions(new OverrideTableName(tableName),
|
||||
new OptionSpecifyType("id", id.getClass()),
|
||||
new OptionSpecifyType("covers", remoteKey.getClass(), true));
|
||||
if (columnId != null && !columnId.equals("id")) {
|
||||
options.add(new OptionRenameColumn("id", columnId));
|
||||
new OptionSpecifyType("id", clazzPrimaryKeyValue.getClass()),
|
||||
new OptionSpecifyType("covers", valueToAdd.getClass(), true));
|
||||
if (clazzPrimaryKeyName != null && !clazzPrimaryKeyName.equals("id")) {
|
||||
options.add(new OptionRenameColumn("id", clazzPrimaryKeyName));
|
||||
}
|
||||
if (columnList != null && !columnList.equals("covers")) {
|
||||
options.add(new OptionRenameColumn("covers", columnList));
|
||||
if (fieldNameToUpdate != null && !fieldNameToUpdate.equals("covers")) {
|
||||
options.add(new OptionRenameColumn("covers", fieldNameToUpdate));
|
||||
}
|
||||
final TableCoversGeneric data = ioDb.get(TableCoversGeneric.class, id, options.getAllArray());
|
||||
final TableCoversGeneric data = ioDb.get(TableCoversGeneric.class, clazzPrimaryKeyValue, options.getAllArray());
|
||||
if (data.covers == null) {
|
||||
data.covers = new ArrayList<>();
|
||||
}
|
||||
for (final Object elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
if (elem.equals(valueToAdd)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
data.covers.add(remoteKey);
|
||||
data.covers.add(valueToAdd);
|
||||
ioDb.update(data, data.id, List.of("covers"), options.getAllArray());
|
||||
}
|
||||
|
||||
public static void removeLink(
|
||||
final DBAccess ioDb,
|
||||
final Class<?> clazz,
|
||||
final String columnId,
|
||||
final Object id,
|
||||
final String columnList,
|
||||
final Object remoteKey) throws Exception {
|
||||
if (ioDb instanceof final DBAccessSQL daSQL) {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final QueryOptions options = new QueryOptions(new OverrideTableName(tableName),
|
||||
new OptionSpecifyType("id", id.getClass()),
|
||||
new OptionSpecifyType("covers", remoteKey.getClass(), true));
|
||||
if (columnId != null && !columnId.equals("id")) {
|
||||
options.add(new OptionRenameColumn("id", columnId));
|
||||
}
|
||||
if (columnList != null && !columnList.equals("covers")) {
|
||||
options.add(new OptionRenameColumn("covers", columnList));
|
||||
}
|
||||
final TableCoversGeneric data = ioDb.get(TableCoversGeneric.class, id, options.getAllArray());
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
final List<Object> newList = new ArrayList<>();
|
||||
for (final Object elem : data.covers) {
|
||||
if (elem.equals(remoteKey)) {
|
||||
continue;
|
||||
}
|
||||
newList.add(elem);
|
||||
}
|
||||
data.covers = newList;
|
||||
ioDb.update(data, data.id, List.of("covers"), options.getAllArray());
|
||||
} else if (ioDb instanceof final DBAccessMorphia dam) {
|
||||
|
||||
} else {
|
||||
throw new DataAccessException("DataAccess Not managed");
|
||||
final String clazzPrimaryKeyName,
|
||||
final Object clazzPrimaryKeyValue,
|
||||
final String fieldNameToUpdate,
|
||||
final Object valueToRemove) throws Exception {
|
||||
final String tableName = AnnotationTools.getTableName(clazz);
|
||||
final QueryOptions options = new QueryOptions(new OverrideTableName(tableName),
|
||||
new OptionSpecifyType("id", clazzPrimaryKeyValue.getClass()),
|
||||
new OptionSpecifyType("covers", valueToRemove.getClass(), true));
|
||||
if (clazzPrimaryKeyName != null && !clazzPrimaryKeyName.equals("id")) {
|
||||
options.add(new OptionRenameColumn("id", clazzPrimaryKeyName));
|
||||
}
|
||||
if (fieldNameToUpdate != null && !fieldNameToUpdate.equals("covers")) {
|
||||
options.add(new OptionRenameColumn("covers", fieldNameToUpdate));
|
||||
}
|
||||
final TableCoversGeneric data = ioDb.get(TableCoversGeneric.class, clazzPrimaryKeyValue, options.getAllArray());
|
||||
if (data.covers == null) {
|
||||
return;
|
||||
}
|
||||
final List<Object> newList = new ArrayList<>();
|
||||
for (final Object elem : data.covers) {
|
||||
if (elem.equals(valueToRemove)) {
|
||||
continue;
|
||||
}
|
||||
newList.add(elem);
|
||||
}
|
||||
data.covers = newList;
|
||||
ioDb.update(data, data.id, List.of("covers"), options.getAllArray());
|
||||
}
|
||||
}
|
||||
|
@ -76,7 +76,7 @@ public class Condition extends QueryOption {
|
||||
}
|
||||
final List<Bson> filter = new ArrayList<>();
|
||||
if (exclude_deleted && deletedFieldName != null) {
|
||||
filter.add(Filters.eq(deletedFieldName, false));
|
||||
filter.add(Filters.or(Filters.eq(deletedFieldName, false), Filters.exists(deletedFieldName, false)));
|
||||
}
|
||||
// Check if we have a condition to generate
|
||||
if (this.condition != null) {
|
||||
|
@ -16,6 +16,6 @@ public class InputException extends Exception {
|
||||
public InputException(final String variable, final String message) {
|
||||
super(message);
|
||||
this.missingVariable = variable;
|
||||
this.status = Response.Status.NOT_ACCEPTABLE;
|
||||
this.status = Response.Status.BAD_REQUEST;
|
||||
}
|
||||
}
|
||||
|
@ -133,7 +133,9 @@ public class MigrationEngine {
|
||||
private void createTableIfAbleOrWaitAdmin(final DbConfig configInput) throws MigrationException {
|
||||
if (ConfigBaseVariable.getDBAbleToCreate()) {
|
||||
final DbConfig config = configInput.clone();
|
||||
config.setDbName(null);
|
||||
if (!"MONGO".equalsIgnoreCase(config.getType())) {
|
||||
config.setDbName(null);
|
||||
}
|
||||
final String dbName = configInput.getDbName();
|
||||
LOGGER.info("Verify existance of '{}'", dbName);
|
||||
try (final DBAccess da = DBAccess.createInterface(config)) {
|
||||
|
@ -5,6 +5,7 @@ import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
@ -17,13 +18,17 @@ import java.util.List;
|
||||
import org.apache.tika.Tika;
|
||||
import org.bson.types.ObjectId;
|
||||
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.annotation.AnnotationTools.FieldName;
|
||||
import org.kar.archidata.api.DataResource;
|
||||
import org.kar.archidata.dataAccess.DBAccess;
|
||||
import org.kar.archidata.dataAccess.QueryAnd;
|
||||
import org.kar.archidata.dataAccess.QueryCondition;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.kar.archidata.dataAccess.addOnSQL.AddOnDataJson;
|
||||
import org.kar.archidata.dataAccess.options.Condition;
|
||||
import org.kar.archidata.dataAccess.options.ReadAllColumn;
|
||||
import org.kar.archidata.exception.DataAccessException;
|
||||
import org.kar.archidata.exception.FailException;
|
||||
import org.kar.archidata.exception.InputException;
|
||||
import org.kar.archidata.model.Data;
|
||||
@ -391,6 +396,13 @@ public class DataTools {
|
||||
}
|
||||
// Fist step: retrieve all the Id of each parents:...
|
||||
LOGGER.info("Find typeNode");
|
||||
AddOnDataJson.addLink(ioDb, clazz, null, id, null, data.oid);
|
||||
|
||||
final Field idField = AnnotationTools.getIdField(clazz);
|
||||
if (idField == null) {
|
||||
throw new DataAccessException(
|
||||
"The class have no annotation @Id ==> can not determine the default type searching");
|
||||
}
|
||||
final FieldName fieldName = AnnotationTools.getFieldName(idField, new QueryOptions());
|
||||
AddOnDataJson.addLink(ioDb, clazz, fieldName.inTable(), id, null, data.oid);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.kar.archidata.tools;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
@ -8,9 +9,12 @@ import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpRequest.BodyPublishers;
|
||||
import java.net.http.HttpRequest.Builder;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.kar.archidata.exception.RESTErrorResponseException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -66,6 +70,21 @@ public class RESTApi {
|
||||
return modelSendJson("GET", clazz, urlOffset, null);
|
||||
}
|
||||
|
||||
public HttpResponse<byte[]> getRaw(final String urlOffset) throws IOException, InterruptedException {
|
||||
final Builder requestBuilding = createRequestBuilder(urlOffset);
|
||||
final HttpRequest request = requestBuilding.method("GET", BodyPublishers.ofString("")).build();
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
// client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
|
||||
return client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
||||
}
|
||||
|
||||
public <TYPE_RESPONSET, TYPE_BODY> TYPE_RESPONSET postMultipart(
|
||||
final Class<TYPE_RESPONSET> clazz,
|
||||
final String urlOffset,
|
||||
final Map<String, Object> data) throws RESTErrorResponseException, IOException, InterruptedException {
|
||||
return modelSendMultipart("POST", clazz, urlOffset, data);
|
||||
}
|
||||
|
||||
public <TYPE_RESPONSET, TYPE_BODY> TYPE_RESPONSET post(
|
||||
final Class<TYPE_RESPONSET> clazz,
|
||||
final String urlOffset,
|
||||
@ -108,6 +127,13 @@ public class RESTApi {
|
||||
return modelSendMap("PUT", clazz, urlOffset, data);
|
||||
}
|
||||
|
||||
public <TYPE_RESPONSE> TYPE_RESPONSE putMultipart(
|
||||
final Class<TYPE_RESPONSE> clazz,
|
||||
final String urlOffset,
|
||||
final Map<String, Object> data) throws RESTErrorResponseException, IOException, InterruptedException {
|
||||
return modelSendMultipart("PUT", clazz, urlOffset, data);
|
||||
}
|
||||
|
||||
public <TYPE_RESPONSE, TYPE_BODY> TYPE_RESPONSE patch(
|
||||
final Class<TYPE_RESPONSE> clazz,
|
||||
final String urlOffset,
|
||||
@ -142,21 +168,74 @@ public class RESTApi {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <TYPE_RESPONSE> TYPE_RESPONSE modelSendJson(
|
||||
final String model,
|
||||
final Class<TYPE_RESPONSE> clazz,
|
||||
final String urlOffset,
|
||||
String body) throws RESTErrorResponseException, IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
// client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
|
||||
public Builder createRequestBuilder(final String urlOffset) {
|
||||
Builder requestBuilding = HttpRequest.newBuilder().version(Version.HTTP_1_1)
|
||||
.uri(URI.create(this.baseUrl + urlOffset));
|
||||
LOGGER.trace("call {}: {}", model, URI.create(this.baseUrl + urlOffset));
|
||||
LOGGER.trace("DATA: {}", body);
|
||||
if (this.token != null) {
|
||||
requestBuilding = requestBuilding.header(HttpHeaders.AUTHORIZATION, "Bearer " + this.token);
|
||||
}
|
||||
return requestBuilding;
|
||||
}
|
||||
|
||||
public <TYPE_RESPONSE> TYPE_RESPONSE modelSendMultipart(
|
||||
final String model,
|
||||
final Class<TYPE_RESPONSE> clazzReturn,
|
||||
final String urlOffset,
|
||||
final Map<String, Object> params) throws RESTErrorResponseException, IOException, InterruptedException {
|
||||
|
||||
LOGGER.trace("call (MULTIPART) {}: {}", model, URI.create(this.baseUrl + urlOffset));
|
||||
Builder requestBuilding = createRequestBuilder(urlOffset);
|
||||
|
||||
// Create multipart key element
|
||||
final String boundary = (new ObjectId()).toString();
|
||||
requestBuilding = requestBuilding.header("Content-Type", "multipart/form-data; boundary=" + boundary);
|
||||
// create the body;
|
||||
final List<byte[]> bodyParts = new ArrayList<>();
|
||||
|
||||
for (final Map.Entry<String, Object> entry : params.entrySet()) {
|
||||
final StringBuilder partHeader = new StringBuilder();
|
||||
partHeader.append("--").append(boundary).append("\r\n");
|
||||
if (entry.getValue() instanceof File) {
|
||||
final File file = (File) entry.getValue();
|
||||
partHeader.append("Content-Disposition: form-data; name=\"").append(entry.getKey())
|
||||
.append("\"; filename=\"").append(file.getName()).append("\"\r\n");
|
||||
partHeader.append("Content-Type: application/octet-stream\r\n\r\n");
|
||||
bodyParts.add(partHeader.toString().getBytes());
|
||||
bodyParts.add(Files.readAllBytes(file.toPath()));
|
||||
bodyParts.add("\r\n".getBytes());
|
||||
} else {
|
||||
partHeader.append("Content-Disposition: form-data; name=\"").append(entry.getKey())
|
||||
.append("\"\r\n\r\n");
|
||||
if (entry.getValue() == null) {
|
||||
partHeader.append("null\r\n");
|
||||
} else {
|
||||
partHeader.append(entry.getValue().toString()).append("\r\n");
|
||||
}
|
||||
bodyParts.add(partHeader.toString().getBytes());
|
||||
}
|
||||
}
|
||||
bodyParts.add(("--" + boundary + "--\r\n").getBytes());
|
||||
|
||||
final int totalSize = bodyParts.stream().mapToInt(b -> b.length).sum();
|
||||
final byte[] finalBody = new byte[totalSize];
|
||||
int position = 0;
|
||||
for (final byte[] part : bodyParts) {
|
||||
System.arraycopy(part, 0, finalBody, position, part.length);
|
||||
position += part.length;
|
||||
}
|
||||
|
||||
final HttpRequest request = requestBuilding.method(model, BodyPublishers.ofByteArray(finalBody)).build();
|
||||
return callAndParseRequest(clazzReturn, request);
|
||||
}
|
||||
|
||||
public <TYPE_RESPONSE> TYPE_RESPONSE modelSendJson(
|
||||
final String model,
|
||||
final Class<TYPE_RESPONSE> clazzReturn,
|
||||
final String urlOffset,
|
||||
String body) throws RESTErrorResponseException, IOException, InterruptedException {
|
||||
LOGGER.trace("DATA: {}", body);
|
||||
Builder requestBuilding = createRequestBuilder(urlOffset);
|
||||
|
||||
if (body == null) {
|
||||
body = "";
|
||||
} else {
|
||||
@ -164,7 +243,19 @@ public class RESTApi {
|
||||
}
|
||||
LOGGER.trace("publish body: {}", body);
|
||||
final HttpRequest request = requestBuilding.method(model, BodyPublishers.ofString(body)).build();
|
||||
return callAndParseRequest(clazzReturn, request);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <TYPE_RESPONSE> TYPE_RESPONSE callAndParseRequest(
|
||||
final Class<TYPE_RESPONSE> clazzReturn,
|
||||
final HttpRequest request) throws RESTErrorResponseException, IOException, InterruptedException {
|
||||
final HttpClient client = HttpClient.newHttpClient();
|
||||
// client.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
|
||||
final HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
if (clazzReturn == HttpResponse.class) {
|
||||
return (TYPE_RESPONSE) httpResponse;
|
||||
}
|
||||
if (httpResponse.statusCode() < 200 || httpResponse.statusCode() >= 300) {
|
||||
LOGGER.trace("Receive Error: {}", httpResponse.body());
|
||||
try {
|
||||
@ -188,14 +279,14 @@ public class RESTApi {
|
||||
+ httpResponse.statusCode() + "] " + httpResponse.body());
|
||||
}
|
||||
}
|
||||
if (clazz == Void.class || clazz == void.class) {
|
||||
if (clazzReturn == Void.class || clazzReturn == void.class) {
|
||||
return null;
|
||||
}
|
||||
if (clazz.equals(String.class)) {
|
||||
if (clazzReturn.equals(String.class)) {
|
||||
return (TYPE_RESPONSE) httpResponse.body();
|
||||
}
|
||||
LOGGER.trace("Receive model: {} with data: '{}'", clazz.getCanonicalName(), httpResponse.body());
|
||||
return this.mapper.readValue(httpResponse.body(), clazz);
|
||||
LOGGER.trace("Receive model: {} with data: '{}'", clazzReturn.getCanonicalName(), httpResponse.body());
|
||||
return this.mapper.readValue(httpResponse.body(), clazzReturn);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
|
@ -122,7 +122,9 @@ public class ConfigureDb {
|
||||
LOGGER.error("Fail to clean the DB");
|
||||
return;
|
||||
}
|
||||
config.setDbName(null);
|
||||
if (!"MONGO".equalsIgnoreCase(modeTest)) {
|
||||
config.setDbName(null);
|
||||
}
|
||||
LOGGER.info("Remove the DB and create a new one '{}'", config.getDbName());
|
||||
try (final DBAccess daRoot = DBAccess.createInterface(config)) {
|
||||
if ("SQLITE-MEMORY".equalsIgnoreCase(modeTest)) {
|
||||
|
@ -23,7 +23,6 @@ import test.kar.archidata.dataAccess.model.TypeManyToOneLongRoot;
|
||||
import test.kar.archidata.dataAccess.model.TypeManyToOneLongRootExpand;
|
||||
import test.kar.archidata.dataAccess.model.TypeManyToOneUUIDRemote;
|
||||
import test.kar.archidata.dataAccess.model.TypeManyToOneUUIDRoot;
|
||||
import test.kar.archidata.dataAccess.model.TypeManyToOneUUIDRootExpand;
|
||||
|
||||
@ExtendWith(StepwiseExtension.class)
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
@ -118,63 +117,4 @@ public class TestManyToOneLong {
|
||||
Assertions.assertNull(retrieve2.remote);
|
||||
}
|
||||
|
||||
@Order(3)
|
||||
@Test
|
||||
public void testRemoteUUID() throws Exception {
|
||||
TypeManyToOneUUIDRemote remote = new TypeManyToOneUUIDRemote();
|
||||
remote.data = "remote1";
|
||||
final TypeManyToOneUUIDRemote insertedRemote1 = ConfigureDb.da.insert(remote);
|
||||
Assertions.assertEquals(insertedRemote1.data, remote.data);
|
||||
|
||||
remote = new TypeManyToOneUUIDRemote();
|
||||
remote.data = "remote2";
|
||||
final TypeManyToOneUUIDRemote insertedRemote2 = ConfigureDb.da.insert(remote);
|
||||
Assertions.assertEquals(insertedRemote2.data, remote.data);
|
||||
|
||||
final TypeManyToOneUUIDRoot test = new TypeManyToOneUUIDRoot();
|
||||
test.otherData = "kjhlkjlkj";
|
||||
test.remoteUuid = insertedRemote2.uuid;
|
||||
final TypeManyToOneUUIDRoot insertedData = ConfigureDb.da.insert(test);
|
||||
Assertions.assertNotNull(insertedData);
|
||||
Assertions.assertNotNull(insertedData.uuid);
|
||||
Assertions.assertEquals(test.otherData, insertedData.otherData);
|
||||
Assertions.assertEquals(insertedRemote2.uuid, insertedData.remoteUuid);
|
||||
|
||||
TypeManyToOneUUIDRoot retrieve = ConfigureDb.da.get(TypeManyToOneUUIDRoot.class, insertedData.uuid);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.uuid);
|
||||
Assertions.assertEquals(insertedData.uuid, retrieve.uuid);
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||
Assertions.assertEquals(insertedRemote2.uuid, retrieve.remoteUuid);
|
||||
|
||||
TypeManyToOneUUIDRootExpand retrieve2 = ConfigureDb.da.get(TypeManyToOneUUIDRootExpand.class,
|
||||
insertedData.uuid);
|
||||
Assertions.assertNotNull(retrieve2);
|
||||
Assertions.assertNotNull(retrieve2.uuid);
|
||||
Assertions.assertEquals(insertedData.uuid, retrieve2.uuid);
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve2.otherData);
|
||||
Assertions.assertNotNull(retrieve2.remote);
|
||||
Assertions.assertEquals(insertedRemote2.uuid, retrieve2.remote.uuid);
|
||||
Assertions.assertEquals(insertedRemote2.data, retrieve2.remote.data);
|
||||
|
||||
// remove values:
|
||||
final long count = ConfigureDb.da.delete(TypeManyToOneUUIDRemote.class, insertedRemote2.uuid);
|
||||
Assertions.assertEquals(1, count);
|
||||
|
||||
// check fail:
|
||||
|
||||
retrieve = ConfigureDb.da.get(TypeManyToOneUUIDRoot.class, insertedData.uuid);
|
||||
Assertions.assertNotNull(retrieve);
|
||||
Assertions.assertNotNull(retrieve.uuid);
|
||||
Assertions.assertEquals(insertedData.uuid, retrieve.uuid);
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve.otherData);
|
||||
Assertions.assertEquals(insertedRemote2.uuid, retrieve.remoteUuid);
|
||||
|
||||
retrieve2 = ConfigureDb.da.get(TypeManyToOneUUIDRootExpand.class, insertedData.uuid);
|
||||
Assertions.assertNotNull(retrieve2);
|
||||
Assertions.assertNotNull(retrieve2.uuid);
|
||||
Assertions.assertEquals(insertedData.uuid, retrieve2.uuid);
|
||||
Assertions.assertEquals(insertedData.otherData, retrieve2.otherData);
|
||||
Assertions.assertNull(retrieve2.remote);
|
||||
}
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
@Table(name = "TypeManyToOneLongRoot")
|
||||
//for Mongo
|
||||
//for Morphia
|
||||
@Entity(value = "TypeManyToOneLongRoot")
|
||||
public class TypeManyToOneLongRootExpand extends GenericData {
|
||||
|
||||
|
@ -1 +1 @@
|
||||
0.25.4
|
||||
0.25.6
|
||||
|
Loading…
x
Reference in New Issue
Block a user