changeset 1010:160dfe770618 lua-scripting

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Jul 2014 17:05:00 +0200
parents 26642cecd36d
children 23590917e83e
files Core/Lua/LuaFunctionCall.cpp OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp OrthancServer/OrthancRestApi/OrthancRestApi.h OrthancServer/ServerContext.cpp Resources/Samples/Lua/Autorouting.lua Resources/Toolbox.lua
diffstat 6 files changed, 131 insertions(+), 119 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Lua/LuaFunctionCall.cpp	Wed Jul 09 16:11:44 2014 +0200
+++ b/Core/Lua/LuaFunctionCall.cpp	Wed Jul 09 17:05:00 2014 +0200
@@ -266,6 +266,10 @@
     {
       result = std::string(lua_tostring(lua, top));
     }
+    else if (lua_isboolean(lua, top))
+    {
+      result = static_cast<bool>(lua_toboolean(lua, top));
+    }
     else
     {
       LOG(WARNING) << "Unsupported Lua type when returning Json";
--- a/OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp	Wed Jul 09 16:11:44 2014 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestAnonymizeModify.cpp	Wed Jul 09 17:05:00 2014 +0200
@@ -33,7 +33,6 @@
 #include "../PrecompiledHeadersServer.h"
 #include "OrthancRestApi.h"
 
-#include "../DicomModification.h"
 #include "../FromDcmtkBridge.h"
 
 #include <glog/logging.h>
@@ -111,13 +110,11 @@
   }
 
 
