in plugins/actions/simpleeval/src/main/java/org/apache/hop/workflow/actions/simpleeval/ActionSimpleEval.java [347:1052]
public Result execute(Result previousResult, int nr) throws HopException {
Result result = previousResult;
result.setNrErrors(1);
result.setResult(false);
String sourcevalue = null;
switch (valueType) {
case FIELD:
List<RowMetaAndData> rows = result.getRows();
RowMetaAndData resultRow = null;
if (isDetailed()) {
logDetailed(
BaseMessages.getString(
PKG,
"ActionSimpleEval.Log.ArgFromPrevious.Found",
(rows != null ? rows.size() : 0) + ""));
}
if (rows.isEmpty()) {
rows = null;
logError(BaseMessages.getString(PKG, "ActionSimpleEval.Error.NoRows"));
return result;
}
// get first row
resultRow = rows.get(0);
String realfieldname = resolve(fieldName);
int indexOfField = -1;
indexOfField = resultRow.getRowMeta().indexOfValue(realfieldname);
if (indexOfField == -1) {
logError(
BaseMessages.getString(PKG, "ActionSimpleEval.Error.FieldNotExist", realfieldname));
resultRow = null;
rows = null;
return result;
}
sourcevalue = resultRow.getString(indexOfField, null);
if (sourcevalue == null) {
sourcevalue = "";
}
resultRow = null;
rows = null;
break;
case VARIABLE:
if (Utils.isEmpty(variableName)) {
logError(BaseMessages.getString(PKG, "ActionSimpleEval.Error.VariableMissing"));
return result;
}
if (isSuccessWhenVarSet()) {
// return variable name
// remove specifications if needed
String variableName = StringUtil.getVariableName(Const.NVL(getVariableName(), ""));
// Get value, if the variable is not set, Null will be returned
String value = getVariable(variableName);
if (value != null) {
if (isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "ActionSimpleEval.VariableSet", variableName));
}
result.setResult(true);
result.setNrErrors(0);
return result;
} else {
if (isDetailed()) {
logDetailed(
BaseMessages.getString(PKG, "ActionSimpleEval.VariableNotSet", variableName));
}
// this action does not set errors upon evaluation, independently of the
// outcome of the check
result.setNrErrors(0);
return result;
}
}
sourcevalue = resolve(getVariableWithSpec());
break;
default:
break;
}
if (isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "ActionSimpleEval.Log.ValueToevaluate", sourcevalue));
}
boolean success = false;
String realCompareValue = resolve(compareValue);
if (realCompareValue == null) {
realCompareValue = "";
}
String realMinValue = resolve(minValue);
String realMaxValue = resolve(maxValue);
switch (fieldType) {
case STRING:
switch (successStringCondition) {
case EQUAL: // equal
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
success = (sourcevalue.equals(realCompareValue));
if (valueType == ValueType.VARIABLE && !success && Utils.isEmpty(realCompareValue)) {
// make the empty value evaluate to true when compared to a not set variable
String key = StringUtil.getVariableName(variableName);
if (System.getProperty(key) == null) {
success = true;
}
}
break;
case DIFFERENT: // different
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
success = (!sourcevalue.equals(realCompareValue));
break;
case CONTAINS: // contains
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
success = (sourcevalue.contains(realCompareValue));
break;
case NOT_CONTAINS: // not contains
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
success = (!sourcevalue.contains(realCompareValue));
break;
case START_WITH: // starts with
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
success = (sourcevalue.startsWith(realCompareValue));
break;
case NOT_START_WITH: // not start with
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
success = (!sourcevalue.startsWith(realCompareValue));
break;
case END_WITH: // ends with
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
success = (sourcevalue.endsWith(realCompareValue));
break;
case NOT_END_WITH: // not ends with
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
success = (!sourcevalue.endsWith(realCompareValue));
break;
case REGEX: // regexp
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
success = (Pattern.compile(realCompareValue).matcher(sourcevalue).matches());
break;
case IN_LIST: // in list
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
realCompareValue = Const.NVL(realCompareValue, "");
String[] parts = realCompareValue.split(",");
for (int i = 0; i < parts.length && !success; i++) {
success = (sourcevalue.equals(parts[i].trim()));
}
break;
case NOT_IN_LIST: // not in list
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
realCompareValue = Const.NVL(realCompareValue, "");
parts = realCompareValue.split(",");
success = true;
for (int i = 0; i < parts.length && success; i++) {
success = !(sourcevalue.equals(parts[i].trim()));
}
break;
default:
break;
}
break;
case NUMBER:
double valuenumber;
try {
valuenumber = Double.parseDouble(sourcevalue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
sourcevalue,
e.getMessage()));
return result;
}
double valuecompare;
switch (successNumberCondition) {
case EQUAL: // equal
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
valuecompare = Double.parseDouble(realCompareValue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
realCompareValue,
e.getMessage()));
return result;
}
success = (valuenumber == valuecompare);
break;
case DIFFERENT: // different
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
valuecompare = Double.parseDouble(realCompareValue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
realCompareValue,
e.getMessage()));
return result;
}
success = (valuenumber != valuecompare);
break;
case SMALLER: // smaller
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
valuecompare = Double.parseDouble(realCompareValue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
realCompareValue,
e.getMessage()));
return result;
}
success = (valuenumber < valuecompare);
break;
case SMALLER_EQUAL: // smaller or equal
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
valuecompare = Double.parseDouble(realCompareValue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
realCompareValue,
e.getMessage()));
return result;
}
success = (valuenumber <= valuecompare);
break;
case GREATER: // greater
try {
valuecompare = Double.parseDouble(realCompareValue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
realCompareValue,
e.getMessage()));
return result;
}
success = (valuenumber > valuecompare);
break;
case GREATER_EQUAL: // greater or equal
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
valuecompare = Double.parseDouble(realCompareValue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
realCompareValue,
e.getMessage()));
return result;
}
success = (valuenumber >= valuecompare);
break;
case BETWEEN: // between min and max
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG, "ActionSimpleEval.Log.CompareWithValues", realMinValue, realMaxValue));
}
double valuemin;
try {
valuemin = Double.parseDouble(realMinValue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
realMinValue,
e.getMessage()));
return result;
}
double valuemax;
try {
valuemax = Double.parseDouble(realMaxValue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
realMaxValue,
e.getMessage()));
return result;
}
if (valuemin >= valuemax) {
logError(
BaseMessages.getString(
PKG, "ActionSimpleEval.Error.IncorrectNumbers", realMinValue, realMaxValue));
return result;
}
success = (valuenumber >= valuemin && valuenumber <= valuemax);
break;
case IN_LIST: // in list
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
String[] parts = realCompareValue.split(",");
for (int i = 0; i < parts.length && !success; i++) {
try {
valuecompare = Double.parseDouble(parts[i]);
} catch (Exception e) {
logError(
toString(),
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
parts[i],
e.getMessage()));
return result;
}
success = (valuenumber == valuecompare);
}
break;
case NOT_IN_LIST: // not in list
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
realCompareValue = Const.NVL(realCompareValue, "");
parts = realCompareValue.split(",");
success = true;
for (int i = 0; i < parts.length && success; i++) {
try {
valuecompare = Double.parseDouble(parts[i]);
} catch (Exception e) {
logError(
toString(),
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_ERROR_UNPARSABLE_NUMBER,
parts[i],
e.getMessage()));
return result;
}
success = (valuenumber != valuecompare);
}
break;
default:
break;
}
break;
case DATE_TIME:
String realMask = resolve(mask);
SimpleDateFormat df = new SimpleDateFormat();
if (!Utils.isEmpty(realMask)) {
df.applyPattern(realMask);
}
Date datevalue = null;
try {
datevalue = convertToDate(sourcevalue, realMask, df);
} catch (Exception e) {
logError(e.getMessage());
return result;
}
Date datecompare;
switch (successNumberCondition) {
case EQUAL: // equal
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
datecompare = convertToDate(realCompareValue, realMask, df);
} catch (Exception e) {
logError(e.getMessage());
return result;
}
success = (datevalue.equals(datecompare));
break;
case DIFFERENT: // different
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
datecompare = convertToDate(realCompareValue, realMask, df);
} catch (Exception e) {
logError(e.getMessage());
return result;
}
success = (!datevalue.equals(datecompare));
break;
case SMALLER: // smaller
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
datecompare = convertToDate(realCompareValue, realMask, df);
} catch (Exception e) {
logError(e.getMessage());
return result;
}
success = (datevalue.before(datecompare));
break;
case SMALLER_EQUAL: // smaller or equal
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
datecompare = convertToDate(realCompareValue, realMask, df);
} catch (Exception e) {
logError(e.getMessage());
return result;
}
success = (datevalue.before(datecompare) || datevalue.equals(datecompare));
break;
case GREATER: // greater
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
datecompare = convertToDate(realCompareValue, realMask, df);
} catch (Exception e) {
logError(e.getMessage());
return result;
}
success = (datevalue.after(datecompare));
break;
case GREATER_EQUAL: // greater or equal
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
try {
datecompare = convertToDate(realCompareValue, realMask, df);
} catch (Exception e) {
logError(e.getMessage());
return result;
}
success = (datevalue.after(datecompare) || datevalue.equals(datecompare));
break;
case BETWEEN: // between min and max
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG, "ActionSimpleEval.Log.CompareWithValues", realMinValue, realMaxValue));
}
Date datemin;
try {
datemin = convertToDate(realMinValue, realMask, df);
} catch (Exception e) {
logError(e.getMessage());
return result;
}
Date datemax;
try {
datemax = convertToDate(realMaxValue, realMask, df);
} catch (Exception e) {
logError(e.getMessage());
return result;
}
if (datemin.after(datemax) || datemin.equals(datemax)) {
logError(
BaseMessages.getString(
PKG, "ActionSimpleEval.Error.IncorrectDates", realMinValue, realMaxValue));
return result;
}
success =
((datevalue.after(datemin) || datevalue.equals(datemin))
&& (datevalue.before(datemax) || datevalue.equals(datemax)));
break;
case IN_LIST: // in list
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
String[] parts = realCompareValue.split(",");
for (int i = 0; i < parts.length && !success; i++) {
try {
datecompare = convertToDate(realCompareValue, realMask, df);
} catch (Exception e) {
logError(toString(), e.getMessage());
return result;
}
success = (datevalue.equals(datecompare));
}
break;
case NOT_IN_LIST: // not in list
if (isDebug()) {
logDebug(
BaseMessages.getString(
PKG,
CONST_ACTION_SIMPLE_EVAL_LOG_COMPARE_WITH_VALUE,
sourcevalue,
realCompareValue));
}
realCompareValue = Const.NVL(realCompareValue, "");
parts = realCompareValue.split(",");
success = true;
for (int i = 0; i < parts.length && success; i++) {
try {
datecompare = convertToDate(realCompareValue, realMask, df);
} catch (Exception e) {
logError(toString(), e.getMessage());
return result;
}
success = (!datevalue.equals(datecompare));
}
break;
default:
break;
}
df = null;
break;
case BOOLEAN:
boolean valuebool;
try {
valuebool = ValueMetaBase.convertStringToBoolean(sourcevalue);
} catch (Exception e) {
logError(
BaseMessages.getString(
PKG, "ActionSimpleEval.Error.UnparsableBoolean", sourcevalue, e.getMessage()));
return result;
}
switch (successBooleanCondition) {
case FALSE: // false
success = (!valuebool);
break;
case TRUE: // true
success = (valuebool);
break;
default:
break;
}
break;
default:
break;
}
result.setResult(success);
// This action does not set errors upon evaluation, independently of the outcome of the check
result.setNrErrors(0);
return result;
}