changeset 436:d51186bf7602

read access to metadata
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 15 May 2013 16:28:00 +0200
parents 28ba73274919
children beca6747945e
files Core/EnumerationDictionary.h OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/OrthancRestApi.cpp OrthancServer/ServerEnumerations.cpp OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h UnitTests/ServerIndex.cpp UnitTests/main.cpp
diffstat 9 files changed, 101 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/Core/EnumerationDictionary.h	Wed May 15 15:57:05 2013 +0200
+++ b/Core/EnumerationDictionary.h	Wed May 15 16:28:00 2013 +0200
@@ -102,14 +102,15 @@
         }
       }
 
-      const std::string& Translate(Enumeration e) const
+      std::string Translate(Enumeration e) const
       {
         typename EnumerationToString::const_iterator
           found = enumerationToString_.find(e);
 
         if (found == enumerationToString_.end())
         {
-          throw OrthancException(ErrorCode_ParameterOutOfRange);
+          // No name for this item
+          return boost::lexical_cast<std::string>(static_cast<int>(e));
         }
         else
         {
--- a/OrthancServer/DatabaseWrapper.cpp	Wed May 15 15:57:05 2013 +0200
+++ b/OrthancServer/DatabaseWrapper.cpp	Wed May 15 16:28:00 2013 +0200
@@ -360,6 +360,23 @@
     }
   }
 
+  bool DatabaseWrapper::ListAvailableMetadata(std::list<MetadataType>& target,
+                                              int64_t id)
+  {
+    target.clear();
+
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT type FROM Metadata WHERE id=?");
+    s.BindInt(0, id);
+
+    while (s.Step())
+    {
+      target.push_back(static_cast<MetadataType>(s.ColumnInt(0)));
+    }
+
+    return true;
+  }
+
+
   std::string DatabaseWrapper::GetMetadata(int64_t id,
                                            MetadataType type,
                                            const std::string& defaultValue)
--- a/OrthancServer/DatabaseWrapper.h	Wed May 15 15:57:05 2013 +0200
+++ b/OrthancServer/DatabaseWrapper.h	Wed May 15 16:28:00 2013 +0200
@@ -112,6 +112,9 @@
                         int64_t id,
                         MetadataType type);
 
+    bool ListAvailableMetadata(std::list<MetadataType>& target,
+                               int64_t id);
+
     std::string GetMetadata(int64_t id,
                             MetadataType type,
                             const std::string& defaultValue = "");
--- a/OrthancServer/OrthancRestApi.cpp	Wed May 15 15:57:05 2013 +0200
+++ b/OrthancServer/OrthancRestApi.cpp	Wed May 15 16:28:00 2013 +0200
@@ -1497,6 +1497,46 @@
   }
 
 
+  // Handling of metadata -----------------------------------------------------
+
+  template <enum ResourceType resourceType>
+  static void ListMetadata(RestApi::GetCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+    
+    std::string publicId = call.GetUriComponent("id", "");
+    std::list<MetadataType> metadata;
+    if (context.GetIndex().ListAvailableMetadata(metadata, publicId))
+    {
+      Json::Value result = Json::arrayValue;
+
+      for (std::list<MetadataType>::const_iterator 
+             it = metadata.begin(); it != metadata.end(); it++)
+      {
+        result.append(EnumerationToString(*it));
+      }
+
+      call.GetOutput().AnswerJson(result);
+    }
+  }
+
+
+  template <enum ResourceType resourceType>
+  static void GetMetadata(RestApi::GetCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+    
+    std::string publicId = call.GetUriComponent("id", "");
+    std::string name = call.GetUriComponent("name", "");
+    MetadataType metadata = StringToMetadata(name);
+
+    std::string value;
+    if (context.GetIndex().LookupMetadata(value, publicId, metadata))
+    {
+      call.GetOutput().AnswerBuffer(value, "text/plain");
+    }
+  }
+
 
   // Registration of the various REST handlers --------------------------------
 
@@ -1532,6 +1572,15 @@
     Register("/studies/{id}/archive", GetArchive<ResourceType_Study>);
     Register("/series/{id}/archive", GetArchive<ResourceType_Series>);
 