-  static bool ParseModifyRequest(DicomModification& target,
-                                 const RestApiPostCall& call)
+
+  bool OrthancRestApi::ParseModifyRequest(DicomModification& target,
+                                          const Json::Value& request)
   {
-    // curl http://localhost:8042/series/95a6e2bf-9296e2cc-bf614e2f-22b391ee-16e010e0/modify -X POST -d '{"Replace":{"InstitutionName":"My own clinic"}}'
-
-    Json::Value request;
-    if (call.ParseJsonRequest(request) && request.isObject())
+    if (request.isObject())
     {
       if (request.isMember("RemovePrivateTags"))
       {
@@ -143,6 +140,23 @@
   }
 
 
+  static bool ParseModifyRequest(DicomModification& target,
+                                 const RestApiPostCall& call)
+  {
+    // curl http://localhost:8042/series/95a6e2bf-9296e2cc-bf614e2f-22b391ee-16e010e0/modify -X POST -d '{"Replace":{"InstitutionName":"My own clinic"}}'
+
+    Json::Value request;
+    if (call.ParseJsonRequest(request))
+    {
+      return OrthancRestApi::ParseModifyRequest(target, request);
+    }
+    else
+    {
+      return false;
+    }
+  }
+
+
   static bool ParseAnonymizationRequest(DicomModification& target,
                                         RestApiPostCall& call)
   {
--- a/OrthancServer/OrthancRestApi/OrthancRestApi.h	Wed Jul 09 16:11:44 2014 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestApi.h	Wed Jul 09 17:05:00 2014 +0200
@@ -32,8 +32,9 @@
 
 #pragma once
 
+#include "../../Core/RestApi/RestApi.h"
+#include "../DicomModification.h"
 #include "../ServerContext.h"
-#include "../../Core/RestApi/RestApi.h"
 
 #include <set>
 
@@ -80,5 +81,8 @@
     void AnswerStoredInstance(RestApiPostCall& call,
                               const std::string& publicId,
                               StoreStatus status) const;
+
+    static bool ParseModifyRequest(DicomModification& target,
+                                   const Json::Value& request);
   };
 }
--- a/OrthancServer/ServerContext.cpp	Wed Jul 09 16:11:44 2014 +0200
+++ b/OrthancServer/ServerContext.cpp	Wed Jul 09 17:05:00 2014 +0200
@@ -48,6 +48,7 @@
 #include "Scheduler/ModifyInstanceCommand.h"
 #include "Scheduler/StoreScuCommand.h"
 #include "Scheduler/StorePeerCommand.h"
+#include "OrthancRestApi/OrthancRestApi.h"
 
 
 
@@ -128,22 +129,22 @@
   {
     if (operation == "delete")
     {
-      LOG(INFO) << "Lua script to delete instance " << parameters["instance"].asString();
+      LOG(INFO) << "Lua script to delete instance " << parameters["Instance"].asString();
       return new DeleteInstanceCommand(context);
     }
 
     if (operation == "store-scu")
     {
-      std::string modality = parameters["modality"].asString();
-      LOG(INFO) << "Lua script to send instance " << parameters["instance"].asString()
+      std::string modality = parameters["Modality"].asString();
+      LOG(INFO) << "Lua script to send instance " << parameters["Instance"].asString()
                 << " to modality " << modality << " using Store-SCU";
       return new StoreScuCommand(context, Configuration::GetModalityUsingSymbolicName(modality));
     }
 
     if (operation == "store-peer")
     {
-      std::string peer = parameters["peer"].asString();
-      LOG(INFO) << "Lua script to send instance " << parameters["instance"].asString()
+      std::string peer = parameters["Peer"].asString();
+      LOG(INFO) << "Lua script to send instance " << parameters["Instance"].asString()
                 << " to peer " << peer << " using HTTP";
 
       OrthancPeerParameters parameters;
@@ -153,28 +154,9 @@
 
     if (operation == "modify")
     {
-      LOG(INFO) << "Lua script to modify instance " << parameters["instance"].asString();
+      LOG(INFO) << "Lua script to modify instance " << parameters["Instance"].asString();
       std::auto_ptr<ModifyInstanceCommand> command(new ModifyInstanceCommand(context));
-
-      if (parameters.isMember("replacements"))
-      {
-        const Json::Value& replacements = parameters["replacements"];
-        if (replacements.type() != Json::objectValue)
-        {
-          throw OrthancException(ErrorCode_BadParameterType);
-        }
-
-        Json::Value::Members members = replacements.getMemberNames();
-        for (Json::Value::Members::const_iterator
-               it = members.begin(); it != members.end(); ++it)
-        {
-          command->GetModification().Replace(FromDcmtkBridge::ParseTag(*it),
-                                             replacements[*it].asString());
-        }
-
-        // TODO OTHER PARAMETERS OF MODIFY
-      }
-
+      OrthancRestApi::ParseModifyRequest(command->GetModification(), parameters);
       return command.release();
     }
 
@@ -213,36 +195,30 @@
       for (Json::Value::ArrayIndex i = 0; i < operations.size(); ++i)
       {
         if (operations[i].type() != Json::objectValue ||
-            !operations[i].isMember("operation"))
+            !operations[i].isMember("Operation"))
         {
           throw OrthancException(ErrorCode_InternalError);
         }
 
         const Json::Value& parameters = operations[i];
-        std::string operation = parameters["operation"].asString();
+        std::string operation = parameters["Operation"].asString();
 
         ServerCommandInstance& command = job.AddCommand(ParseOperation(*this, operation, operations[i]));
         
-        if (parameters.isMember("instance") &&
-            parameters["instance"].asString() != "")
-        {
-          command.AddInput(parameters["instance"].asString());
-        }
-        else if (previousCommand != NULL)
-        {
-          previousCommand->ConnectOutput(command);
-        }
-        else
+        if (!parameters.isMember("Instance"))
         {
           throw OrthancException(ErrorCode_InternalError);
         }
 
-        /*
-          TODO
-if (previousCommand != NULL)
+        std::string instance = parameters["Instance"].asString();
+        if (instance.empty())
         {
-          previousCommand->ConnectNext(command);
-          }*/
+          previousCommand->ConnectOutput(command);
+        }
+        else 
+        {
+          command.AddInput(instance);
+        }
 
         previousCommand = &command;
       }
@@ -333,7 +309,7 @@
         }
         catch (OrthancException& e)
         {
-          LOG(ERROR) << "Lua error in OnStoredInstance: " << e.What();
+          LOG(ERROR) << "Error in OnStoredInstance callback (Lua): " << e.What();
         }
       }
 
