Skip to content

Commit

Permalink
[Fix #2113] Optimization for user tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Oct 15, 2024
1 parent a4f9386 commit b28fccf
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public void indexProcessInstanceEvent(ProcessInstanceDataEvent<?> event) {
ProcessInstanceStorage storage = manager.getProcessInstanceStorage();
if (event instanceof MultipleProcessInstanceDataEvent) {
storage.indexGroup(((MultipleProcessInstanceDataEvent) event));
}
if (event instanceof ProcessInstanceErrorDataEvent) {
} else if (event instanceof ProcessInstanceErrorDataEvent) {
storage.indexError((ProcessInstanceErrorDataEvent) event);
} else if (event instanceof ProcessInstanceNodeDataEvent) {
storage.indexNode((ProcessInstanceNodeDataEvent) event);
Expand All @@ -106,16 +105,8 @@ public void indexProcessDefinition(ProcessDefinitionDataEvent definitionDataEven
public <T> void indexUserTaskInstanceEvent(UserTaskInstanceDataEvent<T> event) {
UserTaskInstanceStorage storage = manager.getUserTaskInstanceStorage();
if (event instanceof MultipleUserTaskInstanceDataEvent) {
for (UserTaskInstanceDataEvent<?> item : ((MultipleUserTaskInstanceDataEvent) event).getData()) {
indexUserTaskInstanceEvent(storage, item);
}
} else {
indexUserTaskInstanceEvent(storage, event);
}
}

private void indexUserTaskInstanceEvent(UserTaskInstanceStorage storage, UserTaskInstanceDataEvent<?> event) {
if (event instanceof UserTaskInstanceAssignmentDataEvent) {
storage.indexGroup((MultipleUserTaskInstanceDataEvent) event);
} else if (event instanceof UserTaskInstanceAssignmentDataEvent) {
storage.indexAssignment((UserTaskInstanceAssignmentDataEvent) event);
} else if (event instanceof UserTaskInstanceAttachmentDataEvent) {
storage.indexAttachment((UserTaskInstanceAttachmentDataEvent) event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.kie.kogito.index.storage;

import org.kie.kogito.event.usertask.MultipleUserTaskInstanceDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceAssignmentDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceAttachmentDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceCommentDataEvent;
Expand All @@ -40,4 +41,6 @@ public interface UserTaskInstanceStorage extends StorageFetcher<String, UserTask
void indexComment(UserTaskInstanceCommentDataEvent event);

void indexVariable(UserTaskInstanceVariableDataEvent event);

void indexGroup(MultipleUserTaskInstanceDataEvent event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;

import org.kie.kogito.event.usertask.MultipleUserTaskInstanceDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceAssignmentDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceAttachmentDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceCommentDataEvent;
Expand Down Expand Up @@ -82,6 +83,27 @@ public void indexVariable(UserTaskInstanceVariableDataEvent event) {
@Override
public void indexComment(UserTaskInstanceCommentDataEvent event) {
index(event, commentMerger);
}

@Override
public void indexGroup(MultipleUserTaskInstanceDataEvent events) {

for (UserTaskInstanceDataEvent<?> event : events.getData()) {

if (event instanceof UserTaskInstanceAssignmentDataEvent) {
index((UserTaskInstanceAssignmentDataEvent) event, assignmentMerger);
} else if (event instanceof UserTaskInstanceAttachmentDataEvent) {
index((UserTaskInstanceAttachmentDataEvent) event, attachmentMerger);
} else if (event instanceof UserTaskInstanceDeadlineDataEvent) {
index((UserTaskInstanceDeadlineDataEvent) event, deadlineMerger);
} else if (event instanceof UserTaskInstanceStateDataEvent) {
index((UserTaskInstanceStateDataEvent) event, stateMerger);
} else if (event instanceof UserTaskInstanceCommentDataEvent) {
index((UserTaskInstanceCommentDataEvent) event, commentMerger);
} else if (event instanceof UserTaskInstanceVariableDataEvent) {
index((UserTaskInstanceVariableDataEvent) event, variableMerger);
}
}

}

Expand All @@ -99,4 +121,5 @@ private <T extends UserTaskInstanceDataEvent<?>> void index(T event, UserTaskIns
}
storage.put(event.getKogitoUserTaskInstanceId(), merger.merge(taskInstance, event));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
package org.kie.kogito.index.jpa.storage;

import java.net.URI;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.kie.kogito.event.usertask.MultipleUserTaskInstanceDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceAssignmentDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceAssignmentEventBody;
import org.kie.kogito.event.usertask.UserTaskInstanceAttachmentDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceAttachmentEventBody;
import org.kie.kogito.event.usertask.UserTaskInstanceCommentDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceCommentEventBody;
import org.kie.kogito.event.usertask.UserTaskInstanceDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceDeadlineDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceStateDataEvent;
import org.kie.kogito.event.usertask.UserTaskInstanceStateEventBody;
Expand Down Expand Up @@ -66,20 +70,69 @@ public UserTaskInstanceEntityStorage(UserTaskInstanceEntityRepository repository
super(repository, UserTaskInstanceEntity.class, mapper::mapToModel);
}

private UserTaskInstanceEntity findOrInit(String taskId) {
return repository.findByIdOptional(taskId).orElseGet(() -> {
UserTaskInstanceEntity ut = new UserTaskInstanceEntity();
ut.setId(taskId);
repository.persist(ut);
return ut;
});
@Override
@Transactional
public void indexGroup(MultipleUserTaskInstanceDataEvent events) {
Map<String, UserTaskInstanceEntity> taskMap = new HashMap<>();
for (UserTaskInstanceDataEvent<?> event : events.getData()) {
indexEvent(taskMap.computeIfAbsent(event.getKogitoUserTaskInstanceId(), id -> findOrInit(id)), event);
}
}

@Override
@Transactional
public void indexAssignment(UserTaskInstanceAssignmentDataEvent event) {
indexAssignment(findOrInit(event), event);
}

@Override
@Transactional
public void indexAttachment(UserTaskInstanceAttachmentDataEvent event) {
indexAttachment(findOrInit(event), event);
}

@Override
@Transactional
public void indexDeadline(UserTaskInstanceDeadlineDataEvent event) {
indexDeadline(findOrInit(event), event);
}

@Override
@Transactional
public void indexState(UserTaskInstanceStateDataEvent event) {
indexState(findOrInit(event), event);
}

@Override
@Transactional
public void indexComment(UserTaskInstanceCommentDataEvent event) {
indexComment(findOrInit(event), event);
}

@Override
@Transactional
public void indexVariable(UserTaskInstanceVariableDataEvent event) {
indexVariable(findOrInit(event), event);
}

private void indexEvent(UserTaskInstanceEntity task, UserTaskInstanceDataEvent<?> event) {
if (event instanceof UserTaskInstanceAssignmentDataEvent) {
indexAssignment(task, (UserTaskInstanceAssignmentDataEvent) event);
} else if (event instanceof UserTaskInstanceAttachmentDataEvent) {
indexAttachment(task, (UserTaskInstanceAttachmentDataEvent) event);
} else if (event instanceof UserTaskInstanceDeadlineDataEvent) {
indexDeadline(task, (UserTaskInstanceDeadlineDataEvent) event);
} else if (event instanceof UserTaskInstanceStateDataEvent) {
indexState(task, (UserTaskInstanceStateDataEvent) event);
} else if (event instanceof UserTaskInstanceCommentDataEvent) {
indexComment(task, (UserTaskInstanceCommentDataEvent) event);
} else if (event instanceof UserTaskInstanceVariableDataEvent) {
indexVariable(task, (UserTaskInstanceVariableDataEvent) event);
}
}

private void indexAssignment(UserTaskInstanceEntity userTaskInstance, UserTaskInstanceAssignmentDataEvent event) {
UserTaskInstanceAssignmentEventBody body = event.getData();
UserTaskInstanceEntity userTaskInstance = findOrInit(event.getKogitoUserTaskInstanceId());
switch (body.getAssignmentType()) {
case "USER_OWNERS":
userTaskInstance.setPotentialUsers(new HashSet<>(body.getUsers()));
Expand All @@ -97,13 +150,9 @@ public void indexAssignment(UserTaskInstanceAssignmentDataEvent event) {
userTaskInstance.setAdminUsers(new HashSet<>(body.getUsers()));
break;
}
repository.flush();
}

@Override
@Transactional
public void indexAttachment(UserTaskInstanceAttachmentDataEvent event) {
UserTaskInstanceEntity userTaskInstance = findOrInit(event.getKogitoUserTaskInstanceId());
private void indexAttachment(UserTaskInstanceEntity userTaskInstance, UserTaskInstanceAttachmentDataEvent event) {
UserTaskInstanceAttachmentEventBody body = event.getData();
List<AttachmentEntity> attachments = userTaskInstance.getAttachments();
switch (body.getEventType()) {
Expand All @@ -127,17 +176,12 @@ public void indexAttachment(UserTaskInstanceAttachmentDataEvent event) {
}
}

@Override
@Transactional
public void indexDeadline(UserTaskInstanceDeadlineDataEvent event) {
findOrInit(event.getKogitoUserTaskInstanceId());
private void indexDeadline(UserTaskInstanceEntity userTaskInstance, UserTaskInstanceDeadlineDataEvent event) {
// deadlines ignored for now
}

@Override
@Transactional
public void indexState(UserTaskInstanceStateDataEvent event) {
private void indexState(UserTaskInstanceEntity task, UserTaskInstanceStateDataEvent event) {
UserTaskInstanceStateEventBody body = event.getData();
UserTaskInstanceEntity task = findOrInit(event.getKogitoUserTaskInstanceId());
task.setProcessInstanceId(body.getProcessInstanceId());
task.setProcessId(event.getKogitoProcessId());
task.setRootProcessId(event.getKogitoRootProcessId());
Expand All @@ -163,11 +207,8 @@ private String getEndpoint(URI source, String pId, String taskName, String taskI
return source.toString() + format("/%s/%s/%s", pId, name, taskId);
}

@Override
@Transactional
public void indexComment(UserTaskInstanceCommentDataEvent event) {
private void indexComment(UserTaskInstanceEntity userTaskInstance, UserTaskInstanceCommentDataEvent event) {
UserTaskInstanceCommentEventBody body = event.getData();
UserTaskInstanceEntity userTaskInstance = findOrInit(event.getKogitoUserTaskInstanceId());
List<CommentEntity> comments = userTaskInstance.getComments();
switch (body.getEventType()) {
case UserTaskInstanceCommentEventBody.EVENT_TYPE_ADDED:
Expand All @@ -190,10 +231,7 @@ public void indexComment(UserTaskInstanceCommentDataEvent event) {
}
}

@Override
@Transactional
public void indexVariable(UserTaskInstanceVariableDataEvent event) {
UserTaskInstanceEntity userTaskInstance = findOrInit(event.getKogitoUserTaskInstanceId());
private void indexVariable(UserTaskInstanceEntity userTaskInstance, UserTaskInstanceVariableDataEvent event) {
UserTaskInstanceVariableEventBody body = event.getData();
if (body.getVariableType().equals("INPUT")) {
ObjectNode objectNode = userTaskInstance.getInputs();
Expand All @@ -211,4 +249,17 @@ public void indexVariable(UserTaskInstanceVariableDataEvent event) {
userTaskInstance.setOutputs(objectNode);
}
}

private UserTaskInstanceEntity findOrInit(UserTaskInstanceDataEvent<?> event) {
return findOrInit(event.getKogitoUserTaskInstanceId());
}

private UserTaskInstanceEntity findOrInit(String taskId) {
return repository.findByIdOptional(taskId).orElseGet(() -> {
UserTaskInstanceEntity ut = new UserTaskInstanceEntity();
ut.setId(taskId);
repository.persist(ut);
return ut;
});
}
}

0 comments on commit b28fccf

Please sign in to comment.