
com.xresch.cfw.features.query.functions.CFWQueryFunctionIf Maven / Gradle / Ivy
package com.xresch.cfw.features.query.functions;
import java.util.ArrayList;
import java.util.TreeSet;
import com.xresch.cfw._main.CFW;
import com.xresch.cfw.features.query.CFWQueryContext;
import com.xresch.cfw.features.query.CFWQueryFunction;
import com.xresch.cfw.features.query.EnhancedJsonObject;
import com.xresch.cfw.features.query.FeatureQuery;
import com.xresch.cfw.features.query.parse.QueryPartValue;
/************************************************************************************************************
*
* @author Reto Scheiwiller, (c) Copyright 2023
* @license MIT-License
************************************************************************************************************/
public class CFWQueryFunctionIf extends CFWQueryFunction {
public static final String FUNCTION_NAME = "if";
public CFWQueryFunctionIf(CFWQueryContext context) {
super(context);
}
/***********************************************************************************************
*
***********************************************************************************************/
@Override
public String uniqueName() {
return FUNCTION_NAME;
}
/***********************************************************************************************
*
***********************************************************************************************/
@Override
public TreeSet getTags(){
TreeSet tags = new TreeSet<>();
tags.add(CFWQueryFunction.TAG_CODING);
return tags;
}
/***********************************************************************************************
*
***********************************************************************************************/
@Override
public String descriptionSyntax() {
return FUNCTION_NAME+"(condition, trueValue, falseValue)";
}
/***********************************************************************************************
*
***********************************************************************************************/
@Override
public String descriptionShort() {
return "Evaluates the condition, returns the respective value for true or false.";
}
/***********************************************************************************************
*
***********************************************************************************************/
@Override
public String descriptionSyntaxDetailsHTML() {
return ""
+"- condition: The condition to evaluate for the if-statement.
"
+"- trueValue: The value to return if the condition is true.
"
+"- falseValue: The value to return if the condition is false.
"
+"
"
;
}
/***********************************************************************************************
*
***********************************************************************************************/
@Override
public String descriptionHTML() {
return CFW.Files.readPackageResource(FeatureQuery.PACKAGE_MANUAL+".functions", "function_"+FUNCTION_NAME+".html");
}
/***********************************************************************************************
*
***********************************************************************************************/
@Override
public boolean supportsAggregation() {
return false;
}
/***********************************************************************************************
*
***********************************************************************************************/
@Override
public void aggregate(EnhancedJsonObject object,ArrayList parameters) {
// not supported
}
/***********************************************************************************************
*
***********************************************************************************************/
@Override
public QueryPartValue execute(EnhancedJsonObject object, ArrayList parameters) {
//----------------------------------
// Return same value if not second param
if(parameters.size() >= 2) {
QueryPartValue condition = parameters.get(0);
QueryPartValue trueValue = parameters.get(1);
QueryPartValue falseValue = (parameters.size() >= 3) ? parameters.get(2) : QueryPartValue.newString("");
if(condition.getAsBoolean()) {
return trueValue;
}else {
return falseValue;
}
}
//----------------------------------
// Return empty string if not enough params
return QueryPartValue.newNull();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy