mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-20 22:18:43 +00:00
CE-1068: initial checkin of variables in filter handling
This commit is contained in:
@ -557,7 +557,7 @@ public class GenerateReportAction extends AbstractQActionFunction<ReportInput, R
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
private void setInputValuesInQueryFilter(ReportInput reportInput, QQueryFilter queryFilter)
|
||||
private void setInputValuesInQueryFilter(ReportInput reportInput, QQueryFilter queryFilter) throws QException
|
||||
{
|
||||
if(queryFilter == null || queryFilter.getCriteria() == null)
|
||||
{
|
||||
|
@ -25,13 +25,17 @@ package com.kingsrook.qqq.backend.core.model.actions.tables.query;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.instances.QMetaDataVariableInterpreter;
|
||||
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.expressions.FilterVariableExpression;
|
||||
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
|
||||
import com.kingsrook.qqq.backend.core.utils.StringUtils;
|
||||
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
|
||||
|
||||
|
||||
@ -411,6 +415,83 @@ public class QQueryFilter implements Serializable, Cloneable
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Replaces any FilterVariables' variableNames with one constructed from the field
|
||||
** name, criteria, and index, camel style
|
||||
**
|
||||
*******************************************************************************/
|
||||
public void prepForBackend()
|
||||
{
|
||||
Map<String, Integer> fieldOperatorMap = new HashMap<>();
|
||||
for(QFilterCriteria criterion : getCriteria())
|
||||
{
|
||||
if(criterion.getValues() != null)
|
||||
{
|
||||
int criteriaIndex = 1;
|
||||
int valueIndex = 0;
|
||||
for(Serializable value : criterion.getValues())
|
||||
{
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// keep track of what the index is for this criterion, this way if there are //
|
||||
// more than one with the same id/operator values, we can differentiate //
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
String backendName = getBackendName(criterion, valueIndex);
|
||||
if(!fieldOperatorMap.containsKey(backendName))
|
||||
{
|
||||
fieldOperatorMap.put(backendName, criteriaIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
criteriaIndex = fieldOperatorMap.get(backendName) + 1;
|
||||
fieldOperatorMap.put(backendName, criteriaIndex);
|
||||
}
|
||||
|
||||
if(value instanceof FilterVariableExpression fve)
|
||||
{
|
||||
if(criteriaIndex > 1)
|
||||
{
|
||||
backendName += criteriaIndex;
|
||||
}
|
||||
fve.setVariableName(backendName);
|
||||
}
|
||||
|
||||
valueIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** builds up a backend name for a field variable expression
|
||||
**
|
||||
*******************************************************************************/
|
||||
private String getBackendName(QFilterCriteria criterion, int valueIndex)
|
||||
{
|
||||
StringBuilder backendName = new StringBuilder(criterion.getFieldName());
|
||||
for(String operatorParts : criterion.getOperator().name().split("_"))
|
||||
{
|
||||
backendName.append(StringUtils.ucFirst(operatorParts.toLowerCase()));
|
||||
}
|
||||
|
||||
if(criterion.getOperator().equals(QCriteriaOperator.BETWEEN) || criterion.getOperator().equals(QCriteriaOperator.NOT_BETWEEN))
|
||||
{
|
||||
if(valueIndex == 0)
|
||||
{
|
||||
backendName.append("From");
|
||||
}
|
||||
else
|
||||
{
|
||||
backendName.append("To");
|
||||
}
|
||||
}
|
||||
|
||||
return (backendName.toString());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Replace any criteria values that look like ${input.XXX} with the value of XXX
|
||||
** from the supplied inputValues map.
|
||||
@ -419,7 +500,7 @@ public class QQueryFilter implements Serializable, Cloneable
|
||||
** QQueryFilter - e.g., if it's one that defined in metaData, and that we don't
|
||||
** want to be (permanently) changed!!
|
||||
*******************************************************************************/
|
||||
public void interpretValues(Map<String, Serializable> inputValues)
|
||||
public void interpretValues(Map<String, Serializable> inputValues) throws QException
|
||||
{
|
||||
QMetaDataVariableInterpreter variableInterpreter = new QMetaDataVariableInterpreter();
|
||||
variableInterpreter.addValueMap("input", inputValues);
|
||||
@ -433,11 +514,18 @@ public class QQueryFilter implements Serializable, Cloneable
|
||||
{
|
||||
if(value instanceof AbstractFilterExpression<?>)
|
||||
{
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// todo - do we want to try to interpret values within the expression? //
|
||||
// e.g., greater than now minus ${input.noOfDays} //
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
newValues.add(value);
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
// if a filter variable expression, evaluate the input values, which //
|
||||
// will replace the variables with the corresponding actual values //
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
if(value instanceof FilterVariableExpression filterVariableExpression)
|
||||
{
|
||||
newValues.add(filterVariableExpression.evaluateInputValues(inputValues));
|
||||
}
|
||||
else
|
||||
{
|
||||
newValues.add(value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -23,6 +23,8 @@ package com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions;
|
||||
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@ -33,7 +35,17 @@ public abstract class AbstractFilterExpression<T extends Serializable> implement
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public abstract T evaluate();
|
||||
public abstract T evaluate() throws QException;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public T evaluateInputValues(Map<String, Serializable> inputValues) throws QException
|
||||
{
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,213 @@
|
||||
/*
|
||||
* 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.io.Serializable;
|
||||
import java.util.Map;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
public class FilterVariableExpression extends AbstractFilterExpression<Serializable>
|
||||
{
|
||||
private String variableName;
|
||||
private String fieldName;
|
||||
private String operator;
|
||||
private int valueIndex = 0;
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public Serializable evaluate() throws QException
|
||||
{
|
||||
throw (new QUserFacingException("Missing variable value."));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public Serializable evaluateInputValues(Map<String, Serializable> inputValues) throws QException
|
||||
{
|
||||
if(!inputValues.containsKey(variableName))
|
||||
{
|
||||
throw (new QUserFacingException("Missing variable value."));
|
||||
}
|
||||
return (inputValues.get(variableName));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Constructor
|
||||
**
|
||||
*******************************************************************************/
|
||||
public FilterVariableExpression()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Constructor
|
||||
**
|
||||
*******************************************************************************/
|
||||
private FilterVariableExpression(String fieldName, int valueIndex)
|
||||
{
|
||||
this.fieldName = fieldName;
|
||||
this.valueIndex = valueIndex;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for valueIndex
|
||||
*******************************************************************************/
|
||||
public int getValueIndex()
|
||||
{
|
||||
return (this.valueIndex);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for valueIndex
|
||||
*******************************************************************************/
|
||||
public void setValueIndex(int valueIndex)
|
||||
{
|
||||
this.valueIndex = valueIndex;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for valueIndex
|
||||
*******************************************************************************/
|
||||
public FilterVariableExpression withValueIndex(int valueIndex)
|
||||
{
|
||||
this.valueIndex = valueIndex;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for fieldName
|
||||
*******************************************************************************/
|
||||
public String getFieldName()
|
||||
{
|
||||
return (this.fieldName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for fieldName
|
||||
*******************************************************************************/
|
||||
public void setFieldName(String fieldName)
|
||||
{
|
||||
this.fieldName = fieldName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for fieldName
|
||||
*******************************************************************************/
|
||||
public FilterVariableExpression withFieldName(String fieldName)
|
||||
{
|
||||
this.fieldName = fieldName;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for variableName
|
||||
*******************************************************************************/
|
||||
public String getVariableName()
|
||||
{
|
||||
return (this.variableName);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for variableName
|
||||
*******************************************************************************/
|
||||
public void setVariableName(String variableName)
|
||||
{
|
||||
this.variableName = variableName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for variableName
|
||||
*******************************************************************************/
|
||||
public FilterVariableExpression withVariableName(String variableName)
|
||||
{
|
||||
this.variableName = variableName;
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Getter for operator
|
||||
*******************************************************************************/
|
||||
public String getOperator()
|
||||
{
|
||||
return (this.operator);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Setter for operator
|
||||
*******************************************************************************/
|
||||
public void setOperator(String operator)
|
||||
{
|
||||
this.operator = operator;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Fluent setter for operator
|
||||
*******************************************************************************/
|
||||
public FilterVariableExpression withOperator(String operator)
|
||||
{
|
||||
this.operator = operator;
|
||||
return (this);
|
||||
}
|
||||
|
||||
}
|
@ -23,6 +23,7 @@ package com.kingsrook.qqq.backend.core.model.actions.tables.query.expressions;
|
||||
|
||||
|
||||
import java.time.Instant;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@ -35,7 +36,7 @@ public class Now extends AbstractFilterExpression<Instant>
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public Instant evaluate()
|
||||
public Instant evaluate() throws QException
|
||||
{
|
||||
return (Instant.now());
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
@ -119,7 +120,7 @@ public class NowWithOffset extends AbstractFilterExpression<Instant>
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public Instant evaluate()
|
||||
public Instant evaluate() throws QException
|
||||
{
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Instant doesn't let us plus/minus WEEK, MONTH, or YEAR... //
|
||||
|
@ -28,6 +28,7 @@ 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.QException;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QRuntimeException;
|
||||
import com.kingsrook.qqq.backend.core.utils.ValueUtils;
|
||||
|
||||
@ -95,7 +96,7 @@ public class ThisOrLastPeriod extends AbstractFilterExpression<Instant>
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Override
|
||||
public Instant evaluate()
|
||||
public Instant evaluate() throws QException
|
||||
{
|
||||
ZoneId zoneId = ValueUtils.getSessionOrInstanceZoneId();
|
||||
|
||||
|
@ -35,6 +35,7 @@ import com.kingsrook.qqq.backend.core.model.actions.reporting.pivottable.PivotTa
|
||||
import com.kingsrook.qqq.backend.core.model.actions.reporting.pivottable.PivotTableGroupBy;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.reporting.pivottable.PivotTableValue;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.insert.InsertInput;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.query.QQueryFilter;
|
||||
import com.kingsrook.qqq.backend.core.model.actions.tables.update.UpdateInput;
|
||||
import com.kingsrook.qqq.backend.core.model.data.QRecord;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
@ -110,10 +111,12 @@ public class SavedReportTableCustomizer implements TableCustomizerInterface
|
||||
{
|
||||
try
|
||||
{
|
||||
////////////////////////////////////////////////////////////////
|
||||
// nothing to validate on filter, other than, we can parse it //
|
||||
////////////////////////////////////////////////////////////////
|
||||
SavedReportToReportMetaDataAdapter.getQQueryFilter(queryFilterJson);
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
// validate that we can parse the filter, then prep it for the backend //
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
QQueryFilter filter = SavedReportToReportMetaDataAdapter.getQQueryFilter(queryFilterJson);
|
||||
filter.prepForBackend();
|
||||
record.setValue("queryFilterJson", filter);
|
||||
}
|
||||
catch(IOException e)
|
||||
{
|
||||
|
@ -32,6 +32,8 @@ import java.util.ListIterator;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import com.kingsrook.qqq.backend.core.exceptions.QException;
|
||||
import com.kingsrook.qqq.backend.core.logging.QLogger;
|
||||
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;
|
||||
@ -50,6 +52,9 @@ import org.apache.commons.lang.NotImplementedException;
|
||||
*******************************************************************************/
|
||||
public class BackendQueryFilterUtils
|
||||
{
|
||||
private static final QLogger LOG = QLogger.getLogger(BackendQueryFilterUtils.class);
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
** Test if record matches filter.
|
||||
@ -138,7 +143,14 @@ public class BackendQueryFilterUtils
|
||||
Serializable criteriaValue = valueListIterator.next();
|
||||
if(criteriaValue instanceof AbstractFilterExpression<?> expression)
|
||||
{
|
||||
valueListIterator.set(expression.evaluate());
|
||||
try
|
||||
{
|
||||
valueListIterator.set(expression.evaluate());
|
||||
}
|
||||
catch(QException qe)
|
||||
{
|
||||
LOG.warn("Unexpected exception caught evaluating expression", qe);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user