changeset 437:beca6747945e

PUT on metadata
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 15 May 2013 16:43:30 +0200
parents d51186bf7602
children 7bbe77cb9e12
files OrthancServer/OrthancRestApi.cpp OrthancServer/ServerEnumerations.cpp OrthancServer/ServerEnumerations.h UnitTests/main.cpp
diffstat 4 files changed, 39 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi.cpp	Wed May 15 16:28:00 2013 +0200
+++ b/OrthancServer/OrthancRestApi.cpp	Wed May 15 16:43:30 2013 +0200
@@ -1499,7 +1499,6 @@
 
   // Handling of metadata -----------------------------------------------------
 
-  template <enum ResourceType resourceType>
   static void ListMetadata(RestApi::GetCall& call)
   {
     RETRIEVE_CONTEXT(call);
@@ -1521,7 +1520,6 @@
   }
 
 
-  template <enum ResourceType resourceType>
   static void GetMetadata(RestApi::GetCall& call)
   {
     RETRIEVE_CONTEXT(call);
@@ -1538,6 +1536,26 @@
   }
 
 
+  static void SetMetadata(RestApi::PutCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+
+    std::string publicId = call.GetUriComponent("id", "");
+    std::string name = call.GetUriComponent("name", "");
+    MetadataType metadata = StringToMetadata(name);
+    std::string value = call.GetPutBody();
+
+    if (metadata >= MetadataType_StartUser &&
+        metadata <= MetadataType_EndUser)
+    {
+      // It is forbidden to modify internal metadata
+      context.GetIndex().SetMetadata(publicId, metadata, value);
+      call.GetOutput().AnswerBuffer("", "text/plain");
+    }
+  }
+
+
+
   // Registration of the various REST handlers --------------------------------
 
   OrthancRestApi::OrthancRestApi(ServerContext& context) : 
@@ -1572,14 +1590,18 @@
     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("/instances/{id}/metadata", ListMetadata);
+    Register("/instances/{id}/metadata/{name}", GetMetadata);
+    Register("/instances/{id}/metadata/{name}", SetMetadata);
+    Register("/patients/{id}/metadata", ListMetadata);
+    Register("/patients/{id}/metadata/{name}", GetMetadata);
+    Register("/patients/{id}/metadata/{name}", SetMetadata);
+    Register("/series/{id}/metadata", ListMetadata);
+    Register("/series/{id}/metadata/{name}", GetMetadata);
+    Register("/series/{id}/metadata/{name}", SetMetadata);
+    Register("/studies/{id}/metadata", ListMetadata);
+    Register("/studies/{id}/metadata/{name}", GetMetadata);
+    Register("/studies/{id}/metadata/{name}", SetMetadata);
 
     Register("/patients/{id}/protected", IsProtectedPatient);
     Register("/patients/{id}/protected", SetPatientProtection);
--- a/OrthancServer/ServerEnumerations.cpp	Wed May 15 16:28:00 2013 +0200
+++ b/OrthancServer/ServerEnumerations.cpp	Wed May 15 16:43:30 2013 +0200
@@ -68,10 +68,12 @@
     dictMetadataType_.Add(static_cast<MetadataType>(metadata), name);
   }
 
-  const char* EnumerationToString(MetadataType type)
+  std::string EnumerationToString(MetadataType type)
   {
+    // This function MUST return a "std::string" and not "const
+    // char*", as the result is not a static string
     boost::mutex::scoped_lock lock(enumerationsMutex_);
-    return dictMetadataType_.Translate(type).c_str();
+    return dictMetadataType_.Translate(type);
   }
 
   MetadataType StringToMetadata(const std::string& str)
--- a/OrthancServer/ServerEnumerations.h	Wed May 15 16:28:00 2013 +0200
+++ b/OrthancServer/ServerEnumerations.h	Wed May 15 16:43:30 2013 +0200
@@ -115,7 +115,7 @@
 
   const char* EnumerationToString(ResourceType type);
 
-  const char* EnumerationToString(MetadataType type);
+  std::string EnumerationToString(MetadataType type);
 
   const char* EnumerationToString(SeriesStatus status);
 
--- a/UnitTests/main.cpp	Wed May 15 16:28:00 2013 +0200
+++ b/UnitTests/main.cpp	Wed May 15 16:43:30 2013 +0200
@@ -384,8 +384,8 @@
 
   ASSERT_STREQ("CompletedSeries", EnumerationToString(ChangeType_CompletedSeries));
 
-  ASSERT_STREQ("IndexInSeries", EnumerationToString(MetadataType_Instance_IndexInSeries));
-  ASSERT_STREQ("LastUpdate", EnumerationToString(MetadataType_LastUpdate));
+  ASSERT_EQ("IndexInSeries", EnumerationToString(MetadataType_Instance_IndexInSeries));
+  ASSERT_EQ("LastUpdate", EnumerationToString(MetadataType_LastUpdate));
 
   ASSERT_EQ(2047, StringToMetadata("2047"));
   ASSERT_THROW(StringToMetadata("Ceci est un test"), OrthancException);