CE-1955 Add support for value-mapping on wide-mode associated fields

This commit is contained in:
2025-01-06 16:35:06 -06:00
parent e9fc5f81d2
commit f54b2b79db
4 changed files with 43 additions and 7 deletions

View File

@ -107,7 +107,12 @@ public class BulkInsertPrepareValueMappingStep implements BackendStep
runBackendStepInput.addValue("valueMappingFieldIndex", valueMappingFieldIndex); runBackendStepInput.addValue("valueMappingFieldIndex", valueMappingFieldIndex);
String fullFieldName = fieldNamesToDoValueMapping.get(valueMappingFieldIndex); String fullFieldName = fieldNamesToDoValueMapping.get(valueMappingFieldIndex);
TableAndField tableAndField = getTableAndField(runBackendStepInput.getValueString("tableName"), fullFieldName); String fieldNameWithoutWideSuffix = fullFieldName;
if(fieldNameWithoutWideSuffix.contains(","))
{
fieldNameWithoutWideSuffix = fieldNameWithoutWideSuffix.replaceFirst(",.*", "");
}
TableAndField tableAndField = getTableAndField(runBackendStepInput.getValueString("tableName"), fieldNameWithoutWideSuffix);
runBackendStepInput.addValue("valueMappingField", new QFrontendFieldMetaData(tableAndField.field())); runBackendStepInput.addValue("valueMappingField", new QFrontendFieldMetaData(tableAndField.field()));
runBackendStepInput.addValue("valueMappingFullFieldName", fullFieldName); runBackendStepInput.addValue("valueMappingFullFieldName", fullFieldName);
@ -213,6 +218,17 @@ public class BulkInsertPrepareValueMappingStep implements BackendStep
StorageInput storageInput = BulkInsertStepUtils.getStorageInputForTheFile(runBackendStepInput); StorageInput storageInput = BulkInsertStepUtils.getStorageInputForTheFile(runBackendStepInput);
BulkInsertMapping bulkInsertMapping = (BulkInsertMapping) runBackendStepInput.getValue("bulkInsertMapping"); BulkInsertMapping bulkInsertMapping = (BulkInsertMapping) runBackendStepInput.getValue("bulkInsertMapping");
List<Integer> wideAssociationIndexes = null;
if(fullFieldName.contains(","))
{
wideAssociationIndexes = new ArrayList<>();
String indexes = fullFieldName.substring(fullFieldName.lastIndexOf(",") + 1);
for(String index : indexes.split("\\."))
{
wideAssociationIndexes.add(Integer.parseInt(index));
}
}
String associationNameChain = null; String associationNameChain = null;
if(fullFieldName.contains(".")) if(fullFieldName.contains("."))
{ {
@ -227,7 +243,7 @@ public class BulkInsertPrepareValueMappingStep implements BackendStep
{ {
Set<String> values = new LinkedHashSet<>(); Set<String> values = new LinkedHashSet<>();
BulkLoadFileRow headerRow = bulkInsertMapping.getHasHeaderRow() ? fileToRowsInterface.next() : null; BulkLoadFileRow headerRow = bulkInsertMapping.getHasHeaderRow() ? fileToRowsInterface.next() : null;
Map<String, Integer> fieldIndexes = bulkInsertMapping.getFieldIndexes(table, associationNameChain, headerRow); Map<String, Integer> fieldIndexes = bulkInsertMapping.getFieldIndexes(table, associationNameChain, headerRow, wideAssociationIndexes);
int index = fieldIndexes.get(field.getName()); int index = fieldIndexes.get(field.getName());
while(fileToRowsInterface.hasNext()) while(fileToRowsInterface.hasNext())

View File

@ -94,12 +94,19 @@ public class BulkLoadValueMapper
QFieldMetaData field = table.getField(valueEntry.getKey()); QFieldMetaData field = table.getField(valueEntry.getKey());
Serializable value = valueEntry.getValue(); Serializable value = valueEntry.getValue();
String fieldNamePlusWideIndex = field.getName();
if(record.getBackendDetail("wideAssociationIndexes") != null)
{
ArrayList<Integer> indexes = (ArrayList<Integer>) record.getBackendDetail("wideAssociationIndexes");
fieldNamePlusWideIndex += "," + StringUtils.join(",", indexes);
}
/////////////////// ///////////////////
// value mappin' // // value mappin' //
/////////////////// ///////////////////
if(mappingForTable.containsKey(field.getName()) && value != null) if(mappingForTable.containsKey(fieldNamePlusWideIndex) && value != null)
{ {
Serializable mappedValue = mappingForTable.get(field.getName()).get(ValueUtils.getValueAsString(value)); Serializable mappedValue = mappingForTable.get(fieldNamePlusWideIndex).get(ValueUtils.getValueAsString(value));
if(mappedValue != null) if(mappedValue != null)
{ {
value = mappedValue; value = mappedValue;

View File

@ -28,6 +28,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import com.google.gson.reflect.TypeToken;
import com.kingsrook.qqq.backend.core.context.QContext; import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException; import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.data.QRecord; import com.kingsrook.qqq.backend.core.model.data.QRecord;
@ -185,6 +186,15 @@ public class WideRowsToRecordWithExplicitFieldNameSuffixIndexBasedMapping implem
} }
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// stash the wide-association indexes in records, so that in the value mapper, we know if if this is, e.g., ,1, or ,2.3, for value-mapping //
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(CollectionUtils.nullSafeHasContents(wideAssociationIndexes))
{
ArrayList<Integer> indexesArrayList = CollectionUtils.useOrWrap(wideAssociationIndexes, new TypeToken<>() {});
record.addBackendDetail("wideAssociationIndexes", indexesArrayList);
}
return record; return record;
} }

View File

@ -53,7 +53,7 @@ class WideRowsToRecordWithExplicitFieldNameSuffixIndexBasedMappingTest extends B
{ {
String csv = """ String csv = """
orderNo, Ship To, lastName, SKU 1, Quantity 1, SKU 2, Quantity 2, SKU 3, Quantity 3 orderNo, Ship To, lastName, SKU 1, Quantity 1, SKU 2, Quantity 2, SKU 3, Quantity 3
1, Homer, Simpson, DONUT, 12, BEER, 500, COUCH, 1 1, Homer, Simpson, DONUT, 12, BEER, 500, COUCH, two
2, Ned, Flanders, BIBLE, 7, LAWNMOWER, 1 2, Ned, Flanders, BIBLE, 7, LAWNMOWER, 1
"""; """;
@ -74,6 +74,9 @@ class WideRowsToRecordWithExplicitFieldNameSuffixIndexBasedMappingTest extends B
"orderLine.quantity,2", "Quantity 3" "orderLine.quantity,2", "Quantity 3"
)) ))
.withMappedAssociations(List.of("orderLine")) .withMappedAssociations(List.of("orderLine"))
.withFieldNameToValueMapping(Map.of(
"orderLine.quantity,2", Map.of("two", 2)
))
.withTableName(TestUtils.TABLE_NAME_ORDER) .withTableName(TestUtils.TABLE_NAME_ORDER)
.withLayout(BulkInsertMapping.Layout.WIDE) .withLayout(BulkInsertMapping.Layout.WIDE)
.withHasHeaderRow(true); .withHasHeaderRow(true);
@ -85,7 +88,7 @@ class WideRowsToRecordWithExplicitFieldNameSuffixIndexBasedMappingTest extends B
assertEquals(1, order.getValueInteger("orderNo")); assertEquals(1, order.getValueInteger("orderNo"));
assertEquals("Homer", order.getValueString("shipToName")); assertEquals("Homer", order.getValueString("shipToName"));
assertEquals(List.of("DONUT", "BEER", "COUCH"), getValues(order.getAssociatedRecords().get("orderLine"), "sku")); assertEquals(List.of("DONUT", "BEER", "COUCH"), getValues(order.getAssociatedRecords().get("orderLine"), "sku"));
assertEquals(List.of(12, 500, 1), getValues(order.getAssociatedRecords().get("orderLine"), "quantity")); assertEquals(List.of(12, 500, 2), getValues(order.getAssociatedRecords().get("orderLine"), "quantity"));
assertEquals(1, ((List<?>) order.getBackendDetail("fileRows")).size()); assertEquals(1, ((List<?>) order.getBackendDetail("fileRows")).size());
assertEquals("Row 2", order.getAssociatedRecords().get("orderLine").get(0).getBackendDetail("rowNos")); assertEquals("Row 2", order.getAssociatedRecords().get("orderLine").get(0).getBackendDetail("rowNos"));