mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 21:20:45 +00:00
Initial support for associated records (implemented insert, delete).
Include "api" on audit.
This commit is contained in:
@ -54,6 +54,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.processes.QProcessMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.security.RecordSecurityLock;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import com.kingsrook.qqq.backend.core.model.session.QSession;
|
||||
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
|
||||
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
|
||||
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
|
||||
@ -106,6 +107,13 @@ public class DMLAuditAction extends AbstractQActionFunction<DMLAuditInput, DMLAu
|
||||
}
|
||||
}
|
||||
|
||||
QSession qSession = QContext.getQSession();
|
||||
String apiVersion = qSession.getValue("apiVersion");
|
||||
if(apiVersion != null)
|
||||
{
|
||||
contextSuffix += (" via API Version: " + apiVersion);
|
||||
}
|
||||
|
||||
AuditInput auditInput = new AuditInput();
|
||||
if(auditLevel.equals(AuditLevel.RECORD) || (auditLevel.equals(AuditLevel.FIELD) && !dmlType.supportsFields))
|
||||
{
|
||||
@ -125,7 +133,7 @@ public class DMLAuditAction extends AbstractQActionFunction<DMLAuditInput, DMLAu
|
||||
///////////////////////////////////////////////////////////////////
|
||||
// do many audits, all with field level details, for FIELD level //
|
||||
///////////////////////////////////////////////////////////////////
|
||||
QPossibleValueTranslator qPossibleValueTranslator = new QPossibleValueTranslator(QContext.getQInstance(), QContext.getQSession());
|
||||
QPossibleValueTranslator qPossibleValueTranslator = new QPossibleValueTranslator(QContext.getQInstance(), qSession);
|
||||
qPossibleValueTranslator.translatePossibleValuesInRecords(table, CollectionUtils.mergeLists(recordList, oldRecordList));
|
||||
|
||||
//////////////////////////////////////////
|
||||
|
@ -28,6 +28,7 @@ import java.util.List;
|
||||
import com.kingsrook.qqq.backend.core.actions.ActionHelper;
|
||||
import com.kingsrook.qqq.backend.core.actions.audits.DMLAuditAction;
|
||||
import com.kingsrook.qqq.backend.core.actions.interfaces.DeleteInterface;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.audits.DMLAuditInput;
|
||||
@ -40,6 +41,9 @@ 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.metadata.audits.AuditLevel;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.joins.QJoinMetaData;
|
||||
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.modules.backend.QBackendModuleDispatcher;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleInterface;
|
||||
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
|
||||
@ -91,6 +95,9 @@ public class DeleteAction
|
||||
List<QRecord> recordListForAudit = getRecordListForAuditIfNeeded(deleteInput);
|
||||
|
||||
DeleteOutput deleteOutput = deleteInterface.execute(deleteInput);
|
||||
|
||||
manageAssociations(deleteInput);
|
||||
|
||||
// todo post-customization - can do whatever w/ the result if you want
|
||||
|
||||
new DMLAuditAction().execute(new DMLAuditInput().withTableActionInput(deleteInput).withRecordList(recordListForAudit));
|
||||
@ -100,6 +107,46 @@ public class DeleteAction
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private void manageAssociations(DeleteInput deleteInput) throws QException
|
||||
{
|
||||
QTableMetaData table = deleteInput.getTable();
|
||||
for(Association association : CollectionUtils.nonNullList(table.getAssociations()))
|
||||
{
|
||||
// e.g., order -> orderLine
|
||||
QJoinMetaData join = QContext.getQInstance().getJoin(association.getJoinName()); // todo ... ever need to flip?
|
||||
// just assume this, at least for now... if(BooleanUtils.isTrue(association.getDoInserts()))
|
||||
|
||||
QQueryFilter filter = new QQueryFilter();
|
||||
|
||||
if(join.getJoinOns().size() == 1 && join.getJoinOns().get(0).getLeftField().equals(table.getPrimaryKeyField()))
|
||||
{
|
||||
filter.addCriteria(new QFilterCriteria(join.getJoinOns().get(0).getRightField(), QCriteriaOperator.IN, deleteInput.getPrimaryKeys()));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw (new QException("Join of this type is not supported for an associated delete at this time..."));
|
||||
}
|
||||
|
||||
QTableMetaData associatedTable = QContext.getQInstance().getTable(association.getAssociatedTableName());
|
||||
|
||||
QueryInput queryInput = new QueryInput();
|
||||
queryInput.setTableName(association.getAssociatedTableName());
|
||||
queryInput.setFilter(filter);
|
||||
QueryOutput queryOutput = new QueryAction().execute(queryInput);
|
||||
List<Serializable> associatedKeys = queryOutput.getRecords().stream().map(r -> r.getValue(associatedTable.getPrimaryKeyField())).toList();
|
||||
|
||||
DeleteInput nextLevelDeleteInput = new DeleteInput();
|
||||
nextLevelDeleteInput.setTableName(association.getAssociatedTableName());
|
||||
nextLevelDeleteInput.setPrimaryKeys(associatedKeys);
|
||||
DeleteOutput nextLevelDeleteOutput = new DeleteAction().execute(nextLevelDeleteInput);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
@ -23,6 +23,7 @@ package com.kingsrook.qqq.backend.core.actions.tables;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
@ -40,12 +41,16 @@ import com.kingsrook.qqq.backend.core.actions.customizers.QCodeLoader;
|
||||
import com.kingsrook.qqq.backend.core.actions.customizers.TableCustomizers;
|
||||
import com.kingsrook.qqq.backend.core.actions.tables.helpers.UniqueKeyHelper;
|
||||
import com.kingsrook.qqq.backend.core.actions.values.ValueBehaviorApplier;
|
||||
import com.kingsrook.qqq.backend.core.context.QContext;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.audits.DMLAuditInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertOutput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.joins.JoinOn;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.joins.QJoinMetaData;
|
||||
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.model.metadata.tables.UniqueKey;
|
||||
import com.kingsrook.qqq.backend.core.modules.backend.QBackendModuleDispatcher;
|
||||
@ -75,15 +80,16 @@ public class InsertAction extends AbstractQActionFunction<InsertInput, InsertOut
|
||||
Optional<AbstractPostInsertCustomizer> postInsertCustomizer = QCodeLoader.getTableCustomizer(AbstractPostInsertCustomizer.class, table, TableCustomizers.POST_INSERT_RECORD.getRole());
|
||||
setAutomationStatusField(insertInput);
|
||||
|
||||
ValueBehaviorApplier.applyFieldBehaviors(insertInput.getInstance(), table, insertInput.getRecords());
|
||||
// todo - need to handle records with errors coming out of here...
|
||||
|
||||
QBackendModuleInterface qModule = getBackendModuleInterface(insertInput);
|
||||
// todo pre-customization - just get to modify the request?
|
||||
|
||||
ValueBehaviorApplier.applyFieldBehaviors(insertInput.getInstance(), table, insertInput.getRecords());
|
||||
setErrorsIfUniqueKeyErrors(insertInput, table);
|
||||
|
||||
InsertOutput insertOutput = qModule.getInsertInterface().execute(insertInput);
|
||||
|
||||
manageAssociations(table, insertOutput.getRecords());
|
||||
|
||||
// todo post-customization - can do whatever w/ the result if you want
|
||||
|
||||
new DMLAuditAction().execute(new DMLAuditInput().withTableActionInput(insertInput).withRecordList(insertOutput.getRecords()));
|
||||
@ -98,6 +104,43 @@ public class InsertAction extends AbstractQActionFunction<InsertInput, InsertOut
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private void manageAssociations(QTableMetaData table, List<QRecord> insertedRecords) throws QException
|
||||
{
|
||||
for(Association association : CollectionUtils.nonNullList(table.getAssociations()))
|
||||
{
|
||||
// e.g., order -> orderLine
|
||||
QJoinMetaData join = QContext.getQInstance().getJoin(association.getJoinName()); // todo ... ever need to flip?
|
||||
// just assume this, at least for now... if(BooleanUtils.isTrue(association.getDoInserts()))
|
||||
|
||||
List<QRecord> nextLevelInserts = new ArrayList<>();
|
||||
for(QRecord record : insertedRecords)
|
||||
{
|
||||
if(record.getAssociatedRecords() != null && record.getAssociatedRecords().containsKey(association.getName()))
|
||||
{
|
||||
for(QRecord associatedRecord : CollectionUtils.nonNullList(record.getAssociatedRecords().get(association.getName())))
|
||||
{
|
||||
for(JoinOn joinOn : join.getJoinOns())
|
||||
{
|
||||
associatedRecord.setValue(joinOn.getRightField(), record.getValue(joinOn.getLeftField()));
|
||||
}
|
||||
nextLevelInserts.add(associatedRecord);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
InsertInput nextLevelInsertInput = new InsertInput();
|
||||
nextLevelInsertInput.setTableName(association.getAssociatedTableName());
|
||||
nextLevelInsertInput.setRecords(nextLevelInserts);
|
||||
InsertOutput nextLevelInsertOutput = new InsertAction().execute(nextLevelInsertInput);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
@ -28,6 +28,7 @@ import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -65,6 +66,8 @@ public class QRecord implements Serializable
|
||||
private Map<String, Serializable> backendDetails = new LinkedHashMap<>();
|
||||
private List<String> errors = new ArrayList<>();
|
||||
|
||||
private Map<String, List<QRecord>> associatedRecords = new HashMap<>();
|
||||
|
||||
public static final String BACKEND_DETAILS_TYPE_JSON_SOURCE_OBJECT = "jsonSourceObject";
|
||||
|
||||
|
||||
@ -102,6 +105,7 @@ public class QRecord implements Serializable
|
||||
this.displayValues = doDeepCopy(record.displayValues);
|
||||
this.backendDetails = doDeepCopy(record.backendDetails);
|
||||
this.errors = doDeepCopy(record.errors);
|
||||
this.associatedRecords = doDeepCopy(record.associatedRecords);
|
||||
}
|
||||
|
||||
|
||||
@ -575,4 +579,66 @@ public class QRecord implements Serializable
|
||||
return (QRecordEntity.fromQRecord(c, this));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for associatedRecords
|
||||
*******************************************************************************/
|
||||
public Map<String, List<QRecord>> getAssociatedRecords()
|
||||
{
|
||||
return (this.associatedRecords);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for associatedRecords
|
||||
*******************************************************************************/
|
||||
public void setAssociatedRecords(Map<String, List<QRecord>> associatedRecords)
|
||||
{
|
||||
this.associatedRecords = associatedRecords;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for associatedRecords
|
||||
*******************************************************************************/
|
||||
public QRecord withAssociatedRecords(Map<String, List<QRecord>> associatedRecords)
|
||||
{
|
||||
this.associatedRecords = associatedRecords;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for associatedRecords
|
||||
*******************************************************************************/
|
||||
public QRecord withAssociatedRecords(String name, List<QRecord> associatedRecords)
|
||||
{
|
||||
if(this.associatedRecords == null)
|
||||
{
|
||||
this.associatedRecords = new HashMap<>();
|
||||
}
|
||||
this.associatedRecords.put(name, associatedRecords);
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for associatedRecord
|
||||
*******************************************************************************/
|
||||
public QRecord withAssociatedRecord(String name, QRecord associatedRecord)
|
||||
{
|
||||
if(this.associatedRecords == null)
|
||||
{
|
||||
this.associatedRecords = new HashMap<>();
|
||||
}
|
||||
this.associatedRecords.putIfAbsent(name, new ArrayList<>());
|
||||
this.associatedRecords.get(name).add(associatedRecord);
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,128 @@
|
||||
/*
|
||||
* QQQ - Low-code Application Framework for Engineers.
|
||||
* Copyright (C) 2021-2023. Kingsrook, LLC
|
||||
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
|
||||
* contact@kingsrook.com
|
||||
* https://github.com/Kingsrook/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.kingsrook.qqq.backend.core.model.metadata.tables;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** definition of a qqq table that is "associated" with another table, e.g.,
|
||||
** managed along with it - such as child-records under a parent record.
|
||||
*******************************************************************************/
|
||||
public class Association
|
||||
{
|
||||
private String name;
|
||||
private String associatedTableName;
|
||||
private String joinName;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for name
|
||||
*******************************************************************************/
|
||||
public String getName()
|
||||
{
|
||||
return (this.name);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for name
|
||||
*******************************************************************************/
|
||||
public void setName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for name
|
||||
*******************************************************************************/
|
||||
public Association withName(String name)
|
||||
{
|
||||
this.name = name;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for associatedTableName
|
||||
*******************************************************************************/
|
||||
public String getAssociatedTableName()
|
||||
{
|
||||
return (this.associatedTableName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for associatedTableName
|
||||
*******************************************************************************/
|
||||
public void setAssociatedTableName(String associatedTableName)
|
||||
{
|
||||
this.associatedTableName = associatedTableName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for associatedTableName
|
||||
*******************************************************************************/
|
||||
public Association withAssociatedTableName(String associatedTableName)
|
||||
{
|
||||
this.associatedTableName = associatedTableName;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for joinName
|
||||
*******************************************************************************/
|
||||
public String getJoinName()
|
||||
{
|
||||
return (this.joinName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for joinName
|
||||
*******************************************************************************/
|
||||
public void setJoinName(String joinName)
|
||||
{
|
||||
this.joinName = joinName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for joinName
|
||||
*******************************************************************************/
|
||||
public Association withJoinName(String joinName)
|
||||
{
|
||||
this.joinName = joinName;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
@ -72,6 +72,7 @@ public class QTableMetaData implements QAppChildMetaData, Serializable, MetaData
|
||||
|
||||
private Map<String, QFieldMetaData> fields;
|
||||
private List<UniqueKey> uniqueKeys;
|
||||
private List<Association> associations;
|
||||
|
||||
private List<RecordSecurityLock> recordSecurityLocks;
|
||||
private QPermissionRules permissionRules;
|
||||
@ -1237,4 +1238,50 @@ public class QTableMetaData implements QAppChildMetaData, Serializable, MetaData
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for associations
|
||||
*******************************************************************************/
|
||||
public List<Association> getAssociations()
|
||||
{
|
||||
return (this.associations);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for associations
|
||||
*******************************************************************************/
|
||||
public void setAssociations(List<Association> associations)
|
||||
{
|
||||
this.associations = associations;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for associations
|
||||
*******************************************************************************/
|
||||
public QTableMetaData withAssociations(List<Association> associations)
|
||||
{
|
||||
this.associations = associations;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for associations
|
||||
*******************************************************************************/
|
||||
public QTableMetaData withAssociation(Association association)
|
||||
{
|
||||
if(this.associations == null)
|
||||
{
|
||||
this.associations = new ArrayList<>();
|
||||
}
|
||||
this.associations.add(association);
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -31,7 +31,10 @@ import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteOutput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterOrderBy;
|
||||
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.audits.AuditsMetaDataProvider;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
|
||||
@ -149,4 +152,145 @@ class DeleteActionTest extends BaseTest
|
||||
assertTrue(audits.stream().allMatch(r -> r.getValueString("message").equals("Record was Deleted")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testAssociatedDeletes() throws QException
|
||||
{
|
||||
{
|
||||
InsertInput insertInput = new InsertInput();
|
||||
insertInput.setTableName(TestUtils.TABLE_NAME_ORDER);
|
||||
insertInput.setRecords(List.of(
|
||||
new QRecord().withValue("id", 1),
|
||||
new QRecord().withValue("id", 2),
|
||||
new QRecord().withValue("id", 3)
|
||||
));
|
||||
new InsertAction().execute(insertInput);
|
||||
}
|
||||
|
||||
{
|
||||
InsertInput insertInput = new InsertInput();
|
||||
insertInput.setTableName(TestUtils.TABLE_NAME_ORDER_EXTRINSIC);
|
||||
insertInput.setRecords(List.of(
|
||||
new QRecord().withValue("id", 1).withValue("orderId", 1),
|
||||
new QRecord().withValue("id", 2).withValue("orderId", 1),
|
||||
new QRecord().withValue("id", 3).withValue("orderId", 1),
|
||||
new QRecord().withValue("id", 4).withValue("orderId", 1),
|
||||
new QRecord().withValue("id", 5).withValue("orderId", 3),
|
||||
new QRecord().withValue("id", 6).withValue("orderId", 3),
|
||||
new QRecord().withValue("id", 7).withValue("orderId", 3)
|
||||
));
|
||||
new InsertAction().execute(insertInput);
|
||||
}
|
||||
|
||||
{
|
||||
InsertInput insertInput = new InsertInput();
|
||||
insertInput.setTableName(TestUtils.TABLE_NAME_LINE_ITEM);
|
||||
insertInput.setRecords(List.of(
|
||||
new QRecord().withValue("id", 1).withValue("orderId", 1),
|
||||
new QRecord().withValue("id", 2).withValue("orderId", 1),
|
||||
new QRecord().withValue("id", 3).withValue("orderId", 2),
|
||||
new QRecord().withValue("id", 4).withValue("orderId", 3),
|
||||
new QRecord().withValue("id", 5).withValue("orderId", 3),
|
||||
new QRecord().withValue("id", 6).withValue("orderId", 3)
|
||||
));
|
||||
new InsertAction().execute(insertInput);
|
||||
}
|
||||
|
||||
{
|
||||
InsertInput insertInput = new InsertInput();
|
||||
insertInput.setTableName(TestUtils.TABLE_NAME_LINE_ITEM_EXTRINSIC);
|
||||
insertInput.setRecords(List.of(
|
||||
new QRecord().withValue("id", 1).withValue("lineItemId", 1), // orderId: 1
|
||||
new QRecord().withValue("id", 2).withValue("lineItemId", 1), // orderId: 1
|
||||
new QRecord().withValue("id", 3).withValue("lineItemId", 2), // orderId: 1
|
||||
new QRecord().withValue("id", 4).withValue("lineItemId", 2), // orderId: 1
|
||||
new QRecord().withValue("id", 5).withValue("lineItemId", 3), // orderId: 2
|
||||
new QRecord().withValue("id", 6).withValue("lineItemId", 3), // orderId: 2
|
||||
new QRecord().withValue("id", 7).withValue("lineItemId", 4), // orderId: 3
|
||||
new QRecord().withValue("id", 8).withValue("lineItemId", 5), // orderId: 3
|
||||
new QRecord().withValue("id", 9).withValue("lineItemId", 6) // orderId: 3
|
||||
));
|
||||
new InsertAction().execute(insertInput);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////
|
||||
// assert about how many things we originally inserted //
|
||||
/////////////////////////////////////////////////////////
|
||||
assertEquals(3, queryTable(TestUtils.TABLE_NAME_ORDER).size());
|
||||
assertEquals(7, queryTable(TestUtils.TABLE_NAME_ORDER_EXTRINSIC).size());
|
||||
assertEquals(6, queryTable(TestUtils.TABLE_NAME_LINE_ITEM).size());
|
||||
assertEquals(9, queryTable(TestUtils.TABLE_NAME_LINE_ITEM_EXTRINSIC).size());
|
||||
|
||||
////////////////////////////////
|
||||
// delete (cascading) order 1 //
|
||||
////////////////////////////////
|
||||
DeleteInput deleteInput = new DeleteInput();
|
||||
deleteInput.setTableName(TestUtils.TABLE_NAME_ORDER);
|
||||
deleteInput.setPrimaryKeys(List.of(1));
|
||||
new DeleteAction().execute(deleteInput);
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// assert that the associated data were deleted //
|
||||
//////////////////////////////////////////////////
|
||||
assertEquals(2, queryTable(TestUtils.TABLE_NAME_ORDER).size());
|
||||
assertEquals(3, queryTable(TestUtils.TABLE_NAME_ORDER_EXTRINSIC).size());
|
||||
assertEquals(4, queryTable(TestUtils.TABLE_NAME_LINE_ITEM).size());
|
||||
assertEquals(5, queryTable(TestUtils.TABLE_NAME_LINE_ITEM_EXTRINSIC).size());
|
||||
|
||||
////////////////////
|
||||
// delete order 2 //
|
||||
////////////////////
|
||||
deleteInput.setPrimaryKeys(List.of(2));
|
||||
new DeleteAction().execute(deleteInput);
|
||||
|
||||
//////////////////////////////////////////////////
|
||||
// assert that the associated data were deleted //
|
||||
//////////////////////////////////////////////////
|
||||
assertEquals(1, queryTable(TestUtils.TABLE_NAME_ORDER).size());
|
||||
assertEquals(3, queryTable(TestUtils.TABLE_NAME_ORDER_EXTRINSIC).size());
|
||||
assertEquals(3, queryTable(TestUtils.TABLE_NAME_LINE_ITEM).size());
|
||||
assertEquals(3, queryTable(TestUtils.TABLE_NAME_LINE_ITEM_EXTRINSIC).size());
|
||||
|
||||
////////////////////
|
||||
// delete order 3 //
|
||||
////////////////////
|
||||
deleteInput.setPrimaryKeys(List.of(3));
|
||||
new DeleteAction().execute(deleteInput);
|
||||
|
||||
///////////////////////////////
|
||||
// everything is deleted now //
|
||||
///////////////////////////////
|
||||
assertEquals(0, queryTable(TestUtils.TABLE_NAME_ORDER).size());
|
||||
assertEquals(0, queryTable(TestUtils.TABLE_NAME_ORDER_EXTRINSIC).size());
|
||||
assertEquals(0, queryTable(TestUtils.TABLE_NAME_LINE_ITEM).size());
|
||||
assertEquals(0, queryTable(TestUtils.TABLE_NAME_LINE_ITEM_EXTRINSIC).size());
|
||||
|
||||
////////////////////////////////////////////////
|
||||
// make sure no errors if we try more deletes //
|
||||
////////////////////////////////////////////////
|
||||
deleteInput.setPrimaryKeys(List.of(3));
|
||||
new DeleteAction().execute(deleteInput);
|
||||
|
||||
deleteInput.setPrimaryKeys(List.of(1, 2, 3, 4));
|
||||
new DeleteAction().execute(deleteInput);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private static List<QRecord> queryTable(String tableName) throws QException
|
||||
{
|
||||
QueryInput queryInput = new QueryInput();
|
||||
queryInput.setTableName(tableName);
|
||||
queryInput.setFilter(new QQueryFilter().withOrderBy(new QFilterOrderBy("id")));
|
||||
QueryOutput queryOutput = new QueryAction().execute(queryInput);
|
||||
return (queryOutput.getRecords());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -209,4 +209,78 @@ class InsertActionTest extends BaseTest
|
||||
assertTrue(CollectionUtils.nullSafeIsEmpty(insertOutput.getRecords().get(1).getErrors()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testInsertAssociations() throws QException
|
||||
{
|
||||
QInstance qInstance = QContext.getQInstance();
|
||||
QContext.getQSession().withSecurityKeyValue("storeId", 1);
|
||||
|
||||
InsertInput insertInput = new InsertInput();
|
||||
insertInput.setTableName(TestUtils.TABLE_NAME_ORDER);
|
||||
insertInput.setRecords(List.of(
|
||||
new QRecord().withValue("storeId", 1).withValue("orderNo", "ORD123")
|
||||
|
||||
.withAssociatedRecord("orderLine", new QRecord().withValue("sku", "BASIC1").withValue("quantity", 1)
|
||||
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "LINE-EXT-1.1").withValue("value", "LINE-VAL-1")))
|
||||
|
||||
.withAssociatedRecord("orderLine", new QRecord().withValue("sku", "BASIC2").withValue("quantity", 2)
|
||||
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "LINE-EXT-2.1").withValue("value", "LINE-VAL-2"))
|
||||
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "LINE-EXT-2.2").withValue("value", "LINE-VAL-3")))
|
||||
|
||||
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "MY-FIELD-1").withValue("value", "MY-VALUE-1"))
|
||||
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "MY-FIELD-2").withValue("value", "MY-VALUE-2"))
|
||||
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "MY-FIELD-3").withValue("value", "MY-VALUE-3")),
|
||||
|
||||
new QRecord().withValue("storeId", 1).withValue("orderNo", "ORD124")
|
||||
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "YOUR-FIELD-1").withValue("value", "YOUR-VALUE-1"))
|
||||
));
|
||||
new InsertAction().execute(insertInput);
|
||||
|
||||
List<QRecord> orders = TestUtils.queryTable(qInstance, TestUtils.TABLE_NAME_ORDER);
|
||||
assertEquals(2, orders.size());
|
||||
assertEquals(1, orders.get(0).getValueInteger("id"));
|
||||
assertEquals(2, orders.get(1).getValueInteger("id"));
|
||||
assertEquals("ORD123", orders.get(0).getValueString("orderNo"));
|
||||
assertEquals("ORD124", orders.get(1).getValueString("orderNo"));
|
||||
|
||||
List<QRecord> orderLines = TestUtils.queryTable(qInstance, TestUtils.TABLE_NAME_LINE_ITEM);
|
||||
assertEquals(2, orderLines.size());
|
||||
assertEquals(1, orderLines.get(0).getValueInteger("orderId"));
|
||||
assertEquals(1, orderLines.get(1).getValueInteger("orderId"));
|
||||
assertEquals("BASIC1", orderLines.get(0).getValueString("sku"));
|
||||
assertEquals("BASIC2", orderLines.get(1).getValueString("sku"));
|
||||
|
||||
List<QRecord> orderExtrinsics = TestUtils.queryTable(qInstance, TestUtils.TABLE_NAME_ORDER_EXTRINSIC);
|
||||
assertEquals(4, orderExtrinsics.size());
|
||||
assertEquals(1, orderExtrinsics.get(0).getValueInteger("orderId"));
|
||||
assertEquals(1, orderExtrinsics.get(1).getValueInteger("orderId"));
|
||||
assertEquals(1, orderExtrinsics.get(2).getValueInteger("orderId"));
|
||||
assertEquals(2, orderExtrinsics.get(3).getValueInteger("orderId"));
|
||||
assertEquals("MY-FIELD-1", orderExtrinsics.get(0).getValueString("key"));
|
||||
assertEquals("MY-FIELD-2", orderExtrinsics.get(1).getValueString("key"));
|
||||
assertEquals("MY-FIELD-3", orderExtrinsics.get(2).getValueString("key"));
|
||||
assertEquals("YOUR-FIELD-1", orderExtrinsics.get(3).getValueString("key"));
|
||||
assertEquals("MY-VALUE-1", orderExtrinsics.get(0).getValueString("value"));
|
||||
assertEquals("MY-VALUE-2", orderExtrinsics.get(1).getValueString("value"));
|
||||
assertEquals("MY-VALUE-3", orderExtrinsics.get(2).getValueString("value"));
|
||||
assertEquals("YOUR-VALUE-1", orderExtrinsics.get(3).getValueString("value"));
|
||||
|
||||
List<QRecord> lineItemExtrinsics = TestUtils.queryTable(qInstance, TestUtils.TABLE_NAME_LINE_ITEM_EXTRINSIC);
|
||||
assertEquals(3, lineItemExtrinsics.size());
|
||||
assertEquals(1, lineItemExtrinsics.get(0).getValueInteger("lineItemId"));
|
||||
assertEquals(2, lineItemExtrinsics.get(1).getValueInteger("lineItemId"));
|
||||
assertEquals(2, lineItemExtrinsics.get(2).getValueInteger("lineItemId"));
|
||||
assertEquals("LINE-EXT-1.1", lineItemExtrinsics.get(0).getValueString("key"));
|
||||
assertEquals("LINE-EXT-2.1", lineItemExtrinsics.get(1).getValueString("key"));
|
||||
assertEquals("LINE-EXT-2.2", lineItemExtrinsics.get(2).getValueString("key"));
|
||||
assertEquals("LINE-VAL-1", lineItemExtrinsics.get(0).getValueString("value"));
|
||||
assertEquals("LINE-VAL-2", lineItemExtrinsics.get(1).getValueString("value"));
|
||||
assertEquals("LINE-VAL-3", lineItemExtrinsics.get(2).getValueString("value"));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -91,6 +91,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.reporting.ReportType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.security.FieldSecurityLock;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.security.QSecurityKeyType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.security.RecordSecurityLock;
|
||||
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.model.metadata.tables.UniqueKey;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.automation.AutomationStatusTracking;
|
||||
@ -126,10 +127,12 @@ public class TestUtils
|
||||
public static final String APP_NAME_PEOPLE = "peopleApp";
|
||||
public static final String APP_NAME_MISCELLANEOUS = "miscellaneous";
|
||||
|
||||
public static final String TABLE_NAME_PERSON = "person";
|
||||
public static final String TABLE_NAME_SHAPE = "shape";
|
||||
public static final String TABLE_NAME_ORDER = "order";
|
||||
public static final String TABLE_NAME_LINE_ITEM = "orderLine";
|
||||
public static final String TABLE_NAME_PERSON = "person";
|
||||
public static final String TABLE_NAME_SHAPE = "shape";
|
||||
public static final String TABLE_NAME_ORDER = "order";
|
||||
public static final String TABLE_NAME_LINE_ITEM = "orderLine";
|
||||
public static final String TABLE_NAME_LINE_ITEM_EXTRINSIC = "orderLineExtrinsic";
|
||||
public static final String TABLE_NAME_ORDER_EXTRINSIC = "orderExtrinsic";
|
||||
|
||||
public static final String PROCESS_NAME_GREET_PEOPLE = "greet";
|
||||
public static final String PROCESS_NAME_GREET_PEOPLE_INTERACTIVE = "greetInteractive";
|
||||
@ -185,8 +188,12 @@ public class TestUtils
|
||||
qInstance.addTable(defineTableBasepull());
|
||||
qInstance.addTable(defineTableOrder());
|
||||
qInstance.addTable(defineTableLineItem());
|
||||
qInstance.addTable(defineTableLineItemExtrinsic());
|
||||
qInstance.addTable(defineTableOrderExtrinsic());
|
||||
|
||||
qInstance.addJoin(defineJoinOrderLineItem());
|
||||
qInstance.addJoin(defineJoinLineItemLineItemExtrinsic());
|
||||
qInstance.addJoin(defineJoinOrderOrderExtrinsic());
|
||||
|
||||
qInstance.addPossibleValueSource(defineAutomationStatusPossibleValueSource());
|
||||
qInstance.addPossibleValueSource(defineStatesPossibleValueSource());
|
||||
@ -538,9 +545,12 @@ public class TestUtils
|
||||
.withRecordSecurityLock(new RecordSecurityLock()
|
||||
.withSecurityKeyType(SECURITY_KEY_TYPE_STORE)
|
||||
.withFieldName("storeId"))
|
||||
.withAssociation(new Association().withName("orderLine").withAssociatedTableName(TABLE_NAME_LINE_ITEM).withJoinName("orderLineItem"))
|
||||
.withAssociation(new Association().withName("extrinsics").withAssociatedTableName(TABLE_NAME_ORDER_EXTRINSIC).withJoinName("orderOrderExtrinsic"))
|
||||
.withField(new QFieldMetaData("id", QFieldType.INTEGER).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("createDate", QFieldType.DATE_TIME).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("modifyDate", QFieldType.DATE_TIME).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("orderNo", QFieldType.STRING))
|
||||
.withField(new QFieldMetaData("orderDate", QFieldType.DATE))
|
||||
.withField(new QFieldMetaData("storeId", QFieldType.INTEGER))
|
||||
.withField(new QFieldMetaData("total", QFieldType.DECIMAL).withDisplayFormat(DisplayFormat.CURRENCY).withFieldSecurityLock(new FieldSecurityLock()
|
||||
@ -561,6 +571,7 @@ public class TestUtils
|
||||
.withName(TABLE_NAME_LINE_ITEM)
|
||||
.withBackendName(MEMORY_BACKEND_NAME)
|
||||
.withPrimaryKeyField("id")
|
||||
.withAssociation(new Association().withName("extrinsics").withAssociatedTableName(TABLE_NAME_LINE_ITEM_EXTRINSIC).withJoinName("lineItemLineItemExtrinsic"))
|
||||
.withField(new QFieldMetaData("id", QFieldType.INTEGER).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("createDate", QFieldType.DATE_TIME).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("modifyDate", QFieldType.DATE_TIME).withIsEditable(false))
|
||||
@ -572,6 +583,44 @@ public class TestUtils
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Define the lineItemExtrinsic table used in standard tests.
|
||||
*******************************************************************************/
|
||||
public static QTableMetaData defineTableLineItemExtrinsic()
|
||||
{
|
||||
return new QTableMetaData()
|
||||
.withName(TABLE_NAME_LINE_ITEM_EXTRINSIC)
|
||||
.withBackendName(MEMORY_BACKEND_NAME)
|
||||
.withPrimaryKeyField("id")
|
||||
.withField(new QFieldMetaData("id", QFieldType.INTEGER).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("createDate", QFieldType.DATE_TIME).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("modifyDate", QFieldType.DATE_TIME).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("lineItemId", QFieldType.INTEGER))
|
||||
.withField(new QFieldMetaData("key", QFieldType.STRING))
|
||||
.withField(new QFieldMetaData("value", QFieldType.STRING));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Define the orderExtrinsic table used in standard tests.
|
||||
*******************************************************************************/
|
||||
public static QTableMetaData defineTableOrderExtrinsic()
|
||||
{
|
||||
return new QTableMetaData()
|
||||
.withName(TABLE_NAME_ORDER_EXTRINSIC)
|
||||
.withBackendName(MEMORY_BACKEND_NAME)
|
||||
.withPrimaryKeyField("id")
|
||||
.withField(new QFieldMetaData("id", QFieldType.INTEGER).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("createDate", QFieldType.DATE_TIME).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("modifyDate", QFieldType.DATE_TIME).withIsEditable(false))
|
||||
.withField(new QFieldMetaData("orderId", QFieldType.INTEGER))
|
||||
.withField(new QFieldMetaData("key", QFieldType.STRING))
|
||||
.withField(new QFieldMetaData("value", QFieldType.STRING));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@ -588,6 +637,38 @@ public class TestUtils
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public static QJoinMetaData defineJoinLineItemLineItemExtrinsic()
|
||||
{
|
||||
return new QJoinMetaData()
|
||||
.withName("lineItemLineItemExtrinsic")
|
||||
.withType(JoinType.ONE_TO_MANY)
|
||||
.withLeftTable(TABLE_NAME_LINE_ITEM)
|
||||
.withRightTable(TABLE_NAME_LINE_ITEM_EXTRINSIC)
|
||||
.withJoinOn(new JoinOn("id", "lineItemId"))
|
||||
.withOrderBy(new QFilterOrderBy("key"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public static QJoinMetaData defineJoinOrderOrderExtrinsic()
|
||||
{
|
||||
return new QJoinMetaData()
|
||||
.withName("orderOrderExtrinsic")
|
||||
.withType(JoinType.ONE_TO_MANY)
|
||||
.withLeftTable(TABLE_NAME_ORDER)
|
||||
.withRightTable(TABLE_NAME_ORDER_EXTRINSIC)
|
||||
.withJoinOn(new JoinOn("id", "orderId"))
|
||||
.withOrderBy(new QFilterOrderBy("key"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
|
Reference in New Issue
Block a user