CE-1955 - more flexible handling of inbound types for looking up possible values

This commit is contained in:
2024-12-16 08:27:21 -06:00
parent 000226c30a
commit db526009d2
2 changed files with 80 additions and 9 deletions

View File

@ -40,6 +40,7 @@ import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldMetaData;
import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
import com.kingsrook.qqq.backend.core.model.metadata.possiblevalues.QPossibleValue;
import com.kingsrook.qqq.backend.core.model.metadata.possiblevalues.QPossibleValueSource;
import com.kingsrook.qqq.backend.core.model.metadata.tables.Association;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.processes.implementations.bulk.insert.model.BulkInsertMapping;
@ -170,20 +171,44 @@ public class BulkLoadValueMapper
***************************************************************************/
private static void handlePossibleValues(QFieldMetaData field, ListingHash<String, QRecord> fieldPossibleValueToRecordMap, String associationNamePrefixForFields, String tableLabelPrefix) throws QException
{
QPossibleValueSource possibleValueSource = QContext.getQInstance().getPossibleValueSource(field.getPossibleValueSourceName());
Set<String> values = fieldPossibleValueToRecordMap.keySet();
Map<String, Serializable> valuesToValueInPvsIdTypeMap = new HashMap<>();
Map<String, QPossibleValue<?>> valuesFound = new HashMap<>();
Set<String> valuesNotFound = new HashSet<>(values);
Set<Serializable> valuesNotFound = new HashSet<>();
////////////////////////////////////////////////////////
// do a search, trying to use all given values as ids //
////////////////////////////////////////////////////////
SearchPossibleValueSourceInput searchPossibleValueSourceInput = new SearchPossibleValueSourceInput();
searchPossibleValueSourceInput.setPossibleValueSourceName(field.getPossibleValueSourceName());
ArrayList<Serializable> idList = new ArrayList<>(values);
ArrayList<Serializable> idList = new ArrayList<>();
for(String value : values)
{
Serializable valueInPvsIdType = value;
try
{
valueInPvsIdType = ValueUtils.getValueAsFieldType(possibleValueSource.getIdType(), value);
}
catch(Exception e)
{
////////////////////////////
// leave as original type //
////////////////////////////
}
valuesToValueInPvsIdTypeMap.put(value, valueInPvsIdType);
idList.add(valueInPvsIdType);
valuesNotFound.add(valueInPvsIdType);
}
searchPossibleValueSourceInput.setIdList(idList);
searchPossibleValueSourceInput.setLimit(values.size());
LOG.debug("Searching possible value source by ids during bulk load mapping", logPair("pvsName", field.getPossibleValueSourceName()), logPair("noOfIds", idList.size()), logPair("firstId", () -> idList.get(0)));
SearchPossibleValueSourceOutput searchPossibleValueSourceOutput = new SearchPossibleValueSourceAction().execute(searchPossibleValueSourceInput);
SearchPossibleValueSourceOutput searchPossibleValueSourceOutput = idList.isEmpty() ? new SearchPossibleValueSourceOutput() : new SearchPossibleValueSourceAction().execute(searchPossibleValueSourceInput);
////////////////////////////////////////////////////////////////////////////////////////////////////
// for each possible value found, remove it from the set of ones not-found, and store it as a hit //
@ -202,7 +227,7 @@ public class BulkLoadValueMapper
{
searchPossibleValueSourceInput = new SearchPossibleValueSourceInput();
searchPossibleValueSourceInput.setPossibleValueSourceName(field.getPossibleValueSourceName());
ArrayList<String> labelList = new ArrayList<>(valuesNotFound);
List<String> labelList = valuesNotFound.stream().map(ValueUtils::getValueAsString).toList();
searchPossibleValueSourceInput.setLabelList(labelList);
searchPossibleValueSourceInput.setLimit(valuesNotFound.size());
@ -221,11 +246,14 @@ public class BulkLoadValueMapper
for(Map.Entry<String, List<QRecord>> entry : fieldPossibleValueToRecordMap.entrySet())
{
String value = entry.getKey();
Serializable valueInPvsIdType = valuesToValueInPvsIdTypeMap.get(entry.getKey());
String pvsIdAsString = ValueUtils.getValueAsString(valueInPvsIdType);
for(QRecord record : entry.getValue())
{
if(valuesFound.containsKey(value))
if(valuesFound.containsKey(pvsIdAsString))
{
record.setValue(field.getName(), valuesFound.get(value).getId());
record.setValue(field.getName(), valuesFound.get(pvsIdAsString).getId());
}
else
{

View File

@ -23,6 +23,7 @@ package com.kingsrook.qqq.backend.core.processes.implementations.bulk.insert.map
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.backend.core.BaseTest;
@ -36,6 +37,7 @@ import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
/*******************************************************************************
@ -113,6 +115,47 @@ class BulkLoadValueMapperTest extends BaseTest
/***************************************************************************
**
***************************************************************************/
void testPossibleValue(Serializable inputValue, Serializable expectedValue, boolean expectErrors) throws QException
{
QRecord inputRecord = new QRecord().withValue("homeStateId", inputValue);
BulkLoadValueMapper.valueMapping(List.of(inputRecord), new BulkInsertMapping(), QContext.getQInstance().getTable(TestUtils.TABLE_NAME_PERSON_MEMORY));
assertEquals(expectedValue, inputRecord.getValue("homeStateId"));
if(expectErrors)
{
assertThat(inputRecord.getErrors().get(0)).isInstanceOf(BulkLoadPossibleValueError.class);
}
else
{
assertThat(inputRecord.getErrors()).isNullOrEmpty();
}
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testPossibleValues() throws QException
{
testPossibleValue(1, 1, false);
testPossibleValue("1", 1, false);
testPossibleValue("1.0", 1, false);
testPossibleValue(new BigDecimal("1.0"), 1, false);
testPossibleValue("IL", 1, false);
testPossibleValue(512, 512, true); // an id, but not in the PVS
testPossibleValue("USA", "USA", true);
testPossibleValue(true, true, true);
testPossibleValue(new BigDecimal("4.7"), new BigDecimal("4.7"), true);
}
/***************************************************************************
**
***************************************************************************/