changeset 5615:a10978a5e65c find-refactoring

expansion of single resources using ResourceFinder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 09 May 2024 12:24:29 +0200
parents 4640b7ae9a11
children 1e92fb051fd7
files OrthancServer/Sources/Database/Compatibility/GenericFind.cpp OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp OrthancServer/Sources/ResourceFinder.cpp OrthancServer/Sources/ResourceFinder.h
diffstat 4 files changed, 107 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Thu May 09 11:59:56 2024 +0200
+++ b/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Thu May 09 12:24:29 2024 +0200
@@ -32,17 +32,22 @@
 {
   namespace Compatibility
   {
+    static bool IsRequestWithoutContraint(const FindRequest& request)
+    {
+      return (request.GetDicomTagConstraintsCount() == 0 &&
+              request.GetMetadataConstraintsCount() == 0 &&
+              request.GetLabels().empty() &&
+              request.GetOrdering().empty());
+    }
+
     void GenericFind::ExecuteFind(std::list<std::string>& identifiers,
                                   const FindRequest& request)
     {
-      if (!request.GetOrthancIdentifiers().HasPatientId() &&
+      if (IsRequestWithoutContraint(request) &&
+          !request.GetOrthancIdentifiers().HasPatientId() &&
           !request.GetOrthancIdentifiers().HasStudyId() &&
           !request.GetOrthancIdentifiers().HasSeriesId() &&
-          !request.GetOrthancIdentifiers().HasInstanceId() &&
-          request.GetDicomTagConstraintsCount() == 0 &&
-          request.GetMetadataConstraintsCount() == 0 &&
-          request.GetLabels().empty() &&
-          request.GetOrdering().empty())
+          !request.GetOrthancIdentifiers().HasInstanceId())
       {
         if (request.HasLimits())
         {
@@ -53,6 +58,38 @@
           transaction_.GetAllPublicIds(identifiers, request.GetLevel());
         }
       }
+      else if (IsRequestWithoutContraint(request) &&
+               request.GetOrthancIdentifiers().HasPatientId() &&
+               !request.GetOrthancIdentifiers().HasStudyId() &&
+               !request.GetOrthancIdentifiers().HasSeriesId() &&
+               !request.GetOrthancIdentifiers().HasInstanceId())
+      {
+        identifiers.push_back(request.GetOrthancIdentifiers().GetPatientId());
+      }
+      else if (IsRequestWithoutContraint(request) &&
+               !request.GetOrthancIdentifiers().HasPatientId() &&
+               request.GetOrthancIdentifiers().HasStudyId() &&
+               !request.GetOrthancIdentifiers().HasSeriesId() &&
+               !request.GetOrthancIdentifiers().HasInstanceId())
+      {
+        identifiers.push_back(request.GetOrthancIdentifiers().GetStudyId());
+      }
+      else if (IsRequestWithoutContraint(request) &&
+               !request.GetOrthancIdentifiers().HasPatientId() &&
+               !request.GetOrthancIdentifiers().HasStudyId() &&
+               request.GetOrthancIdentifiers().HasSeriesId() &&
+               !request.GetOrthancIdentifiers().HasInstanceId())
+      {
+        identifiers.push_back(request.GetOrthancIdentifiers().GetSeriesId());
+      }
+      else if (IsRequestWithoutContraint(request) &&
+               !request.GetOrthancIdentifiers().HasPatientId() &&
+               !request.GetOrthancIdentifiers().HasStudyId() &&
+               !request.GetOrthancIdentifiers().HasSeriesId() &&
+               request.GetOrthancIdentifiers().HasInstanceId())
+      {
+        identifiers.push_back(request.GetOrthancIdentifiers().GetInstanceId());
+      }
       else
       {
         throw OrthancException(ErrorCode_NotImplemented);  // Not supported
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Thu May 09 11:59:56 2024 +0200
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Thu May 09 12:24:29 2024 +0200
@@ -340,11 +340,35 @@
     std::set<DicomTag> requestedTags;
     OrthancRestApi::GetRequestedTags(requestedTags, call);
 
-    Json::Value json;
-    if (OrthancRestApi::GetContext(call).ExpandResource(
-          json, call.GetUriComponent("id", ""), resourceType, format, requestedTags, true /* allowStorageAccess */))
+    if (true)
     {
-      call.GetOutput().AnswerJson(json);
+      /**
+       * EXPERIMENTAL VERSION
+       **/
+
+      ResourceFinder finder(resourceType, true /* expand */);
+      finder.AddRequestedTags(requestedTags);
+      finder.SetFormat(OrthancRestApi::GetDicomFormat(call, DicomToJsonFormat_Human));
+      finder.SetOrthancId(resourceType, call.GetUriComponent("id", ""));
+
+      Json::Value json;
+      if (finder.ExecuteOneResource(json, OrthancRestApi::GetContext(call)))
+      {
+        call.GetOutput().AnswerJson(json);
+      }
+    }
+    else
+    {
+      /**
+       * VERSION IN ORTHANC <= 1.12.3
+       **/
+
+      Json::Value json;
+      if (OrthancRestApi::GetContext(call).ExpandResource(
+            json, call.GetUriComponent("id", ""), resourceType, format, requestedTags, true /* allowStorageAccess */))
+      {
+        call.GetOutput().AnswerJson(json);
+      }
     }
   }
 
--- a/OrthancServer/Sources/ResourceFinder.cpp	Thu May 09 11:59:56 2024 +0200
+++ b/OrthancServer/Sources/ResourceFinder.cpp	Thu May 09 12:24:29 2024 +0200
@@ -581,4 +581,31 @@
       }
     }
   }
+
+
+  bool ResourceFinder::ExecuteOneResource(Json::Value& target,
+                                          ServerContext& context)
+  {
+    Json::Value answer;
+    Execute(answer, context);
+
+    if (answer.type() != Json::arrayValue)
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+    else if (answer.size() > 1)
+    {
+      throw OrthancException(ErrorCode_DatabasePlugin);
+    }
+    else if (answer.empty())
+    {
+      // Inexistent resource (or was deleted between the first and second phases)
+      return false;
+    }
+    else
+    {
+      target = answer[0];
+      return true;
+    }
+  }
 }
--- a/OrthancServer/Sources/ResourceFinder.h	Thu May 09 11:59:56 2024 +0200
+++ b/OrthancServer/Sources/ResourceFinder.h	Thu May 09 12:24:29 2024 +0200
@@ -55,6 +55,12 @@
     ResourceFinder(ResourceType level,
                    bool expand);
 
+    void SetOrthancId(ResourceType level,
+                      const std::string& id)
+    {
+      request_.SetOrthancId(level, id);
+    }
+
     void SetFormat(DicomToJsonFormat format)
     {
       format_ = format;
@@ -77,5 +83,8 @@
 
     void Execute(Json::Value& target,
                  ServerContext& context);
+
+    bool ExecuteOneResource(Json::Value& target,
+                            ServerContext& context);
   };
 }