diff OrthancServer/Sources/Database/FindResponse.cpp @ 5772:093a8693ba16 find-refactoring

replaced SetRetrieveOneInstanceIdentifier() by SetRetrieveOneInstanceMetadataAndAttachments()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 11 Sep 2024 20:49:34 +0200
parents f39406a9eda4
children f96abfe08946
line wrap: on
line diff
--- a/OrthancServer/Sources/Database/FindResponse.cpp	Wed Sep 11 16:31:11 2024 +0200
+++ b/OrthancServer/Sources/Database/FindResponse.cpp	Wed Sep 11 20:49:34 2024 +0200
@@ -483,17 +483,63 @@
   }
 
 
-  const std::string& FindResponse::Resource::GetOneInstanceIdentifier() const
+  void FindResponse::Resource::SetOneInstanceMetadataAndAttachments(const std::string& instancePublicId,
+                                                                    const std::map<MetadataType, std::string>& metadata,
+                                                                    const std::map<FileContentType, FileInfo>& attachments)
   {
-    const std::set<std::string>& instances = GetChildrenInformation(ResourceType_Instance).GetIdentifiers();
-
-    if (instances.size() == 0)
+    if (hasOneInstanceMetadataAndAttachments_)
     {
-      throw OrthancException(ErrorCode_BadSequenceOfCalls);  // HasOneInstanceIdentifier() should have been called
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+    else if (instancePublicId.empty())
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
     }
     else
     {
-      return *instances.begin();
+      hasOneInstanceMetadataAndAttachments_ = true;
+      oneInstancePublicId_ = instancePublicId;
+      oneInstanceMetadata_ = metadata;
+      oneInstanceAttachments_ = attachments;
+    }
+  }
+
+
+  const std::string& FindResponse::Resource::GetOneInstancePublicId() const
+  {
+    if (hasOneInstanceMetadataAndAttachments_)
+    {
+      return oneInstancePublicId_;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+  }
+
+
+  const std::map<MetadataType, std::string>& FindResponse::Resource::GetOneInstanceMetadata() const
+  {
+    if (hasOneInstanceMetadataAndAttachments_)
+    {
+      return oneInstanceMetadata_;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+  }
+
+
+  const std::map<FileContentType, FileInfo>& FindResponse::Resource::GetOneInstanceAttachments() const
+  {
+    if (hasOneInstanceMetadataAndAttachments_)
+    {
+      return oneInstanceAttachments_;
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
     }
   }
 
@@ -542,6 +588,25 @@
   }
 
 
+  static void DebugAttachments(Json::Value& target,
+                               const std::map<FileContentType, FileInfo>& attachments)
+  {
+    Json::Value v = Json::objectValue;
+    for (std::map<FileContentType, FileInfo>::const_iterator it = attachments.begin();
+         it != attachments.end(); ++it)
+    {
+      if (it->first != it->second.GetContentType())
+      {
+        throw OrthancException(ErrorCode_DatabasePlugin);
+      }
+      else
+      {
+        DebugAddAttachment(v, it->second);
+      }
+    }
+  }
+
+
   static void DebugSetOfStrings(Json::Value& target,
                                 const std::set<std::string>& values)
   {
@@ -633,25 +698,13 @@
 
     if (request.IsRetrieveAttachments())
     {
-      Json::Value v = Json::objectValue;
-      for (std::map<FileContentType, FileInfo>::const_iterator it = attachments_.begin();
-           it != attachments_.end(); ++it)
-      {
-        if (it->first != it->second.GetContentType())
-        {
-          throw OrthancException(ErrorCode_DatabasePlugin);
-        }
-        else
-        {
-          DebugAddAttachment(v, it->second);
-        }
-      }
-      target["Attachments"] = v;
+      DebugAttachments(target["Attachments"], attachments_);
     }
 
-    if (request.IsRetrieveOneInstanceIdentifier())
+    if (request.IsRetrieveOneInstanceMetadataAndAttachments())
     {
-      target["OneInstance"] = GetOneInstanceIdentifier();
+      DebugMetadata(target["OneInstance"]["Metadata"], GetOneInstanceMetadata());
+      DebugAttachments(target["OneInstance"]["Attachments"], GetOneInstanceAttachments());
     }
   }