changeset 5619:1864b16bc7b1 find-refactoring

added FindRequest::ParentRetrieveSpecification
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 11 May 2024 12:13:31 +0200
parents 824a5fb0774e
children 4bfd885fb45f
files OrthancServer/Sources/Database/Compatibility/GenericFind.cpp OrthancServer/Sources/Database/FindRequest.cpp OrthancServer/Sources/Database/FindRequest.h OrthancServer/Sources/Database/FindResponse.cpp OrthancServer/Sources/ResourceFinder.cpp
diffstat 5 files changed, 158 insertions(+), 230 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Sat May 11 11:23:25 2024 +0200
+++ b/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Sat May 11 12:13:31 2024 +0200
@@ -239,8 +239,8 @@
           return ResourceType_Patient;
 
         case ResourceType_Study:
-          if (request.IsRetrieveMainDicomTags(ResourceType_Patient) ||
-              request.IsRetrieveMetadata(ResourceType_Patient))
+          if (request.GetParentRetrieveSpecification(ResourceType_Patient).IsRetrieveMainDicomTags() ||
+              request.GetParentRetrieveSpecification(ResourceType_Patient).IsRetrieveMetadata())
           {
             return ResourceType_Patient;
           }
@@ -250,13 +250,13 @@
           }
 
         case ResourceType_Series:
-          if (request.IsRetrieveMainDicomTags(ResourceType_Patient) ||
-              request.IsRetrieveMetadata(ResourceType_Patient))
+          if (request.GetParentRetrieveSpecification(ResourceType_Patient).IsRetrieveMainDicomTags() ||
+              request.GetParentRetrieveSpecification(ResourceType_Patient).IsRetrieveMetadata())
           {
             return ResourceType_Patient;
           }
-          else if (request.IsRetrieveMainDicomTags(ResourceType_Study) ||
-                   request.IsRetrieveMetadata(ResourceType_Study))
+          else if (request.GetParentRetrieveSpecification(ResourceType_Study).IsRetrieveMainDicomTags() ||
+                   request.GetParentRetrieveSpecification(ResourceType_Study).IsRetrieveMetadata())
           {
             return ResourceType_Study;
           }
@@ -266,18 +266,18 @@
           }
 
         case ResourceType_Instance:
-          if (request.IsRetrieveMainDicomTags(ResourceType_Patient) ||
-              request.IsRetrieveMetadata(ResourceType_Patient))
+          if (request.GetParentRetrieveSpecification(ResourceType_Patient).IsRetrieveMainDicomTags() ||
+              request.GetParentRetrieveSpecification(ResourceType_Patient).IsRetrieveMetadata())
           {
             return ResourceType_Patient;
           }
-          else if (request.IsRetrieveMainDicomTags(ResourceType_Study) ||
-                   request.IsRetrieveMetadata(ResourceType_Study))
+          else if (request.GetParentRetrieveSpecification(ResourceType_Study).IsRetrieveMainDicomTags() ||
+                   request.GetParentRetrieveSpecification(ResourceType_Study).IsRetrieveMetadata())
           {
             return ResourceType_Study;
           }
-          else if (request.IsRetrieveMainDicomTags(ResourceType_Series) ||
-                   request.IsRetrieveMetadata(ResourceType_Series))
+          else if (request.GetParentRetrieveSpecification(ResourceType_Series).IsRetrieveMainDicomTags() ||
+                   request.GetParentRetrieveSpecification(ResourceType_Series).IsRetrieveMetadata())
           {
             return ResourceType_Series;
           }
@@ -343,40 +343,44 @@
         resource->SetParentIdentifier(parent);
       }
 
