Skip to content

Commit

Permalink
fix(miranum): complex object mapping (#362)
Browse files Browse the repository at this point in the history
Closes #360

---------
Co-authored-by: Lukas Mösle <[email protected]>
  • Loading branch information
DaAnda97 authored May 29, 2024
1 parent 6c988cb commit bfe9320
Show file tree
Hide file tree
Showing 11 changed files with 241 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.ClassUtils;
import org.camunda.bpm.client.variable.ClientValues;
import org.camunda.bpm.client.variable.impl.value.JsonValueImpl;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.engine.variable.type.PrimitiveValueType;
import org.camunda.bpm.engine.variable.value.TypedValue;

import java.util.HashMap;
Expand Down Expand Up @@ -36,7 +36,7 @@ public Map<String, Object> mapFromEngineData(final VariableMap variables) {
final Map<String, Object> data = new HashMap<>();
variables.keySet().forEach(key -> {
final TypedValue value = variables.getValueTyped(key);
if (value.getType().getName().equals(PrimitiveValueType.OBJECT.getName())) {
if (value.getType().equals(ClientValues.JSON)) {
try {
data.put(key, this.mapFromEngineData(value.getValue()));
} catch (final JsonProcessingException e) {
Expand All @@ -50,7 +50,6 @@ public Map<String, Object> mapFromEngineData(final VariableMap variables) {
}

public Object mapFromEngineData(final Object value) throws JsonProcessingException {
// TODO: Check if Variables.SerializationDataFormats.JSON. May we support XML and JAVA as well?
final ObjectMapper mapper = new ObjectMapper();
if (value.toString().startsWith("[")) {
return mapper.readValue(value.toString(), new TypeReference<List<?>>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.miragon.miranum.connect.camnda7.remote.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import org.camunda.bpm.client.variable.ClientValues;
import org.camunda.bpm.client.variable.impl.value.JsonValueImpl;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.Variables;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -54,11 +56,9 @@ public void testMap_EmptyInput_ShouldReturnEmptyMap() throws JsonProcessingExcep
@Test
public void testMapFromEngineData_JsonObjectValue_ShouldReturnMap() {
// Given a VariableMap with a JSON object value
VariableMap variableMap = Variables.createVariables()
.putValueTyped("var1", Variables
.objectValue("{\"key\":\"value\"}")
.serializationDataFormat(Variables.SerializationDataFormats.JSON)
.create());
VariableMap variableMap = Variables
.createVariables()
.putValueTyped("var1", new JsonValueImpl("{\"key\":\"value\"}"));

// When
Map<String, Object> result = mapper.mapFromEngineData(variableMap);
Expand All @@ -71,12 +71,9 @@ public void testMapFromEngineData_JsonObjectValue_ShouldReturnMap() {
@Test
public void testMapFromEngineData_JsonArrayToList() {
// Given a VariableMap with a JSON list
String jsonArray = "[{\"key\":\"value1\"}, {\"key2\":\"value2\"}]";
VariableMap variableMap = Variables.createVariables()
.putValueTyped("var1", Variables
.objectValue(jsonArray)
.serializationDataFormat(Variables.SerializationDataFormats.JSON)
.create());
VariableMap variableMap = Variables
.createVariables()
.putValueTyped("var1", new JsonValueImpl("[{\"key\":\"value1\"}, {\"key2\":\"value2\"}]"));

// When
Map<String, Object> result = mapper.mapFromEngineData(variableMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
import io.miragon.miranum.connect.worker.api.WorkerSubscription;
import io.miragon.miranum.connect.worker.impl.WorkerExecutor;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.client.ExternalTaskClient;
import org.camunda.bpm.client.task.ExternalTask;
import org.camunda.bpm.client.task.ExternalTaskService;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

import static java.util.Objects.nonNull;

@RequiredArgsConstructor
@Log
@Slf4j
public class Camunda7WorkerAdapter implements WorkerSubscription {

private final ExternalTaskClient externalTaskClient;
Expand All @@ -37,18 +38,19 @@ public void execute(final WorkerExecutor executor, final ExternalTask externalTa
Integer workerRetries = null;
try {
final Map<String, Object> data = camunda7PojoMapper.mapFromEngineData(externalTask.getAllVariablesTyped());
log.debug("Worker {} called with parameters {}", executor.getType(), data);
workerRetries = (Integer) data.get("retries");
final Map<String, Object> result = this.workerExecuteApi.execute(executor, data);
service.complete(externalTask, null, camunda7PojoMapper.mapToEngineData(result));
} catch (final BusinessException exception) {
log.severe("use case could not be executed " + exception.getMessage());
log.warn("Use case could not be executed {}", exception.getMessage());
service.handleBpmnError(externalTask, exception.getCode(), exception.getMessage());
} catch (final TechnicalException error) {
log.severe("Technical error while executing task " + error.getMessage());
log.warn("Technical error while executing task {}", error.getMessage());
service.handleFailure(externalTask, error.getMessage(), Arrays.toString(error.getStackTrace()), 0, 0L);
} catch (final Exception error) {
int retries = getRemainingRetries(externalTask.getRetries(), workerRetries);
log.severe("Error while executing external task " + error.getMessage());
log.error("Error while executing external task {}", error.getMessage());
service.handleFailure(externalTask, error.getMessage(), Arrays.toString(error.getStackTrace()), retries, 5000L);
}
}
Expand All @@ -66,15 +68,16 @@ public void execute(final WorkerExecutor executor, final ExternalTask externalTa
* @return The remaining number of retries for the task.
*/
private int getRemainingRetries(Integer externalTaskRetries, Integer workerRetries) {
int retries = 0;
if (Objects.isNull(externalTaskRetries)) {
retries = Objects.isNull(workerRetries) ?
camunda7WorkerProperties.getDefaultRetries() :
workerRetries;
} else {
int retries;

if (nonNull(externalTaskRetries)) {
retries = externalTaskRetries;
} else if (nonNull(workerRetries)) {
retries = workerRetries;
} else {
retries = camunda7WorkerProperties.getDefaultRetries();
}
retries -= 1;
return Math.max(retries, 0);

return Math.max(retries - 1, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import io.miragon.miranum.connect.message.impl.DeliverMessagePort;
import io.miragon.miranum.connect.message.impl.MessageCorrelationException;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;

@Log
@RequiredArgsConstructor
public class Camunda8MessageAdapter implements DeliverMessagePort {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.miragon.miranum.connect.elementtemplate.api;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.extern.java.Log;

@Log
public enum PropertyType {
STRING("String"),
TEXT("Text"),
Expand All @@ -26,7 +24,7 @@ public String getType() {

public static PropertyType getType(Class<?> type) {
if (type != String.class) {
log.warning(String.format("Unsupported type \"%s\". PropertyType String is assumed.", type.getName()));
throw new RuntimeException(String.format("Unsupported type '%s'. PropertyType String is assumed.", type.getName()));
}
return PropertyType.STRING;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import io.miragon.miranum.connect.worker.api.WorkerRegistry;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

@Log
@RequiredArgsConstructor
public class WorkerAnnotationBeanPostProcessor implements BeanPostProcessor {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Map<String, Object> execute(final WorkerExecutor executor, final Object d
private Object mapInput(final Class<?> inputType, final Object object) {
final ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
return Objects.nonNull(inputType) ? mapper.convertValue(object, inputType) : null;
}

Expand Down
Loading

0 comments on commit bfe9320

Please sign in to comment.