diff --git a/data-index/data-index-common/src/main/java/org/kie/kogito/index/service/IndexingService.java b/data-index/data-index-common/src/main/java/org/kie/kogito/index/service/IndexingService.java index 947e1972bf..44cc68f5e1 100644 --- a/data-index/data-index-common/src/main/java/org/kie/kogito/index/service/IndexingService.java +++ b/data-index/data-index-common/src/main/java/org/kie/kogito/index/service/IndexingService.java @@ -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); @@ -106,16 +105,8 @@ public void indexProcessDefinition(ProcessDefinitionDataEvent definitionDataEven public void indexUserTaskInstanceEvent(UserTaskInstanceDataEvent 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); @@ -130,6 +121,10 @@ private void indexUserTaskInstanceEvent(UserTaskInstanceStorage storage, UserTas } } + private void indexUserTaskInstanceEvent(UserTaskInstanceStorage storage, UserTaskInstanceDataEvent event) { + + } + public void indexJob(Job job) { manager.getJobsStorage().put(job.getId(), job); } diff --git a/data-index/data-index-storage/data-index-storage-api/src/main/java/org/kie/kogito/index/storage/UserTaskInstanceStorage.java b/data-index/data-index-storage/data-index-storage-api/src/main/java/org/kie/kogito/index/storage/UserTaskInstanceStorage.java index f315ae4f73..58c586fd62 100644 --- a/data-index/data-index-storage/data-index-storage-api/src/main/java/org/kie/kogito/index/storage/UserTaskInstanceStorage.java +++ b/data-index/data-index-storage/data-index-storage-api/src/main/java/org/kie/kogito/index/storage/UserTaskInstanceStorage.java @@ -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; @@ -40,4 +41,6 @@ public interface UserTaskInstanceStorage extends StorageFetcher 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); + } + } } @@ -99,4 +121,5 @@ private > void index(T event, UserTaskIns } storage.put(event.getKogitoUserTaskInstanceId(), merger.merge(taskInstance, event)); } + } diff --git a/data-index/data-index-storage/data-index-storage-jpa-common/src/main/java/org/kie/kogito/index/jpa/storage/UserTaskInstanceEntityStorage.java b/data-index/data-index-storage/data-index-storage-jpa-common/src/main/java/org/kie/kogito/index/jpa/storage/UserTaskInstanceEntityStorage.java index 1e2c127a36..cb75b1e31a 100644 --- a/data-index/data-index-storage/data-index-storage-jpa-common/src/main/java/org/kie/kogito/index/jpa/storage/UserTaskInstanceEntityStorage.java +++ b/data-index/data-index-storage/data-index-storage-jpa-common/src/main/java/org/kie/kogito/index/jpa/storage/UserTaskInstanceEntityStorage.java @@ -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; @@ -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 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())); @@ -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 attachments = userTaskInstance.getAttachments(); switch (body.getEventType()) { @@ -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()); @@ -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 comments = userTaskInstance.getComments(); switch (body.getEventType()) { case UserTaskInstanceCommentEventBody.EVENT_TYPE_ADDED: @@ -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(); @@ -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; + }); + } }