Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kavitha| add tags for encounter tags based on split patterns #22

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading