Skip to content

Commit 75116cf

Browse files
committed
Refactored DiagnosticReportMapper.
1 parent cd66513 commit 75116cf

File tree

2 files changed

+39
-48
lines changed

2 files changed

+39
-48
lines changed

service/src/main/java/uk/nhs/adaptors/gp2gp/ehr/mapper/diagnosticreport/DiagnosticReportMapper.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,25 @@ public String mapDiagnosticReportToCompoundStatement(DiagnosticReport diagnostic
8080
final IdMapper idMapper = messageContext.getIdMapper();
8181
markObservationsAsProcessed(idMapper, observations);
8282

83-
List<Observation> filingComments = getFilingComments(observations);
84-
List<Observation> observationsExcludingFilingComments = assignDummySpecimensToObservationsWithNoSpecimen(
85-
observations.stream()
86-
.filter(Predicate.not(DiagnosticReportMapper::isFilingComment))
87-
.toList(),
88-
specimens);
83+
List<Observation> observationsWithAttachedDummySpecimens =
84+
new ArrayList<>(assignDummySpecimensToObservationsWithNoSpecimen(observations, specimens));
8985

90-
observations = addDummyObservationsToObservationList(observationsExcludingFilingComments, specimens, diagnosticReport);
91-
observations.addAll(filingComments);
92-
List<Observation> finalObservations = observations;
86+
observations = addDummyObservationsToObservationList(
87+
observationsWithAttachedDummySpecimens,
88+
specimens,
89+
diagnosticReport);
90+
91+
List<Observation> observationsWithDummySpecimensAndObservations = observations;
9392

9493
String mappedSpecimens = specimens.stream()
9594
.map(specimen -> specimenMapper.mapSpecimenToCompoundStatement(specimen,
96-
observationsForSpecimen(specimen, finalObservations),
95+
observationsForSpecimen(specimen, observationsWithDummySpecimensAndObservations),
9796
diagnosticReport))
9897
.collect(Collectors.joining());
9998

100-
String reportLevelNarrativeStatements = prepareReportLevelNarrativeStatements(diagnosticReport, finalObservations);
99+
String reportLevelNarrativeStatements = prepareReportLevelNarrativeStatements(
100+
diagnosticReport,
101+
observationsWithDummySpecimensAndObservations);
101102

102103
var diagnosticReportCompoundStatementTemplateParameters = DiagnosticReportCompoundStatementTemplateParameters.builder()
103104
.compoundStatementId(idMapper.getOrNew(ResourceType.DiagnosticReport, diagnosticReport.getIdElement()))
@@ -171,7 +172,11 @@ private boolean hasObservationsWithoutSpecimen(List<Observation> observations) {
171172
private List<Observation> assignDummySpecimensToObservationsWithNoSpecimen(
172173
List<Observation> observations, List<Specimen> specimens) {
173174

175+
List<Observation> filingComments = getFilingComments(observations);
176+
observations = new ArrayList<>(stripFilingComments(observations));
177+
174178
if (!hasObservationsWithoutSpecimen(observations)) {
179+
observations.addAll(filingComments);
175180
return observations;
176181
}
177182

@@ -188,6 +193,11 @@ private List<Observation> assignDummySpecimensToObservationsWithNoSpecimen(
188193
}
189194
}
190195

196+
if (observations.isEmpty()) {
197+
return filingComments;
198+
}
199+
200+
observations.addAll(filingComments);
191201
return observations;
192202
}
193203

@@ -399,6 +409,12 @@ static boolean isFilingComment(Observation observation) {
399409
.anyMatch(coding -> COMMENT_NOTE.equals(coding.getCode()));
400410
}
401411

412+
private List<Observation> stripFilingComments(List<Observation> observations) {
413+
return observations.stream()
414+
.filter(Predicate.not(DiagnosticReportMapper::isFilingComment))
415+
.toList();
416+
}
417+
402418
private List<Observation> getFilingComments(List<Observation> observations) {
403419
return observations.stream()
404420
.filter(DiagnosticReportMapper::isFilingComment)

service/src/test/resources/ehr/request/fhir/output/expected-xml-with-standalone-observations.xml

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -115,42 +115,6 @@
115115
<availabilityTime value="20030221115000"/>
116116
</NarrativeStatement>
117117
</component>
118-
<component typeCode="COMP" contextConductionInd="true">
119-
<CompoundStatement classCode="CLUSTER" moodCode="EVN">
120-
<id root="test-id-3"/>
121-
<code code="123038009" codeSystem="2.16.840.1.113883.2.1.3.2.4.15"
122-
displayName="specimen (specimen)"/>
123-
<statusCode code="COMPLETE"/>
124-
<effectiveTime>
125-
<center nullFlavor="NI"/>
126-
</effectiveTime>
127-
<availabilityTime value="20030221115000"/>
128-
<specimen typeCode="SPC">
129-
<specimenRole classCode="SPEC">
130-
<id root="test-id-3"/>
131-
<id root="2.16.840.1.113883.2.1.4.5.2" extension="DUMMY"/>
132-
<effectiveTime>
133-
<center value="20030221115000"/>
134-
</effectiveTime>
135-
<specimenSpecimenMaterial determinerCode="INSTANCE" classCode="MAT">
136-
<desc>UNKNOWN</desc>
137-
</specimenSpecimenMaterial>
138-
</specimenRole>
139-
</specimen>
140-
<component typeCode="COMP" contextConductionInd="true">
141-
<NarrativeStatement classCode="OBS" moodCode="EVN">
142-
<id root="test-id-3"/>
143-
<text mediaType="text/x-h7uk-pmip">CommentType:AGGREGATE COMMENT SET
144-
CommentDate:UNK
145-
146-
EMPTY REPORT
147-
</text>
148-
<statusCode code="COMPLETE"/>
149-
<availabilityTime nullFlavor="UNK"/>
150-
</NarrativeStatement>
151-
</component>
152-
</CompoundStatement>
153-
</component>
154118
<component typeCode="COMP" contextConductionInd="true">
155119
<CompoundStatement classCode="CLUSTER" moodCode="EVN">
156120
<id root="test-id-3"/>
@@ -181,12 +145,23 @@
181145

182146
Received Date: 2003-01-09 13:54
183147

184-
EMPTY SPECIMEN
185148
</text>
186149
<statusCode code="COMPLETE"/>
187150
<availabilityTime value="20030221115000"/>
188151
</NarrativeStatement>
189152
</component>
153+
<component typeCode="COMP" contextConductionInd="true">
154+
<NarrativeStatement classCode="OBS" moodCode="EVN">
155+
<id root="test-id-3" />
156+
<text mediaType="text/x-h7uk-pmip">CommentType:AGGREGATE
157+
COMMENT SET
158+
CommentDate:UNK
159+
160+
EMPTY REPORT</text>
161+
<statusCode code="COMPLETE" />
162+
<availabilityTime nullFlavor="UNK" />
163+
</NarrativeStatement>
164+
</component>
190165
</CompoundStatement>
191166
</component>
192167
</CompoundStatement>

0 commit comments

Comments
 (0)