Skip to content

Commit

Permalink
Merge pull request #40 from axonivy-market/rtModel
Browse files Browse the repository at this point in the history
fix: deserialization of migrated 11.2 runtime config model
  • Loading branch information
ivy-rew authored Jan 22, 2024
2 parents 3f69967 + 9ef159e commit d22d5cf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public class DecisionActivity extends AbstractUserProcessExtension {

@Override
public CompositeObject perform(IRequestId requestId, CompositeObject in, IIvyScriptContext context) throws Exception {
String ruleModelJson = getConfiguration();
RulesModel model = RulesModelSerialization.deserialize(ruleModelJson);
RulesModel model = RulesModelSerialization.deserialize(getConfig());
InputStream dmnInputStream = new DmnSerializer(model).serialize();
Map<String, Object> result = new DmnExecutor(dmnInputStream, in).execute();
if (!result.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.axonivy.ivy.process.element.rule.model;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -15,43 +13,48 @@
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;

import ch.ivyteam.ivy.process.extension.ProgramConfig;

public class RulesModelSerialization {
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final Set<String> KEYS = Set.of(
"rows", "actionColumns", "conditionColumns"
);

static {
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
}

private RulesModelSerialization() {}

public static Map<String, String> serialize(RulesModel model) throws JsonProcessingException {
var json = objectMapper.writeValueAsString(model);
var tree = (ObjectNode) objectMapper.readValue(json, JsonNode.class);
var json = MAPPER.writeValueAsString(model);
var tree = (ObjectNode) MAPPER.readValue(json, JsonNode.class);
Map<String, String> conf = new HashMap<>();
tree.fields().forEachRemaining(et -> {
conf.put(et.getKey(), et.getValue().toString());
});
return conf;
}

public static RulesModel deserialize(String jsonModel)
throws JsonParseException, JsonMappingException, IOException {
if (StringUtils.isEmpty(jsonModel)) {
return new RulesModel();
}
return objectMapper.readValue(jsonModel, RulesModel.class);
public static RulesModel deserialize(ProgramConfig program) throws Exception {
var config = KEYS.stream()
.filter(key -> program.get(key) != null)
.map(key -> Map.entry(key, program.get(key)))
.collect(Collectors.toMap(et -> et.getKey(), et -> et.getValue()));
return deserialize(config);
}

public static RulesModel deserialize(Map<String, String> userConf) throws Exception {
var obj = JsonNodeFactory.instance.objectNode();
for(var et : userConf.entrySet()) {
obj.set(et.getKey(), read(et.getValue()));
}
return objectMapper.readValue(obj.toString(), RulesModel.class);
return MAPPER.readValue(obj.toString(), RulesModel.class);
}

private static JsonNode read(String value) throws JsonMappingException, JsonProcessingException {
return objectMapper.readValue(value, JsonNode.class);
return MAPPER.readValue(value, JsonNode.class);
}

}

0 comments on commit d22d5cf

Please sign in to comment.