Merge branch 'release/0.16.0'

This commit is contained in:
Tim Chamberlain
2023-07-24 15:19:57 -05:00
43 changed files with 2227 additions and 158 deletions

View File

@ -44,7 +44,7 @@
</modules>
<properties>
<revision>0.16.0-SNAPSHOT</revision>
<revision>0.16.0</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

View File

@ -25,6 +25,7 @@ package com.kingsrook.qqq.backend.core.actions.async;
import java.io.Serializable;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import com.kingsrook.qqq.backend.core.actions.reporting.BufferedRecordPipe;
import com.kingsrook.qqq.backend.core.actions.reporting.RecordPipe;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.logging.QLogger;
@ -142,6 +143,11 @@ public class AsyncRecordPipeLoop
jobState = asyncJobStatus.getState();
}
if(recordPipe instanceof BufferedRecordPipe bufferedRecordPipe)
{
bufferedRecordPipe.finalFlush();
}
LOG.debug("Job [" + jobUUID + "][" + jobName + "] completed with status: " + asyncJobStatus);
///////////////////////////////////

View File

@ -194,7 +194,7 @@ public class DMLAuditAction extends AbstractQActionFunction<DMLAuditInput, DMLAu
continue;
}
if(field.getType().equals(QFieldType.BLOB))
if(field.getType().equals(QFieldType.BLOB) || field.getType().needsMasked())
{
detailRecord = new QRecord().withValue("message", "Set " + field.getLabel());
}
@ -209,7 +209,7 @@ public class DMLAuditAction extends AbstractQActionFunction<DMLAuditInput, DMLAu
{
if(!Objects.equals(oldValue, value))
{
if(field.getType().equals(QFieldType.BLOB))
if(field.getType().equals(QFieldType.BLOB) || field.getType().needsMasked())
{
if(oldValue == null)
{

View File

@ -502,9 +502,12 @@ public class RunProcessAction
QBackendMetaData backendMetaData = QContext.getQInstance().getBackend(process.getSchedule().getVariantBackend());
if(session.getBackendVariants() == null || !session.getBackendVariants().containsKey(backendMetaData.getVariantOptionsTableTypeValue()))
{
throw (new QException("Could not find Backend Variant information for Backend '" + backendMetaData.getName() + "'"));
LOG.info("Could not find Backend Variant information for Backend '" + backendMetaData.getName() + "'");
}
else
{
basepullKeyValue += "-" + session.getBackendVariants().get(backendMetaData.getVariantOptionsTableTypeValue());
}
basepullKeyValue += "-" + session.getBackendVariants().get(backendMetaData.getVariantOptionsTableTypeValue());
}
return (basepullKeyValue);

View File

@ -42,6 +42,7 @@ import com.kingsrook.qqq.backend.core.actions.customizers.TableCustomizers;
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.LogPair;
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.delete.DeleteInput;
@ -117,12 +118,14 @@ public class DeleteAction
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// if there's a query filter, but the interface doesn't support using a query filter, then do a query for the filter, to get a list of primary keys instead //
// or - anytime there are associations on the table we want primary keys, as that's what the manage associations method uses //
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if(deleteInput.getQueryFilter() != null && !deleteInterface.supportsQueryFilterInput())
if(deleteInput.getQueryFilter() != null && (!deleteInterface.supportsQueryFilterInput() || CollectionUtils.nullSafeHasContents(table.getAssociations())))
{
LOG.info("Querying for primary keys, for backend module " + qModule.getBackendType() + " which does not support queryFilter input for deletes");
LOG.info("Querying for primary keys, for table " + table.getName() + " in backend module " + qModule.getBackendType() + " which does not support queryFilter input for deletes (or the table has associations)");
List<Serializable> primaryKeyList = getPrimaryKeysFromQueryFilter(deleteInput);
deleteInput.setPrimaryKeys(primaryKeyList);
primaryKeys = primaryKeyList;
if(primaryKeyList.isEmpty())
{
@ -165,10 +168,22 @@ public class DeleteAction
if(!primaryKeysToRemoveFromInput.isEmpty())
{
primaryKeys.removeAll(primaryKeysToRemoveFromInput);
if(primaryKeys == null)
{
LOG.warn("There were primary keys to remove from the input, but no primary key list (filter supplied as input?)", new LogPair("primaryKeysToRemoveFromInput", primaryKeysToRemoveFromInput));
}
else
{
primaryKeys.removeAll(primaryKeysToRemoveFromInput);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
// stash a copy of primary keys that didn't have errors (for use in manageAssociations below) //
////////////////////////////////////////////////////////////////////////////////////////////////
Set<Serializable> primaryKeysWithoutErrors = new HashSet<>(CollectionUtils.nonNullList(primaryKeys));
////////////////////////////////////
// have the backend do the delete //
////////////////////////////////////
@ -187,11 +202,13 @@ public class DeleteAction
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// if a record had a validation warning, but then an execution error, remove it from the warning list - so it's only in one of them. //
// also, always remove from
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
for(QRecord outputRecordWithError : outputRecordsWithErrors)
{
Serializable pkey = outputRecordWithError.getValue(primaryKeyFieldName);
recordsWithValidationWarnings.remove(pkey);
primaryKeysWithoutErrors.remove(pkey);
}
///////////////////////////////////////////////////////////////////////////////////////////
@ -211,15 +228,23 @@ public class DeleteAction
////////////////////////////////////////
// delete associations, if applicable //
////////////////////////////////////////
manageAssociations(deleteInput);
manageAssociations(primaryKeysWithoutErrors, deleteInput);
///////////////////////////////////
// do the audit //
// todo - add input.omitDmlAudit //
///////////////////////////////////
DMLAuditInput dmlAuditInput = new DMLAuditInput().withTableActionInput(deleteInput);
oldRecordList.ifPresent(l -> dmlAuditInput.setRecordList(l));
new DMLAuditAction().execute(dmlAuditInput);
//////////////////
// do the audit //
//////////////////
if(deleteInput.getOmitDmlAudit())
{
LOG.debug("Requested to omit DML audit");
}
else
{
DMLAuditInput dmlAuditInput = new DMLAuditInput()
.withTableActionInput(deleteInput)
.withAuditContext(deleteInput.getAuditContext());
oldRecordList.ifPresent(l -> dmlAuditInput.setRecordList(l));
new DMLAuditAction().execute(dmlAuditInput);
}
//////////////////////////////////////////////////////////////
// finally, run the post-delete customizer, if there is one //
@ -340,7 +365,7 @@ public class DeleteAction
/*******************************************************************************
**
*******************************************************************************/
private void manageAssociations(DeleteInput deleteInput) throws QException
private void manageAssociations(Set<Serializable> primaryKeysWithoutErrors, DeleteInput deleteInput) throws QException
{
QTableMetaData table = deleteInput.getTable();
for(Association association : CollectionUtils.nonNullList(table.getAssociations()))
@ -353,7 +378,7 @@ public class DeleteAction
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()));
filter.addCriteria(new QFilterCriteria(join.getJoinOns().get(0).getRightField(), QCriteriaOperator.IN, new ArrayList<>(primaryKeysWithoutErrors)));
}
else
{

View File

@ -155,7 +155,10 @@ public class InsertAction extends AbstractQActionFunction<InsertInput, InsertOut
}
else
{
new DMLAuditAction().execute(new DMLAuditInput().withTableActionInput(insertInput).withRecordList(insertOutput.getRecords()));
new DMLAuditAction().execute(new DMLAuditInput()
.withTableActionInput(insertInput)
.withAuditContext(insertInput.getAuditContext())
.withRecordList(insertOutput.getRecords()));
}
//////////////////////////////////////////////////////////////

View File

@ -0,0 +1,174 @@
/*
* 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.actions.tables;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import com.kingsrook.qqq.backend.core.actions.AbstractQActionFunction;
import com.kingsrook.qqq.backend.core.actions.QBackendTransaction;
import com.kingsrook.qqq.backend.core.actions.tables.helpers.UniqueKeyHelper;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
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.insert.InsertOutput;
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.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.replace.ReplaceInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.replace.ReplaceOutput;
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.data.QRecord;
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.utils.CollectionUtils;
/*******************************************************************************
** Action to do a "replace" - e.g: Update rows with unique-key values that are
** already in the table; insert rows whose unique keys weren't already in the
** table, and delete rows that weren't in the input (all based on a
** UniqueKey that's part of the input)
**
** Note - the filter in the ReplaceInput - its role is to limit what rows are
** potentially deleted. e.g., if you have a table that's segmented, and you're
** only replacing a particular segment of it (say, for 1 client), then you pass
** in a filter that finds rows matching that segment. See Test for example.
*******************************************************************************/
public class ReplaceAction extends AbstractQActionFunction<ReplaceInput, ReplaceOutput>
{
/*******************************************************************************
**
*******************************************************************************/
@Override
public ReplaceOutput execute(ReplaceInput input) throws QException
{
ReplaceOutput output = new ReplaceOutput();
QBackendTransaction transaction = input.getTransaction();
boolean weOwnTheTransaction = false;
try
{
QTableMetaData table = input.getTable();
UniqueKey uniqueKey = input.getKey();
String primaryKeyField = table.getPrimaryKeyField();
if(transaction == null)
{
InsertInput insertInput = new InsertInput();
insertInput.setTableName(input.getTableName());
transaction = new InsertAction().openTransaction(insertInput);
weOwnTheTransaction = true;
}
List<QRecord> insertList = new ArrayList<>();
List<QRecord> updateList = new ArrayList<>();
List<Serializable> primaryKeysToKeep = new ArrayList<>();
for(List<QRecord> page : CollectionUtils.getPages(input.getRecords(), 1000))
{
///////////////////////////////////////////////////////////////////////////////////
// originally it was thought that we'd need to pass the filter in here //
// but, it's been decided not to. the filter only applies to what we can delete //
///////////////////////////////////////////////////////////////////////////////////
Map<List<Serializable>, Serializable> existingKeys = UniqueKeyHelper.getExistingKeys(transaction, table, page, uniqueKey);
for(QRecord record : page)
{
Optional<List<Serializable>> keyValues = UniqueKeyHelper.getKeyValues(table, uniqueKey, record);
if(keyValues.isPresent())
{
if(existingKeys.containsKey(keyValues.get()))
{
Serializable primaryKey = existingKeys.get(keyValues.get());
record.setValue(primaryKeyField, primaryKey);
updateList.add(record);
primaryKeysToKeep.add(primaryKey);
}
else
{
insertList.add(record);
}
}
}
}
InsertInput insertInput = new InsertInput();
insertInput.setTableName(table.getName());
insertInput.setRecords(insertList);
insertInput.setTransaction(transaction);
insertInput.setOmitDmlAudit(input.getOmitDmlAudit());
InsertOutput insertOutput = new InsertAction().execute(insertInput);
primaryKeysToKeep.addAll(insertOutput.getRecords().stream().map(r -> r.getValue(primaryKeyField)).toList());
output.setInsertOutput(insertOutput);
UpdateInput updateInput = new UpdateInput();
updateInput.setTableName(table.getName());
updateInput.setRecords(updateList);
updateInput.setTransaction(transaction);
updateInput.setOmitDmlAudit(input.getOmitDmlAudit());
UpdateOutput updateOutput = new UpdateAction().execute(updateInput);
output.setUpdateOutput(updateOutput);
QQueryFilter deleteFilter = new QQueryFilter(new QFilterCriteria(primaryKeyField, QCriteriaOperator.NOT_IN, primaryKeysToKeep));
if(input.getFilter() != null)
{
deleteFilter.addSubFilter(input.getFilter());
}
DeleteInput deleteInput = new DeleteInput();
deleteInput.setTableName(table.getName());
deleteInput.setQueryFilter(deleteFilter);
deleteInput.setTransaction(transaction);
deleteInput.setOmitDmlAudit(input.getOmitDmlAudit());
DeleteOutput deleteOutput = new DeleteAction().execute(deleteInput);
output.setDeleteOutput(deleteOutput);
if(weOwnTheTransaction)
{
transaction.commit();
}
return (output);
}
catch(Exception e)
{
if(weOwnTheTransaction)
{
transaction.rollback();
}
throw (new QException("Error executing replace action", e));
}
finally
{
if(weOwnTheTransaction)
{
transaction.close();
}
}
}
}

View File

@ -46,7 +46,7 @@ public class CacheUtils
/*******************************************************************************
**
*******************************************************************************/
static QRecord mapSourceRecordToCacheRecord(QTableMetaData table, QRecord recordFromSource)
static QRecord mapSourceRecordToCacheRecord(QTableMetaData table, QRecord recordFromSource, CacheUseCase cacheUseCase)
{
QRecord cacheRecord = new QRecord(recordFromSource);
@ -58,7 +58,10 @@ public class CacheUtils
{
if(fieldName.equals(table.getPrimaryKeyField()))
{
cacheRecord.removeValue(fieldName);
if(!cacheUseCase.getDoCopySourcePrimaryKeyToCache())
{
cacheRecord.removeValue(fieldName);
}
}
else if(!cacheRecord.getValues().containsKey(fieldName))
{

View File

@ -79,9 +79,9 @@ public class QueryActionCacheHelper
{
private static final QLogger LOG = QLogger.getLogger(QueryActionCacheHelper.class);
private boolean isQueryInputCacheable = false;
private Set<CacheUseCase.Type> cacheUseCases = new HashSet<>();
private CacheUseCase.Type activeCacheUseCase = null;
private boolean isQueryInputCacheable = false;
private Map<CacheUseCase.Type, CacheUseCase> cacheUseCaseMap = new HashMap<>();
private CacheUseCase activeCacheUseCase = null;
private UniqueKey cacheUniqueKey = null;
private ListingHash<String, Serializable> uniqueKeyValues = new ListingHash<>();
@ -122,7 +122,7 @@ public class QueryActionCacheHelper
QTableMetaData table = queryInput.getTable();
List<QRecord> recordsToReturn = recordsFromSource.stream()
.map(r -> CacheUtils.mapSourceRecordToCacheRecord(table, r))
.map(r -> CacheUtils.mapSourceRecordToCacheRecord(table, r, activeCacheUseCase))
.toList();
queryOutput.addRecords(recordsToReturn);
@ -220,7 +220,7 @@ public class QueryActionCacheHelper
List<QRecord> refreshedRecordsToReturn = recordsFromSource.stream()
.map(r ->
{
QRecord recordToCache = CacheUtils.mapSourceRecordToCacheRecord(table, r);
QRecord recordToCache = CacheUtils.mapSourceRecordToCacheRecord(table, r, activeCacheUseCase);
recordToCache.setValue(table.getPrimaryKeyField(), uniqueKeyToPrimaryKeyMap.get(getUniqueKeyValues(recordToCache)));
return (recordToCache);
})
@ -391,10 +391,10 @@ public class QueryActionCacheHelper
for(CacheUseCase cacheUseCase : CollectionUtils.nonNullList(table.getCacheOf().getUseCases()))
{
cacheUseCases.add(cacheUseCase.getType());
cacheUseCaseMap.put(cacheUseCase.getType(), cacheUseCase);
}
if(cacheUseCases.contains(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY))
if(cacheUseCaseMap.containsKey(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY))
{
if(queryInput.getFilter() == null)
{
@ -475,7 +475,7 @@ public class QueryActionCacheHelper
return;
}
LOG.trace("Unable to cache: No supported use case: " + cacheUseCases);
LOG.trace("Unable to cache: No supported use case: " + cacheUseCaseMap.keySet());
}
@ -491,7 +491,7 @@ public class QueryActionCacheHelper
{
this.cacheUniqueKey = uniqueKey;
isQueryInputCacheable = true;
activeCacheUseCase = CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY;
activeCacheUseCase = cacheUseCaseMap.get(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY);
return true;
}
}
@ -536,14 +536,14 @@ public class QueryActionCacheHelper
List<QRecord> recordsFromSource = null;
QTableMetaData table = queryInput.getTable();
if(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY.equals(activeCacheUseCase))
if(CacheUseCase.Type.UNIQUE_KEY_TO_UNIQUE_KEY.equals(activeCacheUseCase.getType()))
{
recordsFromSource = getFromCachedSourceForUniqueKeyToUniqueKey(queryInput, uniqueKeyValues, table.getCacheOf().getSourceTable());
}
else
{
// todo!!
throw (new NotImplementedException("Not-yet-implemented cache use case type: " + activeCacheUseCase));
throw (new NotImplementedException("Not-yet-implemented cache use case type: " + activeCacheUseCase.getType()));
}
return (recordsFromSource);

View File

@ -51,6 +51,17 @@ public class CountInput extends AbstractTableActionInput
/*******************************************************************************
** Constructor
**
*******************************************************************************/
public CountInput(String tableName)
{
setTableName(tableName);
}
/*******************************************************************************
** Getter for filter
**
@ -152,4 +163,15 @@ public class CountInput extends AbstractTableActionInput
return (this);
}
/*******************************************************************************
** Fluent setter for filter
*******************************************************************************/
public CountInput withFilter(QQueryFilter filter)
{
this.filter = filter;
return (this);
}
}

View File

@ -43,6 +43,9 @@ public class DeleteInput extends AbstractTableActionInput
private QQueryFilter queryFilter;
private InputSource inputSource = QInputSource.SYSTEM;
private boolean omitDmlAudit = false;
private String auditContext = null;
/*******************************************************************************
@ -211,4 +214,66 @@ public class DeleteInput extends AbstractTableActionInput
return (this);
}
/*******************************************************************************
** Getter for omitDmlAudit
*******************************************************************************/
public boolean getOmitDmlAudit()
{
return (this.omitDmlAudit);
}
/*******************************************************************************
** Setter for omitDmlAudit
*******************************************************************************/
public void setOmitDmlAudit(boolean omitDmlAudit)
{
this.omitDmlAudit = omitDmlAudit;
}
/*******************************************************************************
** Fluent setter for omitDmlAudit
*******************************************************************************/
public DeleteInput withOmitDmlAudit(boolean omitDmlAudit)
{
this.omitDmlAudit = omitDmlAudit;
return (this);
}
/*******************************************************************************
** Getter for auditContext
*******************************************************************************/
public String getAuditContext()
{
return (this.auditContext);
}
/*******************************************************************************
** Setter for auditContext
*******************************************************************************/
public void setAuditContext(String auditContext)
{
this.auditContext = auditContext;
}
/*******************************************************************************
** Fluent setter for auditContext
*******************************************************************************/
public DeleteInput withAuditContext(String auditContext)
{
this.auditContext = auditContext;
return (this);
}
}

View File

@ -46,6 +46,7 @@ public class InsertInput extends AbstractTableActionInput
private boolean skipUniqueKeyCheck = false;
private boolean omitDmlAudit = false;
private String auditContext = null;
@ -284,4 +285,35 @@ public class InsertInput extends AbstractTableActionInput
return (this);
}
/*******************************************************************************
** Getter for auditContext
*******************************************************************************/
public String getAuditContext()
{
return (this.auditContext);
}
/*******************************************************************************
** Setter for auditContext
*******************************************************************************/
public void setAuditContext(String auditContext)
{
this.auditContext = auditContext;
}
/*******************************************************************************
** Fluent setter for auditContext
*******************************************************************************/
public InsertInput withAuditContext(String auditContext)
{
this.auditContext = auditContext;
return (this);
}
}

View File

@ -26,8 +26,9 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.kingsrook.qqq.backend.core.logging.QLogger;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions.AbstractFilterExpression;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.serialization.QFilterCriteriaDeserializer;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
@ -36,6 +37,7 @@ import com.kingsrook.qqq.backend.core.utils.StringUtils;
* A single criteria Component of a Query
*
*******************************************************************************/
@JsonDeserialize(using = QFilterCriteriaDeserializer.class)
public class QFilterCriteria implements Serializable, Cloneable
{
private static final QLogger LOG = QLogger.getLogger(QFilterCriteria.class);
@ -44,8 +46,10 @@ public class QFilterCriteria implements Serializable, Cloneable
private QCriteriaOperator operator;
private List<Serializable> values;
private String otherFieldName;
private AbstractFilterExpression<?> expression;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// todo - probably implement this as a type of expression - though would require a little special handling i think when evaluating... //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private String otherFieldName;
@ -98,23 +102,6 @@ public class QFilterCriteria implements Serializable, Cloneable
/*******************************************************************************
**
*******************************************************************************/
public QFilterCriteria(String fieldName, QCriteriaOperator operator, AbstractFilterExpression<?> expression)
{
this.fieldName = fieldName;
this.operator = operator;
this.expression = expression;
///////////////////////////////////////
// this guy doesn't like to be null? //
///////////////////////////////////////
this.values = new ArrayList<>();
}
/*******************************************************************************
**
*******************************************************************************/
@ -332,35 +319,4 @@ public class QFilterCriteria implements Serializable, Cloneable
return (rs.toString());
}
/*******************************************************************************
** Getter for expression
*******************************************************************************/
public AbstractFilterExpression<?> getExpression()
{
return (this.expression);
}
/*******************************************************************************
** Setter for expression
*******************************************************************************/
public void setExpression(AbstractFilterExpression<?> expression)
{
this.expression = expression;
}
/*******************************************************************************
** Fluent setter for expression
*******************************************************************************/
public QFilterCriteria withExpression(AbstractFilterExpression<?> expression)
{
this.expression = expression;
return (this);
}
}

View File

@ -28,10 +28,31 @@ import java.io.Serializable;
/*******************************************************************************
**
*******************************************************************************/
public abstract class AbstractFilterExpression<T extends Serializable>
public abstract class AbstractFilterExpression<T extends Serializable> implements Serializable
{
/*******************************************************************************
**
*******************************************************************************/
public abstract T evaluate();
/*******************************************************************************
** To help with serialization, define a "type" in all subclasses
*******************************************************************************/
public String getType()
{
return (getClass().getSimpleName());
}
/*******************************************************************************
** noop - but here so serialization won't be upset about there being a type
** in a json object.
*******************************************************************************/
public void setType(String type)
{
}
}

View File

@ -23,6 +23,10 @@ package com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.TimeUnit;
@ -31,9 +35,9 @@ import java.util.concurrent.TimeUnit;
*******************************************************************************/
public class NowWithOffset extends AbstractFilterExpression<Instant>
{
private final Operator operator;
private final int amount;
private final TimeUnit timeUnit;
private Operator operator;
private int amount;
private ChronoUnit timeUnit;
@ -46,7 +50,17 @@ public class NowWithOffset extends AbstractFilterExpression<Instant>
** Constructor
**
*******************************************************************************/
private NowWithOffset(Operator operator, int amount, TimeUnit timeUnit)
public NowWithOffset()
{
}
/*******************************************************************************
** Constructor
**
*******************************************************************************/
private NowWithOffset(Operator operator, int amount, ChronoUnit timeUnit)
{
this.operator = operator;
this.amount = amount;
@ -59,7 +73,19 @@ public class NowWithOffset extends AbstractFilterExpression<Instant>
** Factory
**
*******************************************************************************/
@Deprecated
public static NowWithOffset minus(int amount, TimeUnit timeUnit)
{
return (minus(amount, timeUnit.toChronoUnit()));
}
/*******************************************************************************
** Factory
**
*******************************************************************************/
public static NowWithOffset minus(int amount, ChronoUnit timeUnit)
{
return (new NowWithOffset(Operator.MINUS, amount, timeUnit));
}
@ -70,7 +96,19 @@ public class NowWithOffset extends AbstractFilterExpression<Instant>
** Factory
**
*******************************************************************************/
@Deprecated
public static NowWithOffset plus(int amount, TimeUnit timeUnit)
{
return (plus(amount, timeUnit.toChronoUnit()));
}
/*******************************************************************************
** Factory
**
*******************************************************************************/
public static NowWithOffset plus(int amount, ChronoUnit timeUnit)
{
return (new NowWithOffset(Operator.PLUS, amount, timeUnit));
}
@ -83,14 +121,24 @@ public class NowWithOffset extends AbstractFilterExpression<Instant>
@Override
public Instant evaluate()
{
/////////////////////////////////////////////////////////////////////////////
// Instant doesn't let us plus/minus WEEK, MONTH, or YEAR... //
// but LocalDateTime does. So, make a LDT in UTC, do the plus/minus, then //
// convert back to Instant @ UTC //
/////////////////////////////////////////////////////////////////////////////
LocalDateTime now = LocalDateTime.now(ZoneId.of("UTC"));
LocalDateTime then;
if(operator.equals(Operator.PLUS))
{
return (Instant.now().plus(amount, timeUnit.toChronoUnit()));
then = now.plus(amount, timeUnit);
}
else
{
return (Instant.now().minus(amount, timeUnit.toChronoUnit()));
then = now.minus(amount, timeUnit);
}
return (then.toInstant(ZoneOffset.UTC));
}
@ -121,7 +169,7 @@ public class NowWithOffset extends AbstractFilterExpression<Instant>
** Getter for timeUnit
**
*******************************************************************************/
public TimeUnit getTimeUnit()
public ChronoUnit getTimeUnit()
{
return timeUnit;
}

View File

@ -0,0 +1,178 @@
/*
* 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.actions.tables.query.expressions;
import java.time.DayOfWeek;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException;
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
/*******************************************************************************
**
*******************************************************************************/
public class ThisOrLastPeriod extends AbstractFilterExpression<Instant>
{
private Operator operator;
private ChronoUnit timeUnit;
public enum Operator
{THIS, LAST}
/*******************************************************************************
** Constructor
**
*******************************************************************************/
public ThisOrLastPeriod()
{
}
/*******************************************************************************
** Constructor
**
*******************************************************************************/
private ThisOrLastPeriod(Operator operator, ChronoUnit timeUnit)
{
this.operator = operator;
this.timeUnit = timeUnit;
}
/*******************************************************************************
** Factory
**
*******************************************************************************/
public static ThisOrLastPeriod this_(ChronoUnit timeUnit)
{
return (new ThisOrLastPeriod(Operator.THIS, timeUnit));
}
/*******************************************************************************
** Factory
**
*******************************************************************************/
public static ThisOrLastPeriod last(int amount, ChronoUnit timeUnit)
{
return (new ThisOrLastPeriod(Operator.LAST, timeUnit));
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public Instant evaluate()
{
ZoneId zoneId = ValueUtils.getSessionOrInstanceZoneId();
switch(timeUnit)
{
case HOURS ->
{
if(operator.equals(Operator.THIS))
{
return Instant.now().truncatedTo(ChronoUnit.HOURS);
}
else
{
return Instant.now().minus(1, ChronoUnit.HOURS).truncatedTo(ChronoUnit.HOURS);
}
}
case DAYS ->
{
Instant startOfToday = ValueUtils.getStartOfTodayInZoneId(zoneId.getId());
return operator.equals(Operator.THIS) ? startOfToday : startOfToday.minus(1, ChronoUnit.DAYS);
}
case WEEKS ->
{
Instant startOfToday = ValueUtils.getStartOfTodayInZoneId(zoneId.getId());
LocalDateTime startOfThisWeekLDT = LocalDateTime.ofInstant(startOfToday, zoneId);
while(startOfThisWeekLDT.get(ChronoField.DAY_OF_WEEK) != DayOfWeek.SUNDAY.getValue())
{
////////////////////////////////////////
// go backwards until sunday is found //
////////////////////////////////////////
startOfThisWeekLDT = startOfThisWeekLDT.minus(1, ChronoUnit.DAYS);
}
Instant startOfThisWeek = startOfThisWeekLDT.toInstant(zoneId.getRules().getOffset(startOfThisWeekLDT));
return operator.equals(Operator.THIS) ? startOfThisWeek : startOfThisWeek.minus(7, ChronoUnit.DAYS);
}
case MONTHS ->
{
Instant startOfThisMonth = ValueUtils.getStartOfMonthInZoneId(zoneId.getId());
LocalDateTime startOfThisMonthLDT = LocalDateTime.ofInstant(startOfThisMonth, ZoneId.of(zoneId.getId()));
LocalDateTime startOfLastMonthLDT = startOfThisMonthLDT.minus(1, ChronoUnit.MONTHS);
Instant startOfLastMonth = startOfLastMonthLDT.toInstant(ZoneId.of(zoneId.getId()).getRules().getOffset(Instant.now()));
return operator.equals(Operator.THIS) ? startOfThisMonth : startOfLastMonth;
}
case YEARS ->
{
Instant startOfThisYear = ValueUtils.getStartOfYearInZoneId(zoneId.getId());
LocalDateTime startOfThisYearLDT = LocalDateTime.ofInstant(startOfThisYear, zoneId);
LocalDateTime startOfLastYearLDT = startOfThisYearLDT.minus(1, ChronoUnit.YEARS);
Instant startOfLastYear = startOfLastYearLDT.toInstant(zoneId.getRules().getOffset(Instant.now()));
return operator.equals(Operator.THIS) ? startOfThisYear : startOfLastYear;
}
default -> throw (new QRuntimeException("Unsupported timeUnit: " + timeUnit));
}
}
/*******************************************************************************
** Getter for operator
**
*******************************************************************************/
public Operator getOperator()
{
return operator;
}
/*******************************************************************************
** Getter for timeUnit
**
*******************************************************************************/
public ChronoUnit getTimeUnit()
{
return timeUnit;
}
}

View File

@ -0,0 +1,129 @@
/*
* 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.actions.tables.query.serialization;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
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.expressions.AbstractFilterExpression;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
/*******************************************************************************
** Custom jackson deserializer, to deal w/ abstract expression field
*******************************************************************************/
public class QFilterCriteriaDeserializer extends StdDeserializer<QFilterCriteria>
{
/*******************************************************************************
**
*******************************************************************************/
public QFilterCriteriaDeserializer()
{
this(null);
}
/*******************************************************************************
**
*******************************************************************************/
public QFilterCriteriaDeserializer(Class<?> vc)
{
super(vc);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public QFilterCriteria deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JacksonException
{
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
ObjectMapper objectMapper = new ObjectMapper();
/////////////////////////////////
// get values out of json node //
/////////////////////////////////
List<Serializable> values = objectMapper.treeToValue(node.get("values"), List.class);
String fieldName = objectMapper.treeToValue(node.get("fieldName"), String.class);
QCriteriaOperator operator = objectMapper.treeToValue(node.get("operator"), QCriteriaOperator.class);
String otherFieldName = objectMapper.treeToValue(node.get("otherFieldName"), String.class);
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// look at all the values - if any of them are actually meant to be an Expression (instance of subclass of AbstractFilterExpression) //
// they'll have deserialized as a Map, with a "type" key. If that's the case, then re/de serialize them into the proper expression type //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ListIterator<Serializable> valuesIterator = CollectionUtils.nonNullList(values).listIterator();
while(valuesIterator.hasNext())
{
Object value = valuesIterator.next();
if(value instanceof Map<?, ?> map && map.containsKey("type"))
{
String expressionType = ValueUtils.getValueAsString(map.get("type"));
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// right now, we'll assume that all expression subclasses are in the same package as AbstractFilterExpression //
// so, we can just do a Class.forName on that name, and use JsonUtils.toObject requesting that class. //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
try
{
String assumedExpressionJSON = JsonUtils.toJson(map);
String className = AbstractFilterExpression.class.getName().replace(AbstractFilterExpression.class.getSimpleName(), expressionType);
Serializable replacementValue = (Serializable) JsonUtils.toObject(assumedExpressionJSON, Class.forName(className));
valuesIterator.set(replacementValue);
}
catch(Exception e)
{
throw (new IOException("Error deserializing criteria value which appeared to be an expression of type [" + expressionType + "] inside QFilterCriteria", e));
}
}
}
///////////////////////////////////
// put fields into return object //
///////////////////////////////////
QFilterCriteria criteria = new QFilterCriteria();
criteria.setFieldName(fieldName);
criteria.setOperator(operator);
criteria.setValues(values);
criteria.setOtherFieldName(otherFieldName);
return (criteria);
}
}

View File

@ -0,0 +1,210 @@
/*
* 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.actions.tables.replace;
import java.util.List;
import com.kingsrook.qqq.backend.core.actions.QBackendTransaction;
import com.kingsrook.qqq.backend.core.model.actions.AbstractTableActionInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey;
/*******************************************************************************
**
*******************************************************************************/
public class ReplaceInput extends AbstractTableActionInput
{
private QBackendTransaction transaction;
private UniqueKey key;
private List<QRecord> records;
private QQueryFilter filter;
private boolean omitDmlAudit = false;
/*******************************************************************************
** Constructor
**
*******************************************************************************/
public ReplaceInput()
{
}
/*******************************************************************************
** Getter for transaction
*******************************************************************************/
public QBackendTransaction getTransaction()
{
return (this.transaction);
}
/*******************************************************************************
** Setter for transaction
*******************************************************************************/
public void setTransaction(QBackendTransaction transaction)
{
this.transaction = transaction;
}
/*******************************************************************************
** Fluent setter for transaction
*******************************************************************************/
public ReplaceInput withTransaction(QBackendTransaction transaction)
{
this.transaction = transaction;
return (this);
}
/*******************************************************************************
** Getter for records
*******************************************************************************/
public List<QRecord> getRecords()
{
return (this.records);
}
/*******************************************************************************
** Setter for records
*******************************************************************************/
public void setRecords(List<QRecord> records)
{
this.records = records;
}
/*******************************************************************************
** Fluent setter for records
*******************************************************************************/
public ReplaceInput withRecords(List<QRecord> records)
{
this.records = records;
return (this);
}
/*******************************************************************************
** Getter for filter
*******************************************************************************/
public QQueryFilter getFilter()
{
return (this.filter);
}
/*******************************************************************************
** Setter for filter
*******************************************************************************/
public void setFilter(QQueryFilter filter)
{
this.filter = filter;
}
/*******************************************************************************
** Fluent setter for filter
*******************************************************************************/
public ReplaceInput withFilter(QQueryFilter filter)
{
this.filter = filter;
return (this);
}
/*******************************************************************************
** Getter for key
*******************************************************************************/
public UniqueKey getKey()
{
return (this.key);
}
/*******************************************************************************
** Setter for key
*******************************************************************************/
public void setKey(UniqueKey key)
{
this.key = key;
}
/*******************************************************************************
** Fluent setter for key
*******************************************************************************/
public ReplaceInput withKey(UniqueKey key)
{
this.key = key;
return (this);
}
/*******************************************************************************
** Getter for omitDmlAudit
*******************************************************************************/
public boolean getOmitDmlAudit()
{
return (this.omitDmlAudit);
}
/*******************************************************************************
** Setter for omitDmlAudit
*******************************************************************************/
public void setOmitDmlAudit(boolean omitDmlAudit)
{
this.omitDmlAudit = omitDmlAudit;
}
/*******************************************************************************
** Fluent setter for omitDmlAudit
*******************************************************************************/
public ReplaceInput withOmitDmlAudit(boolean omitDmlAudit)
{
this.omitDmlAudit = omitDmlAudit;
return (this);
}
}

View File

@ -0,0 +1,133 @@
/*
* 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.actions.tables.replace;
import com.kingsrook.qqq.backend.core.model.actions.AbstractActionOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertOutput;
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateOutput;
/*******************************************************************************
**
*******************************************************************************/
public class ReplaceOutput extends AbstractActionOutput
{
private InsertOutput insertOutput;
private UpdateOutput updateOutput;
private DeleteOutput deleteOutput;
/*******************************************************************************
** Getter for insertOutput
*******************************************************************************/
public InsertOutput getInsertOutput()
{
return (this.insertOutput);
}
/*******************************************************************************
** Setter for insertOutput
*******************************************************************************/
public void setInsertOutput(InsertOutput insertOutput)
{
this.insertOutput = insertOutput;
}
/*******************************************************************************
** Fluent setter for insertOutput
*******************************************************************************/
public ReplaceOutput withInsertOutput(InsertOutput insertOutput)
{
this.insertOutput = insertOutput;
return (this);
}
/*******************************************************************************
** Getter for updateOutput
*******************************************************************************/
public UpdateOutput getUpdateOutput()
{
return (this.updateOutput);
}
/*******************************************************************************
** Setter for updateOutput
*******************************************************************************/
public void setUpdateOutput(UpdateOutput updateOutput)
{
this.updateOutput = updateOutput;
}
/*******************************************************************************
** Fluent setter for updateOutput
*******************************************************************************/
public ReplaceOutput withUpdateOutput(UpdateOutput updateOutput)
{
this.updateOutput = updateOutput;
return (this);
}
/*******************************************************************************
** Getter for deleteOutput
*******************************************************************************/
public DeleteOutput getDeleteOutput()
{
return (this.deleteOutput);
}
/*******************************************************************************
** Setter for deleteOutput
*******************************************************************************/
public void setDeleteOutput(DeleteOutput deleteOutput)
{
this.deleteOutput = deleteOutput;
}
/*******************************************************************************
** Fluent setter for deleteOutput
*******************************************************************************/
public ReplaceOutput withDeleteOutput(DeleteOutput deleteOutput)
{
this.deleteOutput = deleteOutput;
return (this);
}
}

View File

@ -22,6 +22,8 @@
package com.kingsrook.qqq.backend.core.model.metadata.queues;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.TopLevelMetaDataInterface;
import com.kingsrook.qqq.backend.core.model.metadata.scheduleing.QScheduleMetaData;
@ -34,7 +36,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.scheduleing.QScheduleMetaDa
** The processName is the code that runs for messages found on the queue.
** The schedule may not be used by all provider types, but defines when the queue is polled.
*******************************************************************************/
public class QQueueMetaData
public class QQueueMetaData implements TopLevelMetaDataInterface
{
private String name;
private String providerName;
@ -213,4 +215,15 @@ public class QQueueMetaData
return (this);
}
/*******************************************************************************
**
*******************************************************************************/
@Override
public void addSelfToInstance(QInstance qInstance)
{
qInstance.addQueue(this);
}
}

View File

@ -49,6 +49,7 @@ public class CacheUseCase
//////////////////////////
private UniqueKey cacheUniqueKey;
private UniqueKey sourceUniqueKey;
private boolean doCopySourcePrimaryKeyToCache = false;
private List<QQueryFilter> excludeRecordsMatching;
@ -222,4 +223,35 @@ public class CacheUseCase
return (this);
}
/*******************************************************************************
** Getter for doCopySourcePrimaryKeyToCache
*******************************************************************************/
public boolean getDoCopySourcePrimaryKeyToCache()
{
return (this.doCopySourcePrimaryKeyToCache);
}
/*******************************************************************************
** Setter for doCopySourcePrimaryKeyToCache
*******************************************************************************/
public void setDoCopySourcePrimaryKeyToCache(boolean doCopySourcePrimaryKeyToCache)
{
this.doCopySourcePrimaryKeyToCache = doCopySourcePrimaryKeyToCache;
}
/*******************************************************************************
** Fluent setter for doCopySourcePrimaryKeyToCache
*******************************************************************************/
public CacheUseCase withDoCopySourcePrimaryKeyToCache(boolean doCopySourcePrimaryKeyToCache)
{
this.doCopySourcePrimaryKeyToCache = doCopySourcePrimaryKeyToCache;
return (this);
}
}

View File

@ -105,6 +105,7 @@ public class QQQTablesMetaDataProvider
.withCacheSourceMisses(false)
.withCacheUniqueKey(new UniqueKey("name"))
.withSourceUniqueKey(new UniqueKey("name"))
.withDoCopySourcePrimaryKeyToCache(true)
)
);

View File

@ -26,6 +26,7 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@ -232,7 +233,7 @@ public abstract class AbstractTableSyncTransformStep extends AbstractTransformSt
///////////////////////////////////////////////////////////////////////////////////////////////////
// query to see if we already have those records in the destination (to determine insert/update) //
///////////////////////////////////////////////////////////////////////////////////////////////////
Map<Serializable, QRecord> existingRecordsByForeignKey = getExistingRecordsByForeignKey(runBackendStepInput, destinationTableForeignKeyField, destinationTableName, sourceKeyList);
Map<Pair<String, Serializable>, QRecord> existingRecordsByForeignKey = getExistingRecordsByForeignKey(runBackendStepInput, destinationTableForeignKeyField, destinationTableName, sourceKeyList);
/////////////////////////////////////////////////////////////////
// foreach source record, build the record we'll insert/update //
@ -267,13 +268,10 @@ public abstract class AbstractTableSyncTransformStep extends AbstractTransformSt
continue;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
// look for the existing record - note - we may need to type-convert here, the sourceKey value //
// from the source table to the destinationKey. e.g., if source table had an integer, and the //
// destination has a string. //
/////////////////////////////////////////////////////////////////////////////////////////////////
Serializable sourceKeyValueInTargetFieldType = ValueUtils.getValueAsFieldType(destinationForeignKeyField.getType(), sourceKeyValue);
QRecord existingRecord = existingRecordsByForeignKey.get(sourceKeyValueInTargetFieldType);
//////////////////////////////////////////////////////////////
// look for the existing record, to determine insert/update //
//////////////////////////////////////////////////////////////
QRecord existingRecord = getExistingRecord(existingRecordsByForeignKey, destinationForeignKeyField, sourceKeyValue);
QRecord recordToStore;
if(existingRecord != null && config.performUpdates)
@ -333,26 +331,66 @@ public abstract class AbstractTableSyncTransformStep extends AbstractTransformSt
/*******************************************************************************
**
*******************************************************************************/
protected Map<Serializable, QRecord> getExistingRecordsByForeignKey(RunBackendStepInput runBackendStepInput, String destinationTableForeignKeyField, String destinationTableName, List<Serializable> sourceKeyList) throws QException
protected QRecord getExistingRecord(Map<Pair<String, Serializable>, QRecord> existingRecordsByForeignKey, QFieldMetaData destinationForeignKeyField, Serializable sourceKeyValue)
{
Map<Serializable, QRecord> existingRecordsByForeignKey = Collections.emptyMap();
if(!sourceKeyList.isEmpty())
//////////////////////////////////////////////////////////////////////////////////////////////////
// note - we may need to type-convert here, the sourceKey value from the source table to //
// the destinationKey. e.g., if source table had an integer, and the destination has a string. //
//////////////////////////////////////////////////////////////////////////////////////////////////
Serializable sourceKeyValueInTargetFieldType = ValueUtils.getValueAsFieldType(destinationForeignKeyField.getType(), sourceKeyValue);
return (existingRecordsByForeignKey.get(Pair.of(destinationForeignKeyField.getName(), sourceKeyValueInTargetFieldType)));
}
/*******************************************************************************
** Run the existingRecordQueryFilter - to look in the destinationTable for
** any records that may need an update (rather than an insert).
**
** Generally returns a Map, keyed by a Pair of the destinationTableForeignKeyField
** and the value in that field. But, for more complex use-cases, one can override
** the buildExistingRecordsMap method, to make different keys (e.g., if there are
** two possible destinationTableForeignKeyFields).
*******************************************************************************/
protected Map<Pair<String, Serializable>, QRecord> getExistingRecordsByForeignKey(RunBackendStepInput runBackendStepInput, String destinationTableForeignKeyField, String destinationTableName, List<Serializable> sourceKeyList) throws QException
{
if(sourceKeyList.isEmpty())
{
QueryInput queryInput = new QueryInput();
queryInput.setTableName(destinationTableName);
getTransaction().ifPresent(queryInput::setTransaction);
QQueryFilter filter = getExistingRecordQueryFilter(runBackendStepInput, sourceKeyList);
queryInput.setFilter(filter);
return (Collections.emptyMap());
}
Collection<String> associationNamesToInclude = getAssociationNamesToInclude();
if(CollectionUtils.nullSafeHasContents(associationNamesToInclude))
{
queryInput.setIncludeAssociations(true);
queryInput.setAssociationNamesToInclude(associationNamesToInclude);
}
QueryInput queryInput = new QueryInput();
queryInput.setTableName(destinationTableName);
getTransaction().ifPresent(queryInput::setTransaction);
QQueryFilter filter = getExistingRecordQueryFilter(runBackendStepInput, sourceKeyList);
queryInput.setFilter(filter);
QueryOutput queryOutput = new QueryAction().execute(queryInput);
existingRecordsByForeignKey = CollectionUtils.recordsToMap(queryOutput.getRecords(), destinationTableForeignKeyField);
Collection<String> associationNamesToInclude = getAssociationNamesToInclude();
if(CollectionUtils.nullSafeHasContents(associationNamesToInclude))
{
queryInput.setIncludeAssociations(true);
queryInput.setAssociationNamesToInclude(associationNamesToInclude);
}
QueryOutput queryOutput = new QueryAction().execute(queryInput);
return (buildExistingRecordsMap(destinationTableForeignKeyField, queryOutput.getRecords()));
}
/*******************************************************************************
** Overridable point where you can, for example, keys in the existingRecordsMap
** with different fieldNames from the destinationTable.
**
** Note, if you're overriding this method, you'll likely also want & need to
** override getExistingRecord.
*******************************************************************************/
protected Map<Pair<String, Serializable>, QRecord> buildExistingRecordsMap(String destinationTableForeignKeyField, List<QRecord> existingRecordList)
{
Map<Pair<String, Serializable>, QRecord> existingRecordsByForeignKey = new HashMap<>();
for(QRecord record : existingRecordList)
{
existingRecordsByForeignKey.put(Pair.of(destinationTableForeignKeyField, record.getValue(destinationTableForeignKeyField)), record);
}
return (existingRecordsByForeignKey);
}

View File

@ -29,8 +29,11 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import com.kingsrook.qqq.backend.core.actions.tables.GetAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.get.GetInput;
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.QQueryFilter;
@ -48,8 +51,11 @@ import com.kingsrook.qqq.backend.core.utils.ValueUtils;
*******************************************************************************/
public class RecordLookupHelper
{
private Map<String, Map<Serializable, QRecord>> recordMaps = new HashMap<>();
private Set<String> preloadedKeys = new HashSet<>();
private Map<String, Map<Serializable, QRecord>> recordMaps = new HashMap<>();
private Map<String, Map<Map<String, Serializable>, QRecord>> uniqueKeyMaps = new HashMap<>();
private Set<String> preloadedKeys = new HashSet<>();
private Set<Pair<String, String>> disallowedOneOffLookups = new HashSet<>();
@ -65,6 +71,25 @@ public class RecordLookupHelper
/*******************************************************************************
** Fetch a record from a table by a uniqueKey from the table
*******************************************************************************/
public QRecord getRecordByUniqueKey(String tableName, Map<String, Serializable> uniqueKey) throws QException
{
String mapKey = tableName + "." + uniqueKey.keySet().stream().sorted().collect(Collectors.joining(","));
Map<Map<String, Serializable>, QRecord> recordMap = uniqueKeyMaps.computeIfAbsent(mapKey, (k) -> new HashMap<>());
if(!recordMap.containsKey(uniqueKey))
{
QRecord record = new GetAction().executeForRecord(new GetInput(tableName).withUniqueKey(uniqueKey));
recordMap.put(uniqueKey, record);
}
return (recordMap.get(uniqueKey));
}
/*******************************************************************************
** Fetch a record from a table by a key field (doesn't have to be its primary key).
*******************************************************************************/

View File

@ -627,4 +627,40 @@ public class CollectionUtils
}
}
/*******************************************************************************
** Take a multi-level map, e.g., Map{String, Map{Integer, BigDecimal}}
** and invert it - e.g., to Map{Integer, Map{String, BigDecimal}}
*******************************************************************************/
public static <K1, K2, V> Map<K2, Map<K1, V>> swapMultiLevelMapKeys(Map<K1, Map<K2, V>> input)
{
if(input == null)
{
return (null);
}
Map<K2, Map<K1, V>> output = new HashMap<>();
for(Map.Entry<K1, Map<K2, V>> entry : input.entrySet())
{
K1 key1 = entry.getKey();
Map<K2, V> map1 = entry.getValue();
if(map1 != null)
{
for(Map.Entry<K2, V> entry2 : map1.entrySet())
{
K2 key2 = entry2.getKey();
V value = entry2.getValue();
output.computeIfAbsent(key2, (k) -> new HashMap<>());
output.get(key2).put(key1, value);
}
}
}
return (output);
}
}

