From c60a0038ffc3b258ab2fc17157bb419e022fcb2e Mon Sep 17 00:00:00 2001 From: Reguel Wermelinger Date: Thu, 9 Nov 2023 15:03:23 +0100 Subject: [PATCH] XIVY-12538 migrate to process format 11.2 --- .../processes/TaxCalculation.p.json | 131 +----------------- .../rule/model/RulesModelSerialization.java | 27 +++- .../element/rule/ui/DmnTableUiModel.java | 10 +- .../model/TestRulesModelSerialization.java | 4 +- 4 files changed, 39 insertions(+), 133 deletions(-) diff --git a/dmn-decision-table-demo/processes/TaxCalculation.p.json b/dmn-decision-table-demo/processes/TaxCalculation.p.json index 4286b2d..39df984 100644 --- a/dmn-decision-table-demo/processes/TaxCalculation.p.json +++ b/dmn-decision-table-demo/processes/TaxCalculation.p.json @@ -1,5 +1,5 @@ { - "$schema" : "https://json-schema.axonivy.com/process/11.2.1/process.json", + "$schema" : "https://json-schema.axonivy.com/process/11.2.2/process.json", "id" : "15C82A89FA49E8E6", "config" : { "data" : "com.axonivy.ivy.supplements.rule.beans.demos.Data" @@ -35,130 +35,11 @@ "impl" : "DecisionActivity", "name" : "evaluate taxes with decision table", "config" : { - "userConfig" : [ - "{", - " \"conditionColumns\" : [ {", - " \"attributeName\" : \"in.yearlyIncomeDollars\",", - " \"type\" : \"Number\"", - " }, {", - " \"attributeName\" : \"in.yearlyIncomeDollars\",", - " \"type\" : \"Number\"", - " }, {", - " \"attributeName\" : \"in.person.gender\",", - " \"type\" : \"String\"", - " } ],", - " \"actionColumns\" : [ {", - " \"attributeName\" : \"out.taxrate\",", - " \"type\" : \"Number\"", - " } ],", - " \"rows\" : [ {", - " \"cells\" : [ {", - " \"conditionCell\" : {", - " \"operator\" : \"EQUAL_OR_GREATER\",", - " \"arguments\" : [ \"0\" ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"LESS\",", - " \"arguments\" : [ \"50000\" ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"NO_CONDITION\",", - " \"arguments\" : [ ]", - " }", - " }, {", - " \"valueCell\" : {", - " \"value\" : \"0\"", - " }", - " } ]", - " }, {", - " \"cells\" : [ {", - " \"conditionCell\" : {", - " \"operator\" : \"EQUAL_OR_GREATER\",", - " \"arguments\" : [ \"50000\" ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"LESS\",", - " \"arguments\" : [ \"250000\" ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"EQUAL\",", - " \"arguments\" : [ \"male\" ]", - " }", - " }, {", - " \"valueCell\" : {", - " \"value\" : \"15\"", - " }", - " } ]", - " }, {", - " \"cells\" : [ {", - " \"conditionCell\" : {", - " \"operator\" : \"EQUAL_OR_GREATER\",", - " \"arguments\" : [ \"250000\" ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"NO_CONDITION\",", - " \"arguments\" : [ ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"EQUAL\",", - " \"arguments\" : [ \"male\" ]", - " }", - " }, {", - " \"valueCell\" : {", - " \"value\" : \"30\"", - " }", - " } ]", - " }, {", - " \"cells\" : [ {", - " \"conditionCell\" : {", - " \"operator\" : \"EQUAL_OR_GREATER\",", - " \"arguments\" : [ \"50000\" ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"LESS\",", - " \"arguments\" : [ \"250000\" ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"EQUAL\",", - " \"arguments\" : [ \"female\" ]", - " }", - " }, {", - " \"valueCell\" : {", - " \"value\" : \"10\"", - " }", - " } ]", - " }, {", - " \"cells\" : [ {", - " \"conditionCell\" : {", - " \"operator\" : \"EQUAL_OR_GREATER\",", - " \"arguments\" : [ \"250000\" ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"NO_CONDITION\",", - " \"arguments\" : [ ]", - " }", - " }, {", - " \"conditionCell\" : {", - " \"operator\" : \"EQUAL\",", - " \"arguments\" : [ \"female\" ]", - " }", - " }, {", - " \"valueCell\" : {", - " \"value\" : \"20\"", - " }", - " } ]", - " } ]", - "}" - ] + "userConfig" : { + "rows" : "[{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"0\"]}},{\"conditionCell\":{\"operator\":\"LESS\",\"arguments\":[\"50000\"]}},{\"conditionCell\":{\"operator\":\"NO_CONDITION\",\"arguments\":[]}},{\"valueCell\":{\"value\":\"0\"}}]},{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"50000\"]}},{\"conditionCell\":{\"operator\":\"LESS\",\"arguments\":[\"250000\"]}},{\"conditionCell\":{\"operator\":\"EQUAL\",\"arguments\":[\"male\"]}},{\"valueCell\":{\"value\":\"15\"}}]},{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"250000\"]}},{\"conditionCell\":{\"operator\":\"NO_CONDITION\",\"arguments\":[]}},{\"conditionCell\":{\"operator\":\"EQUAL\",\"arguments\":[\"male\"]}},{\"valueCell\":{\"value\":\"30\"}}]},{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"50000\"]}},{\"conditionCell\":{\"operator\":\"LESS\",\"arguments\":[\"250000\"]}},{\"conditionCell\":{\"operator\":\"EQUAL\",\"arguments\":[\"female\"]}},{\"valueCell\":{\"value\":\"10\"}}]},{\"cells\":[{\"conditionCell\":{\"operator\":\"EQUAL_OR_GREATER\",\"arguments\":[\"250000\"]}},{\"conditionCell\":{\"operator\":\"NO_CONDITION\",\"arguments\":[]}},{\"conditionCell\":{\"operator\":\"EQUAL\",\"arguments\":[\"female\"]}},{\"valueCell\":{\"value\":\"20\"}}]}]", + "actionColumns" : "[{\"attributeName\":\"out.taxrate\",\"type\":\"Number\"}]", + "conditionColumns" : "[{\"attributeName\":\"in.yearlyIncomeDollars\",\"type\":\"Number\"},{\"attributeName\":\"in.yearlyIncomeDollars\",\"type\":\"Number\"},{\"attributeName\":\"in.person.gender\",\"type\":\"String\"}]" + } }, "visual" : { "at" : { "x" : 664, "y" : 112 }, diff --git a/dmn-decision-table/src/com/axonivy/ivy/process/element/rule/model/RulesModelSerialization.java b/dmn-decision-table/src/com/axonivy/ivy/process/element/rule/model/RulesModelSerialization.java index 7851948..823f2ab 100644 --- a/dmn-decision-table/src/com/axonivy/ivy/process/element/rule/model/RulesModelSerialization.java +++ b/dmn-decision-table/src/com/axonivy/ivy/process/element/rule/model/RulesModelSerialization.java @@ -1,14 +1,19 @@ package com.axonivy.ivy.process.element.rule.model; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; 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; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import com.fasterxml.jackson.databind.node.ObjectNode; public class RulesModelSerialization { private static final ObjectMapper objectMapper = new ObjectMapper(); @@ -19,8 +24,14 @@ public class RulesModelSerialization { private RulesModelSerialization() {} - public static String serialize(RulesModel model) throws JsonProcessingException { - return objectMapper.writeValueAsString(model); + public static Map serialize(RulesModel model) throws JsonProcessingException { + var json = objectMapper.writeValueAsString(model); + var tree = (ObjectNode) objectMapper.readValue(json, JsonNode.class); + Map conf = new HashMap<>(); + tree.fields().forEachRemaining(et -> { + conf.put(et.getKey(), et.getValue().toString()); + }); + return conf; } public static RulesModel deserialize(String jsonModel) @@ -31,4 +42,16 @@ public static RulesModel deserialize(String jsonModel) return objectMapper.readValue(jsonModel, RulesModel.class); } + public static RulesModel deserialize(Map 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); + } + + private static JsonNode read(String value) throws JsonMappingException, JsonProcessingException { + return objectMapper.readValue(value, JsonNode.class); + } + } diff --git a/dmn-decision-table/src/com/axonivy/ivy/process/element/rule/ui/DmnTableUiModel.java b/dmn-decision-table/src/com/axonivy/ivy/process/element/rule/ui/DmnTableUiModel.java index 6fdb845..ef88317 100644 --- a/dmn-decision-table/src/com/axonivy/ivy/process/element/rule/ui/DmnTableUiModel.java +++ b/dmn-decision-table/src/com/axonivy/ivy/process/element/rule/ui/DmnTableUiModel.java @@ -1,6 +1,7 @@ package com.axonivy.ivy.process.element.rule.ui; import java.util.List; +import java.util.Map; import com.axonivy.ivy.process.element.rule.model.Row; import com.axonivy.ivy.process.element.rule.model.RulesModel; @@ -42,18 +43,19 @@ private void updateRows(List rows) { } private RulesModel loadRulesModel() { + Map conf = element.getUserConfig().configs(); try { - return RulesModelSerialization.deserialize(element.getUserConfig().getRawValue()); + return RulesModelSerialization.deserialize(conf); } catch (Exception ex) { - LOGGER.error("Failed to load decision table config", ex); + LOGGER.error("Failed to load decision table config "+conf, ex); return new RulesModel(); } } public void storeRulesModel() { try { - String json = RulesModelSerialization.serialize(rules); - element.setUserConfig(new UserConfig(json)); + Map conf = RulesModelSerialization.serialize(rules); + element.setUserConfig(new UserConfig(conf)); } catch (JsonProcessingException ex) { throw new RuntimeException("Failed to store decision table config", ex); } diff --git a/dmn-decision-table/test/com/axonivy/ivy/process/element/rule/model/TestRulesModelSerialization.java b/dmn-decision-table/test/com/axonivy/ivy/process/element/rule/model/TestRulesModelSerialization.java index 5c831d1..cf39eea 100644 --- a/dmn-decision-table/test/com/axonivy/ivy/process/element/rule/model/TestRulesModelSerialization.java +++ b/dmn-decision-table/test/com/axonivy/ivy/process/element/rule/model/TestRulesModelSerialization.java @@ -31,8 +31,8 @@ public void serialization() throws Exception { rowModel.addCell(new ValueCell("8.5")); model.addRow(rowModel); - String json = RulesModelSerialization.serialize(model); - RulesModel deserializedModel = RulesModelSerialization.deserialize(json); + var conf = RulesModelSerialization.serialize(model); + RulesModel deserializedModel = RulesModelSerialization.deserialize(conf); assertEquals(model, deserializedModel); }