changeset 5715:593e110de3d8 find-refactoring-clean

integration find-refactoring->find-refactoring-clean
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 13 Jul 2024 00:28:24 +0200
parents 52771e1a8072 (current diff) 2b05428843d2 (diff)
children 77e1ff7f90c7
files OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp OrthancServer/Sources/OrthancWebDav.cpp OrthancServer/Sources/ServerContext.cpp
diffstat 4 files changed, 39 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Fri Jul 12 19:05:16 2024 +0200
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Sat Jul 13 00:28:24 2024 +0200
@@ -130,7 +130,7 @@
 
 
   static bool ExpandResource(Json::Value& target,
-                             ServerIndex& index,
+                             ServerContext& context,
                              ResourceType level,
                              const std::string& identifier,
                              DicomToJsonFormat format,
@@ -140,35 +140,7 @@
     finder.SetOrthancId(level, identifier);
     finder.SetRetrieveMetadata(retrieveMetadata);
 
-    FindResponse response;
-    finder.Execute(response, index);
-
-    if (response.GetSize() != 1)
-    {
-      return false;
-    }
-    else
-    {
-      const FindResponse::Resource& resource = response.GetResourceByIndex(0);
-      finder.Expand(target, resource, index, format);
-
-      if (retrieveMetadata)
-      {
-        const std::map<MetadataType, std::string>& metadata = resource.GetMetadata(level);
-
-        Json::Value tmp;
-
-        for (std::map<MetadataType, std::string>::const_iterator
-               it = metadata.begin(); it != metadata.end(); ++it)
-        {
-          tmp[EnumerationToString(it->first)] = it->second;
-        }
-
-        target["Metadata"] = tmp;
-      }
-
-      return true;
-    }
+    return finder.ExecuteOneResource(target, context, format, retrieveMetadata);
   }
 
 
@@ -232,7 +204,8 @@
     }
 
     Json::Value answer;
-    finder.Execute(answer, OrthancRestApi::GetContext(call), OrthancRestApi::GetDicomFormat(call, DicomToJsonFormat_Human));
+    finder.Execute(answer, OrthancRestApi::GetContext(call),
+                   OrthancRestApi::GetDicomFormat(call, DicomToJsonFormat_Human), false /* no "Metadata" field */);
     call.GetOutput().AnswerJson(answer);
   }
 
@@ -267,7 +240,7 @@
     finder.SetOrthancId(resourceType, call.GetUriComponent("id", ""));
 
     Json::Value json;
-    if (finder.ExecuteOneResource(json, OrthancRestApi::GetContext(call), format))
+    if (finder.ExecuteOneResource(json, OrthancRestApi::GetContext(call), format, false /* no "Metadata" field */))
     {
       call.GetOutput().AnswerJson(json);
     }
@@ -3286,7 +3259,7 @@
       }
 
       Json::Value answer;
-      finder.Execute(answer, context, format);
+      finder.Execute(answer, context, format, false /* no "Metadata" field */);
       call.GetOutput().AnswerJson(answer);
     }
   }
@@ -3329,7 +3302,7 @@
     finder.AddRequestedTags(requestedTags);
 
     Json::Value answer;
-    finder.Execute(answer, OrthancRestApi::GetContext(call), format);
+    finder.Execute(answer, OrthancRestApi::GetContext(call), format, false /* no "Metadata" field */);
     call.GetOutput().AnswerJson(answer);
   }
 
@@ -3443,7 +3416,7 @@
     const DicomToJsonFormat format = OrthancRestApi::GetDicomFormat(call, DicomToJsonFormat_Human);
 
     Json::Value resource;
-    if (ExpandResource(resource, OrthancRestApi::GetIndex(call), currentType, current, format, false))
+    if (ExpandResource(resource, OrthancRestApi::GetContext(call), currentType, current, format, false))
     {
       call.GetOutput().AnswerJson(resource);
     }
@@ -3859,7 +3832,7 @@
                it = interest.begin(); it != interest.end(); ++it)
         {
           Json::Value item;
-          if (ExpandResource(item, OrthancRestApi::GetIndex(call), level, *it, format, metadata))
+          if (ExpandResource(item, OrthancRestApi::GetContext(call), level, *it, format, metadata))
           {
             answer.append(item);
           }
@@ -3878,7 +3851,7 @@
           Json::Value item;
 
           if (index.LookupResourceType(level, *it) &&
-              ExpandResource(item, OrthancRestApi::GetIndex(call), level, *it, format, metadata))
+              ExpandResource(item, OrthancRestApi::GetContext(call), level, *it, format, metadata))
           {
             answer.append(item);
           }
--- a/OrthancServer/Sources/OrthancWebDav.cpp	Fri Jul 12 19:05:16 2024 +0200
+++ b/OrthancServer/Sources/OrthancWebDav.cpp	Sat Jul 13 00:28:24 2024 +0200
@@ -1447,7 +1447,7 @@
     finder.SetDatabaseLookup(query);
 
     Json::Value expanded;