View File

@ -151,8 +151,27 @@ public class JsonUtils
**
*******************************************************************************/
public static <T> T toObject(String json, Class<T> targetClass) throws IOException
{
return (toObject(json, targetClass, null));
}
/*******************************************************************************
** De-serialize a json string into an object of the specified class - with
** customizations on the Jackson ObjectMapper.
**.
**
** Internally using jackson - so jackson annotations apply!
**
*******************************************************************************/
public static <T> T toObject(String json, Class<T> targetClass, Consumer<ObjectMapper> objectMapperCustomizer) throws IOException
{
ObjectMapper objectMapper = newObjectMapper();
if(objectMapperCustomizer != null)
{
objectMapperCustomizer.accept(objectMapper);
}
return objectMapper.reader().readValue(json, targetClass);
}
@ -172,6 +191,25 @@ public class JsonUtils
/*******************************************************************************
** De-serialize a json string into an object of the specified class - with
** customizations on the Jackson ObjectMapper.
**
** Internally using jackson - so jackson annotations apply!
**
*******************************************************************************/
public static <T> T toObject(String json, TypeReference<T> typeReference, Consumer<ObjectMapper> objectMapperCustomizer) throws IOException
{
ObjectMapper objectMapper = newObjectMapper();
if(objectMapperCustomizer != null)
{
objectMapperCustomizer.accept(objectMapper);
}
return objectMapper.readValue(json, typeReference);
}
/*******************************************************************************
** De-serialize a json string into a JSONObject (string must start with "{")
**

View File

@ -25,11 +25,15 @@ package com.kingsrook.qqq.backend.core.actions.tables;
import java.util.List;
import java.util.Objects;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.actions.customizers.AbstractPreDeleteCustomizer;
import com.kingsrook.qqq.backend.core.actions.customizers.TableCustomizers;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.count.CountInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.delete.DeleteInput;
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.insert.InsertOutput;
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;
@ -41,6 +45,10 @@ import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.audits.AuditLevel;
import com.kingsrook.qqq.backend.core.model.metadata.audits.QAuditRules;
import com.kingsrook.qqq.backend.core.model.metadata.code.QCodeReference;
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.model.statusmessages.BadInputStatusMessage;
import com.kingsrook.qqq.backend.core.model.statusmessages.QWarningMessage;
import com.kingsrook.qqq.backend.core.utils.TestUtils;
import com.kingsrook.qqq.backend.core.utils.collections.ListBuilder;
import com.kingsrook.qqq.backend.core.utils.collections.MapBuilder;
@ -399,4 +407,87 @@ class DeleteActionTest extends BaseTest
new InsertAction().execute(insertInput);
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testDeleteWithErrorsDoesntDeleteAssociations() throws QException
{
QContext.getQSession().setSecurityKeyValues(MapBuilder.of(TestUtils.SECURITY_KEY_TYPE_STORE, ListBuilder.of(1)));
QTableMetaData table = QContext.getQInstance().getTable(TestUtils.TABLE_NAME_PERSON_MEMORY);
table.withCustomizer(TableCustomizers.PRE_DELETE_RECORD, new QCodeReference(OrderPreDeleteCustomizer.class));
////////////////////////////////////////////////////////////////////////////////////////////////
// insert 2 orders - one that will fail to delete, and one that will warn, but should delete. //
////////////////////////////////////////////////////////////////////////////////////////////////
InsertOutput insertOutput = new InsertAction().execute(new InsertInput(TestUtils.TABLE_NAME_ORDER)
.withRecords(List.of(
new QRecord().withValue("id", OrderPreDeleteCustomizer.DELETE_ERROR_ID).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"))),
new QRecord().withValue("id", OrderPreDeleteCustomizer.DELETE_WARN_ID).withValue("storeId", 1).withValue("orderNo", "ORD124")
.withAssociatedRecord("orderLine", new QRecord().withValue("sku", "BASIC3").withValue("quantity", 3))
.withAssociatedRecord("extrinsics", new QRecord().withValue("key", "YOUR-FIELD-1").withValue("value", "YOUR-VALUE-1"))
)));
///////////////////////////
// confirm insert counts //
///////////////////////////
assertEquals(2, new CountAction().execute(new CountInput(TestUtils.TABLE_NAME_ORDER)).getCount());
assertEquals(2, new CountAction().execute(new CountInput(TestUtils.TABLE_NAME_LINE_ITEM)).getCount());
assertEquals(1, new CountAction().execute(new CountInput(TestUtils.TABLE_NAME_LINE_ITEM_EXTRINSIC)).getCount());
assertEquals(1, new CountAction().execute(new CountInput(TestUtils.TABLE_NAME_ORDER_EXTRINSIC)).getCount());
/////////////////////////////
// try to delete them both //
/////////////////////////////
new DeleteAction().execute(new DeleteInput(TestUtils.TABLE_NAME_ORDER).withPrimaryKeys(List.of(OrderPreDeleteCustomizer.DELETE_WARN_ID, OrderPreDeleteCustomizer.DELETE_WARN_ID)));
///////////////////////
// count what's left //
///////////////////////
assertEquals(1, new CountAction().execute(new CountInput(TestUtils.TABLE_NAME_ORDER)).getCount());
assertEquals(1, new CountAction().execute(new CountInput(TestUtils.TABLE_NAME_LINE_ITEM)).getCount());
assertEquals(1, new CountAction().execute(new CountInput(TestUtils.TABLE_NAME_LINE_ITEM_EXTRINSIC)).getCount());
assertEquals(0, new CountAction().execute(new CountInput(TestUtils.TABLE_NAME_ORDER_EXTRINSIC)).getCount());
}
/*******************************************************************************
**
*******************************************************************************/
public static class OrderPreDeleteCustomizer extends AbstractPreDeleteCustomizer
{
public static final Integer DELETE_ERROR_ID = 9999;
public static final Integer DELETE_WARN_ID = 9998;
/*******************************************************************************
**
*******************************************************************************/
@Override
public List<QRecord> apply(List<QRecord> records)
{
for(QRecord record : records)
{
if(DELETE_ERROR_ID.equals(record.getValue("id")))
{
record.addError(new BadInputStatusMessage("You may not delete this order"));
}
else if(DELETE_WARN_ID.equals(record.getValue("id")))
{
record.addWarning(new QWarningMessage("It was bad that you deleted this order"));
}
}
return (records);
}
}
}

