33import java .util .List ;
44import java .util .Map ;
55import java .util .Objects ;
6+ import java .util .Optional ;
67import java .util .Set ;
78import java .util .function .BiConsumer ;
89import java .util .stream .Collectors ;
910
1011import org .hl7 .fhir .r4 .model .Bundle ;
1112import org .hl7 .fhir .r4 .model .CodeSystem ;
13+ import org .hl7 .fhir .r4 .model .Coding ;
14+ import org .hl7 .fhir .r4 .model .DecimalType ;
1215import org .hl7 .fhir .r4 .model .MetadataResource ;
1316import org .hl7 .fhir .r4 .model .Resource ;
17+ import org .hl7 .fhir .r4 .model .StringType ;
1418import org .hl7 .fhir .r4 .model .StructureDefinition ;
19+ import org .hl7 .fhir .r4 .model .Task ;
1520import org .springframework .beans .factory .InitializingBean ;
1621
1722import dev .dsf .bpe .ConstantsPing ;
1823import dev .dsf .bpe .PingProcessPluginDefinition ;
1924import dev .dsf .bpe .v1 .ProcessPluginApi ;
2025import dev .dsf .bpe .v1 .ProcessPluginDeploymentStateListener ;
26+ import dev .dsf .fhir .client .FhirWebserviceClient ;
2127
2228public class PingPongProcessPluginDeploymentStateListener
2329 implements ProcessPluginDeploymentStateListener , InitializingBean
@@ -52,6 +58,61 @@ public void onProcessesDeployed(List<String> activeProcesses)
5258
5359 updateOlderResourcesIfCurrentIsNewestResource (ConstantsPing .STRUCTURE_DEFINITION_URL_EXTENSION_PING_STATUS ,
5460 StructureDefinition .class , adaptExtensionStructureDefinitions ());
61+
62+ updateDraftTaskResources ();
63+ }
64+
65+ private void updateDraftTaskResources ()
66+ {
67+ FhirWebserviceClient client = api .getFhirWebserviceClientProvider ().getLocalWebserviceClient ();
68+
69+ List <String > draftTaskResourceProfiles = List .of ("http://dsf.dev/fhir/StructureDefinition/task-start-ping" ,
70+ "http://dsf.dev/fhir/StructureDefinition/task-start-ping-autostart" );
71+
72+ for (String profile : draftTaskResourceProfiles )
73+ {
74+ Optional <Task > optionalTask = searchTask (profile , PingProcessPluginDefinition .RESOURCE_VERSION ).getEntry ()
75+ .stream ().map (Bundle .BundleEntryComponent ::getResource ).map (Task .class ::cast ).findFirst ();
76+
77+ if (optionalTask .isPresent ())
78+ {
79+ Task toUpdate = optionalTask .get ();
80+ adaptDraftTask (toUpdate );
81+ client .update (toUpdate );
82+ }
83+ }
84+ }
85+
86+ private void adaptDraftTask (Task task )
87+ {
88+ Coding downloadResourceSizeBytesCoding = new Coding ();
89+ downloadResourceSizeBytesCoding .setSystem (dev .dsf .bpe .CodeSystem .DsfPing .URL )
90+ .setCode (dev .dsf .bpe .CodeSystem .DsfPing .Code .DOWNLOAD_RESOURCE_SIZE_BYTES .getValue ())
91+ .setVersion (PingProcessPluginDefinition .RESOURCE_VERSION );
92+
93+ Optional <Task .ParameterComponent > optInput = api .getTaskHelper ().getFirstInputParameter (task ,
94+ downloadResourceSizeBytesCoding , DecimalType .class );
95+ if (optInput .isEmpty ())
96+ {
97+ Task .ParameterComponent downloadResourceSizeBytes = new Task .ParameterComponent ();
98+ downloadResourceSizeBytes .getType ().addCoding (downloadResourceSizeBytesCoding );
99+ downloadResourceSizeBytes .setValue (new DecimalType (ConstantsPing .DOWNLOAD_RESOURCE_SIZE_BYTES_DEFAULT ));
100+ task .addInput (downloadResourceSizeBytes );
101+ }
102+
103+ Coding pongTimeoutDurationCoding = new Coding ();
104+ pongTimeoutDurationCoding .setSystem (dev .dsf .bpe .CodeSystem .DsfPing .URL )
105+ .setCode (dev .dsf .bpe .CodeSystem .DsfPing .Code .PONG_TIMEOUT_DURATION_ISO_8601 .getValue ())
106+ .setVersion (PingProcessPluginDefinition .RESOURCE_VERSION );
107+
108+ optInput = api .getTaskHelper ().getFirstInputParameter (task , pongTimeoutDurationCoding , StringType .class );
109+ if (optInput .isEmpty ())
110+ {
111+ Task .ParameterComponent pongTimeoutDuration = new Task .ParameterComponent ();
112+ pongTimeoutDuration .getType ().addCoding (pongTimeoutDurationCoding );
113+ pongTimeoutDuration .setValue (new StringType (ConstantsPing .PONG_TIMEOUT_DURATION_DEFAULT_VALUE ));
114+ task .addInput (pongTimeoutDuration );
115+ }
55116 }
56117
57118 private <T extends MetadataResource > void updateOlderResourcesIfCurrentIsNewestResource (String url , Class <T > type ,
@@ -74,6 +135,12 @@ private Bundle search(Class<? extends Resource> type, String url)
74135 Map .of ("url" , List .of (url )));
75136 }
76137
138+ private Bundle searchTask (String profile , String version )
139+ {
140+ return api .getFhirWebserviceClientProvider ().getLocalWebserviceClient ().search (Task .class ,
141+ Map .of ("_profile" , List .of (profile + "|" + version ), "status" , List .of ("draft" )));
142+ }
143+
77144 private <T extends MetadataResource > List <T > extractAndSortResources (Bundle bundle , Class <T > type , String url )
78145 {
79146 return bundle .getEntry ().stream ().filter (Bundle .BundleEntryComponent ::hasResource )
@@ -104,6 +171,11 @@ private boolean currentIsNewestResource(List<? extends MetadataResource> resourc
104171 .equals (resources .get (resources .size () - 1 ).getVersion ());
105172 }
106173
174+ private <T > Optional <T > getNewestResource (List <T > resources )
175+ {
176+ return resources .isEmpty () ? Optional .empty () : Optional .of (resources .get (resources .size () - 1 ));
177+ }
178+
107179 private MinorMajorVersion getMajorMinorVersion (String version )
108180 {
109181 if (version .matches ("\\ d\\ .\\ d" ))
0 commit comments