# HG changeset patch # User Sebastien Jodogne # Date 1582118389 -3600 # Node ID 231b46ce19844b18825fcf13553d6e7e622ba42b # Parent ea8c1c0e81ebab38e105b5caca6ca4beb047f51a Lua events for deleted/updated resources diff -r ea8c1c0e81eb -r 231b46ce1984 NEWS --- 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 ----------- diff -r ea8c1c0e81eb -r 231b46ce1984 OrthancServer/LuaScripting.cpp --- 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())); + } } diff -r ea8c1c0e81eb -r 231b46ce1984 OrthancServer/LuaScripting.h --- 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);