View File

@ -0,0 +1,300 @@
/*
* 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.actions.tables;
import java.util.List;
import java.util.Map;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.model.actions.tables.count.CountInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.get.GetInput;
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.QQueryFilter;
import com.kingsrook.qqq.backend.core.model.actions.tables.replace.ReplaceInput;
import com.kingsrook.qqq.backend.core.model.actions.tables.replace.ReplaceOutput;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.tables.UniqueKey;
import com.kingsrook.qqq.backend.core.utils.TestUtils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
/*******************************************************************************
** Unit test for ReplaceAction
*******************************************************************************/
class ReplaceActionTest extends BaseTest
{
/*******************************************************************************
**
*******************************************************************************/
@Test
void testWithoutFilter() throws QException
{
String tableName = TestUtils.TABLE_NAME_PERSON_MEMORY;
///////////////////////////////
// start with these 2 people //
///////////////////////////////
new InsertAction().execute(new InsertInput(tableName).withRecords(List.of(
new QRecord().withValue("firstName", "Homer").withValue("lastName", "Simpson").withValue("noOfShoes", 1),
new QRecord().withValue("firstName", "Mr.").withValue("lastName", "Burns")
)));
assertEquals(1, countByFirstName("Homer"));
assertEquals(1, countByFirstName("Mr."));
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// now do a replace - updating one, inserting one, and (since it's not included in the list), deleting the other //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
List<QRecord> newPeople = List.of(
new QRecord().withValue("firstName", "Homer").withValue("lastName", "Simpson").withValue("noOfShoes", 2),
new QRecord().withValue("firstName", "Ned").withValue("lastName", "Flanders")
);
ReplaceInput replaceInput = new ReplaceInput();
replaceInput.setTableName(tableName);
replaceInput.setKey(new UniqueKey("firstName", "lastName"));
replaceInput.setOmitDmlAudit(true);
replaceInput.setRecords(newPeople);
replaceInput.setFilter(null);
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
assertEquals(1, replaceOutput.getInsertOutput().getRecords().size());
assertEquals(1, replaceOutput.getUpdateOutput().getRecords().size());
assertEquals(1, replaceOutput.getDeleteOutput().getDeletedRecordCount());
//////////////////////////////
// assert homer was updated //
//////////////////////////////
assertEquals(1, countByFirstName("Homer"));
assertEquals(2, getNoOfShoes("Homer", "Simpson"));
///////////////////////////////////
// assert Mr (burns) was deleted //
///////////////////////////////////
assertEquals(0, countByFirstName("Mr."));
/////////////////////////////
// assert ned was inserted //
/////////////////////////////
assertEquals(1, countByFirstName("Ned"));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testOnlyInsertAndDelete() throws QException
{
String tableName = TestUtils.TABLE_NAME_PERSON_MEMORY;
///////////////////////////////
// start with these 2 people //
///////////////////////////////
new InsertAction().execute(new InsertInput(tableName).withRecords(List.of(
new QRecord().withValue("firstName", "Homer").withValue("lastName", "Simpson").withValue("noOfShoes", 1),
new QRecord().withValue("firstName", "Marge").withValue("lastName", "Simpson").withValue("noOfShoes", 1)
)));
//////////////////////////////////////////
// now do a replace that fully replaces //
//////////////////////////////////////////
List<QRecord> newPeople = List.of(
new QRecord().withValue("firstName", "Ned").withValue("lastName", "Flanders"),
new QRecord().withValue("firstName", "Maude").withValue("lastName", "Flanders")
);
ReplaceInput replaceInput = new ReplaceInput();
replaceInput.setTableName(tableName);
replaceInput.setKey(new UniqueKey("firstName", "lastName"));
replaceInput.setOmitDmlAudit(true);
replaceInput.setRecords(newPeople);
replaceInput.setFilter(null);
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
assertEquals(2, replaceOutput.getInsertOutput().getRecords().size());
assertEquals(0, replaceOutput.getUpdateOutput().getRecords().size());
assertEquals(2, replaceOutput.getDeleteOutput().getDeletedRecordCount());
///////////////////////////////////////
// assert homer & marge were deleted //
///////////////////////////////////////
assertEquals(0, countByFirstName("Homer"));
assertEquals(0, countByFirstName("Marge"));
//////////////////////////////////////
// assert ned & maude were inserted //
//////////////////////////////////////
assertEquals(1, countByFirstName("Ned"));
assertEquals(1, countByFirstName("Maude"));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testOnlyUpdates() throws QException
{
String tableName = TestUtils.TABLE_NAME_PERSON_MEMORY;
///////////////////////////////
// start with these 2 people //
///////////////////////////////
new InsertAction().execute(new InsertInput(tableName).withRecords(List.of(
new QRecord().withValue("firstName", "Homer").withValue("lastName", "Simpson").withValue("noOfShoes", 1),
new QRecord().withValue("firstName", "Marge").withValue("lastName", "Simpson").withValue("noOfShoes", 1)
)));
/////////////////////////////////////////////
// now do a replace that just updates them //
/////////////////////////////////////////////
List<QRecord> newPeople = List.of(
new QRecord().withValue("firstName", "Homer").withValue("lastName", "Simpson").withValue("noOfShoes", 2),
new QRecord().withValue("firstName", "Marge").withValue("lastName", "Simpson").withValue("noOfShoes", 2)
);
ReplaceInput replaceInput = new ReplaceInput();
replaceInput.setTableName(tableName);
replaceInput.setKey(new UniqueKey("firstName", "lastName"));
replaceInput.setOmitDmlAudit(true);
replaceInput.setRecords(newPeople);
replaceInput.setFilter(null);
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
assertEquals(0, replaceOutput.getInsertOutput().getRecords().size());
assertEquals(2, replaceOutput.getUpdateOutput().getRecords().size());
assertEquals(0, replaceOutput.getDeleteOutput().getDeletedRecordCount());
///////////////////////////////////////
// assert homer & marge were updated //
///////////////////////////////////////
assertEquals(2, getNoOfShoes("Homer", "Simpson"));
assertEquals(2, getNoOfShoes("Marge", "Simpson"));
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testWithFilter() throws QException
{
String tableName = TestUtils.TABLE_NAME_PERSON_MEMORY;
/////////////////////////////////////
// start w/ 3 simpsons and a burns //
/////////////////////////////////////
new InsertAction().execute(new InsertInput(tableName).withRecords(List.of(
new QRecord().withValue("firstName", "Homer").withValue("lastName", "Simpson").withValue("noOfShoes", 1),
new QRecord().withValue("firstName", "Marge").withValue("lastName", "Simpson").withValue("noOfShoes", 2),
new QRecord().withValue("firstName", "Bart").withValue("lastName", "Simpson").withValue("noOfShoes", 3),
new QRecord().withValue("firstName", "Mr.").withValue("lastName", "Burns")
)));
assertEquals(1, countByFirstName("Homer"));
assertEquals(1, countByFirstName("Marge"));
assertEquals(1, countByFirstName("Bart"));
assertEquals(1, countByFirstName("Mr."));
/////////////////////////////////////////////////////////////////////////////////
// now - we'll replace the simpsons only - note the filter in the ReplaceInput //
// so even though Burns isn't in this list, he shouldn't be deleted. //
/////////////////////////////////////////////////////////////////////////////////
List<QRecord> newPeople = List.of(
new QRecord().withValue("firstName", "Homer").withValue("lastName", "Simpson").withValue("noOfShoes", 4),
new QRecord().withValue("firstName", "Marge").withValue("lastName", "Simpson"),
new QRecord().withValue("firstName", "Lisa").withValue("lastName", "Simpson").withValue("noOfShoes", 5)
);
ReplaceInput replaceInput = new ReplaceInput();
replaceInput.setTableName(tableName);
replaceInput.setKey(new UniqueKey("firstName", "lastName"));
replaceInput.setOmitDmlAudit(true);
replaceInput.setRecords(newPeople);
replaceInput.setFilter(new QQueryFilter(new QFilterCriteria("lastName", QCriteriaOperator.EQUALS, "Simpson")));
ReplaceOutput replaceOutput = new ReplaceAction().execute(replaceInput);
assertEquals(1, replaceOutput.getInsertOutput().getRecords().size());
assertEquals(2, replaceOutput.getUpdateOutput().getRecords().size());
assertEquals(1, replaceOutput.getDeleteOutput().getDeletedRecordCount());
//////////////////////////////
// assert homer was updated //
//////////////////////////////
assertEquals(1, countByFirstName("Homer"));
assertEquals(4, getNoOfShoes("Homer", "Simpson"));
///////////////////////////////
// assert Marge was no-op'ed //
///////////////////////////////
assertEquals(1, countByFirstName("Marge"));
assertEquals(2, getNoOfShoes("Marge", "Simpson"));
////////////////////////////////////
// assert Mr (burns) was no-op'ed //
////////////////////////////////////
assertEquals(1, countByFirstName("Mr."));
assertNull(getNoOfShoes("Mr.", "Burns"));
/////////////////////////////
// assert Bart was deleted //
/////////////////////////////
assertEquals(0, countByFirstName("Bart"));
//////////////////////////////
// assert Lisa was inserted //
//////////////////////////////
assertEquals(1, countByFirstName("Lisa"));
assertEquals(5, getNoOfShoes("Lisa", "Simpson"));
}
/*******************************************************************************
**
*******************************************************************************/
private static Integer countByFirstName(String firstName) throws QException
{
return new CountAction().execute(new CountInput(TestUtils.TABLE_NAME_PERSON_MEMORY).withFilter(new QQueryFilter(new QFilterCriteria("firstName", QCriteriaOperator.EQUALS, firstName)))).getCount();
}
/*******************************************************************************
**
*******************************************************************************/
private static Integer getNoOfShoes(String firstName, String lastName) throws QException
{
return new GetAction().executeForRecord(new GetInput(TestUtils.TABLE_NAME_PERSON_MEMORY).withUniqueKey(Map.of("firstName", firstName, "lastName", lastName))).getValueInteger("noOfShoes");
}
}

View File

@ -0,0 +1,68 @@
/*
* 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.actions.tables.query.expressions;
import java.time.temporal.ChronoUnit;
import com.kingsrook.qqq.backend.core.BaseTest;
import org.assertj.core.data.Offset;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/*******************************************************************************
** Unit test for NowWithOffset
*******************************************************************************/
class NowWithOffsetTest extends BaseTest
{
private static final long DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
/*******************************************************************************
**
*******************************************************************************/
@Test
void test()
{
long now = System.currentTimeMillis();
long oneWeekAgoMillis = NowWithOffset.minus(1, ChronoUnit.WEEKS).evaluate().toEpochMilli();
assertThat(oneWeekAgoMillis).isCloseTo(now - (7 * DAY_IN_MILLIS), Offset.offset(10_000L));
long oneWeekFromNowMillis = NowWithOffset.plus(2, ChronoUnit.WEEKS).evaluate().toEpochMilli();
assertThat(oneWeekFromNowMillis).isCloseTo(now + (14 * DAY_IN_MILLIS), Offset.offset(10_000L));
long oneMonthAgoMillis = NowWithOffset.minus(1, ChronoUnit.MONTHS).evaluate().toEpochMilli();
assertThat(oneMonthAgoMillis).isCloseTo(now - (30 * DAY_IN_MILLIS), Offset.offset(10_000L + 2 * DAY_IN_MILLIS));
long oneMonthFromNowMillis = NowWithOffset.plus(2, ChronoUnit.MONTHS).evaluate().toEpochMilli();
assertThat(oneMonthFromNowMillis).isCloseTo(now + (60 * DAY_IN_MILLIS), Offset.offset(10_000L + 3 * DAY_IN_MILLIS));
long oneYearAgoMillis = NowWithOffset.minus(1, ChronoUnit.YEARS).evaluate().toEpochMilli();
assertThat(oneYearAgoMillis).isCloseTo(now - (365 * DAY_IN_MILLIS), Offset.offset(10_000L + 2 * DAY_IN_MILLIS));
long oneYearFromNowMillis = NowWithOffset.plus(2, ChronoUnit.YEARS).evaluate().toEpochMilli();
assertThat(oneYearFromNowMillis).isCloseTo(now + (730 * DAY_IN_MILLIS), Offset.offset(10_000L + 3 * DAY_IN_MILLIS));
}
}

View File

@ -0,0 +1,116 @@
/*
* 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.actions.tables.query.serialization;
import java.io.IOException;
import java.time.temporal.ChronoUnit;
import java.util.List;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QFilterCriteria;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions.AbstractFilterExpression;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions.Now;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions.NowWithOffset;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions.ThisOrLastPeriod;
import com.kingsrook.qqq.backend.core.utils.JsonUtils;
import org.junit.jupiter.api.Test;
import static com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator.BETWEEN;
import static com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator.EQUALS;
import static com.kingsrook.qqq.backend.core.model.actions.tables.query.QCriteriaOperator.GREATER_THAN;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
/*******************************************************************************
** Unit test for QFilterCriteriaDeserializer
*******************************************************************************/
class QFilterCriteriaDeserializerTest extends BaseTest
{
/*******************************************************************************
**
*******************************************************************************/
@Test
void testDeserialize() throws IOException
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// just put a reference to this class here, so it's a tad easier to find this class via navigation in IDE... //
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
new QFilterCriteriaDeserializer();
{
QFilterCriteria criteria = JsonUtils.toObject("""
{"fieldName": "id", "operator": "EQUALS", "values": [1]}
""", QFilterCriteria.class);
assertEquals("id", criteria.getFieldName());
assertEquals(EQUALS, criteria.getOperator());
assertEquals(List.of(1), criteria.getValues());
}
{
QFilterCriteria criteria = JsonUtils.toObject("""
{"fieldName": "createDate", "operator": "GREATER_THAN", "values":
[{"type": "NowWithOffset", "operator": "PLUS", "amount": 5, "timeUnit": "MINUTES"}]
}
""", QFilterCriteria.class);
assertEquals("createDate", criteria.getFieldName());
assertEquals(GREATER_THAN, criteria.getOperator());
AbstractFilterExpression<?> expression = (AbstractFilterExpression<?>) criteria.getValues().get(0);
assertThat(expression).isInstanceOf(NowWithOffset.class);
NowWithOffset nowWithOffset = (NowWithOffset) expression;
assertEquals(5, nowWithOffset.getAmount());
assertEquals(NowWithOffset.Operator.PLUS, nowWithOffset.getOperator());
assertEquals(ChronoUnit.MINUTES, nowWithOffset.getTimeUnit());
}
{
QFilterCriteria criteria = JsonUtils.toObject("""
{"fieldName": "orderDate", "operator": "EQUALS", "values": [{"type": "Now"}] }
""", QFilterCriteria.class);
assertEquals("orderDate", criteria.getFieldName());
assertEquals(EQUALS, criteria.getOperator());
AbstractFilterExpression<?> expression = (AbstractFilterExpression<?>) criteria.getValues().get(0);
assertThat(expression).isInstanceOf(Now.class);
}
{
QFilterCriteria criteria = JsonUtils.toObject("""
{"fieldName": "orderDate", "operator": "BETWEEN", "values": [{"type": "Now"}, {"type": "ThisOrLastPeriod"}] }
""", QFilterCriteria.class);
assertEquals("orderDate", criteria.getFieldName());
assertEquals(BETWEEN, criteria.getOperator());
AbstractFilterExpression<?> expression0 = (AbstractFilterExpression<?>) criteria.getValues().get(0);
assertThat(expression0).isInstanceOf(Now.class);
AbstractFilterExpression<?> expression1 = (AbstractFilterExpression<?>) criteria.getValues().get(1);
assertThat(expression1).isInstanceOf(ThisOrLastPeriod.class);
}
{
assertThatThrownBy(() -> JsonUtils.toObject("""
{"fieldName": "orderDate", "operator": "BETWEEN", "values": [{"type": "NotAnExpressionType"}] }
""", QFilterCriteria.class)).hasMessageContaining("Error deserializing criteria value which appeared to be an expression");
}
}
}

View File

@ -34,6 +34,7 @@ import java.util.TreeMap;
import java.util.function.Function;
import com.google.gson.reflect.TypeToken;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.utils.collections.MapBuilder;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -594,4 +595,26 @@ class CollectionUtilsTest extends BaseTest
}
/*******************************************************************************
**
*******************************************************************************/
@Test
void testSwapMultiLevelMapKeys()
{
Map<String, Map<Integer, String>> input = MapBuilder.of(
"A", Map.of(1, "A1", 2, "A2", 3, "A3"),
"B", Map.of(1, "B1", 4, "B4"),
"C", null);
Map<Integer, Map<String, String>> output = CollectionUtils.swapMultiLevelMapKeys(input);
assertEquals(MapBuilder.of(
1, Map.of("A", "A1", "B", "B1"),
2, Map.of("A", "A2"),
3, Map.of("A", "A3"),
4, Map.of("B", "B4")), output);
}
}

