diff --git a/api/kogito-api/src/main/java/org/kie/kogito/internal/process/event/KogitoProcessEventSupport.java b/api/kogito-api/src/main/java/org/kie/kogito/internal/process/event/KogitoProcessEventSupport.java index d0a2bc155c2..aaab5e5c013 100644 --- a/api/kogito-api/src/main/java/org/kie/kogito/internal/process/event/KogitoProcessEventSupport.java +++ b/api/kogito-api/src/main/java/org/kie/kogito/internal/process/event/KogitoProcessEventSupport.java @@ -71,6 +71,8 @@ void fireAfterVariableChanged(String name, String id, Object oldValue, Object ne void fireOnMessage(KogitoProcessInstance instance, KogitoNodeInstance nodeInstance, KieRuntime kruntime, String messageName, Object messageObject); + void fireOnMigration(KogitoProcessInstance processInstance, KieRuntime runtime); + // user tasks events void fireOneUserTaskStateChange( @@ -166,4 +168,4 @@ void fireOnUserTaskCommentAdded( void removeEventListener(KogitoProcessEventListener listener); -} \ No newline at end of file +} diff --git a/api/kogito-api/src/main/java/org/kie/kogito/process/ProcessInstances.java b/api/kogito-api/src/main/java/org/kie/kogito/process/ProcessInstances.java index 522f04d03f0..81794223a1a 100644 --- a/api/kogito-api/src/main/java/org/kie/kogito/process/ProcessInstances.java +++ b/api/kogito-api/src/main/java/org/kie/kogito/process/ProcessInstances.java @@ -31,9 +31,9 @@ default Optional> findById(String id) { Optional> findById(String id, ProcessInstanceReadMode mode); - default Optional> findByBusinessKey(String id) { return stream().filter(pi -> id.equals(pi.businessKey())).findAny(); + } default void migrate(String targetProcessId, String targetProcessVersion, String[] processIds) throws OperationNotSupportedException { throw new OperationNotSupportedException(); @@ -49,5 +49,4 @@ default Stream> stream() { return stream(ProcessInstanceReadMode.READ_ONLY); } - } diff --git a/api/kogito-api/src/main/java/org/kie/kogito/process/ProcessService.java b/api/kogito-api/src/main/java/org/kie/kogito/process/ProcessService.java index f651d2a8616..1e37bc59d95 100644 --- a/api/kogito-api/src/main/java/org/kie/kogito/process/ProcessService.java +++ b/api/kogito-api/src/main/java/org/kie/kogito/process/ProcessService.java @@ -67,7 +67,7 @@ ProcessInstance createProcessInstance(Process process, S , R> Optional findById(Process process, String id); - void migrate(Process process, String targetProcessId, String targetProcessVersion, String ...id) throws OperationNotSupportedException; + void migrate(Process process, String targetProcessId, String targetProcessVersion, String... id) throws OperationNotSupportedException; void migrate(Process process, String targetProcessId, String targetProcessVersion) throws OperationNotSupportedException; diff --git a/api/kogito-events-api/src/main/java/org/kie/kogito/event/process/ProcessInstanceStateEventBody.java b/api/kogito-events-api/src/main/java/org/kie/kogito/event/process/ProcessInstanceStateEventBody.java index 6c7b7a80c3f..b9bb4fd9ad3 100644 --- a/api/kogito-events-api/src/main/java/org/kie/kogito/event/process/ProcessInstanceStateEventBody.java +++ b/api/kogito-events-api/src/main/java/org/kie/kogito/event/process/ProcessInstanceStateEventBody.java @@ -28,6 +28,7 @@ public class ProcessInstanceStateEventBody { public static final int EVENT_TYPE_STARTED = 1; public static final int EVENT_TYPE_ENDED = 2; + public static final int EVENT_TYPE_MIGRATED = 3; // common fields for events private Date eventDate; diff --git a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/ProcessInstanceEventBatch.java b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/ProcessInstanceEventBatch.java index 715d2b154eb..4e3016fedda 100644 --- a/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/ProcessInstanceEventBatch.java +++ b/api/kogito-events-core/src/main/java/org/kie/kogito/event/impl/ProcessInstanceEventBatch.java @@ -28,6 +28,7 @@ import org.kie.api.event.process.ProcessCompletedEvent; import org.kie.api.event.process.ProcessEvent; +import org.kie.api.event.process.ProcessMigrationEvent; import org.kie.api.event.process.ProcessNodeEvent; import org.kie.api.event.process.ProcessNodeLeftEvent; import org.kie.api.event.process.ProcessNodeTriggeredEvent; @@ -117,6 +118,8 @@ private void addDataEvent(ProcessEvent event) { handleProcesssNodeEvent((SLAViolatedEvent) event); } else if (event instanceof ProcessVariableChangedEvent) { handleProcessVariableEvent((ProcessVariableChangedEvent) event); + } else if (event instanceof ProcessMigrationEvent) { + handleProcessStateEvent((ProcessMigrationEvent) event); } } @@ -293,6 +296,10 @@ private void handleProcessStateEvent(ProcessStartedEvent event) { } + private void handleProcessStateEvent(ProcessMigrationEvent event) { + processedEvents.add(toProcessInstanceStateEvent(event, ProcessInstanceStateEventBody.EVENT_TYPE_MIGRATED)); + } + private ProcessInstanceStateDataEvent toProcessInstanceStateEvent(ProcessEvent event, int eventType) { Map metadata = buildProcessMetadata((KogitoWorkflowProcessInstance) event.getProcessInstance()); diff --git a/jbpm/jbpm-flow-migration/src/main/java/org/jbpm/flow/serialization/migration/StandardMigrationProcessInstanceMarshallerListener.java b/jbpm/jbpm-flow-migration/src/main/java/org/jbpm/flow/serialization/migration/StandardMigrationProcessInstanceMarshallerListener.java index 1073a256435..e3c3d185a40 100644 --- a/jbpm/jbpm-flow-migration/src/main/java/org/jbpm/flow/serialization/migration/StandardMigrationProcessInstanceMarshallerListener.java +++ b/jbpm/jbpm-flow-migration/src/main/java/org/jbpm/flow/serialization/migration/StandardMigrationProcessInstanceMarshallerListener.java @@ -21,6 +21,7 @@ import org.jbpm.flow.migration.MigrationPlanService; import org.jbpm.flow.serialization.ProcessInstanceMarshallerListener; import org.kie.kogito.internal.process.runtime.KogitoNodeInstance; +import org.kie.kogito.internal.process.runtime.KogitoProcessRuntime; import org.kie.kogito.internal.process.runtime.KogitoWorkflowProcessInstance; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,13 +39,14 @@ public StandardMigrationProcessInstanceMarshallerListener() { } @Override - public void afterUnmarshallProcess(KogitoWorkflowProcessInstance processInstance) { + public void afterUnmarshallProcess(KogitoProcessRuntime runtime, KogitoWorkflowProcessInstance processInstance) { LOGGER.debug("Migration processInstance {}", processInstance); migrationPlanService.migrateProcessElement(processInstance); + runtime.getProcessEventSupport().fireOnMigration(processInstance, runtime.getKieRuntime()); } @Override - public void afterUnmarshallNode(KogitoNodeInstance nodeInstance) { + public void afterUnmarshallNode(KogitoProcessRuntime runtime, KogitoNodeInstance nodeInstance) { LOGGER.debug("Migration nodeInstance {}", nodeInstance); migrationPlanService.migrateNodeElement(nodeInstance); } diff --git a/jbpm/jbpm-flow/src/main/java/org/jbpm/process/instance/event/KogitoProcessEventSupportImpl.java b/jbpm/jbpm-flow/src/main/java/org/jbpm/process/instance/event/KogitoProcessEventSupportImpl.java index 2a87fcd4c95..7335e4bddac 100644 --- a/jbpm/jbpm-flow/src/main/java/org/jbpm/process/instance/event/KogitoProcessEventSupportImpl.java +++ b/jbpm/jbpm-flow/src/main/java/org/jbpm/process/instance/event/KogitoProcessEventSupportImpl.java @@ -210,6 +210,12 @@ public void fireOnMessage(final KogitoProcessInstance instance, KogitoNodeInstan notifyAllListeners(l -> l.onMessage(event)); } + @Override + public void fireOnMigration(final KogitoProcessInstance processInstance, KieRuntime kruntime) { + ProcessMigrationEventImpl event = new ProcessMigrationEventImpl(processInstance, kruntime, identityProvider.getName()); + notifyAllListeners(l -> l.onMigration(event)); + } + // users tasks events @Override public void fireOnUserTaskNotStartedDeadline(KogitoProcessInstance instance, diff --git a/jbpm/jbpm-flow/src/main/java/org/jbpm/process/instance/event/ProcessMigrationEventImpl.java b/jbpm/jbpm-flow/src/main/java/org/jbpm/process/instance/event/ProcessMigrationEventImpl.java new file mode 100644 index 00000000000..8f3d64c75b9 --- /dev/null +++ b/jbpm/jbpm-flow/src/main/java/org/jbpm/process/instance/event/ProcessMigrationEventImpl.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jbpm.process.instance.event; + +import org.kie.api.event.process.ProcessMigrationEvent; +import org.kie.api.runtime.KieRuntime; +import org.kie.api.runtime.process.ProcessInstance; + +public class ProcessMigrationEventImpl extends ProcessEvent implements ProcessMigrationEvent { + + private static final long serialVersionUID = 510l; + + public ProcessMigrationEventImpl(ProcessInstance instance, KieRuntime kruntime, String identity) { + super(instance, kruntime, identity); + } + + @Override + public String toString() { + return "==>[ProcessMigratedEventImpl(name=" + getProcessInstance().getProcessName() + "; id=" + getProcessInstance().getProcessId() + ")]"; + } + +} \ No newline at end of file diff --git a/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/impl/AbstractProcess.java b/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/impl/AbstractProcess.java index bc23614f1bd..7870ae60038 100644 --- a/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/impl/AbstractProcess.java +++ b/jbpm/jbpm-flow/src/main/java/org/kie/kogito/process/impl/AbstractProcess.java @@ -187,6 +187,10 @@ protected void registerListeners() { } + public KogitoProcessRuntime getProcessRuntime() { + return this.processRuntime; + } + @Override public void activate() { if (this.activated) { diff --git a/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/ProcessInstanceMarshallerListener.java b/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/ProcessInstanceMarshallerListener.java index 91934c1fba2..080c40873e9 100644 --- a/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/ProcessInstanceMarshallerListener.java +++ b/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/ProcessInstanceMarshallerListener.java @@ -19,23 +19,24 @@ package org.jbpm.flow.serialization; import org.kie.kogito.internal.process.runtime.KogitoNodeInstance; +import org.kie.kogito.internal.process.runtime.KogitoProcessRuntime; import org.kie.kogito.internal.process.runtime.KogitoWorkflowProcessInstance; public interface ProcessInstanceMarshallerListener { - default void afterUnmarshallProcess(KogitoWorkflowProcessInstance processInstance) { + default void afterUnmarshallProcess(KogitoProcessRuntime runtime, KogitoWorkflowProcessInstance processInstance) { } - default void afterUnmarshallNode(KogitoNodeInstance node) { + default void afterUnmarshallNode(KogitoProcessRuntime runtime, KogitoNodeInstance node) { } - default void beforeMarshallNode(KogitoNodeInstance node) { + default void beforeMarshallNode(KogitoProcessRuntime runtime, KogitoNodeInstance node) { } - default void beforeMarshallProcess(KogitoWorkflowProcessInstance node) { + default void beforeMarshallProcess(KogitoProcessRuntime runtime, KogitoWorkflowProcessInstance node) { } diff --git a/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/impl/ProtobufProcessInstanceReader.java b/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/impl/ProtobufProcessInstanceReader.java index 291b18d131f..b31bb7d5399 100644 --- a/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/impl/ProtobufProcessInstanceReader.java +++ b/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/impl/ProtobufProcessInstanceReader.java @@ -87,6 +87,7 @@ import org.kie.api.definition.process.WorkflowElementIdentifier; import org.kie.kogito.internal.process.runtime.KogitoNodeInstance; import org.kie.kogito.internal.process.runtime.KogitoNodeInstanceContainer; +import org.kie.kogito.internal.process.runtime.KogitoProcessRuntime; import org.kie.kogito.process.impl.AbstractProcess; import org.kie.kogito.process.workitem.Attachment; import org.kie.kogito.process.workitem.Comment; @@ -229,7 +230,8 @@ private RuleFlowProcessInstance buildWorkflow(KogitoProcessInstanceProtobuf.Proc if (workflowContext.getIterationLevelsCount() > 0) { processInstance.getIterationLevels().putAll(buildIterationLevels(workflowContext.getIterationLevelsList())); } - Arrays.stream(listeners).forEach(e -> e.afterUnmarshallProcess(processInstance)); + KogitoProcessRuntime runtime = ((AbstractProcess) context.get(MarshallerContextName.MARSHALLER_PROCESS)).getProcessRuntime(); + Arrays.stream(listeners).forEach(e -> e.afterUnmarshallProcess(runtime, processInstance)); return processInstance; } @@ -317,7 +319,8 @@ protected NodeInstanceImpl buildNodeInstance(KogitoTypesProtobuf.NodeInstance no } KogitoNodeInstance kogitoNodeInstance = (KogitoNodeInstance) result; - Arrays.stream(listeners).forEach(e -> e.afterUnmarshallNode(kogitoNodeInstance)); + KogitoProcessRuntime runtime = ((AbstractProcess) context.get(MarshallerContextName.MARSHALLER_PROCESS)).getProcessRuntime(); + Arrays.stream(listeners).forEach(e -> e.afterUnmarshallNode(runtime, kogitoNodeInstance)); return result; } catch (IOException e) { throw new IllegalArgumentException("Cannot read node instance content"); diff --git a/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/impl/ProtobufProcessInstanceWriter.java b/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/impl/ProtobufProcessInstanceWriter.java index 893412fe8ad..a5df6e76ec3 100644 --- a/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/impl/ProtobufProcessInstanceWriter.java +++ b/jbpm/process-serialization-protobuf/src/main/java/org/jbpm/flow/serialization/impl/ProtobufProcessInstanceWriter.java @@ -82,7 +82,9 @@ import org.kie.api.definition.process.WorkflowElementIdentifier; import org.kie.api.runtime.process.NodeInstance; import org.kie.kogito.internal.process.runtime.KogitoNodeInstance; +import org.kie.kogito.internal.process.runtime.KogitoProcessRuntime; import org.kie.kogito.internal.process.runtime.KogitoWorkItem; +import org.kie.kogito.process.impl.AbstractProcess; import org.kie.kogito.process.workitem.Attachment; import org.kie.kogito.process.workitem.Comment; import org.kie.kogito.process.workitem.HumanTaskWorkItem; @@ -109,7 +111,8 @@ public ProtobufProcessInstanceWriter(MarshallerWriterContext context) { } public void writeProcessInstance(WorkflowProcessInstanceImpl workFlow, OutputStream os) throws IOException { - Arrays.stream(listeners).forEach(e -> e.beforeMarshallProcess(workFlow)); + KogitoProcessRuntime runtime = ((AbstractProcess) context.get(MarshallerContextName.MARSHALLER_PROCESS)).getProcessRuntime(); + Arrays.stream(listeners).forEach(e -> e.beforeMarshallProcess(runtime, workFlow)); KogitoProcessInstanceProtobuf.ProcessInstance.Builder instance = KogitoProcessInstanceProtobuf.ProcessInstance.newBuilder() .setId(workFlow.getStringId()) @@ -248,7 +251,8 @@ private List e.beforeMarshallNode((KogitoNodeInstance) nodeInstance)); + KogitoProcessRuntime runtime = ((AbstractProcess) context.get(MarshallerContextName.MARSHALLER_PROCESS)).getProcessRuntime(); + Arrays.stream(listeners).forEach(e -> e.beforeMarshallNode(runtime, (KogitoNodeInstance) nodeInstance)); if (nodeInstance instanceof RuleSetNodeInstance) { return buildRuleSetNodeInstance((RuleSetNodeInstance) nodeInstance);