15
15
import java .util .Objects ;
16
16
import java .util .Optional ;
17
17
import java .util .UUID ;
18
+ import java .util .function .Predicate ;
18
19
import java .util .regex .Pattern ;
19
20
import java .util .stream .Collectors ;
20
21
import java .util .stream .Stream ;
33
34
import org .hl7 .fhir .r4 .model .Endpoint ;
34
35
import org .hl7 .fhir .r4 .model .Endpoint .EndpointStatus ;
35
36
import org .hl7 .fhir .r4 .model .Identifier ;
37
+ import org .hl7 .fhir .r4 .model .Organization ;
36
38
import org .hl7 .fhir .r4 .model .Resource ;
37
39
import org .hl7 .fhir .r4 .model .Task ;
38
40
import org .slf4j .Logger ;
@@ -45,11 +47,11 @@ public class SelectPingTargets extends AbstractServiceDelegate implements Initia
45
47
{
46
48
private static final Logger logger = LoggerFactory .getLogger (SelectPingTargets .class );
47
49
48
- private final OrganizationProvider organizationProvider ;
49
-
50
50
private static final Pattern endpointResouceTypes = Pattern .compile (
51
51
"Endpoint|HealthcareService|ImagingStudy|InsurancePlan|Location|Organization|OrganizationAffiliation|PractitionerRole" );
52
52
53
+ private final OrganizationProvider organizationProvider ;
54
+
53
55
public SelectPingTargets (FhirWebserviceClientProvider clientProvider , TaskHelper taskHelper ,
54
56
ReadAccessHelper readAccessHelper , OrganizationProvider organizationProvider )
55
57
{
@@ -74,21 +76,10 @@ public void doExecute(DelegateExecution execution) throws Exception
74
76
execution .getBusinessKey ()));
75
77
updateLeadingTaskInExecutionVariables (execution , task );
76
78
77
- // String localAddress = endpointProvider.getLocalEndpoint().getAddress();
78
- // List<Target> targets = endpointProvider.getDefaultEndpointsByOrganizationIdentifier().entrySet().stream()
79
- // .filter(a -> !localAddress.equals(a.getValue().getAddress()))
80
- // .map(e -> Target.createBiDirectionalTarget(e.getKey(),
81
- // e.getValue().getIdentifier().stream()
82
- // .filter(i -> NAMINGSYSTEM_HIGHMED_ENDPOINT_IDENTIFIER.equals(i.getSystem())).findFirst()
83
- // .map(Identifier::getValue).get(),
84
- // e.getValue().getAddress(), UUID.randomUUID().toString()))
85
- // .collect(Collectors.toList());
86
-
87
79
Stream <Endpoint > targetEndpoints = getTargetEndpointsSearchParameter (execution ).map (this ::searchForEndpoints )
88
- .orElse (allEndpoints ());
80
+ .orElse (allEndpointsNotLocal ());
89
81
90
- Map <String , Identifier > organizationIdentifierByOrganizationId = organizationProvider .getRemoteOrganizations ()
91
- .stream ()
82
+ Map <String , Identifier > organizationIdentifierByOrganizationId = getAllActiveOrganizations ()
92
83
.collect (Collectors .toMap (o -> o .getIdElement ().getIdPart (),
93
84
o -> o .getIdentifier ().stream ()
94
85
.filter (i -> NAMINGSYSTEM_HIGHMED_ORGANIZATION_IDENTIFIER .equals (i .getSystem ()))
@@ -167,9 +158,14 @@ private Optional<Class<? extends Resource>> getResourceType(UriComponents search
167
158
}
168
159
}
169
160
170
- private Stream <Endpoint > allEndpoints ()
161
+ private Stream <Endpoint > allEndpointsNotLocal ()
162
+ {
163
+ return allEndpoints (1 , 0 ).filter (isLocalEndpoint ().negate ());
164
+ }
165
+
166
+ private Predicate <? super Endpoint > isLocalEndpoint ()
171
167
{
172
- return allEndpoints ( 1 , 0 );
168
+ return e -> Objects . equals ( getFhirWebserviceClientProvider (). getLocalBaseUrl (), e . getAddress () );
173
169
}
174
170
175
171
private Stream <Endpoint > allEndpoints (int page , int currentTotal )
@@ -217,4 +213,34 @@ private Optional<String> getEndpointAddress(Endpoint endpoint)
217
213
{
218
214
return endpoint .hasAddress () ? Optional .of (endpoint .getAddress ()) : Optional .empty ();
219
215
}
216
+
217
+ private Stream <Organization > getAllActiveOrganizations ()
218
+ {
219
+ return getActiveOrganizations (1 , 0 );
220
+ }
221
+
222
+ private Stream <Organization > getActiveOrganizations (int page , int currentTotal )
223
+ {
224
+ Map <String , List <String >> queryParameters = new HashMap <String , List <String >>();
225
+ queryParameters .put ("active" , Collections .singletonList ("true" ));
226
+ queryParameters .put ("_page" , Collections .singletonList (String .valueOf (page )));
227
+
228
+ Bundle searchResult = getFhirWebserviceClientProvider ().getLocalWebserviceClient ()
229
+ .searchWithStrictHandling (Organization .class , queryParameters );
230
+
231
+ if (searchResult .getTotal () > currentTotal + searchResult .getEntry ().size ())
232
+ return Stream .concat (toOrganization (searchResult ),
233
+ getActiveOrganizations (page + 1 , currentTotal + searchResult .getEntry ().size ()));
234
+ else
235
+ return toOrganization (searchResult );
236
+ }
237
+
238
+ private Stream <Organization > toOrganization (Bundle searchResult )
239
+ {
240
+ Objects .requireNonNull (searchResult , "searchResult" );
241
+
242
+ return searchResult .getEntry ().stream ().filter (BundleEntryComponent ::hasResource )
243
+ .filter (e -> e .getResource () instanceof Organization ).map (e -> (Organization ) e .getResource ())
244
+ .filter (e -> e .getActive ());
245
+ }
220
246
}
0 commit comments