+      if (request.IsRetrieveMainDicomTags())
       {
+        RetrieveMainDicomTags(*resource, level, internalId);
+      }
+
+      if (request.IsRetrieveMetadata())
+      {
+        transaction_.GetAllMetadata(resource->GetMetadata(level), internalId);
+      }
+
+      {
+        const ResourceType topLevel = GetTopLevelOfInterest(request);
+
         int64_t currentId = internalId;
         ResourceType currentLevel = level;
-        const ResourceType topLevel = GetTopLevelOfInterest(request);
 
-        for (;;)
+        while (currentLevel != topLevel)
         {
-          if (request.IsRetrieveMainDicomTags(currentLevel))
+          int64_t parentId;
+          if (transaction_.LookupParent(parentId, currentId))
+          {
+            currentId = parentId;
+            currentLevel = GetParentResourceType(currentLevel);
+          }
+          else
+          {
+            throw OrthancException(ErrorCode_DatabasePlugin);
+          }
+
+          if (request.GetParentRetrieveSpecification(currentLevel).IsRetrieveMainDicomTags())
           {
             RetrieveMainDicomTags(*resource, currentLevel, currentId);
           }
 
-          if (request.IsRetrieveMetadata(currentLevel))
+          if (request.GetParentRetrieveSpecification(currentLevel).IsRetrieveMetadata())
           {
             transaction_.GetAllMetadata(resource->GetMetadata(currentLevel), currentId);
           }
-
-          if (currentLevel == topLevel)
-          {
-            break;
-          }
-          else
-          {
-            int64_t parentId;
-            if (transaction_.LookupParent(parentId, currentId))
-            {
-              currentId = parentId;
-              currentLevel = GetParentResourceType(currentLevel);
-            }
-            else
-            {
-              throw OrthancException(ErrorCode_DatabasePlugin);
-            }
-          }
         }
       }
 
