changeset 1006:649d47854314 lua-scripting

proper handling of metadata in Store
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 08 Jul 2014 15:11:00 +0200
parents 84b6d7bca6db
children 871c49c9b11d
files OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/DicomInstanceToStore.h OrthancServer/ServerContext.cpp OrthancServer/ServerEnumerations.h OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h Resources/Samples/Lua/Autorouting.lua Resources/Toolbox.lua UnitTestsSources/ServerIndexTests.cpp
diffstat 10 files changed, 83 insertions(+), 108 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Tue Jul 08 14:34:11 2014 +0200
+++ b/OrthancServer/DatabaseWrapper.cpp	Tue Jul 08 15:11:00 2014 +0200
@@ -1045,4 +1045,21 @@
       result.push_back(s.ColumnInt64(0));
     }
   }
+
+
+  void DatabaseWrapper::GetAllMetadata(std::map<MetadataType, std::string>& result,
+                                       int64_t id)
+  {
+    result.clear();
+
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT type, value FROM Metadata WHERE id=?");
+    s.BindInt64(0, id);
+
+    while (s.Step())
+    {
+      MetadataType key = static_cast<MetadataType>(s.ColumnInt(0));
+      result[key] = s.ColumnString(1);
+    }
+  }
+
 }
--- a/OrthancServer/DatabaseWrapper.h	Tue Jul 08 14:34:11 2014 +0200
+++ b/OrthancServer/DatabaseWrapper.h	Tue Jul 08 15:11:00 2014 +0200
@@ -235,5 +235,8 @@
 
     void LookupTagValue(std::list<int64_t>& result,
                         const std::string& value);
+
+    void GetAllMetadata(std::map<MetadataType, std::string>& result,
+                        int64_t id);
   };
 }
--- a/OrthancServer/DicomInstanceToStore.h	Tue Jul 08 14:34:11 2014 +0200
+++ b/OrthancServer/DicomInstanceToStore.h	Tue Jul 08 15:11:00 2014 +0200
@@ -138,49 +138,6 @@
     };
 
 
-    /*class MemoryBuffer
-    {
-    private:
-      const char* buffer_;
-      size_t size_;
-
-    public:
-      MemoryBuffer() : buffer_(NULL), size_(0)
-      {
-      }
-
-      const char* GetBuffer() const
-      {
-        return buffer_;
-      }
-
-      size_t GetSize() const
-      {
-        return size_;
-      }
-
-      void Assign(const char* buffer, size_t size)
-      {
-        buffer_ = buffer;
-        size_ = size;
-      }
-
-      void Assign(const std::string& buffer)
-      {
-        size_ = buffer.size();
-
-        if (size_ == 0)
-        {
-          buffer_ = NULL;
-        }
-        else
-        {
-          buffer_ = &buffer[0];
-        }
-      }
-    };*/
-
-
     SmartContainer<std::string>  buffer_;
     SmartContainer<ParsedDicomFile>  parsed_;
     SmartContainer<DicomMap>  summary_;
--- a/OrthancServer/ServerContext.cpp	Tue Jul 08 14:34:11 2014 +0200
+++ b/OrthancServer/ServerContext.cpp	Tue Jul 08 15:11:00 2014 +0200
@@ -193,7 +193,9 @@
       attachments.push_back(dicomInfo);
       attachments.push_back(jsonInfo);
 
