99import static uk .nhs .adaptors .gp2gp .ehr .mapper .diagnosticreport .ObservationMapper .NARRATIVE_STATEMENT_TEMPLATE ;
1010
1111import java .util .ArrayList ;
12+ import java .util .Collections ;
1213import java .util .List ;
1314import java .util .Optional ;
1415import java .util .stream .Collectors ;
@@ -74,13 +75,13 @@ public class DiagnosticReportMapper {
7475 private final ConfidentialityService confidentialityService ;
7576
7677 public String mapDiagnosticReportToCompoundStatement (DiagnosticReport diagnosticReport ) {
77- List <Observation > initialObservations = fetchObservations (diagnosticReport );
78- List <Specimen > specimens = fetchSpecimens (diagnosticReport , initialObservations );
78+ List <Observation > observations = fetchObservations (diagnosticReport );
79+ List <Specimen > specimens = fetchSpecimens (diagnosticReport , observations );
7980 final IdMapper idMapper = messageContext .getIdMapper ();
80- markObservationsAsProcessed (idMapper , initialObservations );
81+ markObservationsAsProcessed (idMapper , observations );
8182
8283 final List <Observation > processedObservations =
83- assignDummySpecimensToOrphanedTestResults (initialObservations , specimens );
84+ assignDummySpecimensToOrphanedTestResults (observations , specimens );
8485
8586 String mappedSpecimens = specimens .stream ()
8687 .map (specimen -> specimenMapper .mapSpecimenToCompoundStatement (specimen , processedObservations , diagnosticReport ))
@@ -121,11 +122,12 @@ private List<Specimen> fetchSpecimens(DiagnosticReport diagnosticReport, List<Ob
121122
122123 List <Specimen > specimens = new ArrayList <>();
123124
125+ // At least one specimen is required to exist for any DiagnosticReport
124126 if (!diagnosticReport .hasSpecimen () || hasOrphanedTestResults (observations )) {
125- specimens .add (generateDefaultSpecimen (diagnosticReport ));
127+ specimens .add (generateDummySpecimen (diagnosticReport ));
126128 }
127129
128- InputBundle inputBundleHolder = messageContext .getInputBundleHolder ();
130+ var inputBundleHolder = messageContext .getInputBundleHolder ();
129131 List <Specimen > preExistingSpecimens = diagnosticReport .getSpecimen ()
130132 .stream ()
131133 .map (specimenReference -> inputBundleHolder .getResource (specimenReference .getReferenceElement ()))
@@ -140,7 +142,6 @@ private List<Specimen> fetchSpecimens(DiagnosticReport diagnosticReport, List<Ob
140142 }
141143
142144 private boolean hasOrphanedTestResults (List <Observation > observations ) {
143-
144145 return observations
145146 .stream ()
146147 .anyMatch (observation -> !observation .hasSpecimen ());
@@ -153,7 +154,7 @@ private List<Observation> assignDummySpecimensToOrphanedTestResults(
153154 return observations ;
154155 }
155156
156- // The assumption was made that all test results without a specimen will have the same specimen referenced
157+ // The assumption was made that all test results without a specimen will have the same dummy specimen referenced
157158 Specimen dummySpecimen = specimens .stream ()
158159 .filter (specimen -> specimen .getId ().contains (DUMMY_SPECIMEN_ID_PREFIX ))
159160 .toList ().getFirst ();
@@ -169,7 +170,7 @@ private List<Observation> assignDummySpecimensToOrphanedTestResults(
169170 return observations ;
170171 }
171172
172- private Specimen generateDefaultSpecimen (DiagnosticReport diagnosticReport ) {
173+ private Specimen generateDummySpecimen (DiagnosticReport diagnosticReport ) {
173174 Specimen specimen = new Specimen ();
174175
175176 specimen .setId (DUMMY_SPECIMEN_ID_PREFIX + randomIdGeneratorService .createNewId ());
@@ -181,7 +182,11 @@ private Specimen generateDefaultSpecimen(DiagnosticReport diagnosticReport) {
181182 }
182183
183184 private List <Observation > fetchObservations (DiagnosticReport diagnosticReport ) {
184- InputBundle inputBundleHolder = messageContext .getInputBundleHolder ();
185+ if (!diagnosticReport .hasResult ()) {
186+ return Collections .singletonList (generateDefaultObservation (diagnosticReport ));
187+ }
188+
189+ var inputBundleHolder = messageContext .getInputBundleHolder ();
185190 return diagnosticReport .getResult ().stream ()
186191 .map (Reference ::getReferenceElement )
187192 .map (inputBundleHolder ::getResource )
0 commit comments