diff PostgreSQL/Plugins/CreateInstance.sql @ 90:e61587582cef db-changes

moved extension TagMostRecentPatient into stored procedure CreateInstance
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 16 Jan 2019 18:14:28 +0100
parents 8dd29af7c844
children b559af8fe6e0
line wrap: on
line diff
--- a/PostgreSQL/Plugins/CreateInstance.sql	Wed Jan 16 16:37:52 2019 +0100
+++ b/PostgreSQL/Plugins/CreateInstance.sql	Wed Jan 16 18:14:28 2019 +0100
@@ -12,6 +12,10 @@
   OUT seriesKey BIGINT,
   OUT instanceKey BIGINT) AS $body$
 
+DECLARE
+  patientSeq BIGINT;
+  countRecycling BIGINT;
+
 BEGIN
   SELECT internalId FROM Resources INTO instanceKey WHERE publicId = instance AND resourceType = 3;
 
@@ -62,6 +66,19 @@
 
     INSERT INTO Resources VALUES (DEFAULT, 3, instance, seriesKey) RETURNING internalId INTO instanceKey;
     isNewInstance := 1;
+
+    -- Move the patient to the end of the recycling order
+    SELECT seq FROM PatientRecyclingOrder WHERE patientId = patientKey INTO patientSeq;
+
+    IF NOT (patientSeq IS NULL) THEN
+       -- The patient is not protected
+       SELECT COUNT(*) FROM (SELECT * FROM PatientRecyclingOrder WHERE seq >= patientSeq LIMIT 2) AS tmp INTO countRecycling;
+       IF countRecycling = 2 THEN
+          -- The patient was not at the end of the recycling order
+          DELETE FROM PatientRecyclingOrder WHERE seq = patientSeq;
+          INSERT INTO PatientRecyclingOrder VALUES(DEFAULT, patientKey);
+       END IF;
+    END IF;
   END IF;  
 END;