changeset 2827:d4fd4614f275

IncomingWorklistRequestFilter() to filter incoming C-FIND worklist queries
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 19 Sep 2018 21:10:49 +0200
parents c277e0421200
children 859a4950d48f
files Core/Enumerations.h NEWS OrthancServer/OrthancFindRequestHandler.cpp OrthancServer/OrthancFindRequestHandler.h Plugins/Engine/OrthancPlugins.cpp Plugins/Samples/ModalityWorklists/Plugin.cpp
diffstat 6 files changed, 76 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Enumerations.h	Wed Sep 19 16:06:41 2018 +0200
+++ b/Core/Enumerations.h	Wed Sep 19 21:10:49 2018 +0200
@@ -509,7 +509,10 @@
   enum DicomFromJsonFlags
   {
     DicomFromJsonFlags_DecodeDataUriScheme = (1 << 0),
-    DicomFromJsonFlags_GenerateIdentifiers = (1 << 1)
+    DicomFromJsonFlags_GenerateIdentifiers = (1 << 1),
+
+    // Some predefined combinations
+    DicomFromJsonFlags_None = 0
   };
   
   enum DicomVersion
--- a/NEWS	Wed Sep 19 16:06:41 2018 +0200
+++ b/NEWS	Wed Sep 19 21:10:49 2018 +0200
@@ -29,6 +29,11 @@
 * New primitives to handle jobs from plugins: "OrthancPluginSubmitJob()"
   and "OrthancPluginRegisterJobsUnserializer()"
 
+Lua
+---
+
+* IncomingWorklistRequestFilter() to filter incoming C-FIND worklist queries
+
 Maintenance
 -----------
 
--- a/OrthancServer/OrthancFindRequestHandler.cpp	Wed Sep 19 16:06:41 2018 +0200
+++ b/OrthancServer/OrthancFindRequestHandler.cpp	Wed Sep 19 21:10:49 2018 +0200
@@ -482,7 +482,8 @@
                                                  const DicomMap& source,
                                                  const std::string& remoteIp,
                                                  const std::string& remoteAet,
-                                                 const std::string& calledAet)
+                                                 const std::string& calledAet,
+                                                 ModalityManufacturer manufacturer)
   {
     static const char* LUA_CALLBACK = "IncomingFindRequestFilter";
     
@@ -494,10 +495,8 @@
     }
     else
     {
-      Json::Value origin = Json::objectValue;
-      origin["RemoteIp"] = remoteIp;
-      origin["RemoteAet"] = remoteAet;
-      origin["CalledAet"] = calledAet;
+      Json::Value origin;
+      FormatOrigin(origin, remoteIp, remoteAet, calledAet, manufacturer);
 
       LuaFunctionCall call(lock.GetLua(), LUA_CALLBACK);
       call.PushDicom(source);
@@ -532,7 +531,7 @@
     DicomMap lua;
     const DicomMap* filteredInput = &input;
 
-    if (ApplyLuaFilter(lua, input, remoteIp, remoteAet, calledAet))
+    if (ApplyLuaFilter(lua, input, remoteIp, remoteAet, calledAet, manufacturer))
     {
       filteredInput = &lua;
     }
@@ -675,4 +674,18 @@
 
     answers.SetComplete(complete);
   }
+
+
+  void OrthancFindRequestHandler::FormatOrigin(Json::Value& origin,
+                                               const std::string& remoteIp,
+                                               const std::string& remoteAet,
+                                               const std::string& calledAet,
+                                               ModalityManufacturer manufacturer)
+  {
+    origin = Json::objectValue;
+    origin["RemoteIp"] = remoteIp;
+    origin["RemoteAet"] = remoteAet;
+    origin["CalledAet"] = calledAet;
+    origin["Manufacturer"] = EnumerationToString(manufacturer);
+  }
 }
--- a/OrthancServer/OrthancFindRequestHandler.h	Wed Sep 19 16:06:41 2018 +0200
+++ b/OrthancServer/OrthancFindRequestHandler.h	Wed Sep 19 21:10:49 2018 +0200
@@ -57,7 +57,8 @@
                         const DicomMap& source,
                         const std::string& remoteIp,
                         const std::string& remoteAet,
-                        const std::string& calledAet);
+                        const std::string& calledAet,
+                        ModalityManufacturer manufacturer);
 
   public:
     OrthancFindRequestHandler(ServerContext& context);
