Skip to content

Commit

Permalink
[KOGITO-4502] GET task id API to return task info, not only the input
Browse files Browse the repository at this point in the history
model
  • Loading branch information
fjtirado committed Apr 13, 2021
1 parent 64fcf55 commit 1340607
Show file tree
Hide file tree
Showing 11 changed files with 332 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kie.kogito.process.workitem;

public interface TaskModel<P, R> {

String getId();

String getName();

int getState();

String getPhase();

String getPhaseStatus();

P getParams();

R getResults();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Modifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.Parameter;
import com.github.javaparser.ast.body.VariableDeclarator;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.expr.AssignExpr;
Expand All @@ -51,9 +53,14 @@
import com.github.javaparser.ast.expr.StringLiteralExpr;
import com.github.javaparser.ast.expr.ThisExpr;
import com.github.javaparser.ast.expr.VariableDeclarationExpr;
import com.github.javaparser.ast.nodeTypes.NodeWithType;
import com.github.javaparser.ast.nodeTypes.NodeWithVariables;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.stmt.BreakStmt;
import com.github.javaparser.ast.stmt.ReturnStmt;
import com.github.javaparser.ast.stmt.SwitchEntry;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import com.github.javaparser.ast.type.Type;

import static com.github.javaparser.StaticJavaParser.parse;
import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType;
Expand All @@ -65,6 +72,7 @@ public class UserTaskModelMetaData {

private static final String TASK_INTPUT_CLASS_SUFFIX = "TaskInput";
private static final String TASK_OUTTPUT_CLASS_SUFFIX = "TaskOutput";
private static final String TASK_MODEL_CLASS_SUFFIX = "TaskModel";
private static final String TASK_NAME = "TaskName";
private static final String WORK_ITEM = "workItem";
private static final String PARAMS = "params";
Expand All @@ -84,6 +92,9 @@ public class UserTaskModelMetaData {
private String outputModelClassName;
private String outputModelClassSimpleName;

private String taskModelClassName;
private String taskModelClassSimpleName;

public UserTaskModelMetaData(String packageName, VariableScope processVariableScope, VariableScope variableScope, HumanTaskNode humanTaskNode, String processId) {
this.packageName = packageName;
this.processVariableScope = processVariableScope;
Expand All @@ -97,6 +108,9 @@ public UserTaskModelMetaData(String packageName, VariableScope processVariableSc
this.outputModelClassSimpleName = ucFirst(ProcessToExecModelGenerator.extractProcessId(processId) + "_" + humanTaskNode.getId() + "_" + TASK_OUTTPUT_CLASS_SUFFIX);
this.outputModelClassName = packageName + '.' + outputModelClassSimpleName;

this.taskModelClassSimpleName = ucFirst(ProcessToExecModelGenerator.extractProcessId(processId) + "_" + humanTaskNode.getId() + "_" + TASK_MODEL_CLASS_SUFFIX);
this.taskModelClassName = packageName + '.' + taskModelClassSimpleName;

}

public String generateInput() {
Expand All @@ -109,36 +123,21 @@ public String generateOutput() {
return modelClass.toString();
}

public String getInputModelClassName() {
return inputModelClassName;
}

public void setInputModelClassName(String inputModelClassName) {
this.inputModelClassName = inputModelClassName;
}

public String getInputModelClassSimpleName() {
return inputModelClassSimpleName;
public String generateModel() {
CompilationUnit modelClass = compilationUnitModel();
return modelClass.toString();
}

public void setInputModelClassSimpleName(String inputModelClassSimpleName) {
this.inputModelClassSimpleName = inputModelClassSimpleName;
public String getInputModelClassName() {
return inputModelClassName;
}

public String getOutputModelClassName() {
return outputModelClassName;
}

public void setOutputModelClassName(String outputModelClassName) {
this.outputModelClassName = outputModelClassName;
}

public String getOutputModelClassSimpleName() {
return outputModelClassSimpleName;
}

public void setOutputModelClassSimpleName(String outputModelClassSimpleName) {
this.outputModelClassSimpleName = outputModelClassSimpleName;
public String getTaskModelClassName() {
return taskModelClassName;
}

public String getName() {
Expand Down Expand Up @@ -187,21 +186,6 @@ private CompilationUnit compilationUnitInput() {
VariableDeclarationExpr itemField = new VariableDeclarationExpr(modelType, "item");
staticFromMap.addStatement(new AssignExpr(itemField, new ObjectCreationExpr(null, modelType, NodeList.nodeList()), AssignExpr.Operator.ASSIGN));
NameExpr item = new NameExpr("item");
FieldAccessExpr idField = new FieldAccessExpr(item, "_id");
staticFromMap.addStatement(new AssignExpr(idField, new MethodCallExpr(
new NameExpr(WORK_ITEM), "getId"), AssignExpr.Operator.ASSIGN));

FieldAccessExpr nameField = new FieldAccessExpr(item, "_name");
staticFromMap.addStatement(new AssignExpr(nameField, new MethodCallExpr(
new NameExpr(WORK_ITEM), "getName"), AssignExpr.Operator.ASSIGN));

ClassOrInterfaceType toMap = new ClassOrInterfaceType(null, new SimpleName(Map.class.getSimpleName()),
NodeList.nodeList(new ClassOrInterfaceType(null, String.class.getSimpleName()), new ClassOrInterfaceType(
null,
Object.class.getSimpleName())));
VariableDeclarationExpr paramsField = new VariableDeclarationExpr(toMap, PARAMS);
staticFromMap.addStatement(new AssignExpr(paramsField, new MethodCallExpr(
new NameExpr(WORK_ITEM), "getParameters"), AssignExpr.Operator.ASSIGN));

for (Entry<String, String> entry : humanTaskNode.getInMappings().entrySet()) {

Expand Down Expand Up @@ -268,7 +252,7 @@ private CompilationUnit compilationUnitInput() {
AssignExpr.Operator.ASSIGN));
}
Optional<MethodDeclaration> staticFromMethod = modelClass.findFirst(
MethodDeclaration.class, sl -> sl.getName().asString().equals("from") && sl.isStatic());
MethodDeclaration.class, sl -> sl.getName().asString().equals("fromMap") && sl.isStatic());
if (staticFromMethod.isPresent()) {
MethodDeclaration from = staticFromMethod.get();
from.setType(modelClass.getNameAsString());
Expand Down Expand Up @@ -367,8 +351,62 @@ private CompilationUnit compilationUnitOutput() {
return compilationUnit;
}

private CompilationUnit compilationUnitModel() {
CompilationUnit compilationUnit = parse(this.getClass().getResourceAsStream(
"/class-templates/TaskModelTemplate.java"));
compilationUnit.setPackageDeclaration(packageName);
ClassOrInterfaceDeclaration modelClass = compilationUnit
.findFirst(ClassOrInterfaceDeclaration.class, sl1 -> true).orElseThrow(() -> new IllegalStateException(
"Cannot find class declaration in the template"));
compilationUnit.addOrphanComment(new LineComment("Task model for user task '" + humanTaskNode.getName() +
"' in process '" + processId + "'"));
modelClass.setName(taskModelClassSimpleName);
modelClass.getImplementedTypes().forEach(t -> t
.setTypeArguments(
NodeList.nodeList(parseClassOrInterfaceType(inputModelClassName), parseClassOrInterfaceType(
outputModelClassName))));
modelClass.findAll(NameExpr.class).forEach(this::templateReplacement);
modelClass.findAll(VariableDeclarationExpr.class).forEach(this::templateReplacement);
modelClass.findAll(FieldDeclaration.class).forEach(this::templateReplacement);
modelClass.findAll(ObjectCreationExpr.class).forEach(this::templateReplacement);
modelClass.findAll(MethodDeclaration.class).forEach(this::templateReplacement);
modelClass.findAll(Parameter.class).forEach(this::templateReplacement);
return compilationUnit;
}

private void templateReplacement(NameExpr name) {
name.setName(templateReplacement(name.getNameAsString()));
}

private <T extends Node, R extends Type> void templateReplacement(NodeWithType<T, R> expr) {
expr.setType(templateReplacement(expr.getTypeAsString()));
}

private <T extends Node> void templateReplacement(NodeWithVariables<T> expr) {
for (VariableDeclarator variable : expr.getVariables()) {
variable.setType(templateReplacement(variable.getTypeAsString()));
}
}

public String templateReplacement(String template) {
template = template.replace("$TaskInput$", inputModelClassName);
template = template.replace("$TaskOutput$", outputModelClassName);
template = template.replace("$TaskModel$", taskModelClassName);
return template;
}

public boolean isAdHoc() {
return !Boolean.parseBoolean((String) humanTaskNode.getMetaData(CUSTOM_AUTO_START))
&& (humanTaskNode.getIncomingConnections() == null || humanTaskNode.getIncomingConnections().isEmpty());
}

public SwitchEntry getModelSwitchEntry() {
SwitchEntry entry = new SwitchEntry();
entry.setLabels(NodeList.nodeList(new StringLiteralExpr(getName())));
entry.addStatement(new AssignExpr(new NameExpr("taskModel"), new MethodCallExpr(new NameExpr(
taskModelClassSimpleName), new SimpleName("from")).addArgument(WORK_ITEM), AssignExpr.Operator.ASSIGN));
// yes, thats silly, but if not empty string is not specified parser generates break empty or error
entry.addStatement(new BreakStmt(" "));
return entry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,7 @@

public class XXXTaskInput {

private String _id;
private String _name;

public void setId(String id) {
this._id = id;
}

public String getId() {
return this._id;
}

public void setName(String name) {
this._name = name;
}

public String getName() {
return this._name;
}

public static XXXTaskInput from(org.kie.kogito.process.WorkItem workItem) {
public static XXXTaskInput fromMap (Map<String,Object> params) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2019 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jbpm.process.codegen;

import org.kie.kogito.process.workitem.TaskModel;

public class $TaskModel$ implements TaskModel<$TaskInput$, $TaskOutput$>{

private String id;
private String name;
private int state;
private String phase;
private String phaseStatus;
private $TaskInput$ params;
private $TaskOutput$ results;


public void setId(String id) {
this.id = id;
}

public String getId() {
return id;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public int getState() {
return state;
}

public void setState (int state) {
this.state = state;
}

public String getPhase() {
return phase;
}

public void setPhase (String phase) {
this.phase = phase;
}

public String getPhaseStatus() {
return phaseStatus;
}

public void setPhaseStatus (String phaseStatus) {
this.phaseStatus = phaseStatus;
}

public $TaskInput$ getParams () {
return params;
}

public void setParams ($TaskInput$ params) {
this.params = params;
}

public $TaskOutput$ getResults () {
return results;
}

public void setParams ($TaskOutput$ results) {
this.results = results;
}

public static $TaskModel$ from(org.kie.kogito.process.WorkItem workItem) {
$TaskModel$ taskModel = new $TaskModel$();
taskModel.id= workItem.getId();
taskModel.name = workItem.getName();
taskModel.state = workItem.getState();
taskModel.phaseStatus = workItem.getPhaseStatus();
taskModel.phase = workItem.getPhase();
taskModel.params = $TaskInput$.fromMap(workItem.getParameters());
taskModel.results = $TaskOutput$.fromMap(workItem.getResults());
return taskModel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,20 @@ public Collection<GeneratedFile> generate() {
storeFile(MODEL_TYPE, UserTasksModelClassGenerator.generatedFilePath(ut.getInputModelClassName()), ut.generateInput());

storeFile(MODEL_TYPE, UserTasksModelClassGenerator.generatedFilePath(ut.getOutputModelClassName()), ut.generateOutput());

storeFile(MODEL_TYPE, UserTasksModelClassGenerator.generatedFilePath(ut.getTaskModelClassName()), ut.generateModel());
}
}

if (context().hasREST()) {
for (ProcessResourceGenerator resourceGenerator : rgs) {
storeFile(REST_TYPE, resourceGenerator.generatedFilePath(),
resourceGenerator.generate());

if (resourceGenerator.hasUserTask()) {
storeFile(MODEL_TYPE, UserTasksModelClassGenerator.generatedFilePath(resourceGenerator.getTaskModelFactoryClassName()), resourceGenerator.getTaskModelFactory());
}

}
}

Expand Down
Loading

0 comments on commit 1340607

Please sign in to comment.