changeset 438:7bbe77cb9e12

DELETE metadata
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 15 May 2013 16:51:10 +0200
parents beca6747945e
children 081a44d5110b
files OrthancServer/DatabaseWrapper.cpp OrthancServer/DatabaseWrapper.h OrthancServer/OrthancRestApi.cpp OrthancServer/ServerIndex.cpp OrthancServer/ServerIndex.h UnitTests/ServerIndex.cpp
diffstat 6 files changed, 61 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Wed May 15 16:43:30 2013 +0200
+++ b/OrthancServer/DatabaseWrapper.cpp	Wed May 15 16:51:10 2013 +0200
@@ -340,6 +340,15 @@
     s.Run();
   }
 
+  void DatabaseWrapper::DeleteMetadata(int64_t id,
+                                       MetadataType type)
+  {
+    SQLite::Statement s(db_, SQLITE_FROM_HERE, "DELETE FROM Metadata WHERE id=? and type=?");
+    s.BindInt(0, id);
+    s.BindInt(1, type);
+    s.Run();
+  }
+
   bool DatabaseWrapper::LookupMetadata(std::string& target,
                                        int64_t id,
                                        MetadataType type)
--- a/OrthancServer/DatabaseWrapper.h	Wed May 15 16:43:30 2013 +0200
+++ b/OrthancServer/DatabaseWrapper.h	Wed May 15 16:51:10 2013 +0200
@@ -108,6 +108,9 @@
                      MetadataType type,
                      const std::string& value);
 
+    void DeleteMetadata(int64_t id,
+                        MetadataType type);
+
     bool LookupMetadata(std::string& target,
                         int64_t id,
                         MetadataType type);
--- a/OrthancServer/OrthancRestApi.cpp	Wed May 15 16:43:30 2013 +0200
+++ b/OrthancServer/OrthancRestApi.cpp	Wed May 15 16:51:10 2013 +0200
@@ -1536,6 +1536,24 @@
   }
 
 
+  static void DeleteMetadata(RestApi::DeleteCall& call)
+  {
+    RETRIEVE_CONTEXT(call);
+    
+    std::string publicId = call.GetUriComponent("id", "");
+    std::string name = call.GetUriComponent("name", "");
+    MetadataType metadata = StringToMetadata(name);
+
+    if (metadata >= MetadataType_StartUser &&
+        metadata <= MetadataType_EndUser)
+    {
+      // It is forbidden to modify internal metadata
+      context.GetIndex().DeleteMetadata(publicId, metadata);
+      call.GetOutput().AnswerBuffer("", "text/plain");
+    }
+  }
+
+
   static void SetMetadata(RestApi::PutCall& call)
   {
     RETRIEVE_CONTEXT(call);
@@ -1591,15 +1609,19 @@
     Register("/series/{id}/archive", GetArchive<ResourceType_Series>);
 
     Register("/instances/{id}/metadata", ListMetadata);
+    Register("/instances/{id}/metadata/{name}", DeleteMetadata);
     Register("/instances/{id}/metadata/{name}", GetMetadata);
     Register("/instances/{id}/metadata/{name}", SetMetadata);
     Register("/patients/{id}/metadata", ListMetadata);
+    Register("/patients/{id}/metadata/{name}", DeleteMetadata);
     Register("/patients/{id}/metadata/{name}", GetMetadata);
     Register("/patients/{id}/metadata/{name}", SetMetadata);
     Register("/series/{id}/metadata", ListMetadata);
+    Register("/series/{id}/metadata/{name}", DeleteMetadata);
     Register("/series/{id}/metadata/{name}", GetMetadata);
     Register("/series/{id}/metadata/{name}", SetMetadata);
     Register("/studies/{id}/metadata", ListMetadata);
+    Register("/studies/{id}/metadata/{name}", DeleteMetadata);
     Register("/studies/{id}/metadata/{name}", GetMetadata);
     Register("/studies/{id}/metadata/{name}", SetMetadata);
 
--- a/OrthancServer/ServerIndex.cpp	Wed May 15 16:43:30 2013 +0200
+++ b/OrthancServer/ServerIndex.cpp	Wed May 15 16:51:10 2013 +0200
@@ -1123,6 +1123,23 @@
     db_->SetMetadata(id, type, value);
   }
 
+
+  void ServerIndex::DeleteMetadata(const std::string& publicId,
+                                   MetadataType type)
+  {
+    boost::mutex::scoped_lock lock(mutex_);
+
+    ResourceType rtype;
+    int64_t id;
+    if (!db_->LookupResource(publicId, id, rtype))
+    {
+      throw OrthancException(ErrorCode_UnknownResource);
+    }
+
+    db_->DeleteMetadata(id, type);
+  }
+
+
   bool ServerIndex::LookupMetadata(std::string& target,
                                    const std::string& publicId,
                                    MetadataType type)
--- a/OrthancServer/ServerIndex.h	Wed May 15 16:43:30 2013 +0200
+++ b/OrthancServer/ServerIndex.h	Wed May 15 16:51:10 2013 +0200
@@ -150,6 +150,9 @@
                      MetadataType type,
                      const std::string& value);
 
+    void DeleteMetadata(const std::string& publicId,
+                        MetadataType type);
+
     bool LookupMetadata(std::string& target,
                         const std::string& publicId,
                         MetadataType type);
--- a/UnitTests/ServerIndex.cpp	Wed May 15 16:43:30 2013 +0200
+++ b/UnitTests/ServerIndex.cpp	Wed May 15 16:51:10 2013 +0200
@@ -147,6 +147,13 @@
   index.ListAvailableMetadata(md, a[4]);
   ASSERT_EQ(1u, md.size());
   ASSERT_EQ(MetadataType_Instance_RemoteAet, md.front());
+  index.SetMetadata(a[4], MetadataType_ModifiedFrom, "TUTU");
+  index.ListAvailableMetadata(md, a[4]);
+  ASSERT_EQ(2u, md.size());
+  index.DeleteMetadata(a[4], MetadataType_ModifiedFrom);
+  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());