|
30 | 30 | import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; |
31 | 31 | import org.hl7.fhir.r4.model.Bundle.BundleType; |
32 | 32 | import org.hl7.fhir.r4.model.Bundle.HTTPVerb; |
| 33 | +import org.hl7.fhir.r4.model.Bundle.SearchEntryMode; |
33 | 34 | import org.hl7.fhir.r4.model.DomainResource; |
34 | 35 | import org.hl7.fhir.r4.model.IdType; |
35 | 36 | import org.hl7.fhir.r4.model.Identifier; |
@@ -59,6 +60,54 @@ public abstract class AbstractFhirClient implements GeccoFhirClient |
59 | 60 | private static final Logger logger = LoggerFactory.getLogger(AbstractFhirClient.class); |
60 | 61 | private static final OutcomeLogger outcomeLogger = new OutcomeLogger(logger); |
61 | 62 |
|
| 63 | + private static final class DomainResourceUniqueByUnqualifiedVersionlessId |
| 64 | + { |
| 65 | + private final DomainResource resource; |
| 66 | + private final String unqualifiedVersionlessIdValue; |
| 67 | + |
| 68 | + public DomainResourceUniqueByUnqualifiedVersionlessId(DomainResource resource) |
| 69 | + { |
| 70 | + this.resource = resource; |
| 71 | + |
| 72 | + unqualifiedVersionlessIdValue = resource.getIdElement().toUnqualifiedVersionless().getValue(); |
| 73 | + } |
| 74 | + |
| 75 | + public DomainResource getResource() |
| 76 | + { |
| 77 | + return resource; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public int hashCode() |
| 82 | + { |
| 83 | + final int prime = 31; |
| 84 | + int result = 1; |
| 85 | + result = prime * result |
| 86 | + + ((unqualifiedVersionlessIdValue == null) ? 0 : unqualifiedVersionlessIdValue.hashCode()); |
| 87 | + return result; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public boolean equals(Object obj) |
| 92 | + { |
| 93 | + if (this == obj) |
| 94 | + return true; |
| 95 | + if (obj == null) |
| 96 | + return false; |
| 97 | + if (getClass() != obj.getClass()) |
| 98 | + return false; |
| 99 | + DomainResourceUniqueByUnqualifiedVersionlessId other = (DomainResourceUniqueByUnqualifiedVersionlessId) obj; |
| 100 | + if (unqualifiedVersionlessIdValue == null) |
| 101 | + { |
| 102 | + if (other.unqualifiedVersionlessIdValue != null) |
| 103 | + return false; |
| 104 | + } |
| 105 | + else if (!unqualifiedVersionlessIdValue.equals(other.unqualifiedVersionlessIdValue)) |
| 106 | + return false; |
| 107 | + return true; |
| 108 | + } |
| 109 | + } |
| 110 | + |
62 | 111 | private static final List<String> RESOURCES_WITH_PATIENT_REF = Arrays.asList("AllergyIntolerance", "CarePlan", |
63 | 112 | "CareTeam", "ClinicalImpression", "Composition", "Condition", "Consent", "DetectedIssue", "DeviceRequest", |
64 | 113 | "DeviceUseStatement", "DiagnosticReport", "DocumentManifest", "DocumentReference", "Encounter", |
@@ -539,9 +588,16 @@ protected Stream<DomainResource> getDomainResources(Bundle bundle) |
539 | 588 |
|
540 | 589 | private Stream<DomainResource> getDomainResourcesFromBundle(Bundle bundle) |
541 | 590 | { |
542 | | - return bundle.getEntry().stream().filter(BundleEntryComponent::hasResource) |
543 | | - .map(BundleEntryComponent::getResource).filter(r -> r instanceof DomainResource) |
544 | | - .map(r -> (DomainResource) r); |
| 591 | + // includes first |
| 592 | + return Stream.concat( |
| 593 | + bundle.getEntry().stream().filter(BundleEntryComponent::hasResource) |
| 594 | + .filter(e -> e.hasSearch() && SearchEntryMode.INCLUDE.equals(e.getSearch().getMode())) |
| 595 | + .map(BundleEntryComponent::getResource).filter(r -> r instanceof DomainResource) |
| 596 | + .map(r -> (DomainResource) r), |
| 597 | + bundle.getEntry().stream().filter(BundleEntryComponent::hasResource) |
| 598 | + .filter(e -> e.hasSearch() && SearchEntryMode.MATCH.equals(e.getSearch().getMode())) |
| 599 | + .map(BundleEntryComponent::getResource).filter(r -> r instanceof DomainResource) |
| 600 | + .map(r -> (DomainResource) r)); |
545 | 601 | } |
546 | 602 |
|
547 | 603 | private Stream<DomainResource> doGetDomainResources(String nextUrl, int subTotal) |
@@ -624,4 +680,10 @@ public void updatePatient(Patient patient) |
624 | 680 | throw e; |
625 | 681 | } |
626 | 682 | } |
| 683 | + |
| 684 | + protected Stream<DomainResource> distinctById(Stream<DomainResource> resources) |
| 685 | + { |
| 686 | + return resources.map(DomainResourceUniqueByUnqualifiedVersionlessId::new).distinct() |
| 687 | + .map(DomainResourceUniqueByUnqualifiedVersionlessId::getResource); |
| 688 | + } |
627 | 689 | } |
0 commit comments