Skip to content

Commit

Permalink
[Fix apache#1995] Setting last update when pi is created
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Feb 27, 2024
1 parent cf6576d commit 4d02cd9
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public class ProcessInstanceErrorDataEventMerger extends ProcessInstanceEventMer

@Override
public ProcessInstance merge(ProcessInstance pi, ProcessInstanceDataEvent<?> data) {
pi = getOrNew(pi, data);
ProcessInstanceErrorDataEvent event = (ProcessInstanceErrorDataEvent) data;
pi = getOrNew(pi, data, event.getData().getEventDate());
ProcessInstanceError error = new ProcessInstanceError();
error.setMessage(event.getData().getErrorMessage());
error.setNodeDefinitionId(event.getData().getNodeDefinitionId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,20 @@
package org.kie.kogito.index.storage.merger;

import java.util.ArrayList;
import java.util.Date;

import org.kie.kogito.event.process.ProcessInstanceDataEvent;
import org.kie.kogito.index.DateTimeUtils;
import org.kie.kogito.index.model.ProcessInstance;

public abstract class ProcessInstanceEventMerger implements Merger<ProcessInstanceDataEvent<?>, ProcessInstance> {

protected ProcessInstance getOrNew(ProcessInstance pi, ProcessInstanceDataEvent<?> event) {
protected ProcessInstance getOrNew(ProcessInstance pi, ProcessInstanceDataEvent<?> event, Date date) {
if (pi == null) {
pi = new ProcessInstance();
pi.setId(event.getKogitoProcessInstanceId());
pi.setProcessId(event.getKogitoProcessId());
pi.setLastUpdate(DateTimeUtils.toZonedDateTime(date));
pi.setMilestones(new ArrayList<>());
pi.setNodes(new ArrayList<>());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ public class ProcessInstanceNodeDataEventMerger extends ProcessInstanceEventMerg

@Override
public ProcessInstance merge(ProcessInstance pi, ProcessInstanceDataEvent<?> data) {
pi = getOrNew(pi, data);
ProcessInstanceNodeDataEvent event = (ProcessInstanceNodeDataEvent) data;
pi = getOrNew(pi, data, event.getData().getEventDate());

List<NodeInstance> nodeInstances = Optional.ofNullable(pi.getNodes()).orElse(new ArrayList<>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public class ProcessInstanceStateDataEventMerger extends ProcessInstanceEventMer

@Override
public ProcessInstance merge(ProcessInstance pi, ProcessInstanceDataEvent<?> data) {
pi = getOrNew(pi, data);
ProcessInstanceStateDataEvent event = (ProcessInstanceStateDataEvent) data;
pi = getOrNew(pi, data, event.getData().getEventDate());
LOGGER.debug("Value before merging: {}", pi);
pi.setId(event.getData().getProcessInstanceId());
pi.setVersion(event.getData().getProcessVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,17 @@
import org.kie.kogito.event.process.ProcessInstanceVariableEventBody;
import org.kie.kogito.index.json.JsonUtils;
import org.kie.kogito.index.model.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class ProcessInstanceVariableDataEventMerger extends ProcessInstanceEventMerger {

private static final Logger LOGGER = LoggerFactory.getLogger(ProcessInstanceVariableDataEventMerger.class);

@SuppressWarnings("unchecked")
@Override
public ProcessInstance merge(ProcessInstance pi, ProcessInstanceDataEvent<?> data) {
pi = getOrNew(pi, data);
ProcessInstanceVariableDataEvent event = (ProcessInstanceVariableDataEvent) data;
ProcessInstanceVariableEventBody body = event.getData();
pi = getOrNew(pi, data, body.getEventDate());
pi.setVariables(JsonUtils.mergeVariable(body.getVariableName(), body.getVariableValue(), pi.getVariables()));
return pi;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;

import org.kie.kogito.event.process.ProcessInstanceErrorDataEvent;
Expand Down Expand Up @@ -93,11 +94,12 @@ public void indexVariable(ProcessInstanceVariableDataEvent event) {
indexVariable(event.getData());
}

private ProcessInstanceEntity findOrInit(String processId, String processInstanceId) {
private ProcessInstanceEntity findOrInit(String processId, String processInstanceId, Date date) {
return repository.findByIdOptional(processInstanceId).orElseGet(() -> {
ProcessInstanceEntity pi = new ProcessInstanceEntity();
pi.setProcessId(processId);
pi.setId(processInstanceId);
pi.setLastUpdate(toZonedDateTime(date));
pi.setNodes(new ArrayList<>());
pi.setMilestones(new ArrayList<>());
repository.persist(pi);
Expand All @@ -106,7 +108,7 @@ private ProcessInstanceEntity findOrInit(String processId, String processInstanc
}

private void indexError(ProcessInstanceErrorEventBody error) {
ProcessInstanceEntity pi = findOrInit(error.getProcessId(), error.getProcessInstanceId());
ProcessInstanceEntity pi = findOrInit(error.getProcessId(), error.getProcessInstanceId(), error.getEventDate());
ProcessInstanceErrorEntity errorEntity = pi.getError();
if (errorEntity == null) {
errorEntity = new ProcessInstanceErrorEntity();
Expand All @@ -118,7 +120,7 @@ private void indexError(ProcessInstanceErrorEventBody error) {
}

private void indexNode(ProcessInstanceNodeEventBody data) {
ProcessInstanceEntity pi = findOrInit(data.getProcessId(), data.getProcessInstanceId());
ProcessInstanceEntity pi = findOrInit(data.getProcessId(), data.getProcessInstanceId(), data.getEventDate());
pi.getNodes().stream().filter(n -> n.getId().equals(data.getNodeInstanceId())).findAny().ifPresentOrElse(n -> updateNode(n, data), () -> createNode(pi, data));
if ("MilestoneNode".equals(data.getNodeType())) {
pi.getMilestones().stream().filter(n -> n.getId().equals(data.getNodeInstanceId())).findAny().ifPresentOrElse(n -> updateMilestone(n, data), () -> createMilestone(pi, data));
Expand Down Expand Up @@ -171,12 +173,12 @@ private NodeInstanceEntity updateNode(NodeInstanceEntity nodeInstance, ProcessIn
}

private void indexSLA(ProcessInstanceSLAEventBody data) {
findOrInit(data.getProcessId(), data.getProcessInstanceId());
findOrInit(data.getProcessId(), data.getProcessInstanceId(), data.getEventDate());
repository.flush();
}

private void indexState(ProcessInstanceStateEventBody data, Set<String> addons, String endpoint) {
ProcessInstanceEntity pi = findOrInit(data.getProcessId(), data.getProcessInstanceId());
ProcessInstanceEntity pi = findOrInit(data.getProcessId(), data.getProcessInstanceId(), data.getEventDate());
pi.setVersion(data.getProcessVersion());
pi.setProcessName(data.getProcessName());
pi.setRootProcessInstanceId(data.getRootProcessInstanceId());
Expand All @@ -199,7 +201,7 @@ private void indexState(ProcessInstanceStateEventBody data, Set<String> addons,
}

private void indexVariable(ProcessInstanceVariableEventBody data) {
ProcessInstanceEntity pi = findOrInit(data.getProcessId(), data.getProcessInstanceId());
ProcessInstanceEntity pi = findOrInit(data.getProcessId(), data.getProcessInstanceId(), data.getEventDate());
pi.setVariables(JsonUtils.mergeVariable(data.getVariableName(), data.getVariableValue(), pi.getVariables()));
repository.flush();
}
Expand Down

0 comments on commit 4d02cd9

Please sign in to comment.