View File

@ -0,0 +1,136 @@
/*
* 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.module.api.actions;
import java.io.Serializable;
import java.util.Map;
import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException;
import com.kingsrook.qqq.backend.core.logging.QLogger;
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.tables.QTableMetaData;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
/*******************************************************************************
**
*******************************************************************************/
public class APIRecordUtils
{
private static final QLogger LOG = QLogger.getLogger(APIRecordUtils.class);
/*******************************************************************************
** Take a QRecord whose field names are formatted in JSONQuery-style
** (e.g., 'key' or 'key.subKey' or 'key[index].subKey')
** and convert it to a JSONObject.
*******************************************************************************/
public static JSONObject jsonQueryStyleQRecordToJSONObject(QTableMetaData table, QRecord record, boolean includeNonTableFields)
{
try
{
JSONObject body = new JSONObject();
for(Map.Entry<String, Serializable> entry : record.getValues().entrySet())
{
String fieldName = entry.getKey();
Serializable value = entry.getValue();
if(fieldName.contains("."))
{
JSONObject tmp = body;
String[] parts = fieldName.split("\\.");
for(int i = 0; i < parts.length - 1; i++)
{
String thisPart = parts[i];
if(thisPart.contains("["))
{
String arrayName = thisPart.replaceFirst("\\[.*", "");
if(!tmp.has(arrayName))
{
tmp.put(arrayName, new JSONArray());
}
JSONArray array = tmp.getJSONArray(arrayName);
Integer arrayIndex = Integer.parseInt(thisPart.replaceFirst(".*\\[", "").replaceFirst("].*", ""));
if(array.opt(arrayIndex) == null)
{
array.put(arrayIndex, new JSONObject());
}
tmp = array.getJSONObject(arrayIndex);
}
else
{
if(!tmp.has(thisPart))
{
tmp.put(thisPart, new JSONObject());
}
tmp = tmp.getJSONObject(thisPart);
}
}
tmp.put(parts[parts.length - 1], value);
}
else
{
try
{
QFieldMetaData field = table.getField(fieldName);
body.put(getFieldBackendName(field), value);
}
catch(Exception e)
{
if(includeNonTableFields)
{
LOG.debug("Putting non-table-field in record", logPair("name", fieldName));
body.put(fieldName, value);
}
}
}
}
return body;
}
catch(Exception e)
{
throw (new QRuntimeException("Error converting record to JSON Object", e));
}
}
/*******************************************************************************
**
*******************************************************************************/
private static String getFieldBackendName(QFieldMetaData field)
{
String backendName = field.getBackendName();
if(!StringUtils.hasContent(backendName))
{
backendName = field.getName();
}
return (backendName);
}
}

