diff OrthancServer/LuaScripting.cpp @ 3187:4bbadcd03966

refactoring retrieval of metadata from database
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 04 Feb 2019 12:06:19 +0100
parents 4e43e67f8ecf
children dd1e68f2d0c0
line wrap: on
line diff
--- a/OrthancServer/LuaScripting.cpp	Fri Feb 01 09:28:12 2019 +0100
+++ b/OrthancServer/LuaScripting.cpp	Mon Feb 04 12:06:19 2019 +0100
@@ -164,23 +164,37 @@
         }
       }
       
-      Json::Value tags, metadata;
-      if (that.context_.GetIndex().LookupResource(tags, change_.GetPublicId(), change_.GetResourceType()) &&
-          that.context_.GetIndex().GetMetadata(metadata, change_.GetPublicId()))
+      Json::Value tags;
+      
+      if (that.context_.GetIndex().LookupResource(tags, change_.GetPublicId(), change_.GetResourceType()))
       {
-        LuaScripting::Lock lock(that);
+        std::map<MetadataType, std::string> metadata;
+        that.context_.GetIndex().GetAllMetadata(metadata, change_.GetPublicId());
+        
+        Json::Value formattedMetadata = Json::objectValue;
 
-        if (lock.GetLua().IsExistingFunction(name))
+        for (std::map<MetadataType, std::string>::const_iterator 
+               it = metadata.begin(); it != metadata.end(); ++it)
         {
-          that.InitializeJob();
+          std::string key = EnumerationToString(it->first);
+          formattedMetadata[key] = it->second;
+        }      
+
+        {
+          LuaScripting::Lock lock(that);
 
-          LuaFunctionCall call(lock.GetLua(), name);
-          call.PushString(change_.GetPublicId());
-          call.PushJson(tags["MainDicomTags"]);
-          call.PushJson(metadata);
-          call.Execute();
+          if (lock.GetLua().IsExistingFunction(name))
+          {
+            that.InitializeJob();
 
-          that.SubmitJob();
+            LuaFunctionCall call(lock.GetLua(), name);
+            call.PushString(change_.GetPublicId());
+            call.PushJson(tags["MainDicomTags"]);
+            call.PushJson(formattedMetadata);
+            call.Execute();
+
+            that.SubmitJob();
+          }
         }
       }
     }