|
| 1 | +package dev.dsf.bpe.v2.service; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Arrays; |
| 5 | +import java.util.HashMap; |
| 6 | +import java.util.HashSet; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Objects; |
| 10 | +import java.util.Set; |
| 11 | +import java.util.UUID; |
| 12 | +import java.util.stream.Stream; |
| 13 | + |
| 14 | +import org.hl7.fhir.r4.model.Bundle.BundleEntryComponent; |
| 15 | +import org.hl7.fhir.r4.model.Bundle.SearchEntryMode; |
| 16 | +import org.hl7.fhir.r4.model.Coding; |
| 17 | +import org.hl7.fhir.r4.model.Endpoint; |
| 18 | +import org.hl7.fhir.r4.model.Identifier; |
| 19 | +import org.hl7.fhir.r4.model.Organization; |
| 20 | +import org.hl7.fhir.r4.model.OrganizationAffiliation; |
| 21 | + |
| 22 | +import dev.dsf.bpe.v2.constants.NamingSystems.EndpointIdentifier; |
| 23 | +import dev.dsf.bpe.v2.constants.NamingSystems.OrganizationIdentifier; |
| 24 | +import dev.dsf.bpe.v2.variables.Target; |
| 25 | +import dev.dsf.bpe.v2.variables.TargetImpl; |
| 26 | +import dev.dsf.bpe.v2.variables.Targets; |
| 27 | +import dev.dsf.bpe.v2.variables.TargetsImpl; |
| 28 | + |
| 29 | +public class TargetProviderImpl extends AbstractResourceProvider implements TargetProvider |
| 30 | +{ |
| 31 | + public static class BuilderImpl implements Builder |
| 32 | + { |
| 33 | + private static final record OrganizationAffiliationAndOrganizationAndEndpoint( |
| 34 | + OrganizationAffiliation affiliation, Organization member, Endpoint endpoint) |
| 35 | + { |
| 36 | + } |
| 37 | + |
| 38 | + private final List<OrganizationAffiliation> affiliations = new ArrayList<>(); |
| 39 | + private final Map<String, Organization> organizationsById = new HashMap<>(); |
| 40 | + private final Map<String, Endpoint> endpointsById = new HashMap<>(); |
| 41 | + |
| 42 | + private final Set<String> memberOrganizationIdentifiers = new HashSet<>(); |
| 43 | + |
| 44 | + public BuilderImpl(Identifier[] memberOrganizationIdentifiers) |
| 45 | + { |
| 46 | + if (memberOrganizationIdentifiers != null) |
| 47 | + this.memberOrganizationIdentifiers.addAll(Arrays.stream(memberOrganizationIdentifiers) |
| 48 | + .filter(Objects::nonNull).map(this::identifierToString).toList()); |
| 49 | + } |
| 50 | + |
| 51 | + private String identifierToString(Identifier i) |
| 52 | + { |
| 53 | + return i.getSystem() + "|" + i.getValue(); |
| 54 | + } |
| 55 | + |
| 56 | + private Predicate filter; |
| 57 | + |
| 58 | + protected Target createTarget(String organizationIdentifierValue, String endpointIdentifierValue, |
| 59 | + String endpointAddress, String correlationKey) |
| 60 | + { |
| 61 | + Objects.requireNonNull(organizationIdentifierValue, "organizationIdentifierValue"); |
| 62 | + Objects.requireNonNull(endpointIdentifierValue, "endpointIdentifierValue"); |
| 63 | + Objects.requireNonNull(endpointAddress, "endpointAddress"); |
| 64 | + |
| 65 | + return new TargetImpl(organizationIdentifierValue, endpointIdentifierValue, endpointAddress, |
| 66 | + correlationKey); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public Targets withCorrelationKey() |
| 71 | + { |
| 72 | + return toTargets(true); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Targets withoutCorrelationKey() |
| 77 | + { |
| 78 | + return toTargets(false); |
| 79 | + } |
| 80 | + |
| 81 | + private Targets toTargets(boolean withCorrelationKey) |
| 82 | + { |
| 83 | + List<TargetImpl> targets = affiliations.stream() |
| 84 | + .filter(a -> organizationsById |
| 85 | + .containsKey(a.getParticipatingOrganization().getReferenceElement().getIdPart()) |
| 86 | + && endpointsById.containsKey(a.getEndpointFirstRep().getReferenceElement().getIdPart())) |
| 87 | + .map(a -> new OrganizationAffiliationAndOrganizationAndEndpoint(a, |
| 88 | + organizationsById.get(a.getParticipatingOrganization().getReferenceElement().getIdPart()), |
| 89 | + endpointsById.get(a.getEndpointFirstRep().getReferenceElement().getIdPart()))) |
| 90 | + .filter(a -> OrganizationIdentifier.hasIdentifier(a.member) |
| 91 | + && EndpointIdentifier.hasIdentifier(a.endpoint) && a.endpoint.hasAddressElement() |
| 92 | + && a.endpoint.getAddressElement().hasValue()) |
| 93 | + .filter(a -> memberOrganizationIdentifiers.isEmpty() ? true |
| 94 | + : memberOrganizationIdentifiers.contains( |
| 95 | + OrganizationIdentifier.findFirst(a.member).map(this::identifierToString).get())) |
| 96 | + .filter(a -> filter == null ? true : filter.test(a.affiliation, a.member, a.endpoint)).map(a -> |
| 97 | + { |
| 98 | + String organizationIdentifierValue = OrganizationIdentifier.findFirst(a.member) |
| 99 | + .map(Identifier::getValue).get(); |
| 100 | + String endpointIdentifierValue = EndpointIdentifier.findFirst(a.endpoint) |
| 101 | + .map(Identifier::getValue).get(); |
| 102 | + |
| 103 | + return new TargetImpl(organizationIdentifierValue, endpointIdentifierValue, |
| 104 | + a.endpoint.getAddress(), withCorrelationKey ? UUID.randomUUID().toString() : null); |
| 105 | + }).toList(); |
| 106 | + |
| 107 | + return new TargetsImpl(targets); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Builder filter(Predicate filter) |
| 112 | + { |
| 113 | + this.filter = filter; |
| 114 | + |
| 115 | + return this; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + public TargetProviderImpl(DsfClientProvider clientProvider, String localEndpointAddress) |
| 120 | + { |
| 121 | + super(clientProvider, localEndpointAddress); |
| 122 | + } |
| 123 | + |
| 124 | + protected BuilderImpl createBuilder(Identifier... memberOrganizationIdentifier) |
| 125 | + { |
| 126 | + return new BuilderImpl(memberOrganizationIdentifier); |
| 127 | + } |
| 128 | + |
| 129 | + private Builder toBuilder(Stream<BundleEntryComponent> entries, Identifier... memberOrganizationIdentifier) |
| 130 | + { |
| 131 | + BuilderImpl builder = createBuilder(memberOrganizationIdentifier); |
| 132 | + |
| 133 | + entries.forEach(c -> |
| 134 | + { |
| 135 | + SearchEntryMode mode = c.getSearch().getMode(); |
| 136 | + |
| 137 | + if (SearchEntryMode.MATCH.equals(mode) && c.getResource() instanceof OrganizationAffiliation a) |
| 138 | + builder.affiliations.add(a); |
| 139 | + |
| 140 | + else if (SearchEntryMode.INCLUDE.equals(mode)) |
| 141 | + { |
| 142 | + if (c.getResource() instanceof Organization o) |
| 143 | + builder.organizationsById.put(o.getIdElement().getIdPart(), o); |
| 144 | + else if (c.getResource() instanceof Endpoint e) |
| 145 | + builder.endpointsById.put(e.getIdElement().getIdPart(), e); |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + return builder; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public Builder create(Identifier parentOrganizationIdentifier) |
| 154 | + { |
| 155 | + Objects.requireNonNull(parentOrganizationIdentifier, "parentOrganizationIdentifier"); |
| 156 | + |
| 157 | + Stream<BundleEntryComponent> entries = search(OrganizationAffiliation.class, |
| 158 | + Map.of("active", List.of("true"), "primary-organization:identifier", |
| 159 | + List.of(toSearchParameter(parentOrganizationIdentifier)), "_include", |
| 160 | + List.of("OrganizationAffiliation:endpoint:Endpoint", |
| 161 | + "OrganizationAffiliation:participating-organization:Organization"))); |
| 162 | + |
| 163 | + return toBuilder(entries); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Builder create(Identifier parentOrganizationIdentifier, Coding memberOrganizationRole) |
| 168 | + { |
| 169 | + Objects.requireNonNull(parentOrganizationIdentifier, "parentOrganizationIdentifier"); |
| 170 | + Objects.requireNonNull(memberOrganizationRole, "memberOrganizationRole"); |
| 171 | + |
| 172 | + Stream<BundleEntryComponent> entries = search(OrganizationAffiliation.class, |
| 173 | + Map.of("active", List.of("true"), "primary-organization:identifier", |
| 174 | + List.of(toSearchParameter(parentOrganizationIdentifier)), "_include", |
| 175 | + List.of("OrganizationAffiliation:endpoint:Endpoint", |
| 176 | + "OrganizationAffiliation:participating-organization:Organization"), |
| 177 | + "role", List.of(toSearchParameter(memberOrganizationRole)))); |
| 178 | + |
| 179 | + return toBuilder(entries); |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public Builder create(Identifier parentOrganizationIdentifier, Coding memberOrganizationRole, |
| 184 | + Identifier... memberOrganizationIdentifier) |
| 185 | + { |
| 186 | + Objects.requireNonNull(parentOrganizationIdentifier, "parentOrganizationIdentifier"); |
| 187 | + Objects.requireNonNull(memberOrganizationRole, "memberOrganizationRole"); |
| 188 | + Objects.requireNonNull(memberOrganizationIdentifier, "memberOrganizationIdentifier"); |
| 189 | + |
| 190 | + Stream<BundleEntryComponent> entries = search(OrganizationAffiliation.class, |
| 191 | + Map.of("active", List.of("true"), "primary-organization:identifier", |
| 192 | + List.of(toSearchParameter(parentOrganizationIdentifier)), "_include", |
| 193 | + List.of("OrganizationAffiliation:endpoint:Endpoint", |
| 194 | + "OrganizationAffiliation:participating-organization:Organization"), |
| 195 | + "role", List.of(toSearchParameter(memberOrganizationRole)))); |
| 196 | + |
| 197 | + return toBuilder(entries, memberOrganizationIdentifier); |
| 198 | + } |
| 199 | +} |
0 commit comments