com.fivefaces.cloud.workflow.awsonprem.impl.ChoiceStateExecutorImpl Maven / Gradle / Ivy
package com.fivefaces.cloud.workflow.awsonprem.impl;
import com.amazonaws.services.stepfunctions.builder.conditions.*;
import com.amazonaws.services.stepfunctions.builder.states.Choice;
import com.amazonaws.services.stepfunctions.builder.states.ChoiceState;
import com.amazonaws.services.stepfunctions.builder.states.NextStateTransition;
import com.amazonaws.services.stepfunctions.builder.states.Transition;
import com.fivefaces.cloud.workflow.awsonprem.ChoiceStateExecutor;
import com.fivefaces.cloud.workflow.awsonprem.model.Execution;
import com.jayway.jsonpath.JsonPath;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import java.math.BigInteger;
@Service
@RequiredArgsConstructor
public class ChoiceStateExecutorImpl implements ChoiceStateExecutor {
@Override
public Transition execute(ChoiceState state, Execution execution) {
for (Choice choice : state.getChoices()) {
if (conditionSatisfied(choice.getCondition(), execution)) {
return choice.getTransition();
}
}
if (StringUtils.isNotBlank(state.getDefaultStateName())) {
return NextStateTransition.builder()
.nextStateName(state.getDefaultStateName()).build();
}
throw new IllegalStateException("No choice to execute");
}
private boolean conditionSatisfied(Condition condition, Execution execution) {
if (condition instanceof BinaryCondition) {
return conditionSatisfied((BinaryCondition>) condition, execution);
} else if (condition instanceof NAryCondition) {
return conditionSatisfied((NAryCondition) condition, execution);
} else if (condition instanceof NotCondition) {
execution.addCondition("NOT");
return !conditionSatisfied(((NotCondition) condition).getCondition(), execution);
}
throw getNotImplementedException(condition);
}
private boolean conditionSatisfied(BinaryCondition> condition, Execution execution) {
Object variable = getVariable(condition.getVariable(), execution);
execution.addCondition(
condition.getVariable() + " = " + variable + " " + condition.getClass().getSimpleName() + " Expected = " + condition.getExpectedValue());
if (condition instanceof IsPresentCondition) {
return Boolean.valueOf(variable != null).equals(condition.getExpectedValue());
} else if (condition instanceof StringEqualsCondition) {
return condition.getExpectedValue().equals(variable);
} else if (condition instanceof StringEqualsPathCondition) {
String expectedValue = ((StringEqualsPathCondition) condition).getExpectedValue();
return getVariable(expectedValue, execution).equals(variable);
} else if (condition instanceof IsBooleanCondition) {
return variable instanceof Boolean;
} else if (condition instanceof BooleanEqualsCondition) {
return ((BooleanEqualsCondition) condition).getExpectedValue().equals(variable);
} else if (condition instanceof IsStringCondition) {
return variable instanceof String;
} else if (condition instanceof IsNumericCondition) {
return variable instanceof Double || variable instanceof Integer || variable instanceof BigInteger || variable instanceof Long;
}
throw getNotImplementedException(condition);
}
private boolean conditionSatisfied(NAryCondition condition, Execution execution) {
if (condition instanceof AndCondition) {
execution.addCondition("AND");
boolean satisfied = allConditionsSatisfied((AndCondition) condition, execution);
execution.addCondition("END_AND " + Boolean.valueOf(satisfied).toString());
return satisfied;
} else if (condition instanceof OrCondition) {
execution.addCondition("OR");
boolean satisfied = anyConditionSatisfied((OrCondition) condition, execution);
execution.addCondition("END_OR " + Boolean.valueOf(satisfied).toString());
return satisfied;
}
throw getNotImplementedException(condition);
}
private boolean allConditionsSatisfied(AndCondition condition, Execution execution) {
for (Condition singleCondition : condition.getConditions()) {
if (!conditionSatisfied(singleCondition, execution)) {
return false;
}
}
return true;
}
private boolean anyConditionSatisfied(OrCondition condition, Execution execution) {
for (Condition singleCondition : condition.getConditions()) {
if (conditionSatisfied(singleCondition, execution)) {
return true;
}
}
return false;
}
private Object getVariable(String variable, Execution execution) {
return execution.getInput().read(JsonPath.compile(variable));
}
private IllegalStateException getNotImplementedException(Condition condition) {
return new IllegalStateException("Could not process condition of type " + condition.getClass());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy