Skip to content

Commit f54b2b7

Browse files
committed
CE-1955 Add support for value-mapping on wide-mode associated fields
1 parent e9fc5f8 commit f54b2b7

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/BulkInsertPrepareValueMappingStep.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,13 @@ public void run(RunBackendStepInput runBackendStepInput, RunBackendStepOutput ru
106106

107107
runBackendStepInput.addValue("valueMappingFieldIndex", valueMappingFieldIndex);
108108

109-
String fullFieldName = fieldNamesToDoValueMapping.get(valueMappingFieldIndex);
110-
TableAndField tableAndField = getTableAndField(runBackendStepInput.getValueString("tableName"), fullFieldName);
109+
String fullFieldName = fieldNamesToDoValueMapping.get(valueMappingFieldIndex);
110+
String fieldNameWithoutWideSuffix = fullFieldName;
111+
if(fieldNameWithoutWideSuffix.contains(","))
112+
{
113+
fieldNameWithoutWideSuffix = fieldNameWithoutWideSuffix.replaceFirst(",.*", "");
114+
}
115+
TableAndField tableAndField = getTableAndField(runBackendStepInput.getValueString("tableName"), fieldNameWithoutWideSuffix);
111116

112117
runBackendStepInput.addValue("valueMappingField", new QFrontendFieldMetaData(tableAndField.field()));
113118
runBackendStepInput.addValue("valueMappingFullFieldName", fullFieldName);
@@ -213,6 +218,17 @@ private ArrayList<Serializable> getValuesForField(QTableMetaData table, QFieldMe
213218
StorageInput storageInput = BulkInsertStepUtils.getStorageInputForTheFile(runBackendStepInput);
214219
BulkInsertMapping bulkInsertMapping = (BulkInsertMapping) runBackendStepInput.getValue("bulkInsertMapping");
215220

221+
List<Integer> wideAssociationIndexes = null;
222+
if(fullFieldName.contains(","))
223+
{
224+
wideAssociationIndexes = new ArrayList<>();
225+
String indexes = fullFieldName.substring(fullFieldName.lastIndexOf(",") + 1);
226+
for(String index : indexes.split("\\."))
227+
{
228+
wideAssociationIndexes.add(Integer.parseInt(index));
229+
}
230+
}
231+
216232
String associationNameChain = null;
217233
if(fullFieldName.contains("."))
218234
{
@@ -227,7 +243,7 @@ private ArrayList<Serializable> getValuesForField(QTableMetaData table, QFieldMe
227243
{
228244
Set<String> values = new LinkedHashSet<>();
229245
BulkLoadFileRow headerRow = bulkInsertMapping.getHasHeaderRow() ? fileToRowsInterface.next() : null;
230-
Map<String, Integer> fieldIndexes = bulkInsertMapping.getFieldIndexes(table, associationNameChain, headerRow);
246+
Map<String, Integer> fieldIndexes = bulkInsertMapping.getFieldIndexes(table, associationNameChain, headerRow, wideAssociationIndexes);
231247
int index = fieldIndexes.get(field.getName());
232248

233249
while(fileToRowsInterface.hasNext())

qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/mapping/BulkLoadValueMapper.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,19 @@ private static void valueMapping(List<QRecord> records, BulkInsertMapping mappin
9494
QFieldMetaData field = table.getField(valueEntry.getKey());
9595
Serializable value = valueEntry.getValue();
9696

97+
String fieldNamePlusWideIndex = field.getName();
98+
if(record.getBackendDetail("wideAssociationIndexes") != null)
99+
{
100+
ArrayList<Integer> indexes = (ArrayList<Integer>) record.getBackendDetail("wideAssociationIndexes");
101+
fieldNamePlusWideIndex += "," + StringUtils.join(",", indexes);
102+
}
103+
97104
///////////////////
98105
// value mappin' //
99106
///////////////////
100-
if(mappingForTable.containsKey(field.getName()) && value != null)
107+
if(mappingForTable.containsKey(fieldNamePlusWideIndex) && value != null)
101108
{
102-
Serializable mappedValue = mappingForTable.get(field.getName()).get(ValueUtils.getValueAsString(value));
109+
Serializable mappedValue = mappingForTable.get(fieldNamePlusWideIndex).get(ValueUtils.getValueAsString(value));
103110
if(mappedValue != null)
104111
{
105112
value = mappedValue;

qqq-backend-core/src/main/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/mapping/WideRowsToRecordWithExplicitFieldNameSuffixIndexBasedMapping.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.List;
2929
import java.util.Map;
3030
import java.util.Optional;
31+
import com.google.gson.reflect.TypeToken;
3132
import com.kingsrook.qqq.backend.core.context.QContext;
3233
import com.kingsrook.qqq.backend.core.exceptions.QException;
3334
import com.kingsrook.qqq.backend.core.model.data.QRecord;
@@ -185,6 +186,15 @@ private QRecord makeRecordFromRow(BulkInsertMapping mapping, QTableMetaData tabl
185186
}
186187
}
187188

189+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
190+
// 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 //
191+
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
192+
if(CollectionUtils.nullSafeHasContents(wideAssociationIndexes))
193+
{
194+
ArrayList<Integer> indexesArrayList = CollectionUtils.useOrWrap(wideAssociationIndexes, new TypeToken<>() {});
195+
record.addBackendDetail("wideAssociationIndexes", indexesArrayList);
196+
}
197+
188198
return record;
189199
}
190200

qqq-backend-core/src/test/java/com/kingsrook/qqq/backend/core/processes/implementations/bulk/insert/mapping/WideRowsToRecordWithExplicitFieldNameSuffixIndexBasedMappingTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void testOrderAndLinesWithoutDupes() throws QException
5353
{
5454
String csv = """
5555
orderNo, Ship To, lastName, SKU 1, Quantity 1, SKU 2, Quantity 2, SKU 3, Quantity 3
56-
1, Homer, Simpson, DONUT, 12, BEER, 500, COUCH, 1
56+
1, Homer, Simpson, DONUT, 12, BEER, 500, COUCH, two
5757
2, Ned, Flanders, BIBLE, 7, LAWNMOWER, 1
5858
""";
5959

@@ -74,6 +74,9 @@ void testOrderAndLinesWithoutDupes() throws QException
7474
"orderLine.quantity,2", "Quantity 3"
7575
))
7676
.withMappedAssociations(List.of("orderLine"))
77+
.withFieldNameToValueMapping(Map.of(
78+
"orderLine.quantity,2", Map.of("two", 2)
79+
))
7780
.withTableName(TestUtils.TABLE_NAME_ORDER)
7881
.withLayout(BulkInsertMapping.Layout.WIDE)
7982
.withHasHeaderRow(true);
@@ -85,7 +88,7 @@ void testOrderAndLinesWithoutDupes() throws QException
8588
assertEquals(1, order.getValueInteger("orderNo"));
8689
assertEquals("Homer", order.getValueString("shipToName"));
8790
assertEquals(List.of("DONUT", "BEER", "COUCH"), getValues(order.getAssociatedRecords().get("orderLine"), "sku"));
88-
assertEquals(List.of(12, 500, 1), getValues(order.getAssociatedRecords().get("orderLine"), "quantity"));
91+
assertEquals(List.of(12, 500, 2), getValues(order.getAssociatedRecords().get("orderLine"), "quantity"));
8992
assertEquals(1, ((List<?>) order.getBackendDetail("fileRows")).size());
9093
assertEquals("Row 2", order.getAssociatedRecords().get("orderLine").get(0).getBackendDetail("rowNos"));
9194

0 commit comments

Comments
 (0)