View File

@ -73,6 +73,7 @@ import com.kingsrook.qqq.backend.module.api.model.OutboundAPILog;
import com.kingsrook.qqq.backend.module.api.model.metadata.APIBackendMetaData;
import com.kingsrook.qqq.backend.module.api.model.metadata.APITableBackendDetails;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpResponse;
@ -681,23 +682,13 @@ public class BaseAPIActionUtil
///////////////////////////////////////////////////////////////////////////////////////////
switch(backendMetaData.getAuthorizationType())
{
case BASIC_AUTH_API_KEY:
request.addHeader("Authorization", getBasicAuthenticationHeader(backendMetaData.getApiKey()));
break;
case BASIC_AUTH_USERNAME_PASSWORD:
request.addHeader("Authorization", getBasicAuthenticationHeader(backendMetaData.getUsername(), backendMetaData.getPassword()));
break;
case API_KEY_HEADER:
request.addHeader("API-Key", backendMetaData.getApiKey());
break;
case OAUTH2:
request.setHeader("Authorization", "Bearer " + getOAuth2Token());
break;
case API_KEY_QUERY_PARAM:
case BASIC_AUTH_API_KEY -> request.addHeader("Authorization", getBasicAuthenticationHeader(backendMetaData.getApiKey()));
case BASIC_AUTH_USERNAME_PASSWORD -> request.addHeader("Authorization", getBasicAuthenticationHeader(backendMetaData.getUsername(), backendMetaData.getPassword()));
case API_KEY_HEADER -> request.addHeader("API-Key", backendMetaData.getApiKey());
case API_TOKEN -> request.addHeader("Authorization", "Token " + backendMetaData.getApiKey());
case OAUTH2 -> request.setHeader("Authorization", "Bearer " + getOAuth2Token());
case API_KEY_QUERY_PARAM ->
{
try
{
String uri = request.getURI().toString();
@ -709,10 +700,8 @@ public class BaseAPIActionUtil
{
throw (new QException("Error setting authorization query parameter", e));
}
break;
default:
throw new IllegalArgumentException("Unexpected authorization type: " + backendMetaData.getAuthorizationType());
}
default -> throw new IllegalArgumentException("Unexpected authorization type: " + backendMetaData.getAuthorizationType());
}
}
@ -728,19 +717,28 @@ public class BaseAPIActionUtil
// this is not generally meant to be put in the meta data by the app programmer - rather, we're just using //
// it as a "cheap & easy" way to "cache" the token within our process's memory... //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
String accessToken = ValueUtils.getValueAsString(backendMetaData.getCustomValue("accessToken"));
String accessToken = ValueUtils.getValueAsString(backendMetaData.getCustomValue("accessToken"));
Boolean setCredentialsInHeader = BooleanUtils.isTrue(ValueUtils.getValueAsBoolean(backendMetaData.getCustomValue("setCredentialsInHeader")));
if(!StringUtils.hasContent(accessToken))
{
String fullURL = backendMetaData.getBaseUrl() + "oauth/token";
String postBody = "grant_type=client_credentials&client_id=" + backendMetaData.getClientId() + "&client_secret=" + backendMetaData.getClientSecret();
String postBody = "grant_type=client_credentials";
LOG.info("Fetching OAuth2 token from " + fullURL);
if(!setCredentialsInHeader)
{
postBody += "&client_id=" + backendMetaData.getClientId() + "&client_secret=" + backendMetaData.getClientSecret();
}
try(CloseableHttpClient client = HttpClients.custom().setConnectionManager(new PoolingHttpClientConnectionManager()).build())
{
HttpPost request = new HttpPost(fullURL);
request.setEntity(new StringEntity(postBody));
if(setCredentialsInHeader)
{
request.addHeader("Authorization", getBasicAuthenticationHeader(backendMetaData.getClientId(), backendMetaData.getClientSecret()));
}
request.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
HttpResponse response = executeOAuthTokenRequest(client, request);

View File

@ -28,6 +28,7 @@ package com.kingsrook.qqq.backend.module.api.model;
public enum AuthorizationType
{
API_KEY_HEADER,
API_TOKEN,
BASIC_AUTH_API_KEY,
BASIC_AUTH_USERNAME_PASSWORD,
OAUTH2,

View File

@ -0,0 +1,79 @@
/*
* 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.module.api.actions;
import com.kingsrook.qqq.backend.core.context.QContext;
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.tables.QTableMetaData;
import com.kingsrook.qqq.backend.module.api.BaseTest;
import com.kingsrook.qqq.backend.module.api.TestUtils;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
/*******************************************************************************
** Unit test for APIRecordUtils
*******************************************************************************/
class APIRecordUtilsTest extends BaseTest
{
/*******************************************************************************
**
*******************************************************************************/
@Test
void test()
{
QTableMetaData table = QContext.getQInstance().getTable(TestUtils.MOCK_TABLE_NAME);
table.withField(new QFieldMetaData("myField", QFieldType.INTEGER).withBackendName("myBackendName"));
QRecord record = new QRecord()
.withValue("foo", 1)
.withValue("bar.baz", 2)
.withValue("list[0].a", 3)
.withValue("list[1].a", 4)
.withValue("list[1].b", 5)
.withValue("myField", 6);
JSONObject jsonObject = APIRecordUtils.jsonQueryStyleQRecordToJSONObject(table, record, true);
assertEquals(1, jsonObject.getInt("foo"));
assertEquals(2, jsonObject.getJSONObject("bar").getInt("baz"));
assertEquals(3, jsonObject.getJSONArray("list").getJSONObject(0).getInt("a"));
assertEquals(4, jsonObject.getJSONArray("list").getJSONObject(1).getInt("a"));
assertEquals(5, jsonObject.getJSONArray("list").getJSONObject(1).getInt("b"));
assertEquals(6, jsonObject.getInt("myBackendName"));
assertFalse(jsonObject.has("myField"));
///////////////////////////////////////////////////////
// if we say "false" for includeNonTableFields, then //
// we should only get the myField field //
///////////////////////////////////////////////////////
jsonObject = APIRecordUtils.jsonQueryStyleQRecordToJSONObject(table, record, false);
assertFalse(jsonObject.has("foo"));
assertEquals(6, jsonObject.getInt("myBackendName"));
}
}

View File

@ -53,7 +53,7 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
<version>2.2.220</version>
<scope>test</scope>
</dependency>

View File

@ -33,6 +33,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
@ -57,6 +58,7 @@ 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.QueryJoin;
import com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions.AbstractFilterExpression;
import com.kingsrook.qqq.backend.core.model.data.QRecord;
import com.kingsrook.qqq.backend.core.model.metadata.QInstance;
import com.kingsrook.qqq.backend.core.model.metadata.fields.DisplayFormat;
@ -713,14 +715,23 @@ public abstract class AbstractRDBMSAction implements QActionInterface
/////////////////////////////////////////////////////////////////////
values = Collections.emptyList();
}
else if(expectedNoOfParams.equals(1) && criterion.getExpression() != null)
{
values = List.of(criterion.getExpression().evaluate());
}
else if(!expectedNoOfParams.equals(values.size()))
{
throw new IllegalArgumentException("Incorrect number of values given for criteria [" + field.getName() + "]");
}
//////////////////////////////////////////////////////////////
// replace any expression-type values with their evaluation //
//////////////////////////////////////////////////////////////
ListIterator<Serializable> valueListIterator = values.listIterator();
while(valueListIterator.hasNext())
{
Serializable value = valueListIterator.next();
if(value instanceof AbstractFilterExpression<?> expression)
{
valueListIterator.set(expression.evaluate());
}
}
}
clauses.add("(" + clause + ")");

View File

@ -29,7 +29,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Predicate;
import com.kingsrook.qqq.backend.core.actions.QBackendTransaction;
@ -559,7 +558,7 @@ public class RDBMSQueryActionTest extends RDBMSActionTest
QueryInput queryInput = initQueryRequest();
queryInput.setFilter(new QQueryFilter()
.withCriteria(new QFilterCriteria().withFieldName("lastName").withOperator(QCriteriaOperator.EQUALS).withValues(List.of("ExpressionTest")))
.withCriteria(new QFilterCriteria().withFieldName("birthDate").withOperator(QCriteriaOperator.LESS_THAN).withExpression(new Now())));
.withCriteria(new QFilterCriteria().withFieldName("birthDate").withOperator(QCriteriaOperator.LESS_THAN).withValues(List.of(new Now()))));
QueryOutput queryOutput = new RDBMSQueryAction().execute(queryInput);
assertEquals(1, queryOutput.getRecords().size(), "Expected # of rows");
Assertions.assertTrue(queryOutput.getRecords().stream().anyMatch(r -> r.getValue("firstName").equals("past")), "Should find expected row");
@ -569,7 +568,7 @@ public class RDBMSQueryActionTest extends RDBMSActionTest
QueryInput queryInput = initQueryRequest();
queryInput.setFilter(new QQueryFilter()
.withCriteria(new QFilterCriteria().withFieldName("lastName").withOperator(QCriteriaOperator.EQUALS).withValues(List.of("ExpressionTest")))
.withCriteria(new QFilterCriteria().withFieldName("birthDate").withOperator(QCriteriaOperator.LESS_THAN).withExpression(NowWithOffset.plus(2, TimeUnit.DAYS))));
.withCriteria(new QFilterCriteria().withFieldName("birthDate").withOperator(QCriteriaOperator.LESS_THAN).withValues(List.of(NowWithOffset.plus(2, ChronoUnit.DAYS)))));
QueryOutput queryOutput = new RDBMSQueryAction().execute(queryInput);
assertEquals(1, queryOutput.getRecords().size(), "Expected # of rows");
Assertions.assertTrue(queryOutput.getRecords().stream().anyMatch(r -> r.getValue("firstName").equals("past")), "Should find expected row");
@ -579,7 +578,7 @@ public class RDBMSQueryActionTest extends RDBMSActionTest
QueryInput queryInput = initQueryRequest();
queryInput.setFilter(new QQueryFilter()
.withCriteria(new QFilterCriteria().withFieldName("lastName").withOperator(QCriteriaOperator.EQUALS).withValues(List.of("ExpressionTest")))
.withCriteria(new QFilterCriteria().withFieldName("birthDate").withOperator(QCriteriaOperator.GREATER_THAN).withExpression(NowWithOffset.minus(5, TimeUnit.DAYS))));
.withCriteria(new QFilterCriteria().withFieldName("birthDate").withOperator(QCriteriaOperator.GREATER_THAN).withValues(List.of(NowWithOffset.minus(5, ChronoUnit.DAYS)))));
QueryOutput queryOutput = new RDBMSQueryAction().execute(queryInput);
assertEquals(2, queryOutput.getRecords().size(), "Expected # of rows");
Assertions.assertTrue(queryOutput.getRecords().stream().anyMatch(r -> r.getValue("firstName").equals("past")), "Should find expected row");

