io.miragon.miranum.connect.camnda7.remote.utils.Camunda7RestValueMapper Maven / Gradle / Ivy
package io.miragon.miranum.connect.camnda7.remote.utils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.camunda.community.rest.client.dto.VariableValueDto;
import org.springframework.util.ClassUtils;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
public class Camunda7RestValueMapper {
public Map map(final Map variables) throws JsonProcessingException {
Map map = new HashMap<>();
for (Map.Entry stringObjectEntry : variables.entrySet()) {
Map.Entry stringVariableValueDtoEntry = mapVariable(stringObjectEntry);
if (map.put(stringVariableValueDtoEntry.getKey(), stringVariableValueDtoEntry.getValue()) != null) {
throw new IllegalStateException("Duplicate key");
}
}
return map;
}
public VariableValueDto createValue(final Object value) throws JsonProcessingException {
var variableValueDto = new VariableValueDto();
if (Objects.isNull(value) || ClassUtils.isPrimitiveOrWrapper(value.getClass()) || value instanceof String) {
variableValueDto.setValue(value);
} else {
final ObjectMapper objectMapper = new ObjectMapper();
variableValueDto.setValue(objectMapper.writeValueAsString(value));
variableValueDto.setType("json");
}
return variableValueDto;
}
private Map.Entry mapVariable(final Map.Entry stringObjectEntry) throws JsonProcessingException {
final VariableValueDto value = this.createValue(stringObjectEntry.getValue());
return new AbstractMap.SimpleEntry<>(stringObjectEntry.getKey(), value);
}
}