changeset 5239:178b0434256a db-protobuf

added primitives to exchange labels with plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 05 Apr 2023 11:31:05 +0200
parents 367e8af46cfd
children efaeec259623
files OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto
diffstat 2 files changed, 90 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Wed Apr 05 10:02:19 2023 +0200
+++ b/OrthancServer/Plugins/Engine/OrthancPluginDatabaseV4.cpp	Wed Apr 05 11:31:05 2023 +0200
@@ -976,10 +976,14 @@
         }
       }
 
-      if (!withLabels.empty() ||
-          !withoutLabels.empty())
+      for (std::set<std::string>::const_iterator it = withLabels.begin(); it != withLabels.end(); ++it)
       {
-        throw OrthancException(ErrorCode_NotImplemented);  // TODO
+        request.mutable_lookup_resources()->add_with_labels(*it);
+      }
+      
+      for (std::set<std::string>::const_iterator it = withoutLabels.begin(); it != withoutLabels.end(); ++it)
+      {
+        request.mutable_lookup_resources()->add_without_labels(*it);
       }
       
       DatabasePluginMessages::TransactionResponse response;
@@ -1152,21 +1156,63 @@
     virtual void AddLabel(int64_t resource,
                           const std::string& label) ORTHANC_OVERRIDE
     {
-      throw OrthancException(ErrorCode_NotImplemented);
+      if (database_.HasLabelsSupport())
+      {
+        DatabasePluginMessages::TransactionRequest request;
+        request.mutable_add_label()->set_id(resource);
+        request.mutable_add_label()->set_label(label);
+
+        ExecuteTransaction(DatabasePluginMessages::OPERATION_ADD_LABEL, request);
+      }
+      else
+      {
+        // This method shouldn't have been called
+        throw OrthancException(ErrorCode_InternalError);
+      }
     }
 
 
     virtual void RemoveLabel(int64_t resource,
                              const std::string& label) ORTHANC_OVERRIDE
     {
-      throw OrthancException(ErrorCode_NotImplemented);
+      if (database_.HasLabelsSupport())
+      {
+        DatabasePluginMessages::TransactionRequest request;
+        request.mutable_remove_label()->set_id(resource);
+        request.mutable_remove_label()->set_label(label);
+
+        ExecuteTransaction(DatabasePluginMessages::OPERATION_REMOVE_LABEL, request);
+      }
+      else
+      {
+        // This method shouldn't have been called
+        throw OrthancException(ErrorCode_InternalError);
+      }
     }
 
 
     virtual void ListLabels(std::set<std::string>& target,
                             int64_t resource) ORTHANC_OVERRIDE
     {
-      throw OrthancException(ErrorCode_NotImplemented);
+      if (database_.HasLabelsSupport())
+      {
+        DatabasePluginMessages::TransactionRequest request;
+        request.mutable_list_labels()->set_id(resource);
+
+        DatabasePluginMessages::TransactionResponse response;
+        ExecuteTransaction(response, DatabasePluginMessages::OPERATION_LIST_LABELS, request);
+
+        target.clear();
+        for (int i = 0; i < response.list_labels().labels().size(); i++)
+        {
+          target.insert(response.list_labels().labels(i));
+        }
+      }
+      else
+      {
+        // This method shouldn't have been called
+        throw OrthancException(ErrorCode_InternalError);
+      }
     }
   };
 
--- a/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto	Wed Apr 05 10:02:19 2023 +0200
+++ b/OrthancServer/Plugins/Include/orthanc/OrthancDatabasePlugin.proto	Wed Apr 05 11:31:05 2023 +0200
@@ -259,6 +259,9 @@
   OPERATION_GET_CHILDREN_METADATA = 42;
   OPERATION_GET_LAST_CHANGE_INDEX = 43;
   OPERATION_LOOKUP_RESOURCE_AND_PARENT = 44;
+  OPERATION_ADD_LABEL = 45;     // New in Orthanc 1.12.0
+  OPERATION_REMOVE_LABEL = 46;  // New in Orthanc 1.12.0
+  OPERATION_LIST_LABELS = 47;   // New in Orthanc 1.12.0
 }
 
 message Rollback {
@@ -655,6 +658,8 @@
     ResourceType query_level = 2;
     uint32 limit = 3;
     bool retrieve_instances_ids = 4;
+    repeated string with_labels = 5;     // New in Orthanc 1.12.0
+    repeated string without_labels = 6;  // New in Orthanc 1.12.0
   }
   message Response {
     repeated string resources_ids = 1;
@@ -736,6 +741,33 @@
   }
 }
 
+message AddLabel {
+  message Request {
+    int64 id = 1;
+    string label = 2;
+  }
+  message Response {
+  }
+}
+
+message RemoveLabel {
+  message Request {
+    int64 id = 1;
+    string label = 2;
+  }
+  message Response {
+  }
+}
+
+message ListLabels {
+  message Request {
+    int64 id = 1;
+  }
+  message Response {
+    repeated string labels = 1;
+  }
+}
+
 message TransactionRequest {
   sfixed64              transaction = 1;
   TransactionOperation  operation = 2;
@@ -785,6 +817,9 @@
   GetChildrenMetadata.Request             get_children_metadata = 142;
   GetLastChangeIndex.Request              get_last_change_index = 143;
   LookupResourceAndParent.Request         lookup_resource_and_parent = 144;
+  AddLabel.Request                        add_label = 145;
+  RemoveLabel.Request                     remove_label = 146;
+  ListLabels.Request                      list_labels = 147;
 }
 
 message TransactionResponse {
@@ -833,6 +868,9 @@
   GetChildrenMetadata.Response             get_children_metadata = 142;
   GetLastChangeIndex.Response              get_last_change_index = 143;
   LookupResourceAndParent.Response         lookup_resource_and_parent = 144;
+  AddLabel.Response                        add_label = 145;
+  RemoveLabel.Response                     remove_label = 146;
+  ListLabels.Response                      list_labels = 147;
 }
 
 enum RequestType {