Skip to content

Commit

Permalink
fix user task instance events
Browse files Browse the repository at this point in the history
  • Loading branch information
elguardian committed Sep 15, 2024
1 parent 520f0d8 commit 1d43a67
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@
import org.kie.kogito.event.impl.adapter.DataEventAdapter.DataEventAdapterConfig;
import org.kie.kogito.event.process.ProcessInstanceStateDataEvent;
import org.kie.kogito.event.process.ProcessInstanceStateEventBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultInstanceEventBatch implements EventBatch {

private static final Logger LOG = LoggerFactory.getLogger(DefaultInstanceEventBatch.class);

private String service;
private Addons addons;
private Collection<DataEvent<?>> processedEvents;
Expand Down Expand Up @@ -64,6 +68,7 @@ public int compare(DataEvent<?> event1, DataEvent<?> event2) {

@Override
public void append(Object event) {
LOG.info("event generated {}", event);
this.dataEventAdapters.stream().filter(a -> a.accept(event)).map(a -> a.adapt(event)).forEach(this.processedEvents::add);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ 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.getId());
metadata.put(ProcessInstanceEventMetadata.PROCESS_INSTANCE_ID_META_DATA, pi.getMetadata().get("ProcessInstanceId"));
metadata.put(ProcessInstanceEventMetadata.PROCESS_VERSION_META_DATA, pi.getMetadata().get("ProcessVersion"));
metadata.put(ProcessInstanceEventMetadata.PROCESS_ID_META_DATA, pi.getMetadata().get("ProcessId"));
metadata.put(ProcessInstanceEventMetadata.PROCESS_INSTANCE_STATE_META_DATA, String.valueOf(pi.getMetadata().get("ProcessInstanceState")));
metadata.put(ProcessInstanceEventMetadata.PROCESS_TYPE_META_DATA, pi.getMetadata().get("ProcessType"));
metadata.put(ProcessInstanceEventMetadata.PARENT_PROCESS_INSTANCE_ID_META_DATA, pi.getMetadata().get("ParentProcessInstanceId"));
metadata.put(ProcessInstanceEventMetadata.ROOT_PROCESS_ID_META_DATA, pi.getMetadata().get("RootProcessId"));
metadata.put(ProcessInstanceEventMetadata.ROOT_PROCESS_INSTANCE_ID_META_DATA, pi.getMetadata().get("RootProcessInstanceId"));

metadata.put(UserTaskInstanceEventMetadata.USER_TASK_INSTANCE_ID_META_DATA, pi.getExternalReferenceId());
metadata.put(UserTaskInstanceEventMetadata.USER_TASK_INSTANCE_REFERENCE_ID_META_DATA, pi.getUserTask().getReferenceName());
metadata.put(UserTaskInstanceEventMetadata.USER_TASK_INSTANCE_STATE_META_DATA, pi.getStatus().getName());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,34 @@ static public Optional<WorkItemTransition> userTaskActivateWorkItemHandler(Kogit
UserTask userTask = userTasks.userTaskById((String) workItem.getParameter(KogitoWorkItem.PARAMETER_UNIQUE_TASK_ID));

DefaultUserTaskInstance instance = (DefaultUserTaskInstance) userTask.createInstance();
ofNullable(workItem.getParameters().get(ACTOR_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setPotentialUsers);
ofNullable(workItem.getParameters().get(GROUP_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setPotentialGroups);
ofNullable(workItem.getParameters().get(BUSINESSADMINISTRATOR_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setAdminUsers);
ofNullable(workItem.getParameters().get(BUSINESSADMINISTRATOR_GROUP_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setAdminGroups);
ofNullable(workItem.getParameters().get(EXCLUDED_OWNER_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setExcludedUsers);

instance.setId(workItem.getStringId());
instance.setTaskName((String) workItem.getParameter(TASK_NAME));
instance.setTaskDescription((String) workItem.getParameter(DESCRIPTION));
instance.setTaskPriority(priorityInteger);
instance.setExternalReferenceId(workItem.getStringId());
instance.setMetadata("ProcessId", workItem.getProcessInstance().getProcessId());
instance.setMetadata("ProcessType", workItem.getProcessInstance().getProcess().getType());
instance.setMetadata("ProcessVersion", workItem.getProcessInstance().getProcessVersion());
instance.setMetadata("ProcessInstanceId", workItem.getProcessInstance().getStringId());
instance.setMetadata("ProcessInstanceState", workItem.getProcessInstance().getState());
instance.setMetadata("RootProcessId", workItem.getProcessInstance().getRootProcessId());
instance.setMetadata("RootProcessInstanceId", workItem.getProcessInstance().getRootProcessId());
instance.setMetadata("ParentProcessInstanceId", workItem.getProcessInstance().getRootProcessId());

ofNullable(workItem.getParameters().get(ACTOR_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setPotentialUsers);
ofNullable(workItem.getParameters().get(GROUP_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setPotentialGroups);
ofNullable(workItem.getParameters().get(BUSINESSADMINISTRATOR_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setAdminUsers);
ofNullable(workItem.getParameters().get(BUSINESSADMINISTRATOR_GROUP_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setAdminGroups);
ofNullable(workItem.getParameters().get(EXCLUDED_OWNER_ID)).map(String.class::cast).map(UserTaskKogitoWorkItemHandler::toSet).ifPresent(instance::setExcludedUsers);

instance.setExternalReferenceId(workItem.getStringId());
instance.assign();
instance.transition(instance.createTransitionToken("activate", emptyMap()));
userTask.instances().update(instance);

if (workItem instanceof InternalKogitoWorkItem ikw) {
ikw.setExternalReferenceId(instance.getId());
ikw.setActualOwner(instance.getActualOwner());
}
userTask.instances().create(instance);
return Optional.empty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public UserTaskInstance createInstance() {
instance.setAdminUsers(getAdminUsers());
instance.setPotentialGroups(getPotentialGroups());
instance.setExcludedUsers(getExcludedUsers());
return userTaskInstances.create(instance);
connect(instance);
return instance;
}

private UserTaskInstance disconnect(UserTaskInstance userTaskInstance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void setPotentialUsers(Set<String> potentialUsers) {
Set<String> oldValues = new HashSet<>(this.potentialUsers);
this.potentialUsers = potentialUsers;
if (this.userTaskEventSupport != null) {
this.userTaskEventSupport.fireOnUserTaskAssignmentChange(this, AssignmentType.USER_OWNERS, oldValues, excludedUsers);
this.userTaskEventSupport.fireOnUserTaskAssignmentChange(this, AssignmentType.USER_OWNERS, oldValues, potentialUsers);
}
}

Expand All @@ -307,7 +307,7 @@ public void setPotentialGroups(Set<String> potentialGroups) {
Set<String> oldValues = new HashSet<>(this.potentialGroups);
this.potentialGroups = potentialGroups;
if (this.userTaskEventSupport != null) {
this.userTaskEventSupport.fireOnUserTaskAssignmentChange(this, AssignmentType.USER_GROUPS, oldValues, excludedUsers);
this.userTaskEventSupport.fireOnUserTaskAssignmentChange(this, AssignmentType.USER_GROUPS, oldValues, potentialGroups);
}
}

Expand All @@ -325,7 +325,7 @@ public void setAdminUsers(Set<String> adminUsers) {
Set<String> oldValues = new HashSet<>(this.adminUsers);
this.adminUsers = adminUsers;
if (this.userTaskEventSupport != null) {
this.userTaskEventSupport.fireOnUserTaskAssignmentChange(this, AssignmentType.ADMIN_USERS, oldValues, excludedUsers);
this.userTaskEventSupport.fireOnUserTaskAssignmentChange(this, AssignmentType.ADMIN_USERS, oldValues, adminUsers);
}
}

Expand All @@ -343,7 +343,7 @@ public void setAdminGroups(Set<String> adminGroups) {
Set<String> oldValues = new HashSet<>(this.adminGroups);
this.adminGroups = adminGroups;
if (this.userTaskEventSupport != null) {
this.userTaskEventSupport.fireOnUserTaskAssignmentChange(this, AssignmentType.ADMIN_GROUPS, oldValues, excludedUsers);
this.userTaskEventSupport.fireOnUserTaskAssignmentChange(this, AssignmentType.ADMIN_GROUPS, oldValues, adminGroups);
}
}

Expand Down Expand Up @@ -472,4 +472,10 @@ public Comment findCommentById(String commentId) {
return this.comments.stream().filter(e -> e.getId().equals(commentId)).findAny().orElse(null);
}

@Override
public String toString() {
return "DefaultUserTaskInstance [id=" + id + ", status=" + status + ", actualOwner=" + actualOwner + ", taskName=" + taskName + ", taskDescription=" + taskDescription + ", taskPriority="
+ taskPriority + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ public String[] getOldUsersId() {
public String[] getNewUsersId() {
return newUsersId.toArray(String[]::new);
}

@Override
public String toString() {
return "UserTaskAssignmentEventImpl [assignmentType=" + assignmentType + ", oldUsersId=" + oldUsersId + ", newUsersId=" + newUsersId + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public Attachment getNewAttachment() {
return newAttachment;
}

@Override
public String toString() {
return "UserTaskAttachmentEventImpl [oldAttachment=" + oldAttachment + ", newAttachment=" + newAttachment + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public Comment getOldComment() {
return oldComment;
}

@Override
public String toString() {
return "UserTaskCommentEventImpl [oldComment=" + oldComment + ", newComment=" + newComment + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public DeadlineType getType() {

@Override
public String toString() {
return "HumanTaskDeadlineEventImpl [notification=" + notification + ", type=" +
type + "]";
return "HumanTaskDeadlineEventImpl [notification=" + notification + ", type=" + type + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public String getOldStatus() {
return oldStatus;
}

@Override
public String toString() {
return "UserTaskStateEventImpl [oldStatus=" + oldStatus + ", newStatus=" + newStatus + "]";
}

}

0 comments on commit 1d43a67

Please sign in to comment.