Skip to content

Commit

Permalink
Add mpsii comments mapping refactor (#1477)
Browse files Browse the repository at this point in the history
* refactor:
use stream instead of for loop
use IllegalArgumentException instead of NullPointerException
make tests pass
add helper method isLocalCode
add helper method processCoding
add helper method validateField

* refactoring + all tests pass
  • Loading branch information
jorg3lopez authored Oct 23, 2024
1 parent 39aa1cc commit 2a961d4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,63 @@ public void transform(HealthData<?> resource, Map<String, Object> args) {
var codingMap = getMapFromArgs(args);

var bundle = (Bundle) resource.getUnderlyingData();
var msh41Identifier = extractMsh41Identifier(bundle);
var messageId = HapiHelper.getMessageControlId(bundle);
var observations = HapiHelper.resourcesInBundle(bundle, Observation.class);

for (Observation obv : observations.toList()) {
var codingList = obv.getCode().getCoding();
observations
.filter(this::hasValidCoding)
.forEach(
observation ->
processCoding(observation, codingMap, msh41Identifier, messageId));
}

if (codingList.size() != 1) {
continue;
}
private boolean hasValidCoding(Observation observation) {
var codingList = observation.getCode().getCoding();
return codingList.size() == 1 && isLocalCode(codingList.get(0));
}

var coding = codingList.get(0);
if (!HapiHelper.hasDefinedCoding(
coding, HapiHelper.EXTENSION_ALT_CODING, HapiHelper.LOCAL_CODE)) {
continue;
}
private boolean isLocalCode(Coding coding) {
return HapiHelper.hasDefinedCoding(
coding, HapiHelper.EXTENSION_ALT_CODING, HapiHelper.LOCAL_CODE);
}

var identifier = codingMap.get(coding.getCode());
if (identifier == null) {
logUnmappedLocalCode(bundle, coding);
continue;
}
private String extractMsh41Identifier(Bundle bundle) {
var msh41Identifier = HapiHelper.getMSH4_1Identifier(bundle);
return msh41Identifier != null ? msh41Identifier.getValue() : null;
}

var mappedCoding = getMappedCoding(identifier);
private void processCoding(
Observation observation,
Map<String, IdentifierCode> codingMap,
String msh41Identifier,
String messageId) {
var originalCoding = observation.getCode().getCoding().get(0);
IdentifierCode identifier = codingMap.get(originalCoding.getCode());

if (identifier == null) {
logUnmappedLocalCode(originalCoding, msh41Identifier, messageId);
return;
}

// Add the mapped code as the first in the list, ahead of the existing alternate code
codingList.add(0, mappedCoding);
var mappedCoding = getMappedCoding(identifier);
observation.getCode().getCoding().add(0, mappedCoding);
}

private String validateField(String field, String fieldName) {
if (field == null || field.isBlank()) {
throw new IllegalArgumentException("missing or empty required field " + fieldName);
}
return field;
}

private void logUnmappedLocalCode(Bundle bundle, Coding coding) {
var msh41Identifier = HapiHelper.getMSH4_1Identifier(bundle);
var msh41Value = msh41Identifier != null ? msh41Identifier.getValue() : null;
private void logUnmappedLocalCode(Coding coding, String msh41Identifier, String messageId) {

logger.logWarning(
"Unmapped local code detected: '{}', from sender: '{}', message Id: '{}'",
coding.getCode(),
msh41Value,
HapiHelper.getMessageControlId(bundle));
msh41Identifier,
messageId);
}

private Coding getMappedCoding(IdentifierCode identifierCode) {
Expand Down Expand Up @@ -100,20 +120,10 @@ private Map<String, IdentifierCode> getMapFromArgs(Map<String, Object> args) {
}

private IdentifierCode getIdentifierCode(Map.Entry<String, Map<String, String>> entry) {
var code = entry.getValue().get("code");
var display = entry.getValue().get("display");
var codingSystem = entry.getValue().get("codingSystem");

if (code == null) {
throw new NullPointerException("One or more code objects are missing in codingMap");
}
if (display == null) {
throw new NullPointerException("One or more display objects are missing in codingMap");
}
if (codingSystem == null) {
throw new NullPointerException(
"One or more codingSystem objects are missing in codingMap");
}
var value = entry.getValue();
var code = validateField(value.get("code"), "code");
var display = validateField(value.get("display"), "display");
var codingSystem = validateField(value.get("codingSystem"), "codingSystem");

return new IdentifierCode(code, display, codingSystem);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ class MapLocalObservationCodesTest extends Specification {

def "When args are missing coding system, throws a NullPointerException"() {
given:
def exceptionMessage = "codingSystem"
def exceptionMessage = "missing or empty required field codingSystem"
def bundle = createBundleWithObservation("99717-32", "Adrenoleukodystrophy deficiency newborn screening interpretation", true)
def args = [
"codingMap": [
Expand All @@ -261,7 +261,7 @@ class MapLocalObservationCodesTest extends Specification {
transformClass.transform(new HapiFhirResource(bundle), args)

then:
def exception = thrown(NullPointerException)
def exception = thrown(IllegalArgumentException)
exception.message.contains(exceptionMessage)
}

Expand Down Expand Up @@ -312,7 +312,7 @@ class MapLocalObservationCodesTest extends Specification {

def "When args are missing code, throws a NullPointerException"() {
given:
def exceptionMessage = "code"
def exceptionMessage = "missing or empty required field code"
def bundle = createBundleWithObservation("99717-32", "Adrenoleukodystrophy deficiency newborn screening interpretation", true)
def args = [
"codingMap": [
Expand All @@ -327,13 +327,13 @@ class MapLocalObservationCodesTest extends Specification {
transformClass.transform(new HapiFhirResource(bundle), args)

then:
def exception = thrown(NullPointerException)
def exception = thrown(IllegalArgumentException)
exception.message.contains(exceptionMessage)
}

def "When args are missing display, throws a NullPointerException"() {
given:
def exceptionMessage = "display"
def exceptionMessage = "missing or empty required field display"
def bundle = createBundleWithObservation("99717-32", "Adrenoleukodystrophy deficiency newborn screening interpretation", true)
def args = [
"codingMap": [
Expand All @@ -348,7 +348,7 @@ class MapLocalObservationCodesTest extends Specification {
transformClass.transform(new HapiFhirResource(bundle), args)

then:
def exception = thrown(NullPointerException)
def exception = thrown(IllegalArgumentException)
exception.message.contains(exceptionMessage)
}

Expand Down

0 comments on commit 2a961d4

Please sign in to comment.