4444import org .hl7 .fhir .r4 .model .Procedure ;
4545import org .hl7 .fhir .r4 .model .Reference ;
4646import org .hl7 .fhir .r4 .model .Resource ;
47+ import org .hl7 .fhir .r4 .model .ResourceType ;
4748import org .hl7 .fhir .r4 .model .Task ;
4849import org .hl7 .fhir .r4 .model .Type ;
4950import org .slf4j .Logger ;
@@ -237,19 +238,33 @@ else if (resource instanceof Procedure)
237238
238239 protected String getConditionalUpdateUrl (String pseudonym , DomainResource resource )
239240 {
241+ String patientIdentifier = ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|" + pseudonym ;
242+
240243 if (resource instanceof Patient )
241244 {
242- return "Patient?identifier=" + ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|"
243- + pseudonym ;
245+ return "Patient?identifier=" + patientIdentifier ;
244246 }
245247 else if (resource instanceof Condition )
246248 {
247249 Condition c = (Condition ) resource ;
248250 String profileUrl = getProfileUrl (resource , v -> v .startsWith (NUM_CODEX_STRUCTURE_DEFINITION_PREFIX ));
249251
250- return "Condition?_profile=" + profileUrl + "&recorded-date="
251- + c .getRecordedDateElement ().getValueAsString () + "&patient:identifier="
252- + ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|" + pseudonym ;
252+ String updateUrl = getInitialConditionalUpdateUrl (ResourceType .Condition .name (), profileUrl ,
253+ patientIdentifier );
254+
255+ if (c .hasRecordedDateElement () && c .getRecordedDateElement ().getValueAsString () != null )
256+ updateUrl = updateUrl + "&recorded-date=" + c .getRecordedDateElement ().getValueAsString ();
257+
258+ if (c .hasCategory ())
259+ updateUrl = updateUrl + "&category=" + getCodingUpdateUrl (c .getCategoryFirstRep ().getCodingFirstRep ());
260+
261+ if (c .hasCode ())
262+ updateUrl = updateUrl + "&code=" + getCodingUpdateUrl (c .getCode ().getCodingFirstRep ());
263+
264+ if (c .hasBodySite ())
265+ updateUrl = updateUrl + "&body-site=" + getCodingUpdateUrl (c .getBodySiteFirstRep ().getCodingFirstRep ());
266+
267+ return updateUrl ;
253268 }
254269 else if (resource instanceof Consent )
255270 {
@@ -258,21 +273,20 @@ else if (resource instanceof Consent)
258273
259274 if (NUM_CODEX_DO_NOT_RESUSCITAT_ORDER .equals (profileUrl ))
260275 {
261- boolean scopePresent = c .getScope ().getCoding ().stream ().filter (co -> co . hasSystem () )
276+ boolean scopePresent = c .getScope ().getCoding ().stream ().filter (Coding :: hasSystem )
262277 .filter (co -> "http://terminology.hl7.org/CodeSystem/consentscope" .equals (co .getSystem ()))
263- .filter (co -> co . hasCode ()). filter (co -> "adr" .equals (co .getCode ())). findAny (). isPresent ( );
278+ .filter (Coding :: hasCode ). anyMatch (co -> "adr" .equals (co .getCode ()));
264279 boolean categoryPresent = c .getCategory ().stream ().flatMap (coc -> coc .getCoding ().stream ())
265- .filter (co -> co . hasSystem () )
280+ .filter (Coding :: hasSystem )
266281 .filter (co -> "http://terminology.hl7.org/CodeSystem/consentcategorycodes"
267282 .equals (co .getSystem ()))
268- .filter (co -> co . hasCode ()). filter (co -> "dnr" .equals (co .getCode ())). findAny (). isPresent ( );
283+ .filter (Coding :: hasCode ). anyMatch (co -> "dnr" .equals (co .getCode ()));
269284
270285 if (scopePresent && categoryPresent )
271- return " Consent?_profile=" + profileUrl
286+ return getInitialConditionalUpdateUrl ( ResourceType . Consent . name (), profileUrl , patientIdentifier )
272287 + "&scope=http://terminology.hl7.org/CodeSystem/consentscope|adr"
273- + "&category=http://terminology.hl7.org/CodeSystem/consentcategorycodes|dnr"
274- + "&patient:identifier=" + ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|"
275- + pseudonym ;
288+ + "&category=http://terminology.hl7.org/CodeSystem/consentcategorycodes|dnr" ;
289+
276290 else
277291 throw new RuntimeException ("Resource of type Consent with profile " + profileUrl
278292 + " is missing scope: http://terminology.hl7.org/CodeSystem/consentscope|adr and/or category: http://terminology.hl7.org/CodeSystem/consentcategorycodes|dnr" );
@@ -285,61 +299,99 @@ else if (resource instanceof DiagnosticReport)
285299 DiagnosticReport dr = (DiagnosticReport ) resource ;
286300 String profileUrl = getProfileUrl (resource , v -> v .startsWith (NUM_CODEX_STRUCTURE_DEFINITION_PREFIX ));
287301
288- return "DiagnosticReport?_profile=" + profileUrl + "&date="
289- + dr .getEffectiveDateTimeType ().getValueAsString () + "&patient:identifier="
290- + ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|" + pseudonym ;
302+ String updateUrl = getInitialConditionalUpdateUrl (ResourceType .DiagnosticReport .name (), profileUrl ,
303+ patientIdentifier );
304+
305+ if (dr .hasEffectiveDateTimeType () && dr .getEffectiveDateTimeType ().getValueAsString () != null )
306+ updateUrl = updateUrl + "&date=" + dr .getEffectiveDateTimeType ().getValueAsString ();
307+
308+ if (dr .hasCategory ())
309+ updateUrl = updateUrl + "&category=" + getCodingUpdateUrl (dr .getCategoryFirstRep ().getCodingFirstRep ());
310+
311+ if (dr .hasCode ())
312+ updateUrl = updateUrl + "&code=" + getCodingUpdateUrl (dr .getCode ().getCodingFirstRep ());
313+
314+ return updateUrl ;
291315 }
292316 else if (resource instanceof Immunization )
293317 {
294318 Immunization i = (Immunization ) resource ;
295319 String profileUrl = getProfileUrl (resource , v -> v .startsWith (NUM_CODEX_STRUCTURE_DEFINITION_PREFIX ));
296320
297- return "Immunization?_profile=" + profileUrl + "&date=" + i .getOccurrenceDateTimeType ().getValueAsString ()
298- + "&patient:identifier=" + ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|"
299- + pseudonym ;
321+ String updateUrl = getInitialConditionalUpdateUrl (ResourceType .Immunization .name (), profileUrl ,
322+ patientIdentifier );
323+
324+ if (i .hasOccurrenceDateTimeType () && i .getOccurrenceDateTimeType ().getValueAsString () != null )
325+ updateUrl = updateUrl + "&date=" + i .getOccurrenceDateTimeType ().getValueAsString ();
326+ else if (i .hasStatus ())
327+ updateUrl = updateUrl + "&status=" + i .getStatus ().toCode ();
328+
329+ if (i .hasVaccineCode ())
330+ updateUrl = updateUrl + "&vaccine-code=" + i .getVaccineCode ().getCodingFirstRep ();
331+
332+ return updateUrl ;
300333 }
301334 else if (resource instanceof MedicationStatement )
302335 {
303336 MedicationStatement ms = (MedicationStatement ) resource ;
304337 String profileUrl = getProfileUrl (resource , v -> v .startsWith (NUM_CODEX_STRUCTURE_DEFINITION_PREFIX ));
305338
306- return "MedicationStatement?_profile=" + profileUrl + "&effective="
307- + ms .getEffectiveDateTimeType ().getValueAsString () + "&patient:identifier="
308- + ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|" + pseudonym ;
309- }
339+ String updateUrl = getInitialConditionalUpdateUrl (ResourceType .MedicationStatement .name (), profileUrl ,
340+ patientIdentifier );
310341
342+ if (ms .hasEffectiveDateTimeType () && ms .getEffectiveDateTimeType ().getValueAsString () != null )
343+ updateUrl = updateUrl + "&effective=" + ms .getEffectiveDateTimeType ();
344+
345+ if (!ms .hasEffectiveDateTimeType () && ms .hasStatus ())
346+ updateUrl = updateUrl + "&status=" + ms .getStatus ().toCode ();
347+
348+ if (ms .hasMedicationCodeableConcept ())
349+ updateUrl = updateUrl + "&code="
350+ + getCodingUpdateUrl (ms .getMedicationCodeableConcept ().getCodingFirstRep ());
351+
352+ return updateUrl ;
353+ }
311354 else if (resource instanceof Observation )
312355 {
313356 Observation o = (Observation ) resource ;
314357 String profileUrl = getProfileUrl (resource , v -> v .startsWith (NUM_CODEX_STRUCTURE_DEFINITION_PREFIX )
315358 || MII_LAB_STRUCTURED_DEFINITION .equals (v ));
316359
317- if (MII_LAB_STRUCTURED_DEFINITION .equals (profileUrl ))
318- {
319- Coding loincCode = o .getCode ().getCoding ().stream ()
320- .filter (c -> "http://loinc.org" .equals (c .getSystem ())).findFirst ()
321- .orElseThrow (() -> new RuntimeException ("Observation (" + MII_LAB_STRUCTURED_DEFINITION
322- + ") not supported, missing LOINC code" ));
323-
324- return "Observation?_profile=" + profileUrl + "&date=" + o .getEffectiveDateTimeType ().getValueAsString ()
325- + "&code=" + loincCode .getSystem () + "|" + loincCode .getCode () + "&patient:identifier="
326- + ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|" + pseudonym ;
327- }
328- else
329- {
330- return "Observation?_profile=" + profileUrl + "&date=" + o .getEffectiveDateTimeType ().getValueAsString ()
331- + "&patient:identifier=" + ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|"
332- + pseudonym ;
333- }
360+ String updateUrl = getInitialConditionalUpdateUrl (ResourceType .Observation .name (), profileUrl ,
361+ patientIdentifier );
362+
363+ if (o .hasEffectiveDateTimeType () && o .getEffectiveDateTimeType ().getValueAsString () != null )
364+ updateUrl = updateUrl + "&date=" + o .getEffectiveDateTimeType ().getValueAsString ();
365+
366+ if (o .hasCategory ())
367+ updateUrl = updateUrl + "&category=" + getCodingUpdateUrl (o .getCategoryFirstRep ().getCodingFirstRep ());
368+
369+ if (o .hasCode ())
370+ updateUrl = updateUrl + "&code=" + getCodingUpdateUrl (o .getCode ().getCodingFirstRep ());
371+
372+ return updateUrl ;
334373 }
335374 else if (resource instanceof Procedure )
336375 {
337376 Procedure p = (Procedure ) resource ;
338377 String profileUrl = getProfileUrl (resource , v -> v .startsWith (NUM_CODEX_STRUCTURE_DEFINITION_PREFIX ));
339378
340- return "Procedure?_profile=" + profileUrl + "&date=" + p .getPerformedDateTimeType ().getValueAsString ()
341- + "&patient:identifier=" + ConstantsDataTransfer .NAMING_SYSTEM_NUM_CODEX_DIC_PSEUDONYM + "|"
342- + pseudonym ;
379+ String updateUrl = getInitialConditionalUpdateUrl (ResourceType .Procedure .name (), profileUrl ,
380+ patientIdentifier );
381+
382+ if (p .hasPerformedDateTimeType () && p .getPerformedDateTimeType ().getValueAsString () != null )
383+ updateUrl = updateUrl + "&date=" + p .getPerformedDateTimeType ().getValueAsString ();
384+ else if (p .hasStatus ())
385+ updateUrl = updateUrl + "&status=" + p .getStatus ().toCode ();
386+
387+ if (p .hasCategory ())
388+ updateUrl = updateUrl + "&category=" + getCodingUpdateUrl (p .getCategory ().getCodingFirstRep ());
389+
390+ if (p .hasCode ())
391+ updateUrl = updateUrl + "&code=" + getCodingUpdateUrl (p .getCode ().getCodingFirstRep ());
392+
393+ return updateUrl ;
394+
343395 }
344396 else
345397 throw new RuntimeException ("Resource of type " + resource .getResourceType ().name () + " not supported" );
@@ -351,4 +403,14 @@ private String getProfileUrl(DomainResource resource, Predicate<String> filter)
351403 .orElseThrow (() -> new RuntimeException ("Resource of type " + resource .getResourceType ().name ()
352404 + " not supported, missing NUM or MII profile" ));
353405 }
406+
407+ private String getInitialConditionalUpdateUrl (String resourceName , String profileUrl , String patientIdentifier )
408+ {
409+ return resourceName + "?_profile=" + profileUrl + "&patient:identifier=" + patientIdentifier ;
410+ }
411+
412+ private String getCodingUpdateUrl (Coding coding )
413+ {
414+ return coding .getSystem () + "|" + coding .getCode ();
415+ }
354416}
0 commit comments