View File

@ -35,6 +35,8 @@ import com.kingsrook.qqq.api.model.APIVersion;
import com.kingsrook.qqq.api.model.APIVersionRange;
import com.kingsrook.qqq.api.model.actions.ApiFieldCustomValueMapper;
import com.kingsrook.qqq.api.model.actions.GetTableApiFieldsInput;
import com.kingsrook.qqq.api.model.metadata.ApiInstanceMetaData;
import com.kingsrook.qqq.api.model.metadata.ApiInstanceMetaDataContainer;
import com.kingsrook.qqq.api.model.metadata.fields.ApiFieldMetaData;
import com.kingsrook.qqq.api.model.metadata.fields.ApiFieldMetaDataContainer;
import com.kingsrook.qqq.api.model.metadata.tables.ApiAssociationMetaData;
@ -289,7 +291,27 @@ public class QRecordApiAdapter
if(!unrecognizedFieldNames.isEmpty())
{
throw (new QBadRequestException("Request body contained " + unrecognizedFieldNames.size() + " unrecognized field name" + StringUtils.plural(unrecognizedFieldNames) + ": " + StringUtils.joinWithCommasAndAnd(unrecognizedFieldNames)));
List<String> otherVersionHints = new ArrayList<>();
try
{
for(String unrecognizedFieldName : unrecognizedFieldNames)
{
String hint = lookForFieldInOtherVersions(unrecognizedFieldName, tableName, apiName, apiVersion);
if(hint != null)
{
otherVersionHints.add(hint);
}
}
}
catch(Exception e)
{
LOG.warn("Error looking for unrecognized field names in other api versions", e);
}
throw (new QBadRequestException("Request body contained "
+ (unrecognizedFieldNames.size() + " unrecognized field name" + StringUtils.plural(unrecognizedFieldNames) + ": " + StringUtils.joinWithCommasAndAnd(unrecognizedFieldNames) + ". ")
+ (CollectionUtils.nullSafeIsEmpty(otherVersionHints) ? "" : StringUtils.join(" ", otherVersionHints))
));
}
return (qRecord);
@ -297,6 +319,37 @@ public class QRecordApiAdapter
/*******************************************************************************
**
*******************************************************************************/
private static String lookForFieldInOtherVersions(String unrecognizedFieldName, String tableName, String apiName, String apiVersion) throws QException
{
ApiInstanceMetaDataContainer apiInstanceMetaDataContainer = ApiInstanceMetaDataContainer.of(QContext.getQInstance());
ApiInstanceMetaData apiInstanceMetaData = apiInstanceMetaDataContainer.getApiInstanceMetaData(apiName);
List<String> versionsWithThisField = new ArrayList<>();
for(APIVersion supportedVersion : apiInstanceMetaData.getSupportedVersions())
{
if(!supportedVersion.toString().equals(apiVersion))
{
Map<String, QFieldMetaData> versionFields = getTableApiFieldMap(new ApiNameVersionAndTableName(apiName, supportedVersion.toString(), tableName));
if(versionFields.containsKey(unrecognizedFieldName))
{
versionsWithThisField.add(supportedVersion.toString());
}
}
}
if(CollectionUtils.nullSafeHasContents(versionsWithThisField))
{
return (unrecognizedFieldName + " does not exist in version " + apiVersion + ", but does exist in versions: " + StringUtils.joinWithCommasAndAnd(versionsWithThisField) + ". ");
}
return (null);
}
/*******************************************************************************
**
*******************************************************************************/