+    Register("/instances/{id}/metadata", ListMetadata<ResourceType_Instance>);
+    Register("/instances/{id}/metadata/{name}", GetMetadata<ResourceType_Instance>);
+    Register("/patients/{id}/metadata", ListMetadata<ResourceType_Patient>);
+    Register("/patients/{id}/metadata/{name}", GetMetadata<ResourceType_Patient>);
+    Register("/series/{id}/metadata", ListMetadata<ResourceType_Series>);
+    Register("/series/{id}/metadata/{name}", GetMetadata<ResourceType_Series>);
+    Register("/studies/{id}/metadata", ListMetadata<ResourceType_Study>);
+    Register("/studies/{id}/metadata/{name}", GetMetadata<ResourceType_Study>);
+
     Register("/patients/{id}/protected", IsProtectedPatient);
     Register("/patients/{id}/protected", SetPatientProtection);
     Register("/instances/{id}/file", GetInstanceFile);
--- a/OrthancServer/ServerEnumerations.cpp	Wed May 15 15:57:05 2013 +0200
+++ b/OrthancServer/ServerEnumerations.cpp	Wed May 15 16:28:00 2013 +0200
@@ -44,7 +44,7 @@
   void InitializeServerEnumerations()
   {
     boost::mutex::scoped_lock lock(enumerationsMutex_);
-
+    
     dictMetadataType_.Add(MetadataType_Instance_IndexInSeries, "IndexInSeries");
     dictMetadataType_.Add(MetadataType_Instance_ReceptionDate, "ReceptionDate");
     dictMetadataType_.Add(MetadataType_Instance_RemoteAet, "RemoteAET");
--- a/OrthancServer/ServerIndex.cpp	Wed May 15 15:57:05 2013 +0200
+++ b/OrthancServer/ServerIndex.cpp	Wed May 15 16:28:00 2013 +0200
@@ -1140,6 +1140,22 @@
   }
 
 
+  bool ServerIndex::ListAvailableMetadata(std::list<MetadataType>& target,
+                                          const std::string& publicId)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    ResourceType rtype;
+    int64_t id;
+    if (!db_->LookupResource(publicId, id, rtype))
+    {
+      throw OrthancException(ErrorCode_UnknownResource);
+    }
+
+    return db_->ListAvailableMetadata(target, id);
+  }
+
+
   bool ServerIndex::LookupParent(std::string& target,
                                  const std::string& publicId)
   {
--- a/OrthancServer/ServerIndex.h	Wed May 15 15:57:05 2013 +0200
+++ b/OrthancServer/ServerIndex.h	Wed May 15 16:28:00 2013 +0200
@@ -154,6 +154,9 @@
                         const std::string& publicId,
                         MetadataType type);
 
+    bool ListAvailableMetadata(std::list<MetadataType>& target,
+                               const std::string& publicId);
+
     bool LookupParent(std::string& target,
                       const std::string& publicId);
 
--- a/UnitTests/ServerIndex.cpp	Wed May 15 15:57:05 2013 +0200
+++ b/UnitTests/ServerIndex.cpp	Wed May 15 16:28:00 2013 +0200
@@ -135,10 +135,18 @@
     ASSERT_EQ("e", l.front());
   }
 
+  std::list<MetadataType> md;
+  index.ListAvailableMetadata(md, a[4]);
+  ASSERT_EQ(0u, md.size());
+
   index.AddAttachment(a[4], FileInfo("my json file", FileContentType_Json, 42, CompressionType_Zlib, 21));
   index.AddAttachment(a[4], FileInfo("my dicom file", FileContentType_Dicom, 42));
   index.AddAttachment(a[6], FileInfo("world", FileContentType_Dicom, 44));
   index.SetMetadata(a[4], MetadataType_Instance_RemoteAet, "PINNACLE");
+  
+  index.ListAvailableMetadata(md, a[4]);
+  ASSERT_EQ(1u, md.size());
+  ASSERT_EQ(MetadataType_Instance_RemoteAet, md.front());
 
   ASSERT_EQ(21u + 42u + 44u, index.GetTotalCompressedSize());
   ASSERT_EQ(42u + 42u + 44u, index.GetTotalUncompressedSize());
--- a/UnitTests/main.cpp	Wed May 15 15:57:05 2013 +0200
+++ b/UnitTests/main.cpp	Wed May 15 16:28:00 2013 +0200
@@ -355,6 +355,7 @@
 
   ASSERT_THROW(d.Translate("ReceptionDate"), OrthancException);
   ASSERT_EQ(MetadataType_ModifiedFrom, d.Translate("5"));
+  ASSERT_EQ(256, d.Translate("256"));
 
   d.Add(MetadataType_Instance_ReceptionDate, "ReceptionDate");