@@ -89,5 +90,11 @@
     {
       maxInstances_ = instances;
     }
+
+    static void FormatOrigin(Json::Value& origin,
+                             const std::string& remoteIp,
+                             const std::string& remoteAet,
+                             const std::string& calledAet,
+                             ModalityManufacturer manufacturer);
   };
 }
--- a/Plugins/Engine/OrthancPlugins.cpp	Wed Sep 19 16:06:41 2018 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Wed Sep 19 21:10:49 2018 +0200
@@ -62,6 +62,7 @@
 #include "../../Core/Images/JpegWriter.h"
 #include "../../Core/Images/ImageProcessing.h"
 #include "../../OrthancServer/DefaultDicomImageDecoder.h"
+#include "../../OrthancServer/OrthancFindRequestHandler.h"
 #include "PluginsEnumerations.h"
 #include "PluginsJob.h"
 
@@ -454,11 +455,13 @@
   private:
     OrthancPlugins&  that_;
     std::auto_ptr<HierarchicalMatcher> matcher_;
+    std::auto_ptr<ParsedDicomFile>     filtered_;
     ParsedDicomFile* currentQuery_;
 
     void Reset()
     {
-      matcher_.reset(NULL);
+      matcher_.reset();
+      filtered_.reset();
       currentQuery_ = NULL;
     }
 
@@ -475,12 +478,41 @@
                         const std::string& calledAet,
                         ModalityManufacturer manufacturer)
     {
+      static const char* LUA_CALLBACK = "IncomingWorklistRequestFilter";
+
+      {
+        PImpl::ServerContextLock lock(*that_.pimpl_);
+        LuaScripting::Lock lua(lock.GetContext().GetLuaScripting());
+
+        if (!lua.GetLua().IsExistingFunction(LUA_CALLBACK))
+        {
+          currentQuery_ = &query;
+        }
+        else
+        {
+          Json::Value source, origin;
+          query.DatasetToJson(source, DicomToJsonFormat_Short, DicomToJsonFlags_None, 0);
+
+          OrthancFindRequestHandler::FormatOrigin
+            (origin, remoteIp, remoteAet, calledAet, manufacturer);
+
+          LuaFunctionCall call(lua.GetLua(), LUA_CALLBACK);
+          call.PushJson(source);
+          call.PushJson(origin);
+
+          Json::Value target;
+          call.ExecuteToJson(target, true);
+          
+          filtered_.reset(ParsedDicomFile::CreateFromJson(target, DicomFromJsonFlags_None));
+          currentQuery_ = filtered_.get();
+        }
+      }
+      
+      matcher_.reset(new HierarchicalMatcher(*currentQuery_));
+
       {
         boost::mutex::scoped_lock lock(that_.pimpl_->worklistCallbackMutex_);
 
-        matcher_.reset(new HierarchicalMatcher(query));
-        currentQuery_ = &query;
-
         if (that_.pimpl_->worklistCallback_)
         {
           OrthancPluginErrorCode error = that_.pimpl_->worklistCallback_
--- a/Plugins/Samples/ModalityWorklists/Plugin.cpp	Wed Sep 19 16:06:41 2018 +0200
+++ b/Plugins/Samples/ModalityWorklists/Plugin.cpp	Wed Sep 19 21:10:49 2018 +0200
@@ -92,6 +92,7 @@
 
     static const char* SCHEDULED_PROCEDURE_STEP_SEQUENCE = "0040,0100";
     static const char* SCHEDULED_STATION_AETITLE = "0040,0001";
+    static const char* PREGNANCY_STATUS = "0010,21c0";
 
     if (!json.isMember(SCHEDULED_PROCEDURE_STEP_SEQUENCE))
     {
@@ -118,10 +119,10 @@
       v[0][SCHEDULED_STATION_AETITLE] = issuerAet;
     }
 
-    if (json.isMember("0010,21c0") &&
-        json["0010,21c0"].asString().size() == 0)
+    if (json.isMember(PREGNANCY_STATUS) &&
+        json[PREGNANCY_STATUS].asString().size() == 0)
     {
-      json.removeMember("0010,21c0");
+      json.removeMember(PREGNANCY_STATUS);
     }
 
     // Encode the modified JSON as a DICOM instance, then convert it to a C-Find matcher