sprint-14: minor updates to allow flexibility when extending loadviainsertorupdatestep

This commit is contained in:
Tim Chamberlain
2022-10-26 11:56:17 -05:00
parent 033dbeb76c
commit 77927fd318
2 changed files with 32 additions and 17 deletions

View File

@ -37,6 +37,7 @@ import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateOutput; import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateOutput;
import com.kingsrook.qqq.backend.core.model.data.QRecord; import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData; import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
/******************************************************************************* /*******************************************************************************
@ -49,6 +50,9 @@ public class LoadViaInsertOrUpdateStep extends AbstractLoadStep
{ {
public static final String FIELD_DESTINATION_TABLE = "destinationTable"; public static final String FIELD_DESTINATION_TABLE = "destinationTable";
protected List<QRecord> recordsToInsert = null;
protected List<QRecord> recordsToUpdate = null;
/******************************************************************************* /*******************************************************************************
@ -59,21 +63,9 @@ public class LoadViaInsertOrUpdateStep extends AbstractLoadStep
public void run(RunBackendStepInput runBackendStepInput, RunBackendStepOutput runBackendStepOutput) throws QException public void run(RunBackendStepInput runBackendStepInput, RunBackendStepOutput runBackendStepOutput) throws QException
{ {
QTableMetaData tableMetaData = runBackendStepInput.getInstance().getTable(runBackendStepInput.getValueString(FIELD_DESTINATION_TABLE)); QTableMetaData tableMetaData = runBackendStepInput.getInstance().getTable(runBackendStepInput.getValueString(FIELD_DESTINATION_TABLE));
List<QRecord> recordsToInsert = new ArrayList<>(); evaluateRecords(runBackendStepInput);
List<QRecord> recordsToUpdate = new ArrayList<>();
for(QRecord record : runBackendStepInput.getRecords())
{
if(record.getValue(tableMetaData.getPrimaryKeyField()) == null)
{
recordsToInsert.add(record);
}
else
{
recordsToUpdate.add(record);
}
}
if(!recordsToInsert.isEmpty()) if(CollectionUtils.nullSafeHasContents(recordsToInsert))
{ {
InsertInput insertInput = new InsertInput(runBackendStepInput.getInstance()); InsertInput insertInput = new InsertInput(runBackendStepInput.getInstance());
insertInput.setSession(runBackendStepInput.getSession()); insertInput.setSession(runBackendStepInput.getSession());
@ -84,7 +76,7 @@ public class LoadViaInsertOrUpdateStep extends AbstractLoadStep
runBackendStepOutput.getRecords().addAll(insertOutput.getRecords()); runBackendStepOutput.getRecords().addAll(insertOutput.getRecords());
} }
if(!recordsToUpdate.isEmpty()) if(CollectionUtils.nullSafeHasContents(recordsToUpdate))
{ {
UpdateInput updateInput = new UpdateInput(runBackendStepInput.getInstance()); UpdateInput updateInput = new UpdateInput(runBackendStepInput.getInstance());
updateInput.setSession(runBackendStepInput.getSession()); updateInput.setSession(runBackendStepInput.getSession());
@ -110,4 +102,27 @@ public class LoadViaInsertOrUpdateStep extends AbstractLoadStep
return (Optional.of(new InsertAction().openTransaction(insertInput))); return (Optional.of(new InsertAction().openTransaction(insertInput)));
} }
/*******************************************************************************
**
*******************************************************************************/
protected void evaluateRecords(RunBackendStepInput runBackendStepInput) throws QException
{
QTableMetaData tableMetaData = runBackendStepInput.getInstance().getTable(runBackendStepInput.getValueString(FIELD_DESTINATION_TABLE));
recordsToInsert = new ArrayList<>();
recordsToUpdate = new ArrayList<>();
for(QRecord record : runBackendStepInput.getRecords())
{
if(record.getValue(tableMetaData.getPrimaryKeyField()) == null)
{
recordsToInsert.add(record);
}
else
{
recordsToUpdate.add(record);
}
}
}
} }

View File

@ -374,7 +374,7 @@ public class BaseAPIActionUtil
/******************************************************************************* /*******************************************************************************
** **
*******************************************************************************/ *******************************************************************************/
private JSONObject getJsonObject(HttpResponse response) throws IOException protected JSONObject getJsonObject(HttpResponse response) throws IOException
{ {
int statusCode = response.getStatusLine().getStatusCode(); int statusCode = response.getStatusLine().getStatusCode();
LOG.debug(statusCode); LOG.debug(statusCode);