Skip to content

Commit

Permalink
fix PublishEventsIT
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Sep 4, 2024
1 parent ce6441b commit 4ca4ea0
Show file tree
Hide file tree
Showing 19 changed files with 232 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@

public interface UserTaskInstance {

String id();
String getId();

UserTask getUserTask();

UserTaskModel getUserTaskModel();

UserTaskState status();
UserTaskState getStatus();

boolean hasActualOwner();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.kie.kogito.usertask;

import java.util.Optional;
import java.util.function.Function;

public interface UserTaskInstances {

Expand All @@ -32,4 +33,8 @@ public interface UserTaskInstances {

UserTaskInstance remove(String userTaskInstanceId);

void setReconnectUserTaskInstance(Function<UserTaskInstance, UserTaskInstance> reconnectUserTaskInstance);

void setDisconnectUserTaskInstance(Function<UserTaskInstance, UserTaskInstance> disconnectUserTaskInstance);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,4 +94,9 @@ public static UserTaskState initalized() {
return of(null);
}

@Override
public String toString() {
return "UserTaskState [terminate=" + terminate + ", name=" + name + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -39,6 +41,12 @@ public class UserTaskModel {
private Map<String, Object> inputs;
private Map<String, Object> outputs;

private Map<String, Object> metadata;

public UserTaskModel() {
this.metadata = new HashMap<>();
}

public Map<String, Object> getInputs() {
return inputs;
}
Expand Down Expand Up @@ -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<String, Object> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, Object> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public static Map<String, Object> buildProcessMetadata(KogitoWorkflowProcessInst

public static Map<String, Object> buildUserTaskMetadata(UserTaskInstance pi) {
Map<String, Object> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -130,6 +132,7 @@ static public Optional<WorkItemTransition> 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);
Expand All @@ -139,15 +142,19 @@ static public Optional<WorkItemTransition> 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())));
}
}

Expand All @@ -172,7 +179,7 @@ static public Optional<WorkItemTransition> userTaskClaimWorkItemHandler(KogitoWo
if (workItem instanceof InternalKogitoWorkItem ikw) {
ikw.setActualOwner(ut.getActualOwner());
}
ut.transition(ut.createTransitionToken("claim", data));
userTask.instances().update(ut);
});
return Optional.empty();
}
Expand All @@ -183,6 +190,7 @@ static public Optional<WorkItemTransition> 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();
Expand All @@ -193,6 +201,7 @@ static public Optional<WorkItemTransition> 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();
Expand All @@ -202,7 +211,8 @@ static public Optional<WorkItemTransition> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public class DefaultUserTask implements UserTask {
private Collection<DeadlineInfo<Reassignment>> startReassigments;
private Collection<DeadlineInfo<Reassignment>> endReassigments;

public DefaultUserTask() {
// nothing
}

public DefaultUserTask(String id, String name) {
this(null, id, name, new InMemoryUserTaskInstances());
}
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -273,4 +294,5 @@ protected Set<String> toSet(String value) {
}
return store;
}

}
Loading

0 comments on commit 4ca4ea0

Please sign in to comment.