From 51a71b08c22e03e6a67e574a6258e5b386e21d5f Mon Sep 17 00:00:00 2001 From: Stephan Pelikan Date: Fri, 19 May 2023 13:59:03 +0200 Subject: [PATCH 1/2] Implement feature request #9 --- spring-boot/pom.xml | 2 +- .../Camunda7AdapterConfiguration.java | 46 +++++++++++++++++-- .../service/Camunda7ProcessService.java | 33 ++++++++----- .../camunda7/wiring/Camunda7TaskHandler.java | 12 +++-- .../camunda7/wiring/Camunda7TaskWiring.java | 10 ++-- .../wiring/Camunda7UserTaskHandler.java | 12 +++-- .../wiring/ProcessEntityELResolver.java | 4 +- 7 files changed, 93 insertions(+), 26 deletions(-) diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 501d8ed..abd5bf8 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -19,7 +19,7 @@ io.vanillabp spring-boot-support - 1.0.3 + 1.0.4-SNAPSHOT org.camunda.bpm.springboot diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/Camunda7AdapterConfiguration.java b/spring-boot/src/main/java/io/vanillabp/camunda7/Camunda7AdapterConfiguration.java index 7ec3e2f..5580e97 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/Camunda7AdapterConfiguration.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/Camunda7AdapterConfiguration.java @@ -28,6 +28,9 @@ import org.springframework.context.annotation.Lazy; import org.springframework.data.repository.CrudRepository; +import java.math.BigInteger; +import java.util.function.Function; + import javax.annotation.PostConstruct; @AutoConfigurationPackage(basePackageClasses = Camunda7AdapterConfiguration.class) @@ -59,7 +62,7 @@ public class Camunda7AdapterConfiguration extends AdapterConfigurationBase Camunda7ProcessService newProcessServiceImplementation( final SpringDataUtil springDataUtil, final Class workflowAggregateClass, - final CrudRepository workflowAggregateRepository) { + final Class workflowAggregateIdClass, + final CrudRepository workflowAggregateRepository) { + + final Function parseWorkflowAggregateIdFromBusinessKey; + if (String.class.isAssignableFrom(workflowAggregateIdClass)) { + parseWorkflowAggregateIdFromBusinessKey = businessKey -> businessKey; + } else if (int.class.isAssignableFrom(workflowAggregateIdClass)) { + parseWorkflowAggregateIdFromBusinessKey = businessKey -> Integer.valueOf(businessKey); + } else if (long.class.isAssignableFrom(workflowAggregateIdClass)) { + parseWorkflowAggregateIdFromBusinessKey = businessKey -> Long.valueOf(businessKey); + } else if (float.class.isAssignableFrom(workflowAggregateIdClass)) { + parseWorkflowAggregateIdFromBusinessKey = businessKey -> Float.valueOf(businessKey); + } else if (double.class.isAssignableFrom(workflowAggregateIdClass)) { + parseWorkflowAggregateIdFromBusinessKey = businessKey -> Double.valueOf(businessKey); + } else if (byte.class.isAssignableFrom(workflowAggregateIdClass)) { + parseWorkflowAggregateIdFromBusinessKey = businessKey -> Byte.valueOf(businessKey); + } else if (BigInteger.class.isAssignableFrom(workflowAggregateIdClass)) { + parseWorkflowAggregateIdFromBusinessKey = businessKey -> new BigInteger(businessKey); + } else { + try { + final var valueOfMethod = workflowAggregateIdClass.getMethod("valueOf", String.class); + parseWorkflowAggregateIdFromBusinessKey = businessKey -> { + try { + return valueOfMethod.invoke(null, businessKey); + } catch (Exception e) { + throw new RuntimeException("Could not determine the workflow's aggregate id!", e); + } + }; + } catch (Exception e) { + throw new RuntimeException( + String.format( + "The id's class '%s' of the workflow-aggregate '%s' does not implement a method 'public static %s valueOf(String businessKey)'! Please add this method required by VanillaBP 'camunda7' adapter.", + workflowAggregateIdClass.getName(), + workflowAggregateClass.getName(), + workflowAggregateIdClass.getSimpleName())); + } + } final var result = new Camunda7ProcessService( applicationEventPublisher, @@ -160,7 +199,8 @@ public Camunda7ProcessService newProcessServiceImplementation( workflowAggregate -> springDataUtil.getId(workflowAggregate), workflowAggregateRepository, - workflowAggregateClass); + workflowAggregateClass, + parseWorkflowAggregateIdFromBusinessKey); putConnectableService(workflowAggregateClass, result); diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/service/Camunda7ProcessService.java b/spring-boot/src/main/java/io/vanillabp/camunda7/service/Camunda7ProcessService.java index dc20204..6267bfb 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/service/Camunda7ProcessService.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/service/Camunda7ProcessService.java @@ -22,13 +22,15 @@ public class Camunda7ProcessService private final ProcessEngine processEngine; - private final CrudRepository workflowAggregateRepository; + private final CrudRepository workflowAggregateRepository; private final Class workflowAggregateClass; - private final Function getWorkflowAggregateId; + private final Function getWorkflowAggregateId; private final Function isNewEntity; + + private final Function parseWorkflowAggregateIdFromBusinessKey; private AdapterAwareProcessService parent; @@ -36,9 +38,10 @@ public Camunda7ProcessService( final ApplicationEventPublisher applicationEventPublisher, final ProcessEngine processEngine, final Function isNewEntity, - final Function getWorkflowAggregateId, - final CrudRepository workflowAggregateRepository, - final Class workflowAggregateClass) { + final Function getWorkflowAggregateId, + final CrudRepository workflowAggregateRepository, + final Class workflowAggregateClass, + final Function parseWorkflowAggregateIdFromBusinessKey) { super(); this.applicationEventPublisher = applicationEventPublisher; @@ -47,7 +50,8 @@ public Camunda7ProcessService( this.workflowAggregateClass = workflowAggregateClass; this.isNewEntity = isNewEntity; this.getWorkflowAggregateId = getWorkflowAggregateId; - + this.parseWorkflowAggregateIdFromBusinessKey = parseWorkflowAggregateIdFromBusinessKey; + } @Override @@ -110,12 +114,19 @@ public Class getWorkflowAggregateClass() { } @Override - public CrudRepository getWorkflowAggregateRepository() { + public CrudRepository getWorkflowAggregateRepository() { return workflowAggregateRepository; } - + + public Object getWorkflowAggregateIdFromBusinessKey( + final String businessKey) { + + return parseWorkflowAggregateIdFromBusinessKey.apply(businessKey); + + } + @Override public DE startWorkflow( final DE workflowAggregate) throws Exception { @@ -218,7 +229,7 @@ private DE correlateMessage( final var correlation = processEngine .getRuntimeService() .createMessageCorrelation(messageName) - .processInstanceBusinessKey(id); + .processInstanceBusinessKey(id.toString()); if (correlationIdLocalVariableName != null) { correlation.localVariableEquals( correlationIdLocalVariableName, @@ -244,7 +255,7 @@ private DE correlateMessage( .getRuntimeService() .createExecutionQuery() .messageEventSubscriptionName(messageName) - .processInstanceBusinessKey(id) + .processInstanceBusinessKey(id.toString()) .active(); if (correlationIdLocalVariableName != null) { correlationExecutions.variableValueEquals( @@ -295,7 +306,7 @@ public DE completeUserTask( final var task = processEngine .getTaskService() .createTaskQuery() - .processInstanceBusinessKey(id) + .processInstanceBusinessKey(id.toString()) .taskId(taskId) .singleResult(); diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskHandler.java b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskHandler.java index fd863d1..b75ac74 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskHandler.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskHandler.java @@ -1,5 +1,6 @@ package io.vanillabp.camunda7.wiring; +import io.vanillabp.camunda7.service.Camunda7ProcessService; import io.vanillabp.spi.service.MultiInstanceElementResolver; import io.vanillabp.spi.service.TaskEvent.Event; import io.vanillabp.spi.service.TaskException; @@ -33,15 +34,19 @@ public class Camunda7TaskHandler extends TaskHandlerBase implements JavaDelegate private Object result; + private final Camunda7ProcessService processService; + public Camunda7TaskHandler( final String bpmnProcessId, - final CrudRepository workflowAggregateRepository, + final CrudRepository workflowAggregateRepository, final Object bean, final Method method, - final List parameters) { + final List parameters, + final Camunda7ProcessService processService) { super(workflowAggregateRepository, bean, method, parameters); this.bpmnProcessId = bpmnProcessId; + this.processService = processService; } @@ -74,7 +79,8 @@ public void execute( execution.getId()); super.execute( - execution.getBusinessKey(), + processService.getWorkflowAggregateIdFromBusinessKey( + execution.getBusinessKey()), multiInstanceActivity -> { if (multiInstanceCache[0] == null) { multiInstanceCache[0] = Camunda7TaskHandler.getMultiInstanceContext(execution); diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskWiring.java b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskWiring.java index 00a9f02..209f403 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskWiring.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7TaskWiring.java @@ -48,10 +48,11 @@ protected void connectToBpms( if (connectable.getType() == Camunda7Connectable.Type.USERTASK) { final var taskHandler = new Camunda7UserTaskHandler( - (CrudRepository) repository, + (CrudRepository) repository, bean, method, - parameters); + parameters, + processService); userTaskEventHandler.addTaskHandler(connectable, taskHandler); return; @@ -59,10 +60,11 @@ protected void connectToBpms( final var taskHandler = new Camunda7TaskHandler( connectable.getBpmnProcessId(), - (CrudRepository) repository, + (CrudRepository) repository, bean, method, - parameters); + parameters, + processService); processEntityAwareExpressionManager.addTaskHandler(connectable, taskHandler); diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7UserTaskHandler.java b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7UserTaskHandler.java index 6dd5c51..a5aeffa 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7UserTaskHandler.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/Camunda7UserTaskHandler.java @@ -1,5 +1,6 @@ package io.vanillabp.camunda7.wiring; +import io.vanillabp.camunda7.service.Camunda7ProcessService; import io.vanillabp.spi.service.TaskEvent; import io.vanillabp.springboot.adapter.MultiInstance; import io.vanillabp.springboot.adapter.TaskHandlerBase; @@ -21,13 +22,17 @@ public class Camunda7UserTaskHandler extends TaskHandlerBase implements TaskList private static final Logger logger = LoggerFactory.getLogger(Camunda7UserTaskHandler.class); + private final Camunda7ProcessService processService; + public Camunda7UserTaskHandler( - final CrudRepository workflowAggregateRepository, + final CrudRepository workflowAggregateRepository, final Object bean, final Method method, - final List parameters) { + final List parameters, + final Camunda7ProcessService processService) { super(workflowAggregateRepository, bean, method, parameters); + this.processService = processService; } @@ -48,7 +53,8 @@ public void notify(final DelegateTask delegateTask) { final var execution = delegateTask.getExecution(); super.execute( - execution.getBusinessKey(), + processService.getWorkflowAggregateIdFromBusinessKey( + execution.getBusinessKey()), multiInstanceActivity -> { if (multiInstanceCache[0] == null) { multiInstanceCache[0] = Camunda7TaskHandler.getMultiInstanceContext(execution); diff --git a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/ProcessEntityELResolver.java b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/ProcessEntityELResolver.java index 3b4d4d3..bf9aabd 100644 --- a/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/ProcessEntityELResolver.java +++ b/spring-boot/src/main/java/io/vanillabp/camunda7/wiring/ProcessEntityELResolver.java @@ -113,9 +113,11 @@ public Object getValue(ELContext context, Object base, Object property) { return null; } + final var id = processService + .getWorkflowAggregateIdFromBusinessKey(execution.getBusinessKey()); final var workflowAggregateFound = processService .getWorkflowAggregateRepository() - .findById(execution.getBusinessKey()); + .findById(id); if (workflowAggregateFound.isEmpty()) { return null; } From e7147a9cdf178f4a372a1643b695038a29cc2ec3 Mon Sep 17 00:00:00 2001 From: Stephan Pelikan Date: Tue, 6 Jun 2023 17:46:55 +0200 Subject: [PATCH 2/2] Use release version 1.0.4 of 'VanillaBP Sprint Boot support' --- spring-boot/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index abd5bf8..47fff16 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -19,7 +19,7 @@ io.vanillabp spring-boot-support - 1.0.4-SNAPSHOT + 1.0.4 org.camunda.bpm.springboot