IDEMPIERE-5567 Support of UUID as Key (FHCA-4195) (#2008)

- MappedModelFactory check and register UUID constructor
This commit is contained in:
Carlos Ruiz 2023-09-14 14:18:46 +02:00 committed by GitHub
parent d7d4504271
commit 209663d32a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 47 additions and 8 deletions

View File

@ -105,10 +105,12 @@ public class MappedModelFactory implements IModelFactory, IMappedModelFactory {
public void addMapping(String tableName, Supplier<Class<?>> classSupplier, BiFunction<Integer, String, ? extends PO> recordIdFunction, public void addMapping(String tableName, Supplier<Class<?>> classSupplier, BiFunction<Integer, String, ? extends PO> recordIdFunction,
BiFunction<String, String, ? extends PO> recordUUIDFunction, BiFunction<ResultSet, String, ? extends PO> resultSetFunction) { BiFunction<String, String, ? extends PO> recordUUIDFunction, BiFunction<ResultSet, String, ? extends PO> resultSetFunction) {
classMap.put(tableName, classSupplier); classMap.put(tableName, classSupplier);
recordIdMap.put(tableName, recordIdFunction); if (recordIdFunction != null)
recordIdMap.put(tableName, recordIdFunction);
if (recordUUIDFunction != null) if (recordUUIDFunction != null)
recordUUIDMap.put(tableName, recordUUIDFunction); recordUUIDMap.put(tableName, recordUUIDFunction);
resultSetMap.put(tableName, resultSetFunction); if (resultSetFunction != null)
resultSetMap.put(tableName, resultSetFunction);
} }
@Override @Override
@ -149,11 +151,26 @@ public class MappedModelFactory implements IModelFactory, IMappedModelFactory {
try { try {
final Class<?> clazz = classLoader.loadClass(className); final Class<?> clazz = classLoader.loadClass(className);
Supplier<Class<?>> classSupplier = () -> { return clazz; }; Supplier<Class<?>> classSupplier = () -> { return clazz; };
Constructor<?> idConstructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, int.class, String.class}); RecordIdFunction recordIdFunction = null;
RecordIdFunction recordIdFunction = new RecordIdFunction(idConstructor); try {
Constructor<?> rsConstructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, ResultSet.class, String.class}); Constructor<?> idConstructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, int.class, String.class});
ResultSetFunction resultSetFunction = new ResultSetFunction(rsConstructor); recordIdFunction = new RecordIdFunction(idConstructor);
addMapping(tableName, classSupplier, recordIdFunction, resultSetFunction); } catch (Exception e) {}
RecordUUIDFunction recordUUIDFunction = null;
try {
Constructor<?> uuidConstructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, String.class, String.class});
recordUUIDFunction = new RecordUUIDFunction(uuidConstructor);
} catch (Exception e) {}
ResultSetFunction resultSetFunction = null;
try {
Constructor<?> rsConstructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, ResultSet.class, String.class});
resultSetFunction = new ResultSetFunction(rsConstructor);
} catch (Exception e) {}
if (recordIdFunction == null && recordUUIDFunction == null)
s_log.warning("Model class " + clazz.getName() + " for table " + tableName + " doesn't have ID neither UUID constructor");
if (resultSetFunction == null)
s_log.warning("Model class " + clazz.getName() + " for table " + tableName + " doesn't have ResultSet constructor");
addMapping(tableName, classSupplier, recordIdFunction, recordUUIDFunction, resultSetFunction);
} catch (Exception e) { } catch (Exception e) {
if (s_log.isLoggable(Level.INFO)) if (s_log.isLoggable(Level.INFO))
s_log.log(Level.INFO, e.getMessage(), e); s_log.log(Level.INFO, e.getMessage(), e);
@ -183,7 +200,29 @@ public class MappedModelFactory implements IModelFactory, IMappedModelFactory {
return null; return null;
} }
} }
private static final class RecordUUIDFunction implements BiFunction<String, String, PO> {
private Constructor<?> constructor;
private RecordUUIDFunction(Constructor<?> constructor) {
this.constructor = constructor;
}
@Override
public PO apply(String uuid, String trxName) {
if (constructor != null) {
try {
return (PO) constructor.newInstance(Env.getCtx(), uuid, trxName);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
constructor = null;
throw new RuntimeException(e);
}
}
return null;
}
}
private static final class ResultSetFunction implements BiFunction<ResultSet, String, PO> { private static final class ResultSetFunction implements BiFunction<ResultSet, String, PO> {
private Constructor<?> constructor; private Constructor<?> constructor;