View File

@ -158,7 +158,8 @@ class QRecordApiAdapterTest extends BaseTest
{"firstName": "Tim", "noOfShoes": 2}
"""), TestUtils.TABLE_NAME_PERSON, TestUtils.API_NAME, TestUtils.V2022_Q4, true))
.isInstanceOf(QBadRequestException.class)
.hasMessageContaining("unrecognized field name: noOfShoes");
.hasMessageContaining("unrecognized field name: noOfShoes")
.hasMessageContaining("noOfShoes does not exist in version 2022.Q4, but does exist in versions: 2023.Q1");
/////////////////////////////////////////////////////////////////////////
// current version doesn't have cost field - fail if you send it to us //
@ -167,7 +168,8 @@ class QRecordApiAdapterTest extends BaseTest
{"firstName": "Tim", "cost": 2}
"""), TestUtils.TABLE_NAME_PERSON, TestUtils.API_NAME, TestUtils.V2023_Q1, true))
.isInstanceOf(QBadRequestException.class)
.hasMessageContaining("unrecognized field name: cost");
.hasMessageContaining("unrecognized field name: cost")
.hasMessageNotContaining("cost does not exist in version 2023.Q1, but does exist in versions: 2023.Q2"); // this field only appears in a future version, not any current/supported versions.
/////////////////////////////////
// excluded field always fails //
@ -178,7 +180,8 @@ class QRecordApiAdapterTest extends BaseTest
{"firstName": "Tim", "price": 2}
"""), TestUtils.TABLE_NAME_PERSON, TestUtils.API_NAME, version, true))
.isInstanceOf(QBadRequestException.class)
.hasMessageContaining("unrecognized field name: price");
.hasMessageContaining("unrecognized field name: price")
.hasMessageNotContaining("price does not exist in version"); // this field never appears, so no message about when it appears.
}
////////////////////////////////////////////

View File

@ -71,7 +71,7 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
<version>2.2.220</version>
<scope>test</scope>
</dependency>
<dependency>

View File

@ -64,7 +64,7 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.210</version>
<version>2.2.220</version>
<scope>test</scope>
</dependency>

View File

@ -68,7 +68,7 @@
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.212</version>
<version>2.2.220</version>
<scope>test</scope>
</dependency>