diff --git a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/util/ResourceReferenceUtil.java b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/util/ResourceReferenceUtil.java index 6cc384556..5da1f357c 100644 --- a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/util/ResourceReferenceUtil.java +++ b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/util/ResourceReferenceUtil.java @@ -50,27 +50,29 @@ public void extractChildReferencesFromEhrComposition(RCMRMT030101UKEhrCompositio public void extractChildReferencesFromCompoundStatement(RCMRMT030101UKCompoundStatement compoundStatement, List entryReferences) { - if (compoundStatement != null) { - if (isDiagnosticReport(compoundStatement)) { - addDiagnosticReportEntry(compoundStatement, entryReferences); - } else { - compoundStatement.getComponent().forEach(component -> { - addObservationStatementEntry( - component.getObservationStatement(), entryReferences, compoundStatement); - addPlanStatementEntry(component.getPlanStatement(), entryReferences); - addRequestStatementEntry(component.getRequestStatement(), entryReferences); - addLinkSetEntry(component.getLinkSet(), entryReferences); - addMedicationEntry(component.getMedicationStatement(), entryReferences); + if (compoundStatement == null) { + return; + } + + if (isDiagnosticReport(compoundStatement)) { + addDiagnosticReportEntry(compoundStatement, entryReferences); + return; + } - if (isNotIgnoredResource(compoundStatement, entryReferences)) { - addNarrativeStatementEntry(component.getNarrativeStatement(), entryReferences); - } + compoundStatement.getComponent().forEach(component -> { + addObservationStatementEntry(component.getObservationStatement(), entryReferences, compoundStatement); + addPlanStatementEntry(component.getPlanStatement(), entryReferences); + addRequestStatementEntry(component.getRequestStatement(), entryReferences); + addLinkSetEntry(component.getLinkSet(), entryReferences); + addMedicationEntry(component.getMedicationStatement(), entryReferences); - extractChildReferencesFromCompoundStatement(component.getCompoundStatement(), entryReferences); - }); + if (isNotIgnoredResource(compoundStatement, entryReferences)) { + addNarrativeStatementEntry(component.getNarrativeStatement(), entryReferences); } - } + + extractChildReferencesFromCompoundStatement(component.getCompoundStatement(), entryReferences); + }); } public void extractChildReferencesFromTemplate(RCMRMT030101UKCompoundStatement compoundStatement, diff --git a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/util/XmlUnmarshallUtil.java b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/util/XmlUnmarshallUtil.java index 9430c5b71..800dfaab6 100644 --- a/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/util/XmlUnmarshallUtil.java +++ b/gp2gp-translator/src/main/java/uk/nhs/adaptors/pss/translator/util/XmlUnmarshallUtil.java @@ -3,6 +3,8 @@ import static java.nio.charset.StandardCharsets.UTF_8; import java.io.File; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import jakarta.xml.bind.JAXBContext; import jakarta.xml.bind.JAXBElement; @@ -16,12 +18,26 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public class XmlUnmarshallUtil { + private static final ConcurrentMap, JAXBContext> CONTEXT_CACHE = new ConcurrentHashMap<>(); + + private static JAXBContext getContext(Class clazz) { + return CONTEXT_CACHE.computeIfAbsent(clazz, c -> { + try { + return JAXBContext.newInstance(c); + } catch (JAXBException e) { + throw new RuntimeException(e); + } + }); + } + public static T unmarshallFile(File xmlFile, Class destinationClass) throws JAXBException { - Unmarshaller unmarshaller = createUnmarshaller(destinationClass); + + Unmarshaller unmarshaller = getContext(destinationClass).createUnmarshaller(); JAXBElement unmarshalledMessage = (JAXBElement) unmarshaller.unmarshal(xmlFile); return unmarshalledMessage.getValue(); } + public static T unmarshallString(String xmlString, Class destinationClass) throws JAXBException { Unmarshaller unmarshaller = createUnmarshaller(destinationClass); JAXBElement unmarshalledMessage = (JAXBElement) unmarshaller.unmarshal( diff --git a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/util/ResourceReferenceUtilTest.java b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/util/ResourceReferenceUtilTest.java index 3368066b7..3135ed0a8 100644 --- a/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/util/ResourceReferenceUtilTest.java +++ b/gp2gp-translator/src/test/java/uk/nhs/adaptors/pss/translator/util/ResourceReferenceUtilTest.java @@ -33,8 +33,10 @@ public class ResourceReferenceUtilTest { private static final String XML_RESOURCES_COMPOSITION = "xml/ResourceReference/EhrComposition/"; private static final String XML_RESOURCES_COMPOUND = "xml/ResourceReference/CompoundStatement/"; + private static final int ONE = 1; private static final int TWO = 2; - + private static final int THREE = 3; + private static final int FOUR = 4; @Mock private DatabaseImmunizationChecker immunizationChecker; @@ -42,6 +44,43 @@ public class ResourceReferenceUtilTest { @InjectMocks private ResourceReferenceUtil resourceReferenceUtil; + @Test + public void testNarrativeStatementReferencedAtCompoundStatementLevel() { + final RCMRMT030101UKCompoundStatement compoundStatement + = unmarshallCompoundStatementElement("compound_statement_with_narrative_statement.xml"); + + List references = new ArrayList<>(); + resourceReferenceUtil.extractChildReferencesFromCompoundStatement(compoundStatement, references); + + assertThat(references).hasSize(ONE); + assertThat(references.getFirst().getReference()).isEqualTo("Observation/07F5EAC0-90B5-11EC-B1E5-0800200C9A66"); + } + + @Test + public void testPlanAndRequestStatementReferencedAtCompoundStatementLevel() { + final RCMRMT030101UKCompoundStatement compoundStatement + = unmarshallCompoundStatementElement("compound_statement_with_plan_and_request_statement.xml"); + + List references = new ArrayList<>(); + resourceReferenceUtil.extractChildReferencesFromCompoundStatement(compoundStatement, references); + + assertThat(references).hasSize(FOUR); + assertThat(references.get(2).getReference()).isEqualTo("ProcedureRequest/3316531F-5705-424C-9E1A-EE694FB411B4"); + assertThat(references.get(THREE).getReference()).isEqualTo("ReferralRequest/B4303C92-4D1C-11E3-A2DD-010000000161"); + } + + @Test + public void testPlanStatementReferencedAtEhrCompositionLevel() { + final RCMRMT030101UKEhrComposition ehrComposition + = unmarshallEhrCompositionElement("ehr_composition_with_plan_statement.xml"); + + List references = new ArrayList<>(); + resourceReferenceUtil.extractChildReferencesFromEhrComposition(ehrComposition, references); + + assertThat(references).hasSize(THREE); + assertThat(references.get(2).getReference()).isEqualTo("ProcedureRequest/3316531F-5705-424C-9E1A-EE694FB411B4"); + } + @Test public void testMedicationResourcesReferencedAtEhrCompositionLevel() { final RCMRMT030101UKEhrComposition ehrComposition = unmarshallEhrCompositionElement("ehr_composition_medication.xml"); diff --git a/gp2gp-translator/src/test/resources/xml/ResourceReference/CompoundStatement/compound_statement_with_narrative_statement.xml b/gp2gp-translator/src/test/resources/xml/ResourceReference/CompoundStatement/compound_statement_with_narrative_statement.xml new file mode 100644 index 000000000..a07dfb445 --- /dev/null +++ b/gp2gp-translator/src/test/resources/xml/ResourceReference/CompoundStatement/compound_statement_with_narrative_statement.xml @@ -0,0 +1,96 @@ + + + + + +
+ + + + + + + + +
+ + + + + + + + +
+ + + + + + + +
+ + + EINE KLEINE + + + + + + + CommentType:LAB SPECIMEN COMMENT(E271) + CommentDate:20020330092100 + + Received Date: 2002-03-30 09:21 + + + + + + + + + + + + + Test Range 1 + + + + + + + + + + + +
+ + + + + + Notes on the tired all the time entry + + + + + + + + Comments - Aaaargh + + + + + + + \ No newline at end of file diff --git a/gp2gp-translator/src/test/resources/xml/ResourceReference/CompoundStatement/compound_statement_with_plan_and_request_statement.xml b/gp2gp-translator/src/test/resources/xml/ResourceReference/CompoundStatement/compound_statement_with_plan_and_request_statement.xml new file mode 100644 index 000000000..b0461ac2c --- /dev/null +++ b/gp2gp-translator/src/test/resources/xml/ResourceReference/CompoundStatement/compound_statement_with_plan_and_request_statement.xml @@ -0,0 +1,136 @@ + + + + + +
+ + + + + + + + +
+ + + + + + + + +
+ + + + + + + + + + + capsule + + + + + + + + + + + +
+ + + + + + capsule + + + + + Pharmacy Text: Repeat Dispensing Pharmacy Note. 1 + + + + + Pharmacy Text: Repeat Dispensing Pharmacy Note. 2 + + + + + + + + + + + + + tablet(s) + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + Test request statement text + New line + + +
+ + + Routine + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/gp2gp-translator/src/test/resources/xml/ResourceReference/EhrComposition/ehr_composition_with_plan_statement.xml b/gp2gp-translator/src/test/resources/xml/ResourceReference/EhrComposition/ehr_composition_with_plan_statement.xml new file mode 100644 index 000000000..2861fc1ac --- /dev/null +++ b/gp2gp-translator/src/test/resources/xml/ResourceReference/EhrComposition/ehr_composition_with_plan_statement.xml @@ -0,0 +1,96 @@ + + + + + + + + + + +
+ + + + + + + + + + + capsule + + + + + + + + + + + +
+ + + + + + capsule + + + + + Pharmacy Text: Repeat Dispensing Pharmacy Note. 1 + + + + + Pharmacy Text: Repeat Dispensing Pharmacy Note. 2 + + + + + + + + + + + + + tablet(s) + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + \ No newline at end of file