--- a/OrthancServer/Sources/Database/FindRequest.cpp	Sat May 11 11:23:25 2024 +0200
+++ b/OrthancServer/Sources/Database/FindRequest.cpp	Sat May 11 12:13:31 2024 +0200
@@ -31,19 +31,37 @@
 
 namespace Orthanc
 {
+  FindRequest::ParentRetrieveSpecification& FindRequest::GetParentRetrieveSpecification(ResourceType level)
+  {
+    if (!IsResourceLevelAboveOrEqual(level, level_))
+    {
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+
+    switch (level)
+    {
+      case ResourceType_Patient:
+        return retrieveParentPatient_;
+
+      case ResourceType_Study:
+        return retrieveParentStudy_;
+
+      case ResourceType_Series:
+        return retrieveParentSeries_;
+
+      default:
+        throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+  }
+
+
   FindRequest::FindRequest(ResourceType level) :
     level_(level),
     hasLimits_(false),
     limitsSince_(0),
     limitsCount_(0),
-    retrieveMainDicomTagsPatients_(false),
-    retrieveMainDicomTagsStudies_(false),
-    retrieveMainDicomTagsSeries_(false),
-    retrieveMainDicomTagsInstances_(false),
-    retrieveMetadataPatients_(false),
-    retrieveMetadataStudies_(false),
-    retrieveMetadataSeries_(false),
-    retrieveMetadataInstances_(false),
+    retrieveMainDicomTags_(false),
+    retrieveMetadata_(false),
     retrieveLabels_(false),
     retrieveAttachments_(false),
     retrieveParentIdentifier_(false),
@@ -238,124 +256,6 @@
   }
 
 
-  void FindRequest::SetRetrieveMainDicomTags(ResourceType level,
-                                             bool retrieve)
-  {
-    if (!IsResourceLevelAboveOrEqual(level, level_))
-    {
-      throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-
-    switch (level)
-    {
-      case ResourceType_Patient:
-        retrieveMainDicomTagsPatients_ = retrieve;
-        break;
-
-      case ResourceType_Study:
-        retrieveMainDicomTagsStudies_ = retrieve;
-        break;
-
-      case ResourceType_Series:
-        retrieveMainDicomTagsSeries_ = retrieve;
-        break;
-
-      case ResourceType_Instance:
-        retrieveMainDicomTagsInstances_ = retrieve;
-        break;
-
-      default:
-        throw OrthancException(ErrorCode_InternalError);
-    }
-  }
-
-
-  bool FindRequest::IsRetrieveMainDicomTags(ResourceType level) const
-  {
-    if (!IsResourceLevelAboveOrEqual(level, level_))
-    {
-      throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-
-    switch (level)
-    {
-      case ResourceType_Patient:
-        return retrieveMainDicomTagsPatients_;
-
-      case ResourceType_Study:
-        return retrieveMainDicomTagsStudies_;
-
-      case ResourceType_Series:
-        return retrieveMainDicomTagsSeries_;
-
-      case ResourceType_Instance:
-        return retrieveMainDicomTagsInstances_;
-
-      default:
-        throw OrthancException(ErrorCode_InternalError);
-    }
-  }
-
-
-  void FindRequest::SetRetrieveMetadata(ResourceType level,
-                                        bool retrieve)
-  {
-    if (!IsResourceLevelAboveOrEqual(level, level_))
-    {
-      throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-
-    switch (level)
-    {
-      case ResourceType_Patient:
-        retrieveMetadataPatients_ = retrieve;
-        break;
-
-      case ResourceType_Study:
-        retrieveMetadataStudies_ = retrieve;
-        break;
-
-      case ResourceType_Series:
-        retrieveMetadataSeries_ = retrieve;
-        break;
-
-      case ResourceType_Instance:
-        retrieveMetadataInstances_ = retrieve;
-        break;
-
-      default:
-        throw OrthancException(ErrorCode_InternalError);
-    }
-  }
-
-
-  bool FindRequest::IsRetrieveMetadata(ResourceType level) const
-  {
-    if (!IsResourceLevelAboveOrEqual(level, level_))
-    {
-      throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-
-    switch (level)
-    {
-      case ResourceType_Patient:
-        return retrieveMetadataPatients_;
-
-      case ResourceType_Study:
-        return retrieveMetadataStudies_;
-
-      case ResourceType_Series:
-        return retrieveMetadataSeries_;
-
-      case ResourceType_Instance:
-        return retrieveMetadataInstances_;
-
-      default:
-        throw OrthancException(ErrorCode_InternalError);
-    }
-  }
-
-
   void FindRequest::SetRetrieveParentIdentifier(bool retrieve)
   {
     if (level_ == ResourceType_Patient)
--- a/OrthancServer/Sources/Database/FindRequest.h	Sat May 11 11:23:25 2024 +0200
+++ b/OrthancServer/Sources/Database/FindRequest.h	Sat May 11 12:13:31 2024 +0200
@@ -154,6 +154,41 @@
     };
 
 
+    class ParentRetrieveSpecification : public boost::noncopyable
+    {
+    private:
+      bool  mainDicomTags_;
+      bool  metadata_;
+
+    public:
+      ParentRetrieveSpecification() :
+        mainDicomTags_(false),
+        metadata_(false)
+      {
+      }
+
+      void SetRetrieveMainDicomTags(bool retrieve)
+      {
+        mainDicomTags_ = retrieve;
+      }
+
+      bool IsRetrieveMainDicomTags() const
+      {
+        return mainDicomTags_;
+      }
+
+      void SetRetrieveMetadata(bool retrieve)
+      {
+        metadata_ = retrieve;
+      }
+
+      bool IsRetrieveMetadata() const
+      {
+        return metadata_;
+      }
+    };
+
+
   private:
     // filter & ordering fields
     ResourceType                         level_;                // The level of the response (the filtering on tags, labels and metadata also happens at this level)
@@ -167,17 +202,14 @@
     LabelsConstraint                     labelsContraint_;
     std::deque<Ordering*>                ordering_;             // The ordering criteria (note: the order is important !)
 
-    bool                                 retrieveMainDicomTagsPatients_;
-    bool                                 retrieveMainDicomTagsStudies_;
-    bool                                 retrieveMainDicomTagsSeries_;
-    bool                                 retrieveMainDicomTagsInstances_;
-    bool                                 retrieveMetadataPatients_;
-    bool                                 retrieveMetadataStudies_;
-    bool                                 retrieveMetadataSeries_;
-    bool                                 retrieveMetadataInstances_;
+    bool                                 retrieveMainDicomTags_;
+    bool                                 retrieveMetadata_;
     bool                                 retrieveLabels_;
     bool                                 retrieveAttachments_;
     bool                                 retrieveParentIdentifier_;
+    ParentRetrieveSpecification          retrieveParentPatient_;
+    ParentRetrieveSpecification          retrieveParentStudy_;
+    ParentRetrieveSpecification          retrieveParentSeries_;
     bool                                 retrieveChildrenIdentifiers_;
     std::set<MetadataType>               retrieveChildrenMetadata_;
     bool                                 retrieveOneInstanceIdentifier_;
@@ -260,15 +292,25 @@
       return labelsContraint_;
     }
 
-    void SetRetrieveMainDicomTags(ResourceType level,
-                                  bool retrieve);
+    void SetRetrieveMainDicomTags(bool retrieve)
+    {
+      retrieveMainDicomTags_ = retrieve;
+    }
 
-    bool IsRetrieveMainDicomTags(ResourceType level) const;
+    bool IsRetrieveMainDicomTags() const
+    {
+      return retrieveMainDicomTags_;
+    }
 
-    void SetRetrieveMetadata(ResourceType level,
-                             bool retrieve);
+    void SetRetrieveMetadata(bool retrieve)
+    {
+      retrieveMetadata_ = retrieve;
+    }
 
-    bool IsRetrieveMetadata(ResourceType level) const;
+    bool IsRetrieveMetadata() const
+    {
+      return retrieveMetadata_;
+    }
 
     void SetRetrieveLabels(bool retrieve)
     {
@@ -297,6 +339,13 @@
       return retrieveParentIdentifier_;
     }
 
+    ParentRetrieveSpecification& GetParentRetrieveSpecification(ResourceType level);
+
+    const ParentRetrieveSpecification& GetParentRetrieveSpecification(ResourceType level) const
+    {
+      return const_cast<FindRequest&>(*this).GetParentRetrieveSpecification(level);
+    }
+
     void SetRetrieveChildrenIdentifiers(bool retrieve);
 
     bool IsRetrieveChildrenIdentifiers() const
--- a/OrthancServer/Sources/Database/FindResponse.cpp	Sat May 11 11:23:25 2024 +0200
+++ b/OrthancServer/Sources/Database/FindResponse.cpp	Sat May 11 12:13:31 2024 +0200
@@ -477,63 +477,38 @@
       target["ParentID"] = GetParentIdentifier();
     }
 
-    if (request.IsRetrieveMainDicomTags(ResourceType_Patient))
+    if (request.IsRetrieveMainDicomTags())
     {
       DicomMap m;
-      GetMainDicomTags(m, ResourceType_Patient);
-      DebugDicomMap(target["Patient"]["MainDicomTags"], m);
-    }
-
-    if (request.IsRetrieveMetadata(ResourceType_Patient))
-    {
-      DebugMetadata(target["Patient"]["Metadata"], GetMetadata(ResourceType_Patient));
+      GetMainDicomTags(m, request.GetLevel());
+      DebugDicomMap(target[EnumerationToString(GetLevel())]["MainDicomTags"], m);
     }
 
-    if (request.GetLevel() != ResourceType_Patient)
+    if (request.IsRetrieveMetadata())
     {
-      if (request.IsRetrieveMainDicomTags(ResourceType_Study))
-      {
-        DicomMap m;
-        GetMainDicomTags(m, ResourceType_Study);
-        DebugDicomMap(target["Study"]["MainDicomTags"], m);
-      }
-
-      if (request.IsRetrieveMetadata(ResourceType_Study))
-      {
-        DebugMetadata(target["Study"]["Metadata"], GetMetadata(ResourceType_Study));
-      }
+      DebugMetadata(target[EnumerationToString(GetLevel())]["Metadata"], GetMetadata(request.GetLevel()));
     }
 
-    if (request.GetLevel() != ResourceType_Patient &&
-        request.GetLevel() != ResourceType_Study)
+    static const ResourceType levels[4] = { ResourceType_Patient, ResourceType_Study, ResourceType_Series, ResourceType_Instance };
+
+    for (size_t i = 0; i < 4; i++)
     {
-      if (request.IsRetrieveMainDicomTags(ResourceType_Series))
-      {
-        DicomMap m;
-        GetMainDicomTags(m, ResourceType_Series);
-        DebugDicomMap(target["Series"]["MainDicomTags"], m);
-      }
-
-      if (request.IsRetrieveMetadata(ResourceType_Series))
+      if (levels[i] != request.GetLevel() &&
+          IsResourceLevelAboveOrEqual(levels[i], request.GetLevel()))
       {
-        DebugMetadata(target["Series"]["Metadata"], GetMetadata(ResourceType_Series));
-      }
-    }
+        const char* level = EnumerationToString(levels[i]);
 
-    if (request.GetLevel() != ResourceType_Patient &&
-        request.GetLevel() != ResourceType_Study &&
-        request.GetLevel() != ResourceType_Series)
-    {
-      if (request.IsRetrieveMainDicomTags(ResourceType_Instance))
-      {
-        DicomMap m;
-        GetMainDicomTags(m, ResourceType_Instance);
-        DebugDicomMap(target["Instance"]["MainDicomTags"], m);
-      }
+        if (request.GetParentRetrieveSpecification(levels[i]).IsRetrieveMainDicomTags())
+        {
+          DicomMap m;
+          GetMainDicomTags(m, levels[i]);
+          DebugDicomMap(target[level]["MainDicomTags"], m);
+        }
 
-      if (request.IsRetrieveMetadata(ResourceType_Instance))
-      {
-        DebugMetadata(target["Instance"]["Metadata"], GetMetadata(ResourceType_Instance));
+        if (request.GetParentRetrieveSpecification(levels[i]).IsRetrieveMainDicomTags())
+        {
+          DebugMetadata(target[level]["Metadata"], GetMetadata(levels[i]));
+        }
       }
     }
 
--- a/OrthancServer/Sources/ResourceFinder.cpp	Sat May 11 11:23:25 2024 +0200
+++ b/OrthancServer/Sources/ResourceFinder.cpp	Sat May 11 12:13:31 2024 +0200
@@ -367,8 +367,8 @@
   {
     if (expand)
     {
-      request_.SetRetrieveMainDicomTags(level, true);
-      request_.SetRetrieveMetadata(level, true);
+      request_.SetRetrieveMainDicomTags(true);
+      request_.SetRetrieveMetadata(true);
       request_.SetRetrieveLabels(true);
 
       if (level == ResourceType_Series)
@@ -397,8 +397,8 @@
   {
     if (DicomMap::IsMainDicomTag(tag, ResourceType_Patient))
     {
-      request_.SetRetrieveMainDicomTags(ResourceType_Patient, true);
-      request_.SetRetrieveMetadata(ResourceType_Patient, true);
+      request_.GetParentRetrieveSpecification(ResourceType_Patient).SetRetrieveMainDicomTags(true);
+      request_.GetParentRetrieveSpecification(ResourceType_Patient).SetRetrieveMetadata(true);
       requestedPatientTags_.insert(tag);
     }
     else if (DicomMap::IsMainDicomTag(tag, ResourceType_Study))
@@ -412,8 +412,8 @@
       }
       else
       {
-        request_.SetRetrieveMainDicomTags(ResourceType_Study, true);
-        request_.SetRetrieveMetadata(ResourceType_Study, true);
+        request_.GetParentRetrieveSpecification(ResourceType_Study).SetRetrieveMainDicomTags(true);
+        request_.GetParentRetrieveSpecification(ResourceType_Study).SetRetrieveMetadata(true);
         requestedStudyTags_.insert(tag);
       }
     }
@@ -429,8 +429,8 @@
       }
       else
       {
-        request_.SetRetrieveMainDicomTags(ResourceType_Series, true);
-        request_.SetRetrieveMetadata(ResourceType_Series, true);
+        request_.GetParentRetrieveSpecification(ResourceType_Series).SetRetrieveMainDicomTags(true);
+        request_.GetParentRetrieveSpecification(ResourceType_Series).SetRetrieveMetadata(true);
         requestedSeriesTags_.insert(tag);
       }
     }
@@ -448,8 +448,8 @@
       else
       {
         // Main DICOM tags from the instance level will be retrieved anyway
-        assert(request_.IsRetrieveMainDicomTags(ResourceType_Instance));
-        assert(request_.IsRetrieveMetadata(ResourceType_Instance));
+        assert(request_.IsRetrieveMainDicomTags());
+        assert(request_.IsRetrieveMetadata());
         requestedInstanceTags_.insert(tag);
       }
     }