diff --git a/src/main/java/org/bahmni/eventrouterservice/configuration/RouteDescriptionLoader.java b/src/main/java/org/bahmni/eventrouterservice/configuration/RouteDescriptionLoader.java index c04764b..d690a11 100644 --- a/src/main/java/org/bahmni/eventrouterservice/configuration/RouteDescriptionLoader.java +++ b/src/main/java/org/bahmni/eventrouterservice/configuration/RouteDescriptionLoader.java @@ -126,6 +126,17 @@ public static class AdditionalProperty { private String filterValue; private LinkedHashMap staticProperties = new LinkedHashMap<>(0); private LinkedHashMap dynamicProperties = new LinkedHashMap<>(0); + private LinkedHashMap splitPatternConfigurations = new LinkedHashMap<>(0); + } + + @JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) + @JsonIgnoreProperties(ignoreUnknown = true) + @Getter + @NoArgsConstructor + @AllArgsConstructor + public static class SplitPatternConfiguration { + private String splitPattern; + private Integer splitIndex; } public enum BahmniEventType { diff --git a/src/main/java/org/bahmni/eventrouterservice/route/EventProcessor.java b/src/main/java/org/bahmni/eventrouterservice/route/EventProcessor.java index 0685549..df44fea 100644 --- a/src/main/java/org/bahmni/eventrouterservice/route/EventProcessor.java +++ b/src/main/java/org/bahmni/eventrouterservice/route/EventProcessor.java @@ -8,6 +8,7 @@ import org.apache.camel.Processor; import org.bahmni.eventrouterservice.configuration.RouteDescriptionLoader.RouteDescription; import org.bahmni.eventrouterservice.configuration.RouteDescriptionLoader.AdditionalProperty; +import org.bahmni.eventrouterservice.configuration.RouteDescriptionLoader.SplitPatternConfiguration; import java.util.LinkedHashMap; import java.util.List; @@ -48,7 +49,7 @@ private String addAdditionalProperties(String payloadAsJsonString, List splitPatternConfigurations = obj.getSplitPatternConfigurations(); if (obj.getStaticProperties() != null && obj.getStaticProperties().size() > 0) { obj.getStaticProperties().entrySet().forEach(entry -> { contextRef[0].put(JsonPath.compile(parentPath), entry.getKey(), entry.getValue()); @@ -57,15 +58,21 @@ private String addAdditionalProperties(String payloadAsJsonString, List 0) { obj.getDynamicProperties().entrySet().forEach(entry -> { - contextRef[0] = contextRef[0].map(parentPath, (currentValue, configuration) -> { - Object filterObj = JsonPath.read(currentValue, filterKeyPath); - Object valueObj = JsonPath.read(currentValue, entry.getValue()); - DocumentContext obsContext = JsonPath.parse(currentValue); - if ((filterKeyPath == null && filterValue == null) || (filterObj != null && filterObj.toString().contains(filterValue))) { - obsContext.put(JsonPath.compile("$"), entry.getKey(), valueObj); - } - return obsContext.json(); - }); + Object parentObj = JsonPath.read(payloadAsJsonString, parentPath); + if (parentObj instanceof List && !((List) parentObj).isEmpty()) { + contextRef[0] = contextRef[0].map(parentPath, (currentValue, configuration) -> { + Object filterObj = JsonPath.read(currentValue, filterKeyPath); + Object valueObj = JsonPath.read(currentValue, entry.getValue()); + DocumentContext obsContext = JsonPath.parse(currentValue); + if (splitPatternConfigurations != null && splitPatternConfigurations.get(entry.getKey()) != null) { + valueObj = applySplitPattern(valueObj, splitPatternConfigurations.get(entry.getKey())); + } + if ((filterKeyPath == null && filterValue == null) || (filterObj != null && filterObj.toString().contains(filterValue))) { + obsContext.put(JsonPath.compile("$"), entry.getKey(), valueObj); + } + return obsContext.json(); + }); + } }); } } @@ -76,4 +83,15 @@ private String addAdditionalProperties(String payloadAsJsonString, List= 0 && index < parts.length) { + return parts[index]; + } + } + return value; + } + } diff --git a/src/test/java/org/bahmni/eventrouterservice/route/EventProcessorTest.java b/src/test/java/org/bahmni/eventrouterservice/route/EventProcessorTest.java index 046abe5..cc981b8 100644 --- a/src/test/java/org/bahmni/eventrouterservice/route/EventProcessorTest.java +++ b/src/test/java/org/bahmni/eventrouterservice/route/EventProcessorTest.java @@ -30,7 +30,7 @@ public void givenAdditionalPropertyKeyWithValue_whenStartProcessing_thenShouldAd Destination destination = new Destination(BAHMNI_PATIENT_UPDATED, new Topic("topicName", null), null); LinkedHashMap staticProperties = new LinkedHashMap<>(); staticProperties.put("facility", "Bahmni"); - AdditionalProperty additionalProperty = new AdditionalProperty("$", null, null, staticProperties, null); + AdditionalProperty additionalProperty = new AdditionalProperty("$", null, null, staticProperties, null, null); List additionalProperties = new ArrayList(); additionalProperties.add(additionalProperty); when(routeDescription.getAdditionalProperties()).thenReturn(additionalProperties); @@ -57,7 +57,7 @@ public void givenDestinationBasedOnEventType_whenStartProcessing_thenShouldChang Destination destination = new Destination(BAHMNI_PATIENT_UPDATED, new Topic("topicName", null), null); LinkedHashMap staticProperties = new LinkedHashMap<>(); staticProperties.put("facility", "Bahmni"); - AdditionalProperty additionalProperty = new AdditionalProperty("$", null, null, staticProperties, null); + AdditionalProperty additionalProperty = new AdditionalProperty("$", null, null, staticProperties, null, null); List additionalProperties = new ArrayList(); additionalProperties.add(additionalProperty); when(routeDescription.getAdditionalProperties()).thenReturn(additionalProperties); @@ -97,7 +97,7 @@ public void givenAdditionalPropertyKeyWithValueWithInvalidPayload_whenStartProce RouteDescription routeDescription = mock(RouteDescription.class); LinkedHashMap staticProperties = new LinkedHashMap<>(); staticProperties.put("facility", "Bahmni"); - AdditionalProperty additionalProperty = new AdditionalProperty("$", null, null, staticProperties, null); + AdditionalProperty additionalProperty = new AdditionalProperty("$", null, null, staticProperties, null, null); List additionalProperties = new ArrayList(); additionalProperties.add(additionalProperty); when(routeDescription.getAdditionalProperties()).thenReturn(additionalProperties);