-    finder.Execute(expanded, context, DicomToJsonFormat_Human);
+    finder.Execute(expanded, context, DicomToJsonFormat_Human, false /* don't add "Metadata" */);
 
     if (expanded.size() != 1)
     {
--- a/OrthancServer/Sources/ResourceFinder.cpp	Fri Jul 12 19:05:16 2024 +0200
+++ b/OrthancServer/Sources/ResourceFinder.cpp	Sat Jul 13 00:28:24 2024 +0200
@@ -191,13 +191,19 @@
   void ResourceFinder::Expand(Json::Value& target,
                               const FindResponse::Resource& resource,
                               ServerIndex& index,
-                              DicomToJsonFormat format) const
+                              DicomToJsonFormat format,
+                              bool includeAllMetadata) const
   {
     /**
      * This method closely follows "SerializeExpandedResource()" in
      * "ServerContext.cpp" from Orthanc 1.12.4.
      **/
 
+    if (!expand_)
+    {
+      throw OrthancException(ErrorCode_BadSequenceOfCalls);
+    }
+
     if (resource.GetLevel() != request_.GetLevel())
     {
       throw OrthancException(ErrorCode_InternalError);
@@ -407,7 +413,7 @@
       target["Labels"] = labels;
     }
 
-    if (includeAllMetadata_)  // new in Orthanc 1.12.4
+    if (includeAllMetadata)  // new in Orthanc 1.12.4
     {
       const std::map<MetadataType, std::string>& m = resource.GetMetadata(resource.GetLevel());
 
@@ -483,8 +489,7 @@
     limitsCount_(0),
     expand_(expand),
     allowStorageAccess_(true),
-    hasRequestedTags_(false),
-    includeAllMetadata_(false)
+    hasRequestedTags_(false)
   {
     UpdateRequestLimits();
 
@@ -886,13 +891,6 @@
   }
 
 
-  void ResourceFinder::Execute(FindResponse& response,
-                               ServerIndex& index) const
-  {
-    index.ExecuteFind(response, request_);
-  }
-
-  
   void ResourceFinder::Execute(IVisitor& visitor,
                                ServerContext& context) const
   {
@@ -1003,7 +1001,8 @@
 
   void ResourceFinder::Execute(Json::Value& target,
                                ServerContext& context,
-                               DicomToJsonFormat format) const
+                               DicomToJsonFormat format,
+                               bool includeAllMetadata) const
   {
     class Visitor : public IVisitor
     {
@@ -1013,18 +1012,21 @@
       Json::Value&           target_;
       DicomToJsonFormat      format_;
       bool                   hasRequestedTags_;
+      bool                   includeAllMetadata_;
 
     public:
       Visitor(const ResourceFinder& that,
               ServerIndex& index,
               Json::Value& target,
               DicomToJsonFormat format,
-              bool hasRequestedTags) :
+              bool hasRequestedTags,
+              bool includeAllMetadata) :
         that_(that),
         index_(index),
         target_(target),
         format_(format),
-        hasRequestedTags_(hasRequestedTags)
+        hasRequestedTags_(hasRequestedTags),
+        includeAllMetadata_(includeAllMetadata)
       {
       }
 
@@ -1034,7 +1036,7 @@
         if (that_.expand_)
         {
           Json::Value item;
-          that_.Expand(item, resource, index_, format_);
+          that_.Expand(item, resource, index_, format_, includeAllMetadata_);
 
           if (hasRequestedTags_)
           {
@@ -1058,17 +1060,18 @@
 
     target = Json::arrayValue;
 
-    Visitor visitor(*this, context.GetIndex(), target, format, hasRequestedTags_);
+    Visitor visitor(*this, context.GetIndex(), target, format, hasRequestedTags_, includeAllMetadata);
     Execute(visitor, context);
   }
 
 
   bool ResourceFinder::ExecuteOneResource(Json::Value& target,
                                           ServerContext& context,
-                                          DicomToJsonFormat format) const
+                                          DicomToJsonFormat format,
+                                          bool includeAllMetadata) const
   {
     Json::Value answer;
-    Execute(answer, context, format);
+    Execute(answer, context, format, includeAllMetadata);
 
     if (answer.type() != Json::arrayValue)
     {
--- a/OrthancServer/Sources/ResourceFinder.h	Fri Jul 12 19:05:16 2024 +0200
+++ b/OrthancServer/Sources/ResourceFinder.h	Sat Jul 13 00:28:24 2024 +0200
@@ -74,7 +74,6 @@
     std::set<DicomTag>               requestedInstanceTags_;
     std::set<DicomTag>               requestedTagsFromFileStorage_;
     std::set<DicomTag>               requestedComputedTags_;
-    bool                             includeAllMetadata_;   // Same as: ExpandResourceFlags_IncludeAllMetadata
 
     bool IsRequestedComputedTag(const DicomTag& tag) const
     {
@@ -126,11 +125,6 @@
 
     void SetDatabaseLookup(const DatabaseLookup& lookup);
 
-    void SetIncludeAllMetadata(bool include)
-    {
-      includeAllMetadata_ = include;
-    }
-
     void AddRequestedTag(const DicomTag& tag);
 
     void AddRequestedTags(const std::set<DicomTag>& tags);
@@ -165,23 +159,24 @@
       request_.SetRetrieveAttachments(retrieve);
     }
 
+    // NB: "index" is only used in this method to fill the "IsStable" information
     void Expand(Json::Value& target,
                 const FindResponse::Resource& resource,
                 ServerIndex& index,
-                DicomToJsonFormat format) const;
-
-    void Execute(FindResponse& target,
-                 ServerIndex& index) const;
+                DicomToJsonFormat format,
+                bool includeAllMetadata /* Same as: ExpandResourceFlags_IncludeAllMetadata */) const;
 
     void Execute(IVisitor& visitor,
                  ServerContext& context) const;
 
     void Execute(Json::Value& target,
                  ServerContext& context,
-                 DicomToJsonFormat format) const;
+                 DicomToJsonFormat format,
+                 bool includeAllMetadata) const;
 
     bool ExecuteOneResource(Json::Value& target,
                             ServerContext& context,
-                            DicomToJsonFormat format) const;
+                            DicomToJsonFormat format,
+                            bool includeAllMetadata) const;
   };
 }