--- a/Resources/Samples/Lua/Autorouting.lua	Wed Jul 09 16:11:44 2014 +0200
+++ b/Resources/Samples/Lua/Autorouting.lua	Wed Jul 09 17:05:00 2014 +0200
@@ -1,69 +1,3 @@
-function _InitializeJob()
-   _job = {}
-end
-
-function _AccessJob()
-   return _job
-end
-
-function SendToModality(instanceId, modality)
-   if instanceId == nil then
-      error('Cannot send an nonexistent instance')
-   end
-
-   table.insert(_job, { 
-                   operation = 'store-scu', 
-                   instance = instanceId,
-                   modality = modality 
-                })
-   return instanceId
-end
-
-function SendToPeer(instanceId, peer)
-   if instanceId == nil then
-      error('Cannot send an nonexistent instance')
-   end
-
-   table.insert(_job, { 
-                   operation = 'store-peer', 
-                   instance = instanceId,
-                   peer = peer
-                })
-   return instanceId
-end
-
-function Delete(instanceId)
-   if instanceId == nil then
-      error('Cannot delete an nonexistent instance')
-   end
-
-   table.insert(_job, { 
-                   operation = 'delete', 
-                   instance = instanceId
-                })
-   return nil  -- Forbid chaining
-end
-
-function Modify(instanceId, replacements, removals, removePrivateTags)
-   if instanceId == nil then
-      error('Cannot modify an nonexistent instance')
-   end
-
-   if instanceId == '' then
-      error('Cannot modify twice an instance');
-   end
-
-   table.insert(_job, { 
-                   operation = 'modify', 
-                   instance = instanceId,
-                   replacements = replacements, 
-                   removals = removals,
-                   removePrivateTags = removePrivateTags 
-                })
-   return ''  -- Chain with another operation
-end
-
-
 function OnStoredInstance(instanceId, tags, metadata)
    --PrintRecursive(tags)
    --PrintRecursive(metadata)
@@ -75,7 +9,12 @@
       if string.find(patientName, 'david') ~= nil then
          --Delete(SendToModality(instanceId, 'sample'))
          --Delete(SendToPeer(instanceId, 'peer'))
-         SendToModality(Modify(instanceId, { PatientName = 'Hello^World' }), 'sample')
+         local replace = {}
+         replace['StationName'] = 'My Medical Device'
+
+         local remove = { 'MilitaryRank' }
+
+         Delete(SendToModality(ModifyInstance(instanceId, replace, remove, true), 'sample'))
          Delete(instanceId)
       else
          Delete(instanceId)
--- a/Resources/Toolbox.lua	Wed Jul 09 16:11:44 2014 +0200
+++ b/Resources/Toolbox.lua	Wed Jul 09 17:05:00 2014 +0200
@@ -19,4 +19,79 @@
    return l
 end	
 
+
+
+
+function _InitializeJob()
+   _job = {}
+end
+
+
+function _AccessJob()
+   return _job
+end
+
+
+function SendToModality(instanceId, modality)
+   if instanceId == nil then
+      error('Cannot send a nonexistent instance')
+   end
+
+   table.insert(_job, { 
+                   Operation = 'store-scu', 
+                   Instance = instanceId,
+                   Modality = modality 
+                })
+   return instanceId
+end
+
+
+function SendToPeer(instanceId, peer)
+   if instanceId == nil then
+      error('Cannot send a nonexistent instance')
+   end
+
+   table.insert(_job, { 
+                   Operation = 'store-peer', 
+                   Instance = instanceId,
+                   Peer = peer
+                })
+   return instanceId
+end
+
+
+function Delete(instanceId)
+   if instanceId == nil then
+      error('Cannot delete a nonexistent instance')
+   end
+
+   table.insert(_job, { 
+                   Operation = 'delete', 
+                   Instance = instanceId
+                })
+   return nil  -- Forbid chaining
+end
+
+
+function ModifyInstance(instanceId, replacements, removals, removePrivateTags)
+   if instanceId == nil then
+      error('Cannot modify a nonexistent instance')
+   end
+
+   if instanceId == '' then
+      error('Cannot modify twice an instance');
+   end
+
+   table.insert(_job, { 
+                   Operation = 'modify', 
+                   Instance = instanceId,
+                   Replace = replacements, 
+                   Remove = removals,
+                   RemovePrivateTags = removePrivateTags 
+                })
+   return ''  -- Chain with another operation
+end
+
+
+
 print('Lua toolbox installed')