changeset 3676:231b46ce1984

Lua events for deleted/updated resources
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 19 Feb 2020 14:19:49 +0100
parents ea8c1c0e81eb
children 4182cde57afb
files NEWS OrthancServer/LuaScripting.cpp OrthancServer/LuaScripting.h
diffstat 3 files changed, 128 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Fri Feb 14 17:22:28 2020 +0100
+++ b/NEWS	Wed Feb 19 14:19:49 2020 +0100
@@ -16,6 +16,15 @@
 
 * New sample plugin: "ConnectivityChecks"
 
+Lua
+---
+
+* New events:
+  - "OnDeletedPatient", "OnDeletedStudy", "OnDeletedSeries", "OnDeletedInstance":
+    triggered when a resource is deleted
+  - "OnUpdatedPatient", "OnUpdatedStudy", "OnUpdatedSeries", "OnUpdatedInstance":
+    triggered when an attachment or a metadata is updated
+
 Maintenance
 -----------
 
--- a/OrthancServer/LuaScripting.cpp	Fri Feb 14 17:22:28 2020 +0100
+++ b/OrthancServer/LuaScripting.cpp	Wed Feb 19 14:19:49 2020 +0100
@@ -259,6 +259,114 @@
   };
 
 
+  class LuaScripting::DeleteEvent : public LuaScripting::IEvent
+  {
+  private:
+    ResourceType  level_;
+    std::string   publicId_;
+
+  public:
+    DeleteEvent(ResourceType level,
+                const std::string& publicId) :
+      level_(level),
+      publicId_(publicId)
+    {
+    }
+
+    virtual void Apply(LuaScripting& that)
+    {
+      std::string functionName;
+      
+      switch (level_)
+      {
+        case ResourceType_Patient:
+          functionName = "OnDeletedPatient";
+          break;
+
+        case ResourceType_Study:
+          functionName = "OnDeletedStudy";
+          break;
+
+        case ResourceType_Series:
+          functionName = "OnDeletedSeries";
+          break;
+
+        case ResourceType_Instance:
+          functionName = "OnDeletedInstance";
+          break;
+
+        default:
+          throw OrthancException(ErrorCode_InternalError);
+      }
+
+      {
+        LuaScripting::Lock lock(that);
+
+        if (lock.GetLua().IsExistingFunction(functionName.c_str()))
+        {
+          LuaFunctionCall call(lock.GetLua(), functionName.c_str());
+          call.PushString(publicId_);
+          call.Execute();
+        }
+      }
+    }
+  };
+
+
+  class LuaScripting::UpdateEvent : public LuaScripting::IEvent
+  {
+  private:
+    ResourceType  level_;
+    std::string   publicId_;
+
+  public:
+    UpdateEvent(ResourceType level,
+                const std::string& publicId) :
+      level_(level),
+      publicId_(publicId)
+    {
+    }
+
+    virtual void Apply(LuaScripting& that)
+    {
+      std::string functionName;
+      
+      switch (level_)
+      {
+        case ResourceType_Patient:
+          functionName = "OnUpdatedPatient";
+          break;
+
+        case ResourceType_Study:
+          functionName = "OnUpdatedStudy";
+          break;
+
+        case ResourceType_Series:
+          functionName = "OnUpdatedSeries";
+          break;
+
+        case ResourceType_Instance:
+          functionName = "OnUpdatedInstance";
+          break;
+
+        default:
+          throw OrthancException(ErrorCode_InternalError);
+      }
+
+      {
+        LuaScripting::Lock lock(that);
+
+        if (lock.GetLua().IsExistingFunction(functionName.c_str()))
+        {
+          LuaFunctionCall call(lock.GetLua(), functionName.c_str());
+          call.PushString(publicId_);
+          call.Execute();
+        }
+      }
+    }
+  };
+
+
   ServerContext* LuaScripting::GetServerContext(lua_State *state)
   {
     const void* value = LuaContext::GetGlobalVariable(state, "_ServerContext");
@@ -738,6 +846,15 @@
     {
       pendingEvents_.Enqueue(new StableResourceEvent(change));
     }
+    else if (change.GetChangeType() == ChangeType_Deleted)
+    {
+      pendingEvents_.Enqueue(new DeleteEvent(change.GetResourceType(), change.GetPublicId()));
+    }
+    else if (change.GetChangeType() == ChangeType_UpdatedAttachment ||
+             change.GetChangeType() == ChangeType_UpdatedMetadata)
+    {
+      pendingEvents_.Enqueue(new UpdateEvent(change.GetResourceType(), change.GetPublicId()));
+    }
   }
 
 
--- a/OrthancServer/LuaScripting.h	Fri Feb 14 17:22:28 2020 +0100
+++ b/OrthancServer/LuaScripting.h	Wed Feb 19 14:19:49 2020 +0100
@@ -59,6 +59,8 @@
     class OnStoredInstanceEvent;
     class StableResourceEvent;
     class JobEvent;
+    class DeleteEvent;
+    class UpdateEvent;
 
     static ServerContext* GetServerContext(lua_State *state);