From 209663d32a76f663ca93077c8c9e379e3b9c8088 Mon Sep 17 00:00:00 2001 From: Carlos Ruiz Date: Thu, 14 Sep 2023 14:18:46 +0200 Subject: [PATCH] IDEMPIERE-5567 Support of UUID as Key (FHCA-4195) (#2008) - MappedModelFactory check and register UUID constructor --- .../idempiere/model/MappedModelFactory.java | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 deletions(-) diff --git a/org.adempiere.base/src/org/idempiere/model/MappedModelFactory.java b/org.adempiere.base/src/org/idempiere/model/MappedModelFactory.java index 9265142f01..6d787bff13 100644 --- a/org.adempiere.base/src/org/idempiere/model/MappedModelFactory.java +++ b/org.adempiere.base/src/org/idempiere/model/MappedModelFactory.java @@ -105,10 +105,12 @@ public class MappedModelFactory implements IModelFactory, IMappedModelFactory { public void addMapping(String tableName, Supplier> classSupplier, BiFunction recordIdFunction, BiFunction recordUUIDFunction, BiFunction resultSetFunction) { classMap.put(tableName, classSupplier); - recordIdMap.put(tableName, recordIdFunction); + if (recordIdFunction != null) + recordIdMap.put(tableName, recordIdFunction); if (recordUUIDFunction != null) recordUUIDMap.put(tableName, recordUUIDFunction); - resultSetMap.put(tableName, resultSetFunction); + if (resultSetFunction != null) + resultSetMap.put(tableName, resultSetFunction); } @Override @@ -149,11 +151,26 @@ public class MappedModelFactory implements IModelFactory, IMappedModelFactory { try { final Class clazz = classLoader.loadClass(className); Supplier> classSupplier = () -> { return clazz; }; - Constructor idConstructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, int.class, String.class}); - RecordIdFunction recordIdFunction = new RecordIdFunction(idConstructor); - Constructor rsConstructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, ResultSet.class, String.class}); - ResultSetFunction resultSetFunction = new ResultSetFunction(rsConstructor); - addMapping(tableName, classSupplier, recordIdFunction, resultSetFunction); + RecordIdFunction recordIdFunction = null; + try { + Constructor idConstructor = clazz.getDeclaredConstructor(new Class[]{Properties.class, int.class, String.class}); + recordIdFunction = new RecordIdFunction(idConstructor); + } 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) { if (s_log.isLoggable(Level.INFO)) s_log.log(Level.INFO, e.getMessage(), e); @@ -183,7 +200,29 @@ public class MappedModelFactory implements IModelFactory, IMappedModelFactory { return null; } } - + + private static final class RecordUUIDFunction implements BiFunction { + 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 { private Constructor constructor;