From fb48731d790d77edd687c3678e4dba5387bca9dc Mon Sep 17 00:00:00 2001
From: Alexandre P <apo@kereval.com>
Date: Thu, 19 Aug 2021 16:42:11 +0200
Subject: [PATCH] Writing of Update Method

---
 .../connector/BusinessToFhirConverter.java    | 13 +++---
 .../adapter/connector/UpdateBundle.java       | 43 +++++++++++++++++++
 .../PatientRegistryFeedClient.java            | 28 +++++++++---
 .../provider/ChPatientResourceProvider.java   | 17 +++-----
 4 files changed, 74 insertions(+), 27 deletions(-)
 create mode 100644 src/main/java/net/ihe/gazelle/adapter/connector/UpdateBundle.java

diff --git a/src/main/java/net/ihe/gazelle/adapter/connector/BusinessToFhirConverter.java b/src/main/java/net/ihe/gazelle/adapter/connector/BusinessToFhirConverter.java
index b7860a1..a9101a8 100644
--- a/src/main/java/net/ihe/gazelle/adapter/connector/BusinessToFhirConverter.java
+++ b/src/main/java/net/ihe/gazelle/adapter/connector/BusinessToFhirConverter.java
@@ -41,7 +41,7 @@ public class BusinessToFhirConverter {
 	 */
 	private BusinessToFhirConverter() { }
 
-	public static Bundle uuidToBundle(String uuid,String url) {
+	public static Bundle uuidToBundle(String uuid, String serverUrl) {
 
 		Bundle response = new Bundle();
 		response.setId("BundlePIXmResponse");
@@ -49,21 +49,18 @@ public class BusinessToFhirConverter {
 		metadata.addProfile("http://fhir.ch/ig/ch-epr-mhealth/StructureDefinition/ch-pixm-bundle-response");
 		response.setType(BundleType.MESSAGE);
 		BundleEntryComponent entry = new BundleEntryComponent();
-		//TODO ce champ est en dur, je ne sais pas comment r�cup�rer l'adresse web en direct dans le code. 
-		entry.setFullUrl("http://localhost:8081/pixm_fhir_server/fhir_ch/Patient/" + uuid);
+		entry.setFullUrl(serverUrl + uuid);
 		MessageHeader messageHeader = new MessageHeader();
 		messageHeader.setId(uuid);
 		Narrative narrative = new Narrative();
 		narrative.setStatus(NarrativeStatus.GENERATED);
-		//narrative.setDiv()
+		//narrative.setDiv();
 		messageHeader.setText(narrative);
 		MessageDestinationComponent mdc = new MessageDestinationComponent();
-		//TODO same here
-		mdc.setEndpoint("http://localhost:8081/pixm_fhir_server/fhir_ch/Patient/" + uuid);
+		mdc.setEndpoint(serverUrl + uuid);
 		messageHeader.addDestination(mdc);
 		MessageSourceComponent msc = new MessageSourceComponent();
-		//TODO same here
-		msc.setEndpoint("http://localhost:8081/pixm_fhir_server/fhir_ch/Patient/" + uuid);
+		msc.setEndpoint(serverUrl + uuid);
 		messageHeader.setSource(msc);
 		MessageHeaderResponseComponent mhrc = new MessageHeaderResponseComponent();
 		mhrc.setIdentifier(uuid);
diff --git a/src/main/java/net/ihe/gazelle/adapter/connector/UpdateBundle.java b/src/main/java/net/ihe/gazelle/adapter/connector/UpdateBundle.java
new file mode 100644
index 0000000..dc5040b
--- /dev/null
+++ b/src/main/java/net/ihe/gazelle/adapter/connector/UpdateBundle.java
@@ -0,0 +1,43 @@
+package net.ihe.gazelle.adapter.connector;
+import org.hl7.fhir.r4.model.Bundle;
+import org.hl7.fhir.r4.model.MessageHeader;
+import org.hl7.fhir.r4.model.Meta;
+import org.hl7.fhir.r4.model.Narrative;
+
+public class UpdateBundle {
+
+    /**
+     * private constructor to override the implicit one.
+     */
+    private UpdateBundle() {
+    }
+    public static Bundle prepareBundleAfterUpdateOperation(String uuid, String url) {
+        Bundle response = new Bundle();
+        response.setId("BundlePIXmResponse");
+        Meta metadata = new Meta();
+        metadata.addProfile("http://fhir.ch/ig/ch-epr-mhealth/StructureDefinition/ch-pixm-bundle-response");
+        response.setType(Bundle.BundleType.MESSAGE);
+        response.setMeta(metadata);
+
+        Bundle.BundleEntryComponent entry = new Bundle.BundleEntryComponent();
+        entry.setFullUrl(url + "/pixm_fhir_server/fhir_ch/Patient/" + uuid);
+        MessageHeader messageHeader = new MessageHeader();
+        messageHeader.setId(uuid);
+        Narrative narrative = new Narrative();
+        narrative.setStatus(Narrative.NarrativeStatus.GENERATED);
+        messageHeader.setText(narrative);
+        MessageHeader.MessageHeaderResponseComponent mhrc = new MessageHeader.MessageHeaderResponseComponent();
+        mhrc.setIdentifier(uuid);
+        mhrc.setCode(MessageHeader.ResponseType.OK);
+        messageHeader.setResponse(mhrc);
+        entry.setResource(messageHeader);
+        response.addEntry(entry);
+
+        response.addEntry()
+                .getRequest()
+                .setUrl("PUT")
+                .setMethod(Bundle.HTTPVerb.PUT);
+        return response;
+
+    }
+}
diff --git a/src/main/java/net/ihe/gazelle/application/PatientRegistryFeedClient.java b/src/main/java/net/ihe/gazelle/application/PatientRegistryFeedClient.java
index 57dc91a..187ea37 100644
--- a/src/main/java/net/ihe/gazelle/application/PatientRegistryFeedClient.java
+++ b/src/main/java/net/ihe/gazelle/application/PatientRegistryFeedClient.java
@@ -3,6 +3,7 @@ package net.ihe.gazelle.application;
 import ca.uhn.fhir.rest.server.exceptions.*;
 import net.ihe.gazelle.adapter.connector.BusinessToFhirConverter;
 import net.ihe.gazelle.adapter.connector.DeletionBundle;
+import net.ihe.gazelle.adapter.connector.UpdateBundle;
 import net.ihe.gazelle.adapter.preferences.Preferences;
 import net.ihe.gazelle.app.patientregistryapi.application.PatientFeedException;
 import net.ihe.gazelle.app.patientregistryapi.business.PersonName;
@@ -75,7 +76,6 @@ public class PatientRegistryFeedClient {
             patientRegistryUrl = this.operationalPreferencesService.
                     getStringValue(Preferences.PATIENT_REGISTRY_URL.getNamespace().getValue(), Preferences.PATIENT_REGISTRY_URL.getKey());
             this.client = new PatientFeedClient(new URL(patientRegistryUrl));
-            getUrlOfServer();
         } catch (NamespaceException | PreferenceException e) {
             throw new PatientFeedException(String.format("Unable to retrieve [%s] Preference !", Preferences.PATIENT_REGISTRY_URL.getKey()));
         } catch (MalformedURLException e) {
@@ -149,7 +149,7 @@ public class PatientRegistryFeedClient {
      * @param uuid    : The uuid of the patient corresponding to it in DB
      * @return a String corresponding to the uuid confirming the transaction has been successful.
      */
-    public String updatePatient(net.ihe.gazelle.app.patientregistryapi.business.Patient patient, String uuid) throws PatientFeedException {
+    public Bundle updatePatient(net.ihe.gazelle.app.patientregistryapi.business.Patient patient, String uuid) throws PatientFeedException {
         if (client == null) {
             logger.info("client not set");
             initializeClient();
@@ -162,12 +162,26 @@ public class PatientRegistryFeedClient {
             patient.setUuid(uuid);
             client.updatePatient(patient);
         } catch (PatientFeedException e) {
-            logger.error("pouet pouet ciboulette");
-            //TODO Map errors
+            switch (e.getCause().getMessage()) {
+                case "One patientIdentifier is malformed":
+                    throw new UnprocessableEntityException("", e);
+                case "One PatientIdentifier is duplicated id DB":
+                    throw new ResourceNotFoundException("");
+                case "System not found":
+                    throw new ResourceNotFoundException("System Domain could not be found when updating Patient");
+                case "Unexpected Exception persisting Patient !":
+                    throw new InternalErrorException("", e);
+                case "Null crossReference parameter for update":
+                    throw new InternalErrorException("", e);
+                case "Persistence Error":
+                    throw new InternalErrorException("", e);
+                case "Exception performing the read operation for requested criteria !":
+                    throw new InternalErrorException("", e);
+                default:
+                    throw new InternalErrorException("", e);
+            }
         }
-
-
-        return uuid;
+        return UpdateBundle.prepareBundleAfterUpdateOperation(uuid,serverUrl);
 
     }
 
diff --git a/src/main/java/net/ihe/gazelle/business/provider/ChPatientResourceProvider.java b/src/main/java/net/ihe/gazelle/business/provider/ChPatientResourceProvider.java
index 8377f94..f325415 100644
--- a/src/main/java/net/ihe/gazelle/business/provider/ChPatientResourceProvider.java
+++ b/src/main/java/net/ihe/gazelle/business/provider/ChPatientResourceProvider.java
@@ -144,28 +144,21 @@ public class ChPatientResourceProvider implements IResourceProvider {
     @Update
     public MethodOutcome update(@IdParam IdType theId, @ResourceParam Bundle iti93Bundle) {
         String uuid = theId.getIdPart();
-        if (uuid.isBlank()) {
+        if (uuid.isBlank() || uuid == null) {
             patientLogger.error(NO_ID_PROVIDED);
             throw new InvalidRequestException(NO_ID_PROVIDED);
         }
         try {
-            net.ihe.gazelle.app.patientregistryapi.business.Patient patient = BundleToPatientRegistryConverter.iti93BundleToPatient(iti93Bundle);
+            net.ihe.gazelle.app.patientregistryapi.business.Patient patientToUpdate = BundleToPatientRegistryConverter.iti93BundleToPatient(iti93Bundle);
             MethodOutcome methodOutcome = new MethodOutcome();
-
-            methodOutcome.setResource(new Bundle());
+            methodOutcome.setResource(patientRegistryFeedClient.updatePatient(patientToUpdate, uuid));
             return methodOutcome;
         } catch (ConversionException e) {
             throw new InvalidRequestException("Bundle Could not be converted to HL7 Patient");
+        }catch (PatientFeedException e) {
+            throw new InternalErrorException("Patient Feed client is not set");
         }
 
-//		String versionId = theId.getVersionIdPart(); // this will contain the ETag
-//		String currentVersion = "1"; // populate this with the current version
-//		if (!versionId.equals(currentVersion)) {
-//			throw new ResourceVersionConflictException("Expected version " + currentVersion);
-//		}
-
-            // DONE
-
     }
 
     /**
-- 
GitLab