@@ -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 )
0 commit comments