11package dev .dsf .bpe .listener ;
22
3+ import static dev .dsf .bpe .PingProcessPluginDefinition .RESOURCE_VERSION ;
4+
35import java .util .List ;
46import java .util .Map ;
57import java .util .Objects ;
8+ import java .util .Optional ;
69import java .util .Set ;
710import java .util .function .BiConsumer ;
811import java .util .stream .Collectors ;
912
1013import org .hl7 .fhir .r4 .model .Bundle ;
1114import org .hl7 .fhir .r4 .model .CodeSystem ;
15+ import org .hl7 .fhir .r4 .model .Coding ;
16+ import org .hl7 .fhir .r4 .model .DecimalType ;
1217import org .hl7 .fhir .r4 .model .MetadataResource ;
1318import org .hl7 .fhir .r4 .model .Resource ;
19+ import org .hl7 .fhir .r4 .model .StringType ;
1420import org .hl7 .fhir .r4 .model .StructureDefinition ;
21+ import org .hl7 .fhir .r4 .model .Task ;
1522import org .springframework .beans .factory .InitializingBean ;
1623
1724import dev .dsf .bpe .ConstantsPing ;
18- import dev .dsf .bpe .PingProcessPluginDefinition ;
1925import dev .dsf .bpe .v1 .ProcessPluginApi ;
2026import dev .dsf .bpe .v1 .ProcessPluginDeploymentStateListener ;
27+ import dev .dsf .fhir .client .FhirWebserviceClient ;
2128
2229public class PingPongProcessPluginDeploymentStateListener
2330 implements ProcessPluginDeploymentStateListener , InitializingBean
@@ -52,6 +59,62 @@ public void onProcessesDeployed(List<String> activeProcesses)
5259
5360 updateOlderResourcesIfCurrentIsNewestResource (ConstantsPing .STRUCTURE_DEFINITION_URL_EXTENSION_PING_STATUS ,
5461 StructureDefinition .class , adaptExtensionStructureDefinitions ());
62+
63+ updateDraftTaskResources ();
64+ }
65+
66+ private void updateDraftTaskResources ()
67+ {
68+ FhirWebserviceClient client = api .getFhirWebserviceClientProvider ().getLocalWebserviceClient ();
69+
70+ String pingProcessPrefix = "http://dsf.dev/bpe/Process/ping/" + RESOURCE_VERSION ;
71+ List <String > draftTaskResourceIdentifiers = List .of (pingProcessPrefix + "/task-start-ping" ,
72+ pingProcessPrefix + "task-start-ping-autostart" );
73+
74+ for (String identifier : draftTaskResourceIdentifiers )
75+ {
76+ Optional <Task > optionalTask = searchTask (identifier ).getEntry ().stream ()
77+ .map (Bundle .BundleEntryComponent ::getResource ).map (Task .class ::cast ).findFirst ();
78+
79+ if (optionalTask .isPresent ())
80+ {
81+ Task toUpdate = optionalTask .get ();
82+ adaptDraftTask (toUpdate );
83+ client .update (toUpdate );
84+ }
85+ }
86+ }
87+
88+ private void adaptDraftTask (Task task )
89+ {
90+ Coding downloadResourceSizeBytesCoding = new Coding ();
91+ downloadResourceSizeBytesCoding .setSystem (dev .dsf .bpe .CodeSystem .DsfPing .URL )
92+ .setCode (dev .dsf .bpe .CodeSystem .DsfPing .Code .DOWNLOAD_RESOURCE_SIZE_BYTES .getValue ())
93+ .setVersion (RESOURCE_VERSION );
94+
95+ Optional <Task .ParameterComponent > optInput = api .getTaskHelper ().getFirstInputParameter (task ,
96+ downloadResourceSizeBytesCoding , DecimalType .class );
97+ if (optInput .isEmpty ())
98+ {
99+ Task .ParameterComponent downloadResourceSizeBytes = new Task .ParameterComponent ();
100+ downloadResourceSizeBytes .getType ().addCoding (downloadResourceSizeBytesCoding );
101+ downloadResourceSizeBytes .setValue (new DecimalType (ConstantsPing .DOWNLOAD_RESOURCE_SIZE_BYTES_DEFAULT ));
102+ task .addInput (downloadResourceSizeBytes );
103+ }
104+
105+ Coding pongTimeoutDurationCoding = new Coding ();
106+ pongTimeoutDurationCoding .setSystem (dev .dsf .bpe .CodeSystem .DsfPing .URL )
107+ .setCode (dev .dsf .bpe .CodeSystem .DsfPing .Code .PONG_TIMEOUT_DURATION_ISO_8601 .getValue ())
108+ .setVersion (RESOURCE_VERSION );
109+
110+ optInput = api .getTaskHelper ().getFirstInputParameter (task , pongTimeoutDurationCoding , StringType .class );
111+ if (optInput .isEmpty ())
112+ {
113+ Task .ParameterComponent pongTimeoutDuration = new Task .ParameterComponent ();
114+ pongTimeoutDuration .getType ().addCoding (pongTimeoutDurationCoding );
115+ pongTimeoutDuration .setValue (new StringType (ConstantsPing .PONG_TIMEOUT_DURATION_DEFAULT_VALUE ));
116+ task .addInput (pongTimeoutDuration );
117+ }
55118 }
56119
57120 private <T extends MetadataResource > void updateOlderResourcesIfCurrentIsNewestResource (String url , Class <T > type ,
@@ -74,6 +137,12 @@ private Bundle search(Class<? extends Resource> type, String url)
74137 Map .of ("url" , List .of (url )));
75138 }
76139
140+ private Bundle searchTask (String identifier )
141+ {
142+ return api .getFhirWebserviceClientProvider ().getLocalWebserviceClient ().search (Task .class ,
143+ Map .of ("identifier" , List .of (identifier ), "status" , List .of ("draft" )));
144+ }
145+
77146 private <T extends MetadataResource > List <T > extractAndSortResources (Bundle bundle , Class <T > type , String url )
78147 {
79148 return bundle .getEntry ().stream ().filter (Bundle .BundleEntryComponent ::hasResource )
@@ -100,8 +169,7 @@ else if (version1.major < version2.major)
100169
101170 private boolean currentIsNewestResource (List <? extends MetadataResource > resources )
102171 {
103- return !resources .isEmpty () && PingProcessPluginDefinition .RESOURCE_VERSION
104- .equals (resources .get (resources .size () - 1 ).getVersion ());
172+ return !resources .isEmpty () && RESOURCE_VERSION .equals (resources .get (resources .size () - 1 ).getVersion ());
105173 }
106174
107175 private MinorMajorVersion getMajorMinorVersion (String version )
0 commit comments