Skip to content

Commit

Permalink
small adjustements for workflow dsl to allow to pass data object init…
Browse files Browse the repository at this point in the history
… expressions as lambda, otherwise for split as default path, errorOn as exception class
  • Loading branch information
mswiderski committed Aug 10, 2024
1 parent 3c2c816 commit a61e5c5
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,20 @@ public ErrorNodeBuilder onError(String... errorCodes) {
.errorCodes(errorCodes);
}

/**
* Adds error handling node that allows to move to different path in case of an error
*
* @param errorCodes list of codes to listen on
* @return the builder
*/
@SuppressWarnings("unchecked")
public ErrorNodeBuilder onError(Class<? extends Throwable>... exceptions) {
workflowBuilder.putOnContext(getNode());
workflowBuilder.putBuilderOnContext(null);
return new ErrorNodeBuilder("error on " + node.getName(), (String) this.node.getMetaData("UniqueId"), workflowBuilder)
.errorCodes(Stream.of(exceptions).map(c -> c.getName()).toArray(String[]::new));
}

/**
* Configures the maximum amount of time the node will wait to get response from the service.
* In case it is not successfully within that time it will abort the operation and throw error with <code>408</code> error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ public WorkflowBuilder when(String expression) {
return workflowBuilder;
}

public WorkflowBuilder otherwise() {
workflowBuilder.putOnContext(getNode());
workflowBuilder.putBuilderOnContext(this);

this.returnValueConstraint = new ReturnValueConstraintEvaluator();
returnValueConstraint.setDialect("java");
returnValueConstraint.setName("");
returnValueConstraint.setPriority(1);
returnValueConstraint.setDefault(true);

return workflowBuilder;
}

@Override
protected void apply(Connection connection) {
node.setConstraint(connection, returnValueConstraint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -298,6 +299,75 @@ public WorkflowBuilder listDataObject(String name, Class<?> type, String... tags
return this;
}

/**
* Adds list data object to the workflow definition and returns it so it can be used in expressions
*
* @param name name of the data object
* @param valueExpression expression to initialize the data object with
* @param type type of the list elements
* @param tags optional tags
* @return the builder
*/
public <T> WorkflowBuilder listDataObject(String name, Supplier<T> valueExpression, Class<T> type, String... tags) {
Variable variable = new Variable();
variable.setId(name);
variable.setName(name);
variable.setType(new ObjectDataType(List.class, List.class.getCanonicalName() + "<" + type.getCanonicalName() + ">"));
variable.setMetaData("type", type.getCanonicalName());

if (tags != null && tags.length > 0) {
String variableTags = Stream.of(tags).collect(Collectors.joining(","));
variable.setMetaData("tags", variableTags);

if (variableTags.contains(Variable.BUSINESS_RELEVANT_TAG)) {
this.tags("#{" + name + "}");
}
}
if (valueExpression != null) {
variable.setMetaData(Variable.DEFAULT_VALUE,
"#{" + BuilderContext.get(Thread.currentThread().getStackTrace()[2].getMethodName()) + "}");
}
process.getVariableScope().getVariables().add(variable);
return this;
}

/**
* Adds list data object to the workflow definition and returns it so it can be used in expressions
*
* @param name name of the data object
* @param valueExpression expression to initialize the data object with
* @param type type of the list elements
* @param tags optional tags
* @return the builder
*/
public <T> WorkflowBuilder listDataObject(String name, String valueExpression, Class<T> type, String... tags) {
Variable variable = new Variable();
variable.setId(name);
variable.setName(name);
variable.setType(new ObjectDataType(List.class, List.class.getCanonicalName() + "<" + type.getCanonicalName() + ">"));
variable.setMetaData("type", type.getCanonicalName());

if (tags != null && tags.length > 0) {
String variableTags = Stream.of(tags).collect(Collectors.joining(","));
variable.setMetaData("tags", variableTags);

if (variableTags.contains(Variable.BUSINESS_RELEVANT_TAG)) {
this.tags("#{" + name + "}");
}
}
if (valueExpression != null) {
variable.setMetaData("value",
"#{" + valueExpression + "}");
}
process.getVariableScope().getVariables().add(variable);
return this;
}

public <T> List<T> listDataObject(Class<T> type, String name, Supplier<T> valueExpression, String... tags) {
listDataObject(name, valueExpression, type, tags);
return null;
}

/**
* Adds parameterized data object to the workflow definition and returns it so it can be used in expressions
*
Expand Down Expand Up @@ -373,6 +443,7 @@ public WorkflowBuilder dataObject(String name, Class<?> type, String... tags) {
* Adds data object to the workflow definition
*
* @param name name of the data object that can be later on referenced by
* @param valueExpression expression to initialize the data object with
* @param type type of the data object
* @param tags optional (non null tags) to be assigned to the data object e.g. output, input, required, etc
* @return the builder
Expand All @@ -393,13 +464,61 @@ public WorkflowBuilder dataObject(String name, String valueExpression, Class<?>
}

if (valueExpression != null) {
variable.setMetaData("value", "#{" + valueExpression + "}");
variable.setMetaData(Variable.DEFAULT_VALUE, "#{" + valueExpression + "}");
}

process.getVariableScope().getVariables().add(variable);
return this;
}

/**
* Adds data object to the workflow definition
*
* @param name name of the data object that can be later on referenced by
* @param valueExpression expression to initialize the data object with
* @param type type of the data object
* @param tags optional (non null tags) to be assigned to the data object e.g. output, input, required, etc
* @return the builder
*/
public <T> WorkflowBuilder dataObject(String name, Supplier<T> valueExpression, Class<T> type, String... tags) {
Variable variable = new Variable();
variable.setId(name);
variable.setName(name);
variable.setType(new ObjectDataType(type));

if (tags != null && tags.length > 0) {
String variableTags = Stream.of(tags).collect(Collectors.joining(","));
variable.setMetaData("tags", variableTags);

if (variableTags.contains(Variable.BUSINESS_RELEVANT_TAG)) {
this.tags("#{" + name + "}");
}
}

if (valueExpression != null) {
variable.setMetaData(Variable.DEFAULT_VALUE,
"#{" + BuilderContext.get(Thread.currentThread().getStackTrace()[2].getMethodName()) + "}");
}

process.getVariableScope().getVariables().add(variable);
return this;
}

/**
* Adds parameterized data object to the workflow definition and returns it so it can be used in expressions
*
* @param <T> data type
* @param type type of the data objects
* @param name name of the data object
* @param valueExpression expression to initialize the data object with
* @param tags optional tags
* @return return null as it only records the definition
*/
public <T> T dataObject(Class<T> type, String name, Supplier<T> valueExpression, String... tags) {
dataObject(name, valueExpression, type, tags);
return null;
}

/**
* Adds a starting node
*
Expand Down

0 comments on commit a61e5c5

Please sign in to comment.