diff --git a/api/kogito-api/src/main/java/org/kie/kogito/usertask/UserTaskInstance.java b/api/kogito-api/src/main/java/org/kie/kogito/usertask/UserTaskInstance.java index 710d06b6768..1ec166ec04e 100644 --- a/api/kogito-api/src/main/java/org/kie/kogito/usertask/UserTaskInstance.java +++ b/api/kogito-api/src/main/java/org/kie/kogito/usertask/UserTaskInstance.java @@ -26,13 +26,13 @@ public interface UserTaskInstance { - String id(); + String getId(); UserTask getUserTask(); UserTaskModel getUserTaskModel(); - UserTaskState status(); + UserTaskState getStatus(); boolean hasActualOwner(); diff --git a/api/kogito-api/src/main/java/org/kie/kogito/usertask/UserTaskInstances.java b/api/kogito-api/src/main/java/org/kie/kogito/usertask/UserTaskInstances.java index 0066fbe65f1..57e956bd87f 100644 --- a/api/kogito-api/src/main/java/org/kie/kogito/usertask/UserTaskInstances.java +++ b/api/kogito-api/src/main/java/org/kie/kogito/usertask/UserTaskInstances.java @@ -19,6 +19,7 @@ package org.kie.kogito.usertask; import java.util.Optional; +import java.util.function.Function; public interface UserTaskInstances { @@ -32,4 +33,8 @@ public interface UserTaskInstances { UserTaskInstance remove(String userTaskInstanceId); + void setReconnectUserTaskInstance(Function reconnectUserTaskInstance); + + void setDisconnectUserTaskInstance(Function disconnectUserTaskInstance); + } diff --git a/api/kogito-api/src/main/java/org/kie/kogito/usertask/lifecycle/UserTaskState.java b/api/kogito-api/src/main/java/org/kie/kogito/usertask/lifecycle/UserTaskState.java index 644a7a4d114..71ba89b94d0 100644 --- a/api/kogito-api/src/main/java/org/kie/kogito/usertask/lifecycle/UserTaskState.java +++ b/api/kogito-api/src/main/java/org/kie/kogito/usertask/lifecycle/UserTaskState.java @@ -44,6 +44,22 @@ public static UserTaskState of(String name, TerminationType terminate) { return new UserTaskState(name, terminate); } + public UserTaskState() { + + } + + public TerminationType getTerminate() { + return terminate; + } + + public void setName(String name) { + this.name = name; + } + + public void setTerminate(TerminationType terminate) { + this.terminate = terminate; + } + private UserTaskState(String name, TerminationType terminate) { this.name = name; this.terminate = terminate; @@ -78,4 +94,9 @@ public static UserTaskState initalized() { return of(null); } + @Override + public String toString() { + return "UserTaskState [terminate=" + terminate + ", name=" + name + "]"; + } + } diff --git a/api/kogito-api/src/main/java/org/kie/kogito/usertask/model/UserTaskModel.java b/api/kogito-api/src/main/java/org/kie/kogito/usertask/model/UserTaskModel.java index 3c96e326597..3786a427376 100644 --- a/api/kogito-api/src/main/java/org/kie/kogito/usertask/model/UserTaskModel.java +++ b/api/kogito-api/src/main/java/org/kie/kogito/usertask/model/UserTaskModel.java @@ -18,8 +18,10 @@ */ package org.kie.kogito.usertask.model; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; public class UserTaskModel { @@ -39,6 +41,12 @@ public class UserTaskModel { private Map inputs; private Map outputs; + private Map metadata; + + public UserTaskModel() { + this.metadata = new HashMap<>(); + } + public Map getInputs() { return inputs; } @@ -193,4 +201,47 @@ public void setExternalReferenceId(String externalReferenceId) { this.externalReferenceId = externalReferenceId; } + public void setMetadata(String key, Object value) { + this.metadata.put(key, value); + } + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + + @Override + public int hashCode() { + return Objects.hash(adminGroups, adminUsers, attachments, comments, excludedUsers, externalReferenceId, inputs, metadata, outputs, potentialGroups, potentialUsers, taskDescription, taskName, + taskPriority); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + UserTaskModel other = (UserTaskModel) obj; + return Objects.equals(adminGroups, other.adminGroups) + && Objects.equals(adminUsers, other.adminUsers) + && Objects.equals(attachments, other.attachments) + && Objects.equals(comments, other.comments) + && Objects.equals(excludedUsers, other.excludedUsers) + && Objects.equals(externalReferenceId, other.externalReferenceId) + && Objects.equals(inputs, other.inputs) + && Objects.equals(metadata, other.metadata) + && Objects.equals(outputs, other.outputs) + && Objects.equals(potentialGroups, other.potentialGroups) + && Objects.equals(potentialUsers, other.potentialUsers) + && Objects.equals(taskDescription, other.taskDescription) + && Objects.equals(taskName, other.taskName) + && Objects.equals(taskPriority, other.taskPriority); + } + } diff --git a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/AdapterHelper.java b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/AdapterHelper.java index 8edca5a74ca..0351557eeae 100644 --- a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/AdapterHelper.java +++ b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/AdapterHelper.java @@ -44,9 +44,9 @@ public static Map buildProcessMetadata(KogitoWorkflowProcessInst public static Map buildUserTaskMetadata(UserTaskInstance pi) { Map metadata = new HashMap<>(); - metadata.put(UserTaskInstanceEventMetadata.USER_TASK_INSTANCE_ID_META_DATA, pi.id()); + metadata.put(UserTaskInstanceEventMetadata.USER_TASK_INSTANCE_ID_META_DATA, pi.getId()); metadata.put(UserTaskInstanceEventMetadata.USER_TASK_INSTANCE_REFERENCE_ID_META_DATA, pi.getUserTask().getReferenceName()); - metadata.put(UserTaskInstanceEventMetadata.USER_TASK_INSTANCE_STATE_META_DATA, pi.status().getName()); + metadata.put(UserTaskInstanceEventMetadata.USER_TASK_INSTANCE_STATE_META_DATA, pi.getStatus().getName()); return metadata; } diff --git a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskAssignmentEventDataEventAdapter.java b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskAssignmentEventDataEventAdapter.java index 326964e2ed6..32030bcb380 100644 --- a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskAssignmentEventDataEventAdapter.java +++ b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskAssignmentEventDataEventAdapter.java @@ -41,7 +41,7 @@ public DataEvent adapt(Object payload) { .eventDate(new Date()) .eventUser(event.getEventUser()) .userTaskDefinitionId(event.getUserTask().id()) - .userTaskInstanceId(event.getUserTaskInstance().id()) + .userTaskInstanceId(event.getUserTaskInstance().getId()) .userTaskName(event.getUserTaskModel().getTaskName()) .assignmentType(event.getAssignmentType()) .users(event.getNewUsersId()); diff --git a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskCommentEventDataEventAdapter.java b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskCommentEventDataEventAdapter.java index 115728ea13e..8e3d4d6f787 100644 --- a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskCommentEventDataEventAdapter.java +++ b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskCommentEventDataEventAdapter.java @@ -46,7 +46,7 @@ public DataEvent adapt(Object payload) { UserTaskInstanceCommentEventBody.Builder builder = UserTaskInstanceCommentEventBody.create() .eventType(eventType) .userTaskDefinitionId(event.getUserTask().id()) - .userTaskInstanceId(event.getUserTaskInstance().id()) + .userTaskInstanceId(event.getUserTaskInstance().getId()) .userTaskName(event.getUserTaskModel().getTaskName()); String updatedBy = null; diff --git a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskDeadlineEventDataEventAdapter.java b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskDeadlineEventDataEventAdapter.java index 4da4098a99f..6ec0773d6d2 100644 --- a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskDeadlineEventDataEventAdapter.java +++ b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskDeadlineEventDataEventAdapter.java @@ -41,7 +41,7 @@ public DataEvent adapt(Object payload) { .eventDate(new Date()) .eventUser(event.getEventUser()) .userTaskDefinitionId(event.getUserTask().id()) - .userTaskInstanceId(event.getUserTaskInstance().id()) + .userTaskInstanceId(event.getUserTaskInstance().getId()) .userTaskName(event.getUserTaskModel().getTaskName()) .notification(event.getNotification()); diff --git a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskStateEventDataEventAdapter.java b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskStateEventDataEventAdapter.java index d405078ad55..95d0a3265e7 100644 --- a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskStateEventDataEventAdapter.java +++ b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskStateEventDataEventAdapter.java @@ -49,7 +49,7 @@ public DataEvent adapt(Object payload) { .eventDate(new Date()) .eventUser(event.getEventUser()) .userTaskDefinitionId(event.getUserTask().id()) - .userTaskInstanceId(event.getUserTaskInstance().id()) + .userTaskInstanceId(event.getUserTaskInstance().getId()) .userTaskName(event.getUserTaskModel().getTaskName()) .userTaskDescription(event.getUserTaskModel().getTaskDescription()) .userTaskPriority(priorityStr) @@ -61,7 +61,7 @@ public DataEvent adapt(Object payload) { UserTaskInstanceStateEventBody body = builder.build(); UserTaskInstanceStateDataEvent utEvent = - new UserTaskInstanceStateDataEvent(AdapterHelper.buildSource(getConfig().service(), event.getUserTaskModel().getExternalReferenceId()), getConfig().addons().toString(), + new UserTaskInstanceStateDataEvent(AdapterHelper.buildSource(getConfig().service(), (String) event.getUserTaskModel().getMetadata().get("ProcessId")), getConfig().addons().toString(), event.getEventUser(), metadata, body); diff --git a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskVariableEventDataEventAdapter.java b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskVariableEventDataEventAdapter.java index 6caac8da972..25b1c68cea3 100644 --- a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskVariableEventDataEventAdapter.java +++ b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTaskVariableEventDataEventAdapter.java @@ -41,7 +41,7 @@ public DataEvent adapt(Object payload) { .eventDate(new Date()) .eventUser(event.getEventUser()) .userTaskDefinitionId(event.getUserTask().id()) - .userTaskInstanceId(event.getUserTaskInstance().id()) + .userTaskInstanceId(event.getUserTaskInstance().getId()) .userTaskName(event.getUserTaskModel().getTaskName()) .variableId(event.getVariableName()) .variableName(event.getVariableName()) diff --git a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTastAttachmentEventDataEventAdapter.java b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTastAttachmentEventDataEventAdapter.java index 92f0fce5629..74feafbefa6 100644 --- a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTastAttachmentEventDataEventAdapter.java +++ b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/adapter/UserTastAttachmentEventDataEventAdapter.java @@ -46,7 +46,7 @@ public DataEvent adapt(Object payload) { UserTaskInstanceAttachmentEventBody.Builder builder = UserTaskInstanceAttachmentEventBody.create() .eventType(eventType) .userTaskDefinitionId(event.getUserTask().id()) - .userTaskInstanceId(event.getUserTaskInstance().id()) + .userTaskInstanceId(event.getUserTaskInstance().getId()) .userTaskName(event.getUserTaskModel().getTaskName()); String updatedBy = null; diff --git a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/jbpm/usertask/handler/UserTaskKogitoWorkItemHandler.java b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/jbpm/usertask/handler/UserTaskKogitoWorkItemHandler.java index bd5d85c3e02..0cd62c62c26 100644 --- a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/jbpm/usertask/handler/UserTaskKogitoWorkItemHandler.java +++ b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/jbpm/usertask/handler/UserTaskKogitoWorkItemHandler.java @@ -39,9 +39,11 @@ import org.kie.kogito.usertask.UserTask; import org.kie.kogito.usertask.UserTaskInstance; import org.kie.kogito.usertask.UserTasks; +import org.kie.kogito.usertask.impl.DefaultUserTaskInstance; import org.kie.kogito.usertask.model.UserTaskModel; import static java.util.Collections.emptyMap; +import static java.util.Collections.singletonMap; import static java.util.Optional.ofNullable; /** @@ -130,6 +132,7 @@ static public Optional userTaskActivateWorkItemHandler(Kogit model.setTaskDescription((String) workItem.getParameter(DESCRIPTION)); model.setTaskPriority(priorityInteger); model.setExternalReferenceId(workItem.getStringId()); + model.setMetadata("ProcessId", workItem.getProcessInstance().getProcessId()); ofNullable(workItem.getParameters().get(ACTOR_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(model::setPotentialUsers); ofNullable(workItem.getParameters().get(GROUP_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(model::setPotentialGroups); @@ -139,15 +142,19 @@ static public Optional userTaskActivateWorkItemHandler(Kogit UserTaskInstance instance = userTask.createInstance(model); if (workItem instanceof InternalKogitoWorkItem ikw) { - ikw.setExternalReferenceId(instance.id()); - ikw.setActualOwner(instance.getActualOwner()); + ikw.setExternalReferenceId(instance.getId()); + } + + if (instance instanceof DefaultUserTaskInstance defaultUserTaskInstance) { + defaultUserTaskInstance.setExternalReferenceId(workItem.getStringId()); } instance.transition(instance.createTransitionToken("activate", emptyMap())); + userTask.instances().update(instance); if (instance.getActualOwner() == null) { return Optional.empty(); } else { - return Optional.of(handler.newTransition(TRANSITION_ACTIVATED_CLAIM.id(), workItem.getPhaseStatus(), emptyMap())); + return Optional.of(handler.newTransition(TRANSITION_ACTIVATED_CLAIM.id(), workItem.getPhaseStatus(), singletonMap("ACTUAL_OWNER", instance.getActualOwner()))); } } @@ -172,7 +179,7 @@ static public Optional userTaskClaimWorkItemHandler(KogitoWo if (workItem instanceof InternalKogitoWorkItem ikw) { ikw.setActualOwner(ut.getActualOwner()); } - ut.transition(ut.createTransitionToken("claim", data)); + userTask.instances().update(ut); }); return Optional.empty(); } @@ -183,6 +190,7 @@ static public Optional userTaskReleaseWorkItemHandler(Kogito userTask.instances().findById(workItem.getExternalReferenceId()).ifPresent(ut -> { ut.setActuaOwner(null); ut.transition(ut.createTransitionToken("release", emptyMap())); + userTask.instances().update(ut); }); return Optional.empty(); @@ -193,6 +201,7 @@ static public Optional userTaskCompleteWorkItemHandler(Kogit UserTask userTask = userTasks.userTaskById((String) workItem.getParameter("id")); userTask.instances().findById(workItem.getExternalReferenceId()).ifPresent(ut -> { ut.transition(ut.createTransitionToken("complete", emptyMap())); + userTask.instances().update(ut); }); return Optional.empty(); @@ -202,7 +211,8 @@ static public Optional userTaskAbortWorkItemHandler(KogitoWo UserTasks userTasks = handler.getApplication().get(UserTasks.class); UserTask userTask = userTasks.userTaskById((String) workItem.getParameter("id")); userTask.instances().findById(workItem.getExternalReferenceId()).ifPresent(ut -> { - ut.transition(ut.createTransitionToken("fail", emptyMap())); + ut.transition(ut.createTransitionToken("skip", emptyMap())); + userTask.instances().update(ut); }); return Optional.empty(); } diff --git a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/DefaultUserTask.java b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/DefaultUserTask.java index 639f2763bb8..16a5923a160 100644 --- a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/DefaultUserTask.java +++ b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/DefaultUserTask.java @@ -57,6 +57,10 @@ public class DefaultUserTask implements UserTask { private Collection> startReassigments; private Collection> endReassigments; + public DefaultUserTask() { + // nothing + } + public DefaultUserTask(String id, String name) { this(null, id, name, new InMemoryUserTaskInstances()); } @@ -70,6 +74,8 @@ public DefaultUserTask(Application application, String id, String name, UserTask this.id = id; this.name = name; this.userTaskInstances = userTaskInstances; + this.userTaskInstances.setReconnectUserTaskInstance(this::connect); + this.userTaskInstances.setDisconnectUserTaskInstance(this::disconnect); } public void setApplication(Application application) { @@ -92,15 +98,30 @@ public UserTaskModel createModel() { @Override public UserTaskInstance createInstance(UserTaskModel userTaskModel) { + DefaultUserTaskInstance instance = new DefaultUserTaskInstance(this, userTaskModel); + return userTaskInstances.create(instance); + } + + private UserTaskInstance disconnect(UserTaskInstance userTaskInstance) { + DefaultUserTaskInstance instance = (DefaultUserTaskInstance) userTaskInstance; + instance.setUserTask(null); + instance.setUserTaskEventSupport(null); + instance.setUserTaskLifeCycle(null); + instance.setInstances(null); + return instance; + } + + public UserTaskInstance connect(UserTaskInstance userTaskInstance) { + DefaultUserTaskInstance instance = (DefaultUserTaskInstance) userTaskInstance; UserTaskConfig userTaskConfig = application.config().get(UserTaskConfig.class); KogitoUserTaskEventSupportImpl impl = new KogitoUserTaskEventSupportImpl(userTaskConfig.identityProvider()); userTaskConfig.userTaskEventListeners().listeners().forEach(impl::addEventListener); impl.addEventListener(new UnitOfWorkUserTaskEventListener(application.unitOfWorkManager())); - - DefaultUserTaskInstance instance = new DefaultUserTaskInstance(this, userTaskModel); + instance.setUserTask(this); instance.setUserTaskEventSupport(impl); instance.setUserTaskLifeCycle(userTaskConfig.userTaskLifeCycle()); - return userTaskInstances.create(instance); + instance.setInstances(userTaskInstances); + return instance; } @Override @@ -273,4 +294,5 @@ protected Set toSet(String value) { } return store; } + } diff --git a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/DefaultUserTaskInstance.java b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/DefaultUserTaskInstance.java index 7c8e4fddffa..06db19d8d56 100644 --- a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/DefaultUserTaskInstance.java +++ b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/DefaultUserTaskInstance.java @@ -21,6 +21,7 @@ import java.util.Collections; import java.util.HashSet; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.UUID; @@ -34,19 +35,31 @@ import org.kie.kogito.usertask.lifecycle.UserTaskTransitionToken; import org.kie.kogito.usertask.model.UserTaskModel; +import com.fasterxml.jackson.annotation.JsonIgnore; + public class DefaultUserTaskInstance implements UserTaskInstance { private String id; - private UserTask userTask; + private UserTaskModel userTaskModel; private UserTaskState status; private String actualOwner; private String externalReferenceId; + + @JsonIgnore private UserTaskInstances instances; + @JsonIgnore + private UserTask userTask; + @JsonIgnore private KogitoUserTaskEventSupport userTaskEventSupport; + @JsonIgnore private UserTaskLifeCycle setUserTaskLifeCycle; + public DefaultUserTaskInstance() { + // do nothing + } + public DefaultUserTaskInstance(UserTask userTask, UserTaskModel userTaskModel) { this.id = UUID.randomUUID().toString(); this.userTask = userTask; @@ -70,6 +83,10 @@ public void setUserTaskLifeCycle(UserTaskLifeCycle userTaskLifeCycle) { this.setUserTaskLifeCycle = userTaskLifeCycle; } + public void setInstances(UserTaskInstances instances) { + this.instances = instances; + } + @Override public void complete() { UserTaskTransitionToken transition = this.setUserTaskLifeCycle.newCompleteTransitionToken(this, Collections.emptyMap()); @@ -84,8 +101,12 @@ public void abort() { instances.remove(id); } + public void setId(String id) { + this.id = id; + } + @Override - public String id() { + public String getId() { return this.id; } @@ -94,8 +115,12 @@ public UserTaskModel getUserTaskModel() { return userTaskModel; } + public void setStatus(UserTaskState status) { + this.status = status; + } + @Override - public UserTaskState status() { + public UserTaskState getStatus() { return status; } @@ -119,6 +144,10 @@ public String getExternalReferenceId() { return externalReferenceId; } + public void setExternalReferenceId(String externalReferenceId) { + this.externalReferenceId = externalReferenceId; + } + @Override public UserTaskTransitionToken createTransitionToken(String transitionId, Map data) { return this.setUserTaskLifeCycle.newTransitionToken(transitionId, this, data); @@ -126,9 +155,16 @@ public UserTaskTransitionToken createTransitionToken(String transitionId, Map next = Optional.of(token); + while (next.isPresent()) { + UserTaskTransition transition = next.get().transition(); + next = this.setUserTaskLifeCycle.transition(this, token); + this.status = transition.target(); + this.userTaskEventSupport.fireOneUserTaskStateChange(this, transition.source().getName(), transition.target().getName()); + if (this.status.isTerminate().isPresent()) { + this.instances.remove(this.id); + } + } } @Override @@ -136,4 +172,8 @@ public UserTask getUserTask() { return userTask; } + public void setUserTask(DefaultUserTask userTask) { + this.userTask = userTask; + } + } diff --git a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/InMemoryUserTaskInstances.java b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/InMemoryUserTaskInstances.java index f2ec0abf61f..6704fbd83a8 100644 --- a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/InMemoryUserTaskInstances.java +++ b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/InMemoryUserTaskInstances.java @@ -21,21 +21,45 @@ import java.util.HashMap; import java.util.Map; import java.util.Optional; +import java.util.function.Function; import org.kie.kogito.usertask.UserTaskInstance; import org.kie.kogito.usertask.UserTaskInstances; +import com.fasterxml.jackson.databind.ObjectMapper; + public class InMemoryUserTaskInstances implements UserTaskInstances { - Map userTaskInstances; + private Map userTaskInstances; + private Function reconnectUserTaskInstance; + private Function disconnectUserTaskInstance; + private ObjectMapper mapper; public InMemoryUserTaskInstances() { this.userTaskInstances = new HashMap<>(); + this.reconnectUserTaskInstance = Function.identity(); + this.disconnectUserTaskInstance = Function.identity(); + this.mapper = new ObjectMapper(); + } + + @Override + public void setReconnectUserTaskInstance(Function reconnectUserTaskInstance) { + this.reconnectUserTaskInstance = reconnectUserTaskInstance; + } + + @Override + public void setDisconnectUserTaskInstance(Function disconnectUserTaskInstance) { + this.disconnectUserTaskInstance = disconnectUserTaskInstance; } @Override public Optional findById(String userTaskInstanceId) { - return Optional.ofNullable(userTaskInstances.get(userTaskInstanceId)); + try { + UserTaskInstance userTaskInstance = mapper.readValue(userTaskInstances.get(userTaskInstanceId), DefaultUserTaskInstance.class); + return Optional.ofNullable(reconnectUserTaskInstance.apply(userTaskInstance)); + } catch (Exception e) { + return Optional.empty(); + } } @Override @@ -45,19 +69,33 @@ public boolean exists(String userTaskInstanceId) { @Override public UserTaskInstance create(UserTaskInstance userTaskInstance) { - userTaskInstances.put(userTaskInstance.id(), userTaskInstance); - return userTaskInstance; + try { + byte[] data = mapper.writeValueAsBytes(userTaskInstance); + userTaskInstances.put(userTaskInstance.getId(), data); + return reconnectUserTaskInstance.apply(userTaskInstance); + } catch (Exception e) { + return null; + } } @Override public UserTaskInstance update(UserTaskInstance userTaskInstance) { - userTaskInstances.put(userTaskInstance.id(), userTaskInstance); - return userTaskInstance; + try { + byte[] data = mapper.writeValueAsBytes(userTaskInstance); + userTaskInstances.put(userTaskInstance.getId(), data); + return userTaskInstance; + } catch (Exception e) { + return null; + } } @Override public UserTaskInstance remove(String userTaskInstanceId) { - return userTaskInstances.remove(userTaskInstanceId); + try { + return disconnectUserTaskInstance.apply(mapper.readValue(userTaskInstances.remove(userTaskInstanceId), DefaultUserTaskInstance.class)); + } catch (Exception e) { + return null; + } } } diff --git a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/lifecycle/DefaultUserTaskLifeCycle.java b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/lifecycle/DefaultUserTaskLifeCycle.java index 7a67ca5e62e..da681ed8f4d 100644 --- a/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/lifecycle/DefaultUserTaskLifeCycle.java +++ b/jbpm/jbpm-usertask/src/main/java/org/kie/kogito/usertask/impl/lifecycle/DefaultUserTaskLifeCycle.java @@ -41,8 +41,10 @@ public class DefaultUserTaskLifeCycle implements UserTaskLifeCycle { private static final UserTaskTransition T_NEW_ACTIVE = new DefaultUserTransition("activate", INACTIVE, ACTIVE, DefaultUserTaskLifeCycle::activate); private static final UserTaskTransition T_ACTIVE_RESERVED = new DefaultUserTransition("claim", ACTIVE, RESERVED, DefaultUserTaskLifeCycle::claim); private static final UserTaskTransition T_RESERVED_ACTIVE = new DefaultUserTransition("release", RESERVED, ACTIVE, DefaultUserTaskLifeCycle::release); + private static final UserTaskTransition T_ACTIVE_COMPLETED = new DefaultUserTransition("complete", ACTIVE, COMPLETED, DefaultUserTaskLifeCycle::complete); private static final UserTaskTransition T_RESERVED_COMPLETED = new DefaultUserTransition("complete", RESERVED, COMPLETED, DefaultUserTaskLifeCycle::complete); private static final UserTaskTransition T_RESERVED_SKIPPED = new DefaultUserTransition("skip", RESERVED, OBSOLETE, DefaultUserTaskLifeCycle::skip); + private static final UserTaskTransition T_ACTIVE_SKIPPED = new DefaultUserTransition("skip", ACTIVE, OBSOLETE, DefaultUserTaskLifeCycle::complete); private static final UserTaskTransition T_RESERVED_ERROR = new DefaultUserTransition("fail", RESERVED, ERROR, DefaultUserTaskLifeCycle::fail); private List transitions; @@ -52,7 +54,9 @@ public DefaultUserTaskLifeCycle() { T_NEW_ACTIVE, T_ACTIVE_RESERVED, T_RESERVED_ACTIVE, + T_ACTIVE_COMPLETED, T_RESERVED_COMPLETED, + T_ACTIVE_SKIPPED, T_RESERVED_SKIPPED, T_RESERVED_ERROR); } @@ -64,7 +68,7 @@ public Optional transition(UserTaskInstance userTaskIns @Override public UserTaskTransitionToken newTransitionToken(String transitionId, UserTaskInstance userTaskInstance, Map data) { - UserTaskState state = userTaskInstance.status(); + UserTaskState state = userTaskInstance.getStatus(); UserTaskTransition transition = transitions.stream().filter(e -> e.source().equals(state) && e.id().equals(transitionId)).findAny() .orElseThrow(() -> new RuntimeException("Invalid transition " + transitionId + " from " + state)); return new DefaultUserTaskTransitionToken(transition, data); diff --git a/jbpm/process-workitems/src/main/java/org/jbpm/process/workitem/builtin/SystemOutWorkItemHandler.java b/jbpm/process-workitems/src/main/java/org/jbpm/process/workitem/builtin/SystemOutWorkItemHandler.java index c0128dbc1bd..8a885a3c815 100755 --- a/jbpm/process-workitems/src/main/java/org/jbpm/process/workitem/builtin/SystemOutWorkItemHandler.java +++ b/jbpm/process-workitems/src/main/java/org/jbpm/process/workitem/builtin/SystemOutWorkItemHandler.java @@ -39,4 +39,8 @@ public Optional abortWorkItemHandler(KogitoWorkItemManager m return Optional.of(this.workItemLifeCycle.newTransition("abort", workItem.getPhaseStatus(), workItem.getResults())); } + @Override + public String getName() { + return "Log"; + } } diff --git a/jbpm/process-workitems/src/main/java/org/kie/kogito/process/workitems/impl/KogitoWorkItemImpl.java b/jbpm/process-workitems/src/main/java/org/kie/kogito/process/workitems/impl/KogitoWorkItemImpl.java index 895a5beefc9..af061463843 100755 --- a/jbpm/process-workitems/src/main/java/org/kie/kogito/process/workitems/impl/KogitoWorkItemImpl.java +++ b/jbpm/process-workitems/src/main/java/org/kie/kogito/process/workitems/impl/KogitoWorkItemImpl.java @@ -543,7 +543,7 @@ public String getExternalReferenceId() { @Override public void setExternalReferenceId(String externalReferenceId) { - this.externalReferenceId = id; + this.externalReferenceId = externalReferenceId; } @Override diff --git a/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/java/org/kie/kogito/codegen/tests/PublishEventIT.java b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/java/org/kie/kogito/codegen/tests/PublishEventIT.java index fd49a73f882..39efe723397 100644 --- a/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/java/org/kie/kogito/codegen/tests/PublishEventIT.java +++ b/kogito-codegen-modules/kogito-codegen-processes-integration-tests/src/test/java/org/kie/kogito/codegen/tests/PublishEventIT.java @@ -252,7 +252,7 @@ public void onUserTaskState(UserTaskStateEvent event) { left = findNodeInstanceEvents(events, 2); assertThat(left).hasSize(2).extractingResultOf("getNodeType").containsOnly("HumanTaskNode", "EndNode"); - assertUserTaskInstanceEvent(events.get(1), "SecondTask", null, "1", "Completed", "UserTasksProcess", "Second Task"); + assertUserTaskInstanceEvent(events.get(0), "SecondTask", null, "1", "Completed", "UserTasksProcess", "Second Task"); } @Test @@ -299,12 +299,12 @@ public void testBasicUserTaskProcessAbort() throws Exception { uow.end(); assertThat(processInstance.status()).isEqualTo(ProcessInstance.STATE_ABORTED); events = publisher.extract(); - assertThat(events).hasSize(4); + assertThat(events).hasSize(3); triggered = findNodeInstanceEvents(events, ProcessInstanceNodeEventBody.EVENT_TYPE_ABORTED); assertThat(triggered).hasSize(1).extractingResultOf("getNodeName").containsOnly("First Task"); - assertProcessInstanceEvent(events.get(3), "UserTasksProcess", "UserTasksProcess", ProcessInstance.STATE_ABORTED); + assertProcessInstanceEvent(events.get(2), "UserTasksProcess", "UserTasksProcess", ProcessInstance.STATE_ABORTED); }