44
55import java .nio .file .Path ;
66import java .util .Optional ;
7- import java .util .UUID ;
87import java .util .function .Predicate ;
98
109import org .hl7 .fhir .instance .model .api .IBaseOperationOutcome ;
1110import org .hl7 .fhir .r4 .model .Bundle ;
1211import org .hl7 .fhir .r4 .model .Bundle .BundleEntryComponent ;
1312import org .hl7 .fhir .r4 .model .Bundle .HTTPVerb ;
14- import org .hl7 .fhir .r4 .model .Identifier ;
1513import org .hl7 .fhir .r4 .model .OperationOutcome ;
1614import org .hl7 .fhir .r4 .model .Patient ;
17- import org .hl7 .fhir .r4 .model .Reference ;
1815import org .hl7 .fhir .r4 .model .Resource ;
1916import org .slf4j .Logger ;
2017import org .slf4j .LoggerFactory ;
@@ -60,7 +57,8 @@ public void storeBundle(Bundle bundle)
6057
6158 if (isEntrySupported (entry , e -> !(e .getResource () instanceof Patient )))
6259 createOrUpdateEntry (i , entry , patient );
63- else
60+ else if (!entry .hasResource () || !(entry .getResource () instanceof Patient ))
61+ // only log for non Patients
6462 logger .warn ("Bundle entry at index {} not supported, ignoring entry" , i );
6563 }
6664 }
@@ -91,26 +89,30 @@ private Optional<Patient> createOrUpdatePatient(Bundle bundle)
9189 String pseudonym = getPseudonym (bundlePatient .get ());
9290 Optional <Patient > existingPatient = findPatientInLocalFhirStore (pseudonym );
9391
94- return existingPatient .map (existing -> update (existing , bundlePatient .get ()))
95- .orElseGet (() -> create (bundlePatient .get ()));
92+ return existingPatient .map (existing -> update (existing , bundlePatient .get (), pseudonym ))
93+ .orElseGet (() -> create (bundlePatient .get (), pseudonym ));
9694 }
9795 else
96+ {
97+ logger .info ("Bundle has no Patient" );
9898 return Optional .empty ();
99+ }
99100 }
100101
101- private Optional <Patient > update (Patient existingPatient , Patient newPatient )
102+ private Optional <Patient > update (Patient existingPatient , Patient newPatient , String pseudonym )
102103 {
103- newPatient . setIdElement ( existingPatient . getIdElement (). toVersionless () );
104+ logger . debug ( "Updating Patient with pseudonym {}" , pseudonym );
104105
105- Optional <Identifier > rfc4122Identifier = existingPatient .getIdentifier ().stream ().filter (Identifier ::hasSystem )
106- .filter (i -> RFC_4122_SYSTEM .equals (i .getSystem ())).filter (Identifier ::hasValue ).findFirst ();
107- rfc4122Identifier .ifPresent (i -> newPatient .addIdentifier (i .copy ()));
106+ newPatient .setIdElement (existingPatient .getIdElement ().toVersionless ());
108107
109108 try
110109 {
111110 MethodOutcome outcome = clientFactory .getFhirStoreClient ().update ().resource (newPatient )
112111 .prefer (PreferReturnEnum .REPRESENTATION ).preferResponseType (Patient .class ).execute ();
113112
113+ if (outcome .getOperationOutcome () != null && outcome .getOperationOutcome () instanceof OperationOutcome )
114+ outcomeLogger .logOutcome ((OperationOutcome ) outcome .getOperationOutcome ());
115+
114116 if (outcome .getResource () != null && outcome .getResource () instanceof Patient )
115117 return Optional .of ((Patient ) outcome .getResource ());
116118 else
@@ -147,15 +149,18 @@ private Optional<Patient> update(Patient existingPatient, Patient newPatient)
147149 }
148150 }
149151
150- private Optional <Patient > create (Patient newPatient )
152+ private Optional <Patient > create (Patient newPatient , String pseudonym )
151153 {
152- newPatient . addIdentifier (). setSystem ( RFC_4122_SYSTEM ). setValue ( UUID . randomUUID (). toString () );
154+ logger . debug ( "Creating Patient with pseudonym {}" , pseudonym );
153155
154156 try
155157 {
156158 MethodOutcome outcome = clientFactory .getFhirStoreClient ().create ().resource (newPatient )
157159 .prefer (PreferReturnEnum .REPRESENTATION ).preferResponseType (Patient .class ).execute ();
158160
161+ if (outcome .getOperationOutcome () != null && outcome .getOperationOutcome () instanceof OperationOutcome )
162+ outcomeLogger .logOutcome ((OperationOutcome ) outcome .getOperationOutcome ());
163+
159164 if (Boolean .TRUE .equals (outcome .getCreated ()) && outcome .getResource () != null
160165 && outcome .getResource () instanceof Patient )
161166 return Optional .of ((Patient ) outcome .getResource ());
@@ -195,12 +200,7 @@ private Optional<Patient> create(Patient newPatient)
195200
196201 private void createOrUpdateEntry (int index , BundleEntryComponent entry , Patient patient )
197202 {
198- Reference subjectReference = patient .getIdentifier ().stream ().filter (Identifier ::hasSystem )
199- .filter (i -> RFC_4122_SYSTEM .equals (i .getSystem ())).filter (Identifier ::hasValue ).findFirst ()
200- .map (i -> new Reference ().setIdentifier (i ))
201- .orElse (new Reference (patient .getIdElement ().toVersionless ()));
202- Resource resource = setSubject (entry .getResource (), subjectReference );
203-
203+ Resource resource = entry .getResource ();
204204 String url = entry .getRequest ().getUrl ();
205205
206206 Optional <Resource > existingResource = findResourceInLocalFhirStore (url , resource .getClass ());
@@ -215,7 +215,7 @@ private Optional<Resource> findResourceInLocalFhirStore(String url, Class<? exte
215215 .descending ("_lastUpdated" ).count (1 ).returnBundle (Bundle .class ).execute ();
216216
217217 if (logger .isDebugEnabled ())
218- logger .debug ("Patient search-bundle result: {}" ,
218+ logger .debug ("{} search-bundle result: {}" , resourceType . getAnnotation ( ResourceDef . class ). name () ,
219219 fhirContext .newJsonParser ().encodeResourceToString (patientBundle ));
220220
221221 if (patientBundle .getTotal () > 0 )
@@ -272,6 +272,8 @@ private Optional<Resource> findResourceInLocalFhirStore(String url, Class<? exte
272272
273273 private void update (Resource existingResource , Resource newResource )
274274 {
275+ logger .debug ("Updating {}" , newResource .getResourceType ().name ());
276+
275277 newResource .setIdElement (existingResource .getIdElement ().toVersionless ());
276278
277279 try
@@ -289,6 +291,8 @@ private void update(Resource existingResource, Resource newResource)
289291 throw new RuntimeException ("Count not update " + newResource .getResourceType ().name () + " "
290292 + newResource .getIdElement ().toString ());
291293 }
294+ else if (outcome .getOperationOutcome () != null && outcome .getOperationOutcome () instanceof OperationOutcome )
295+ outcomeLogger .logOutcome ((OperationOutcome ) outcome .getOperationOutcome ());
292296 }
293297 catch (UnprocessableEntityException e )
294298 {
@@ -319,6 +323,8 @@ private void update(Resource existingResource, Resource newResource)
319323
320324 private void create (Resource newResource )
321325 {
326+ logger .debug ("Creating {}" , newResource .getResourceType ().name ());
327+
322328 try
323329 {
324330 MethodOutcome outcome = clientFactory .getFhirStoreClient ().create ().resource (newResource )
@@ -334,6 +340,8 @@ private void create(Resource newResource)
334340 throw new RuntimeException ("Count not create " + newResource .getResourceType ().name () + " "
335341 + newResource .getIdElement ().toString ());
336342 }
343+ else if (outcome .getOperationOutcome () != null && outcome .getOperationOutcome () instanceof OperationOutcome )
344+ outcomeLogger .logOutcome ((OperationOutcome ) outcome .getOperationOutcome ());
337345 }
338346 catch (UnprocessableEntityException e )
339347 {
0 commit comments