From 236eff523eca898bb06954f2687ab319a763cc9e Mon Sep 17 00:00:00 2001 From: Tim Chamberlain Date: Fri, 4 Nov 2022 14:28:15 -0500 Subject: [PATCH] SPRINT-15: added methods to get lists and maps of record entities --- .../processes/utils/GeneralProcessUtils.java | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/processes/utils/GeneralProcessUtils.java b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/processes/utils/GeneralProcessUtils.java index 7962ec68..2490aefa 100644 --- a/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/processes/utils/GeneralProcessUtils.java +++ b/qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/processes/utils/GeneralProcessUtils.java @@ -37,6 +37,7 @@ import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter; import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryInput; import com.kingsrook.qqq.backend.core.model.actions.tables.query.QueryOutput; import com.kingsrook.qqq.backend.core.model.data.QRecord; +import com.kingsrook.qqq.backend.core.model.data.QRecordEntity; import com.kingsrook.qqq.backend.core.utils.ListingHash; @@ -203,6 +204,30 @@ public class GeneralProcessUtils + /******************************************************************************* + ** Load all rows from a table as a RecordEntity. + ** + ** Note, this is inherently unsafe, if you were to call it on a table with + ** too many rows... Caveat emptor. + *******************************************************************************/ + public static List loadTable(AbstractActionInput parentActionInput, String tableName, Class entityClass) throws QException + { + QueryInput queryInput = new QueryInput(parentActionInput.getInstance()); + queryInput.setSession(parentActionInput.getSession()); + queryInput.setTableName(tableName); + QueryOutput queryOutput = new QueryAction().execute(queryInput); + + List rs = new ArrayList<>(); + for(QRecord record : queryOutput.getRecords()) + { + rs.add(QRecordEntity.fromQRecord(entityClass, record)); + } + + return (rs); + } + + + /******************************************************************************* ** Load all rows from a table, into a map, keyed by the keyFieldName. ** @@ -236,6 +261,31 @@ public class GeneralProcessUtils + /******************************************************************************* + ** Note - null values from the key field are NOT put in the map. + *******************************************************************************/ + public static Map loadTableToMap(AbstractActionInput parentActionInput, String tableName, String keyFieldName, Class entityClass) throws QException + { + QueryInput queryInput = new QueryInput(parentActionInput.getInstance()); + queryInput.setSession(parentActionInput.getSession()); + queryInput.setTableName(tableName); + QueryOutput queryOutput = new QueryAction().execute(queryInput); + List records = queryOutput.getRecords(); + + Map map = new HashMap<>(); + for(QRecord record : records) + { + Serializable value = record.getValue(keyFieldName); + if(value != null) + { + map.put(value, QRecordEntity.fromQRecord(entityClass, record)); + } + } + return (map); + } + + + /******************************************************************************* ** Load all rows from a table, into a ListingHash, keyed by the keyFieldName. **