Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
1b4e6ca239 | |||
be8a5c713a | |||
c3f03bc1e8 | |||
c7eadc607d | |||
5f89ff7944 | |||
20d2d004cb |
2
pom.xml
2
pom.xml
@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>kangaroo-and-rabbit</groupId>
|
||||
<artifactId>archidata</artifactId>
|
||||
<version>0.23.4</version>
|
||||
<version>0.24.0</version>
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
<maven.compiler.version>3.1</maven.compiler.version>
|
||||
|
@ -9,6 +9,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.dataAccess.CountInOut;
|
||||
@ -72,7 +73,7 @@ public class AddOnManyToMany 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 ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
|
||||
@ -135,7 +136,11 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
querySelect.append("'");
|
||||
if (objectClass == Long.class) {
|
||||
querySelect.append(SEPARATOR_LONG);
|
||||
} else if (objectClass == UUID.class) {} else {
|
||||
} else if (objectClass == UUID.class) {
|
||||
// ???
|
||||
} else if (objectClass == ObjectId.class) {
|
||||
// ???
|
||||
} else {
|
||||
final Class<?> foreignKeyType = AnnotationTools.getPrimaryKeyField(objectClass).getType();
|
||||
if (foreignKeyType == Long.class) {
|
||||
querySelect.append(SEPARATOR_LONG);
|
||||
@ -193,7 +198,7 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
final Class<?> objectClass = (Class<?>) ((ParameterizedType) field.getGenericType())
|
||||
.getActualTypeArguments()[0];
|
||||
// TODO: manage better the eager and lazy !!
|
||||
if (objectClass == Long.class || objectClass == UUID.class) {
|
||||
if (objectClass == Long.class || objectClass == UUID.class || objectClass == ObjectId.class) {
|
||||
generateConcatQuery(tableName, primaryKey, field, querySelect, query, name, count, options);
|
||||
}
|
||||
final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
|
||||
@ -234,6 +239,11 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
field.set(data, idList);
|
||||
count.inc();
|
||||
return;
|
||||
} else if (objectClass == ObjectId.class) {
|
||||
final List<ObjectId> idList = ioDb.getListOfRawOIDs(rs, count.value);
|
||||
field.set(data, idList);
|
||||
count.inc();
|
||||
return;
|
||||
}
|
||||
final ManyToMany decorators = field.getDeclaredAnnotation(ManyToMany.class);
|
||||
if (decorators == null) {
|
||||
@ -285,6 +295,27 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
};
|
||||
lazyCall.add(lambda);
|
||||
}
|
||||
} else if (foreignKeyType == ObjectId.class) {
|
||||
final List<ObjectId> idList = ioDb.getListOfRawOIDs(rs, count.value);
|
||||
// field.set(data, idList);
|
||||
count.inc();
|
||||
if (idList != null && idList.size() > 0) {
|
||||
final FieldName idField = AnnotationTools.getFieldName(AnnotationTools.getIdField(objectClass),
|
||||
options);
|
||||
// In the lazy mode, the request is done in asynchronous mode, they will be done after...
|
||||
final LazyGetter lambda = () -> {
|
||||
final List<ObjectId> childs = new ArrayList<>(idList);
|
||||
// TODO: update to have get with abstract types ....
|
||||
@SuppressWarnings("unchecked")
|
||||
final Object foreignData = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryInList<>(idField.inTable(), childs)));
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
}
|
||||
field.set(data, foreignData);
|
||||
};
|
||||
lazyCall.add(lambda);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -309,9 +340,10 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
}
|
||||
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() + ">");
|
||||
if (objectClass != Long.class && objectClass != UUID.class && objectClass != ObjectId.class) {
|
||||
throw new DataAccessException(
|
||||
"Can not ManyToMany with other than List<Long> or List<UUID> or List<ObjectId> Model: List<"
|
||||
+ objectClass.getCanonicalName() + ">");
|
||||
}
|
||||
final FieldName columnName = AnnotationTools.getFieldName(field, options);
|
||||
final String linkTableName = generateLinkTableName(tableName, columnName.inTable());
|
||||
@ -348,9 +380,10 @@ public class AddOnManyToMany implements DataAccessAddOn {
|
||||
}
|
||||
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() + ">");
|
||||
if (objectClass != Long.class && objectClass != UUID.class && objectClass != ObjectId.class) {
|
||||
throw new DataAccessException(
|
||||
"Can not ManyToMany with other than List<Long> or List<UUID> or List<ObjectId> Model: List<"
|
||||
+ objectClass.getCanonicalName() + ">");
|
||||
}
|
||||
final FieldName columnName = AnnotationTools.getFieldName(field, options);
|
||||
final String linkTableName = generateLinkTableName(tableName, columnName.inTable());
|
||||
|
@ -7,6 +7,7 @@ import java.sql.Types;
|
||||
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.dataAccess.CountInOut;
|
||||
@ -66,6 +67,8 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
ps.setNull(iii.value, Types.VARCHAR);
|
||||
} else if (field.getType() == UUID.class) {
|
||||
ps.setNull(iii.value, Types.BINARY);
|
||||
} else if (field.getType() == ObjectId.class) {
|
||||
ps.setNull(iii.value, Types.BINARY);
|
||||
}
|
||||
} else if (field.getType() == Long.class) {
|
||||
final Long dataTyped = (Long) data;
|
||||
@ -84,6 +87,11 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
LOGGER.info("Generate UUTD for DB: {}", dataTyped);
|
||||
final byte[] dataByte = UuidUtils.asBytes(dataTyped);
|
||||
ps.setBytes(iii.value, dataByte);
|
||||
} else if (field.getType() == ObjectId.class) {
|
||||
final ObjectId dataTyped = (ObjectId) data;
|
||||
LOGGER.info("Generate ObjectId for DB: {}", dataTyped);
|
||||
final byte[] dataByte = dataTyped.toByteArray();
|
||||
ps.setBytes(iii.value, dataByte);
|
||||
} else {
|
||||
final Field idField = AnnotationTools.getFieldOfId(field.getType());
|
||||
final Object uid = idField.get(data);
|
||||
@ -101,7 +109,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);
|
||||
@ -120,7 +129,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);
|
||||
@ -141,7 +150,8 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
@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) {
|
||||
|| field.getType() == String.class || field.getType() == UUID.class
|
||||
|| field.getType() == ObjectId.class) {
|
||||
querySelect.append(" ");
|
||||
querySelect.append(tableName);
|
||||
querySelect.append(".");
|
||||
@ -230,6 +240,15 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (field.getType() == ObjectId.class) {
|
||||
final byte[] tmp = rs.getBytes(count.value);
|
||||
count.inc();
|
||||
if (!rs.wasNull()) {
|
||||
final ObjectId foreignKey = new ObjectId(tmp);
|
||||
field.set(data, foreignKey);
|
||||
}
|
||||
return;
|
||||
}
|
||||
final Class<?> objectClass = field.getType();
|
||||
final ManyToOne decorators = field.getDeclaredAnnotation(ManyToOne.class);
|
||||
if (decorators == null) {
|
||||
@ -279,6 +298,22 @@ public class AddOnManyToOne implements DataAccessAddOn {
|
||||
};
|
||||
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 = ioDb.getListOfRawOID(rs, count.value);
|
||||
count.inc();
|
||||
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);
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
}
|
||||
field.set(data, foreignData);
|
||||
};
|
||||
lazyCall.add(lambda);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -298,7 +333,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 {
|
||||
|
@ -10,6 +10,7 @@ import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.kar.archidata.annotation.AnnotationTools;
|
||||
import org.kar.archidata.annotation.AnnotationTools.FieldName;
|
||||
import org.kar.archidata.dataAccess.CountInOut;
|
||||
@ -110,7 +111,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);
|
||||
@ -201,7 +202,7 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
return;
|
||||
}
|
||||
// TODO: manage better the eager and lazy !!
|
||||
if (objectClass == Long.class || objectClass == UUID.class) {
|
||||
if (objectClass == Long.class || objectClass == UUID.class || objectClass == ObjectId.class) {
|
||||
generateConcatQuery(tableName, primaryKey, field, querySelect, query, name, count, options,
|
||||
decorators.targetEntity(), decorators.mappedBy());
|
||||
return;
|
||||
@ -250,22 +251,35 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
field.set(data, idList);
|
||||
count.inc();
|
||||
return;
|
||||
} else if (objectClass == ObjectId.class) {
|
||||
final List<ObjectId> idList = ioDb.getListOfRawOIDs(rs, count.value);
|
||||
field.set(data, idList);
|
||||
count.inc();
|
||||
return;
|
||||
}
|
||||
if (objectClass == decorators.targetEntity()) {
|
||||
|
||||
Long parentIdTmp = null;
|
||||
UUID parendUuidTmp = null;
|
||||
ObjectId parendOidTmp = null;
|
||||
try {
|
||||
final String modelData = rs.getString(count.value);
|
||||
parentIdTmp = Long.valueOf(modelData);
|
||||
count.inc();
|
||||
} catch (final NumberFormatException ex) {
|
||||
final List<UUID> idList = ioDb.getListOfRawUUIDs(rs, count.value);
|
||||
parendUuidTmp = idList.get(0);
|
||||
count.inc();
|
||||
try {
|
||||
final List<UUID> idList = ioDb.getListOfRawUUIDs(rs, count.value);
|
||||
parendUuidTmp = idList.get(0);
|
||||
count.inc();
|
||||
} catch (final NumberFormatException ex2) {
|
||||
// TODO : How to manage ObjectId ==> I am not sure it works well...
|
||||
final List<ObjectId> idList = ioDb.getListOfRawOIDs(rs, count.value);
|
||||
parendOidTmp = idList.get(0);
|
||||
count.inc();
|
||||
}
|
||||
}
|
||||
final Long parentId = parentIdTmp;
|
||||
final UUID parendUuid = parendUuidTmp;
|
||||
final ObjectId parendOid = parendOidTmp;
|
||||
final String mappingKey = decorators.mappedBy();
|
||||
// We get the parent ID ... ==> need to request the list of elements
|
||||
if (objectClass == Long.class) {
|
||||
@ -276,6 +290,10 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
LOGGER.error("Need to retreive all primary key of all elements");
|
||||
//field.set(data, idList);
|
||||
return;
|
||||
} else if (objectClass == ObjectId.class) {
|
||||
LOGGER.error("Need to retreive all primary key of all elements");
|
||||
//field.set(data, idList);
|
||||
return;
|
||||
}
|
||||
if (objectClass == decorators.targetEntity()) {
|
||||
if (decorators.fetch() == FetchType.EAGER) {
|
||||
@ -303,6 +321,17 @@ public class AddOnOneToMany implements DataAccessAddOn {
|
||||
field.set(data, foreignData);
|
||||
};
|
||||
lazyCall.add(lambda);
|
||||
} else if (parendOid != null) {
|
||||
final LazyGetter lambda = () -> {
|
||||
@SuppressWarnings("unchecked")
|
||||
final Object foreignData = ioDb.getsWhere(decorators.targetEntity(),
|
||||
new Condition(new QueryCondition(mappingKey, "=", parendOid)));
|
||||
if (foreignData == null) {
|
||||
return;
|
||||
}
|
||||
field.set(data, foreignData);
|
||||
};
|
||||
lazyCall.add(lambda);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import org.kar.archidata.dataAccess.DataFactory;
|
||||
import org.kar.archidata.dataAccess.QueryOptions;
|
||||
import org.kar.archidata.db.DbConfig;
|
||||
import org.kar.archidata.migration.model.Migration;
|
||||
import org.kar.archidata.tools.ConfigBaseVariable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@ -130,35 +131,40 @@ public class MigrationEngine {
|
||||
}
|
||||
|
||||
private void createTableIfAbleOrWaitAdmin(final DbConfig configInput) throws MigrationException {
|
||||
final DbConfig config = configInput.clone();
|
||||
config.setDbName(null);
|
||||
final String dbName = configInput.getDbName();
|
||||
LOGGER.info("Verify existance of '{}'", dbName);
|
||||
try (final DBAccess da = DBAccess.createInterface(config)) {
|
||||
boolean exist = da.isDBExist(dbName);
|
||||
if (!exist) {
|
||||
LOGGER.warn("DB: '{}' DOES NOT EXIST ==> create one", dbName);
|
||||
// create the local DB:
|
||||
da.createDB(dbName);
|
||||
}
|
||||
exist = da.isDBExist(dbName);
|
||||
while (!exist) {
|
||||
LOGGER.error("DB: '{}' DOES NOT EXIST after trying to create one ", dbName);
|
||||
LOGGER.error("Waiting administrator create a new one, we check after 30 seconds...");
|
||||
try {
|
||||
Thread.sleep(30000);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
if (ConfigBaseVariable.getDBAbleToCreate()) {
|
||||
final DbConfig config = configInput.clone();
|
||||
config.setDbName(null);
|
||||
final String dbName = configInput.getDbName();
|
||||
LOGGER.info("Verify existance of '{}'", dbName);
|
||||
try (final DBAccess da = DBAccess.createInterface(config)) {
|
||||
boolean exist = da.isDBExist(dbName);
|
||||
if (!exist) {
|
||||
LOGGER.warn("DB: '{}' DOES NOT EXIST ==> create one", dbName);
|
||||
// create the local DB:
|
||||
da.createDB(dbName);
|
||||
}
|
||||
exist = da.isDBExist(dbName);
|
||||
while (!exist) {
|
||||
LOGGER.error("DB: '{}' DOES NOT EXIST after trying to create one ", dbName);
|
||||
LOGGER.error("Waiting administrator create a new one, we check after 30 seconds...");
|
||||
try {
|
||||
Thread.sleep(30000);
|
||||
} catch (final InterruptedException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
exist = da.isDBExist(dbName);
|
||||
}
|
||||
} catch (final InternalServerErrorException e) {
|
||||
e.printStackTrace();
|
||||
throw new MigrationException("TODO ...");
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new MigrationException("TODO ...");
|
||||
}
|
||||
} catch (final InternalServerErrorException e) {
|
||||
e.printStackTrace();
|
||||
throw new MigrationException("TODO ...");
|
||||
} catch (final IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new MigrationException("TODO ...");
|
||||
} else {
|
||||
final String dbName = configInput.getDbName();
|
||||
LOGGER.warn("DB: '{}' is not check if it EXIST", dbName);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -3,6 +3,7 @@ package org.kar.archidata.tools;
|
||||
public class ConfigBaseVariable {
|
||||
static public String tmpDataFolder;
|
||||
static public String dataFolder;
|
||||
static public String dbAbleToCreate;
|
||||
static public String dbType;
|
||||
static public String dbHost;
|
||||
static public String dbPort;
|
||||
@ -23,6 +24,7 @@ public class ConfigBaseVariable {
|
||||
public static void clearAllValue() {
|
||||
tmpDataFolder = System.getenv("DATA_TMP_FOLDER");
|
||||
dataFolder = System.getenv("DATA_FOLDER");
|
||||
dbAbleToCreate = System.getenv("DB_ABLE_TO_CREATE");
|
||||
dbType = System.getenv("DB_TYPE");
|
||||
dbHost = System.getenv("DB_HOST");
|
||||
dbPort = System.getenv("DB_PORT");
|
||||
@ -58,6 +60,13 @@ public class ConfigBaseVariable {
|
||||
return dataFolder;
|
||||
}
|
||||
|
||||
public static boolean getDBAbleToCreate() {
|
||||
if (dbAbleToCreate == null) {
|
||||
return true;
|
||||
}
|
||||
return Boolean.getBoolean(dbAbleToCreate);
|
||||
}
|
||||
|
||||
public static String getDBType() {
|
||||
if (dbType == null) {
|
||||
return "mysql";
|
||||
|
@ -1 +1 @@
|
||||
0.23.4
|
||||
0.24.0
|
||||
|
Loading…
x
Reference in New Issue
Block a user