Skip to content

Commit

Permalink
add REST api for tasks variables + allow TS tasks (#4105)
Browse files Browse the repository at this point in the history
* add REST api for tasks variables

Signed-off-by: Iliyan Velichkov <[email protected]>

* allow having ts tasks in BPM

Signed-off-by: Iliyan Velichkov <[email protected]>

---------

Signed-off-by: Iliyan Velichkov <[email protected]>
  • Loading branch information
iliyan-velichkov authored Jul 3, 2024
1 parent 536fb89 commit a42c405
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 171 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package org.eclipse.dirigible.components.engine.bpm.flowable.delegate;

import jakarta.annotation.Nullable;
import org.eclipse.dirigible.commons.api.helpers.GsonHelper;
import org.eclipse.dirigible.components.engine.bpm.flowable.dto.ExecutionData;
import org.eclipse.dirigible.graalium.core.DirigibleJavascriptCodeRunner;
Expand All @@ -21,7 +22,6 @@
import org.graalvm.polyglot.Value;
import org.springframework.beans.BeanUtils;

import jakarta.annotation.Nullable;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -36,7 +36,7 @@
public class DirigibleCallDelegate implements JavaDelegate {

/** The js expression regex. */
private static Pattern JS_EXPRESSION_REGEX = Pattern.compile("(.*\\.m?js)(?:\\/(\\w*))?(?:\\/(\\w*))?");
private static final Pattern JS_EXPRESSION_REGEX = Pattern.compile("(.*\\.(?:m?js|ts))(?:\\/(\\w*))?(?:\\/(\\w*))?");

/**
* The handler.
Expand All @@ -48,95 +48,6 @@ public class DirigibleCallDelegate implements JavaDelegate {
*/
private FixedValue type;

/**
* Getter for the handler attribute.
*
* @return the handler
*/
public FixedValue getHandler() {
return handler;
}

/**
* Setter of the handler attribute.
*
* @param handler the handler
*/
public void setHandler(FixedValue handler) {
this.handler = handler;
}

/**
* Getter for the engine attribute.
*
* @return the type
*/
public FixedValue getType() {
return type;
}

/**
* Setter of the engine attribute.
*
* @param type the type
*/
public void setType(FixedValue type) {
this.type = type;
}

/**
* Execute.
*
* @param execution the execution
*/
@Override
public void execute(DelegateExecution execution) {

String action = (String) execution.getVariable(DIRIGIBLE_BPM_INTERNAL_SKIP_STEP);
if (SKIP.getActionName()
.equals(action)) {
execution.removeVariable(DIRIGIBLE_BPM_INTERNAL_SKIP_STEP);
return;
}

Map<Object, Object> context = new HashMap<>();
ExecutionData executionData = new ExecutionData();
BeanUtils.copyProperties(execution, executionData);
context.put("execution", GsonHelper.toJson(executionData));
if (type == null) {
type = new FixedValue("javascript");
}
if (handler == null) {
throw new BpmnError("Handler cannot be null at the call delegate.");
}
executeJSHandler(context);
}

/**
* Execute JS handler.
*
* @param context the context
*/
private void executeJSHandler(Map<Object, Object> context) {
RepositoryPath path = new RepositoryPath(handler.getExpressionText());
JSTask task = JSTask.fromRepositoryPath(path);

try (DirigibleJavascriptCodeRunner runner = new DirigibleJavascriptCodeRunner(context, false)) {
Source source = runner.prepareSource(task.getSourceFilePath());
Value value = runner.run(source);

if (task.hasExportedClassAndMethod()) {
value.getMember(task.getClassName())
.newInstance()
.getMember(task.getMethodName())
.executeVoid();
} else if (task.hasExportedMethod()) {
value.getMember(task.getMethodName())
.executeVoid();
}

}
}

/**
* The Class JSTask.
Expand Down Expand Up @@ -182,7 +93,8 @@ static class JSTask {
static JSTask fromRepositoryPath(RepositoryPath repositoryPath) {
var matcher = JS_EXPRESSION_REGEX.matcher(repositoryPath.getPath());
if (!matcher.matches()) {
throw new BpmnError("Invalid JS expression provided for task!");
throw new BpmnError("Invalid JS expression provided for task! Path [" + repositoryPath.getPath() + "] doesn't match "
+ JS_EXPRESSION_REGEX);
}

String maybeClassName;
Expand Down Expand Up @@ -246,4 +158,94 @@ public boolean hasExportedMethod() {
}
}

/**
* Getter for the handler attribute.
*
* @return the handler
*/
public FixedValue getHandler() {
return handler;
}

/**
* Setter of the handler attribute.
*
* @param handler the handler
*/
public void setHandler(FixedValue handler) {
this.handler = handler;
}

/**
* Getter for the engine attribute.
*
* @return the type
*/
public FixedValue getType() {
return type;
}

/**
* Setter of the engine attribute.
*
* @param type the type
*/
public void setType(FixedValue type) {
this.type = type;
}

/**
* Execute.
*
* @param execution the execution
*/
@Override
public void execute(DelegateExecution execution) {

String action = (String) execution.getVariable(DIRIGIBLE_BPM_INTERNAL_SKIP_STEP);
if (SKIP.getActionName()
.equals(action)) {
execution.removeVariable(DIRIGIBLE_BPM_INTERNAL_SKIP_STEP);
return;
}

Map<Object, Object> context = new HashMap<>();
ExecutionData executionData = new ExecutionData();
BeanUtils.copyProperties(execution, executionData);
context.put("execution", GsonHelper.toJson(executionData));
if (type == null) {
type = new FixedValue("javascript");
}
if (handler == null) {
throw new BpmnError("Handler cannot be null at the call delegate.");
}
executeJSHandler(context);
}

/**
* Execute JS handler.
*
* @param context the context
*/
private void executeJSHandler(Map<Object, Object> context) {
RepositoryPath path = new RepositoryPath(handler.getExpressionText());
JSTask task = JSTask.fromRepositoryPath(path);

try (DirigibleJavascriptCodeRunner runner = new DirigibleJavascriptCodeRunner(context, false)) {
Source source = runner.prepareSource(task.getSourceFilePath());
Value value = runner.run(source);

if (task.hasExportedClassAndMethod()) {
value.getMember(task.getClassName())
.newInstance()
.getMember(task.getMethodName())
.executeVoid();
} else if (task.hasExportedMethod()) {
value.getMember(task.getMethodName())
.executeVoid();
}

}
}

}
Loading

0 comments on commit a42c405

Please sign in to comment.