-      StoreStatus status = index_.Store(dicom.GetSummary(), attachments, dicom.GetRemoteAet(), dicom.GetMetadata());
+      std::map<MetadataType, std::string> instanceMetadata;
+      StoreStatus status = index_.Store(instanceMetadata, dicom.GetSummary(), attachments, 
+                                        dicom.GetRemoteAet(), dicom.GetMetadata());
 
       if (status != StoreStatus_Success)
       {
@@ -225,16 +227,20 @@
       {
         try
         {
+#if 1
           Json::Value metadata = Json::objectValue;
-          for (ServerIndex::MetadataMap::const_iterator 
-                 it = dicom.GetMetadata().begin(); 
-               it != dicom.GetMetadata().end(); ++it)
+          for (std::map<MetadataType, std::string>::const_iterator 
+                 it = instanceMetadata.begin(); 
+               it != instanceMetadata.end(); ++it)
           {
-            if (it->first.first == ResourceType_Instance)
-            {
-              metadata[EnumerationToString(it->first.second)] = it->second;
-            }
+            metadata[EnumerationToString(it->first)] = it->second;
           }
+#else
+          Json::Value metadata;
+          index_.GetMetadata(metadata, resultPublicId);
+#endif
+
+          std::cout << metadata;
 
           ApplyOnStoredInstance(resultPublicId, simplified, metadata);
         }
--- a/OrthancServer/ServerEnumerations.h	Tue Jul 08 14:34:11 2014 +0200
+++ b/OrthancServer/ServerEnumerations.h	Tue Jul 08 15:11:00 2014 +0200
@@ -32,6 +32,7 @@
 #pragma once
 
 #include <string>
+#include <map>
 
 #include "../Core/Enumerations.h"
 
@@ -124,6 +125,8 @@
     ChangeType_StableSeries = 14
   };
 
+
+
   void InitializeServerEnumerations();
 
   void RegisterUserMetadata(int metadata,
--- a/OrthancServer/ServerIndex.cpp	Tue Jul 08 14:34:11 2014 +0200
+++ b/OrthancServer/ServerIndex.cpp	Tue Jul 08 15:11:00 2014 +0200
@@ -382,14 +382,17 @@
   }
 
 
-  StoreStatus ServerIndex::Store(const DicomMap& dicomSummary,
+  StoreStatus ServerIndex::Store(std::map<MetadataType, std::string>& instanceMetadata,
+                                 const DicomMap& dicomSummary,
                                  const Attachments& attachments,
                                  const std::string& remoteAet,
-                                 MetadataMap* metadata)
+                                 const MetadataMap& metadata)
   {
     boost::mutex::scoped_lock lock(mutex_);
     listener_->Reset();
 
+    instanceMetadata.clear();
+
     DicomInstanceHasher hasher(dicomSummary);
 
     try
@@ -403,6 +406,7 @@
         if (db_->LookupResource(hasher.HashInstance(), tmp, type))
         {
           assert(type == ResourceType_Instance);
+          db_->GetAllMetadata(instanceMetadata, tmp);
           return StoreStatus_AlreadyStored;
         }
       }
@@ -521,32 +525,30 @@
       }
 
       // Attach the user-specified metadata
-      if (metadata)
+      for (MetadataMap::const_iterator 
+             it = metadata.begin(); it != metadata.end(); ++it)
       {
-        for (MetadataMap::const_iterator 
-               it = metadata->begin(); it != metadata->end(); ++it)
+        switch (it->first.first)
         {
-          switch (it->first.first)
-          {
-            case ResourceType_Patient:
-              db_->SetMetadata(patient, it->first.second, it->second);
-              break;
+          case ResourceType_Patient:
+            db_->SetMetadata(patient, it->first.second, it->second);
+            break;
 
-            case ResourceType_Study:
-              db_->SetMetadata(study, it->first.second, it->second);
-              break;
+          case ResourceType_Study:
+            db_->SetMetadata(study, it->first.second, it->second);
+            break;
 
-            case ResourceType_Series:
-              db_->SetMetadata(series, it->first.second, it->second);
-              break;
+          case ResourceType_Series:
+            db_->SetMetadata(series, it->first.second, it->second);
+            break;
 
-            case ResourceType_Instance:
-              db_->SetMetadata(instance, it->first.second, it->second);
-              break;
+          case ResourceType_Instance:
+            db_->SetMetadata(instance, it->first.second, it->second);
+            instanceMetadata[it->first.second] = it->second;
+            break;
 
-            default:
-              throw OrthancException(ErrorCode_ParameterOutOfRange);
-          }
+          default:
+            throw OrthancException(ErrorCode_ParameterOutOfRange);
         }
       }
 
@@ -559,24 +561,17 @@
       // Attach the auto-computed metadata for the instance level,
       // reflecting these additions into the input metadata map
       db_->SetMetadata(instance, MetadataType_Instance_ReceptionDate, now);
-      db_->SetMetadata(instance, MetadataType_Instance_RemoteAet, remoteAet);
+      instanceMetadata[MetadataType_Instance_ReceptionDate] = now;
 
-      if (metadata)
-      {
-        (*metadata) [std::make_pair(ResourceType_Instance, MetadataType_Instance_ReceptionDate)] = now;
-        (*metadata) [std::make_pair(ResourceType_Instance, MetadataType_Instance_RemoteAet)] = remoteAet;
-      }
+      db_->SetMetadata(instance, MetadataType_Instance_RemoteAet, remoteAet);
+      instanceMetadata[MetadataType_Instance_RemoteAet] = remoteAet;
 
       const DicomValue* value;
       if ((value = dicomSummary.TestAndGetValue(DICOM_TAG_INSTANCE_NUMBER)) != NULL ||
           (value = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGE_INDEX)) != NULL)
       {
         db_->SetMetadata(instance, MetadataType_Instance_IndexInSeries, value->AsString());
-
-        if (metadata)
-        {
-          (*metadata) [std::make_pair(ResourceType_Instance, MetadataType_Instance_IndexInSeries)] = value->AsString();
-        }
+        instanceMetadata[MetadataType_Instance_IndexInSeries] = value->AsString();
       }
 
       // Check whether the series of this new instance is now completed
