Skip to content

Commit

Permalink
Merge pull request #10 from camunda-community-hub/feature/issue-9_pri…
Browse files Browse the repository at this point in the history
…mary-keys

Also support other types than java.lang.String for workflow aggregates primary keys
  • Loading branch information
stephanpelikan authored Jun 6, 2023
2 parents 4b283ca + e7147a9 commit 3dcfea6
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 26 deletions.
2 changes: 1 addition & 1 deletion spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>io.vanillabp</groupId>
<artifactId>spring-boot-support</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -59,7 +62,7 @@ public class Camunda7AdapterConfiguration extends AdapterConfigurationBase<Camun
public void init() {

logger.debug("Will use SpringDataUtil class '{}'",
AopProxyUtils.ultimateTargetClass(springDataUtil.getClass()));
AopProxyUtils.ultimateTargetClass(springDataUtil));

}

Expand Down Expand Up @@ -150,7 +153,43 @@ public Camunda7TaskWiringPlugin taskWiringCamundaPlugin(
public <DE> Camunda7ProcessService<?> newProcessServiceImplementation(
final SpringDataUtil springDataUtil,
final Class<DE> workflowAggregateClass,
final CrudRepository<DE, String> workflowAggregateRepository) {
final Class<?> workflowAggregateIdClass,
final CrudRepository<DE, Object> workflowAggregateRepository) {

final Function<String, Object> 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<DE>(
applicationEventPublisher,
Expand All @@ -160,7 +199,8 @@ public <DE> Camunda7ProcessService<?> newProcessServiceImplementation(
workflowAggregate ->
springDataUtil.getId(workflowAggregate),
workflowAggregateRepository,
workflowAggregateClass);
workflowAggregateClass,
parseWorkflowAggregateIdFromBusinessKey);

putConnectableService(workflowAggregateClass, result);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ public class Camunda7ProcessService<DE>

private final ProcessEngine processEngine;

private final CrudRepository<DE, String> workflowAggregateRepository;
private final CrudRepository<DE, Object> workflowAggregateRepository;

private final Class<DE> workflowAggregateClass;

private final Function<DE, String> getWorkflowAggregateId;
private final Function<DE, ?> getWorkflowAggregateId;

private final Function<DE, Boolean> isNewEntity;

private final Function<String, Object> parseWorkflowAggregateIdFromBusinessKey;

private AdapterAwareProcessService<DE> parent;

public Camunda7ProcessService(
final ApplicationEventPublisher applicationEventPublisher,
final ProcessEngine processEngine,
final Function<DE, Boolean> isNewEntity,
final Function<DE, String> getWorkflowAggregateId,
final CrudRepository<DE, String> workflowAggregateRepository,
final Class<DE> workflowAggregateClass) {
final Function<DE, ?> getWorkflowAggregateId,
final CrudRepository<DE, Object> workflowAggregateRepository,
final Class<DE> workflowAggregateClass,
final Function<String, Object> parseWorkflowAggregateIdFromBusinessKey) {

super();
this.applicationEventPublisher = applicationEventPublisher;
Expand All @@ -47,7 +50,8 @@ public Camunda7ProcessService(
this.workflowAggregateClass = workflowAggregateClass;
this.isNewEntity = isNewEntity;
this.getWorkflowAggregateId = getWorkflowAggregateId;

this.parseWorkflowAggregateIdFromBusinessKey = parseWorkflowAggregateIdFromBusinessKey;

}

@Override
Expand Down Expand Up @@ -110,12 +114,19 @@ public Class<DE> getWorkflowAggregateClass() {
}

@Override
public CrudRepository<DE, String> getWorkflowAggregateRepository() {
public CrudRepository<DE, Object> getWorkflowAggregateRepository() {

return workflowAggregateRepository;

}


public Object getWorkflowAggregateIdFromBusinessKey(
final String businessKey) {

return parseWorkflowAggregateIdFromBusinessKey.apply(businessKey);

}

@Override
public DE startWorkflow(
final DE workflowAggregate) throws Exception {
Expand Down Expand Up @@ -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,
Expand All @@ -244,7 +255,7 @@ private DE correlateMessage(
.getRuntimeService()
.createExecutionQuery()
.messageEventSubscriptionName(messageName)
.processInstanceBusinessKey(id)
.processInstanceBusinessKey(id.toString())
.active();
if (correlationIdLocalVariableName != null) {
correlationExecutions.variableValueEquals(
Expand Down Expand Up @@ -295,7 +306,7 @@ public DE completeUserTask(
final var task = processEngine
.getTaskService()
.createTaskQuery()
.processInstanceBusinessKey(id)
.processInstanceBusinessKey(id.toString())
.taskId(taskId)
.singleResult();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Object, String> workflowAggregateRepository,
final CrudRepository<Object, Object> workflowAggregateRepository,
final Object bean,
final Method method,
final List<MethodParameter> parameters) {
final List<MethodParameter> parameters,
final Camunda7ProcessService<?> processService) {

super(workflowAggregateRepository, bean, method, parameters);
this.bpmnProcessId = bpmnProcessId;
this.processService = processService;

}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,23 @@ protected void connectToBpms(
if (connectable.getType() == Camunda7Connectable.Type.USERTASK) {

final var taskHandler = new Camunda7UserTaskHandler(
(CrudRepository<Object, String>) repository,
(CrudRepository<Object, Object>) repository,
bean,
method,
parameters);
parameters,
processService);
userTaskEventHandler.addTaskHandler(connectable, taskHandler);
return;

}

final var taskHandler = new Camunda7TaskHandler(
connectable.getBpmnProcessId(),
(CrudRepository<Object, String>) repository,
(CrudRepository<Object, Object>) repository,
bean,
method,
parameters);
parameters,
processService);

processEntityAwareExpressionManager.addTaskHandler(connectable, taskHandler);

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Object, String> workflowAggregateRepository,
final CrudRepository<Object, Object> workflowAggregateRepository,
final Object bean,
final Method method,
final List<MethodParameter> parameters) {
final List<MethodParameter> parameters,
final Camunda7ProcessService<?> processService) {

super(workflowAggregateRepository, bean, method, parameters);
this.processService = processService;

}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 3dcfea6

Please sign in to comment.