changeset 88:eb08ec14fb04 db-changes

new extension implemented: TagMostRecentPatient
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 15 Jan 2019 21:10:50 +0100
parents 48d445f756db
children 3f917ed95daf
files Framework/Plugins/IndexBackend.cpp Framework/Plugins/IndexBackend.h Framework/Plugins/OrthancCppDatabasePlugin.h PostgreSQL/Plugins/PostgreSQLIndex.cpp
diffstat 4 files changed, 93 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Plugins/IndexBackend.cpp	Thu Jan 10 20:39:36 2019 +0100
+++ b/Framework/Plugins/IndexBackend.cpp	Tue Jan 15 21:10:50 2019 +0100
@@ -1882,4 +1882,73 @@
 
     ReadListOfStrings(target, statement, args);
   }
+
+
+  // New primitive since Orthanc 1.5.2
+  void IndexBackend::TagMostRecentPatient(int64_t patient)
+  {
+    int64_t seq;
+    
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager_,
+        "SELECT * FROM PatientRecyclingOrder WHERE seq >= "
+        "(SELECT seq FROM PatientRecyclingOrder WHERE patientid=${id}) ORDER BY seq LIMIT 2");
+
+      statement.SetReadOnly(true);
+      statement.SetParameterType("id", ValueType_Integer64);
+
+      Dictionary args;
+      args.SetIntegerValue("id", patient);
+
+      statement.Execute(args);
+      
+      if (statement.IsDone())
+      {
+        // The patient is protected, don't add it to the recycling order
+        return;
+      }
+
+      seq = ReadInteger64(statement, 0);
+
+      statement.Next();
+
+      if (statement.IsDone())
+      {
+        // The patient is already at the end of the recycling order
+        // (because of the "LIMIT 2" above), no need to modify the table
+        return;
+      }
+    }
+
+    // Delete the old position of the patient in the recycling order
+
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager_,
+        "DELETE FROM PatientRecyclingOrder WHERE seq=${seq}");
+        
+      statement.SetParameterType("seq", ValueType_Integer64);
+        
+      Dictionary args;
+      args.SetIntegerValue("seq", seq);
+        
+      statement.Execute(args);
+    }
+
+    // Add the patient to the end of the recycling order
+
+    {
+      DatabaseManager::CachedStatement statement(
+        STATEMENT_FROM_HERE, manager_,
+        "INSERT INTO PatientRecyclingOrder VALUES(${}, ${id})");
+        
+      statement.SetParameterType("id", ValueType_Integer64);
+        
+      Dictionary args;
+      args.SetIntegerValue("id", patient);
+        
+      statement.Execute(args);
+    }
+  }
 }
--- a/Framework/Plugins/IndexBackend.h	Thu Jan 10 20:39:36 2019 +0100
+++ b/Framework/Plugins/IndexBackend.h	Tue Jan 15 21:10:50 2019 +0100
@@ -281,5 +281,7 @@
     virtual void GetChildrenMetadata(std::list<std::string>& target,
                                      int64_t resourceId,
                                      int32_t metadata);
+
+    virtual void TagMostRecentPatient(int64_t patient);
   };
 }
--- a/Framework/Plugins/OrthancCppDatabasePlugin.h	Thu Jan 10 20:39:36 2019 +0100
+++ b/Framework/Plugins/OrthancCppDatabasePlugin.h	Tue Jan 15 21:10:50 2019 +0100
@@ -532,6 +532,8 @@
                                      int32_t metadata) = 0;
 
     virtual int64_t GetLastChangeIndex() = 0;
+
+    virtual void TagMostRecentPatient(int64_t patientId) = 0;
   };
 
 
@@ -1630,6 +1632,22 @@
       }
       ORTHANC_PLUGINS_DATABASE_CATCH      
     }
+
+
+    // New primitive since Orthanc 1.5.2
+    static OrthancPluginErrorCode TagMostRecentPatient(void* payload,
+                                                       int64_t patientId)
+    {
+      IDatabaseBackend* backend = reinterpret_cast<IDatabaseBackend*>(payload);
+      backend->GetOutput().SetAllowedAnswers(DatabaseBackendOutput::AllowedAnswers_None);
+
+      try
+      {
+        backend->TagMostRecentPatient(patientId);
+        return OrthancPluginErrorCode_Success;
+      }
+      ORTHANC_PLUGINS_DATABASE_CATCH      
+    }
    
 
   public:
@@ -1717,6 +1735,7 @@
       extensions.setResourcesContent = SetResourcesContent;  // Fast setting tags/metadata
       extensions.getChildrenMetadata = GetChildrenMetadata;
       extensions.getLastChangeIndex = GetLastChangeIndex;
+      extensions.tagMostRecentPatient = TagMostRecentPatient;
 
       if (backend.HasCreateInstance())
       {
--- a/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Thu Jan 10 20:39:36 2019 +0100
+++ b/PostgreSQL/Plugins/PostgreSQLIndex.cpp	Tue Jan 15 21:10:50 2019 +0100
@@ -381,6 +381,9 @@
       result.patientId = ReadInteger64(statement, 4);
       result.studyId = ReadInteger64(statement, 5);
       result.seriesId = ReadInteger64(statement, 6);
+
+      // TODO - Move this to the stored procedure
+      TagMostRecentPatient(result.patientId);
     }
   }
 #endif