--- a/OrthancServer/ServerIndex.h	Tue Jul 08 14:34:11 2014 +0200
+++ b/OrthancServer/ServerIndex.h	Tue Jul 08 15:11:00 2014 +0200
@@ -102,11 +102,6 @@
                                /* in  */ int64_t id,
                                /* in  */ ResourceType type);
 
-    StoreStatus Store(const DicomMap& dicomSummary,
-                      const Attachments& attachments,
-                      const std::string& remoteAet,
-                      MetadataMap* metadata);
-
   public:
     ServerIndex(ServerContext& context,
                 const std::string& dbPath);
@@ -129,20 +124,11 @@
     // "count == 0" means no limit on the number of patients
     void SetMaximumPatientCount(unsigned int count);
 
-    StoreStatus Store(const DicomMap& dicomSummary,
-                      const Attachments& attachments,
-                      const std::string& remoteAet)
-    {
-      return Store(dicomSummary, attachments, remoteAet, NULL);
-    }
-
-    StoreStatus Store(const DicomMap& dicomSummary,
+    StoreStatus Store(std::map<MetadataType, std::string>& instanceMetadata,
+                      const DicomMap& dicomSummary,
                       const Attachments& attachments,
                       const std::string& remoteAet,
-                      MetadataMap& metadata)
-    {
-      return Store(dicomSummary, attachments, remoteAet, &metadata);
-    }
+                      const MetadataMap& metadata);
 
     void ComputeStatistics(Json::Value& target);                        
 
--- a/Resources/Samples/Lua/Autorouting.lua	Tue Jul 08 14:34:11 2014 +0200
+++ b/Resources/Samples/Lua/Autorouting.lua	Tue Jul 08 15:11:00 2014 +0200
@@ -1,8 +1,9 @@
 function OnStoredInstance(instance, tags, metadata)
    --PrintRecursive(tags)
-  PrintRecursive(metadata)
-  return { 
-    { "store", instance, "pacs" }, 
-    { "delete", instance } 
-  }
+   PrintRecursive(metadata)
+   print(metadata['RemoteAET'])
+   return { 
+      { "store", instance, "pacs" }, 
+      { "delete", instance } 
+   }
 end
--- a/Resources/Toolbox.lua	Tue Jul 08 14:34:11 2014 +0200
+++ b/Resources/Toolbox.lua	Tue Jul 08 15:11:00 2014 +0200
@@ -6,7 +6,8 @@
 --]]
 
 function PrintRecursive(s, l, i) -- recursive Print (structure, limit, indent)
-   l = (l) or 100; i = i or "";	-- default item limit, indent string
+   l = (l) or 100;  -- default item limit
+   i = i or "";     -- indent string
    if (l<1) then print "ERROR: Item limit reached."; return l-1 end;
    local ts = type(s);
    if (ts ~= "table") then print (i,ts,s); return l-1 end
--- a/UnitTestsSources/ServerIndexTests.cpp	Tue Jul 08 14:34:11 2014 +0200
+++ b/UnitTestsSources/ServerIndexTests.cpp	Tue Jul 08 15:11:00 2014 +0200
@@ -579,7 +579,13 @@
     instance.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, "study-" + id);
     instance.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, "series-" + id);
     instance.SetValue(DICOM_TAG_SOP_INSTANCE_UID, "instance-" + id);
-    ASSERT_EQ(StoreStatus_Success, index.Store(instance, attachments, ""));
+
+    std::map<MetadataType, std::string> instanceMetadata;
+    ServerIndex::MetadataMap metadata;
+    ASSERT_EQ(StoreStatus_Success, index.Store(instanceMetadata, instance, attachments, "", metadata));
+    ASSERT_EQ(2, instanceMetadata.size());
+    ASSERT_NE(instanceMetadata.end(), instanceMetadata.find(MetadataType_Instance_RemoteAet));
+    ASSERT_NE(instanceMetadata.end(), instanceMetadata.find(MetadataType_Instance_ReceptionDate));
 
     DicomInstanceHasher hasher(instance);
     ids.push_back(hasher.HashPatient());