Skip to content

Commit

Permalink
Kavitha| add tags for encounter tags based on split patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
kavitha-sundararajan committed May 3, 2024
1 parent 89bbc2c commit 4a6c601
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ public static class AdditionalProperty {
private String filterValue;
private LinkedHashMap<String, String> staticProperties = new LinkedHashMap<>(0);
private LinkedHashMap<String, String> dynamicProperties = new LinkedHashMap<>(0);
private LinkedHashMap<String, SplitPatternConfiguration> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,7 +49,7 @@ private String addAdditionalProperties(String payloadAsJsonString, List<Addition
String parentPath = obj.getParentPath();
String filterKeyPath = obj.getFilterKeyPath();
String filterValue = obj.getFilterValue();

LinkedHashMap<String, SplitPatternConfiguration> 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());
Expand All @@ -57,15 +58,21 @@ private String addAdditionalProperties(String payloadAsJsonString, List<Addition

if (obj.getDynamicProperties() != null && obj.getDynamicProperties().size() > 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();
});
}
});
}
}
Expand All @@ -76,4 +83,15 @@ private String addAdditionalProperties(String payloadAsJsonString, List<Addition
}
}

private Object applySplitPattern(Object value, SplitPatternConfiguration splitConfig) {
if (value instanceof String) {
String[] parts = ((String) value).split(splitConfig.getSplitPattern());
Integer index = splitConfig.getSplitIndex();
if (index >= 0 && index < parts.length) {
return parts[index];
}
}
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void givenAdditionalPropertyKeyWithValue_whenStartProcessing_thenShouldAd
Destination destination = new Destination(BAHMNI_PATIENT_UPDATED, new Topic("topicName", null), null);
LinkedHashMap<String, String> 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<AdditionalProperty> additionalProperties = new ArrayList();
additionalProperties.add(additionalProperty);
when(routeDescription.getAdditionalProperties()).thenReturn(additionalProperties);
Expand All @@ -57,7 +57,7 @@ public void givenDestinationBasedOnEventType_whenStartProcessing_thenShouldChang
Destination destination = new Destination(BAHMNI_PATIENT_UPDATED, new Topic("topicName", null), null);
LinkedHashMap<String, String> 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<AdditionalProperty> additionalProperties = new ArrayList();
additionalProperties.add(additionalProperty);
when(routeDescription.getAdditionalProperties()).thenReturn(additionalProperties);
Expand Down Expand Up @@ -97,7 +97,7 @@ public void givenAdditionalPropertyKeyWithValueWithInvalidPayload_whenStartProce
RouteDescription routeDescription = mock(RouteDescription.class);
LinkedHashMap<String, String> 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<AdditionalProperty> additionalProperties = new ArrayList();
additionalProperties.add(additionalProperty);
when(routeDescription.getAdditionalProperties()).thenReturn(additionalProperties);
Expand Down

0 comments on commit 4a6c601

Please sign in to comment.