changeset 5594:a906dc19264c find-refactoring

created FindResponse::Resource::Format()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 04 May 2024 15:25:19 +0200
parents 862b54b4cfe2
children a87f2a56257d
files OrthancServer/Sources/Database/Compatibility/GenericFind.cpp OrthancServer/Sources/Database/FindResponse.cpp OrthancServer/Sources/Database/FindResponse.h
diffstat 3 files changed, 174 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Sat May 04 11:35:34 2024 +0200
+++ b/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Sat May 04 15:25:19 2024 +0200
@@ -40,8 +40,8 @@
           !request.GetOrthancIdentifiers().HasInstanceId() &&
           request.GetDicomTagConstraintsCount() == 0 &&
           request.GetMetadataConstraintsCount() == 0 &&
-          request.GetOrdering().empty() &&
-          request.GetLabels().empty())
+          request.GetLabels().empty() &&
+          request.GetOrdering().empty())
       {
         if (request.HasLimits())
         {
@@ -64,9 +64,9 @@
                                     const std::string& identifier)
     {
       int64_t internalId;
-      ResourceType t;
-      if (!transaction_.LookupResource(internalId, t, identifier) ||
-          t != request.GetLevel())
+      ResourceType level;
+      if (!transaction_.LookupResource(internalId, level, identifier) ||
+          level != request.GetLevel())
       {
         throw OrthancException(ErrorCode_InternalError);
       }
@@ -94,6 +94,37 @@
         }
       }
 
+      if (request.IsRetrieveMetadata())
+      {
+        transaction_.GetAllMetadata(resource->GetMetadata(), internalId);
+      }
+
+      if (request.IsRetrieveLabels())
+      {
+        transaction_.ListLabels(resource->GetLabels(), internalId);
+      }
+
+      if (request.IsRetrieveAttachments())
+      {
+        std::set<FileContentType> attachments;
+        transaction_.ListAvailableAttachments(attachments, internalId);
+
+        for (std::set<FileContentType>::const_iterator it = attachments.begin(); it != attachments.end(); ++it)
+        {
+          FileInfo info;
+          int64_t revision;
+          if (transaction_.LookupAttachment(info, revision, internalId, *it) &&
+              info.GetContentType() == *it)
+          {
+            resource->AddAttachment(info);
+          }
+          else
+          {
+            throw OrthancException(ErrorCode_InternalError);
+          }
+        }
+      }
+
       if (request.IsRetrieveParentIdentifier())
       {
         int64_t parentId;
@@ -107,23 +138,22 @@
         }
       }
 
-      // TODO-FIND: Continue
-
-
-      /**
-       * Sanity checks
-       **/
+      if (request.IsRetrieveChildrenIdentifiers())
+      {
+        std::list<std::string> children;
+        transaction_.GetChildrenPublicId(children, internalId);
 
-      if (request.IsRetrieveMainDicomTags())
-      {
-        DicomMap tmp;
-        resource->GetMainDicomTags(tmp);
-        if (tmp.GetSize() == 0)
+        for (std::list<std::string>::const_iterator it = children.begin(); it != children.end(); ++it)
         {
-          throw OrthancException(ErrorCode_InternalError);
+          resource->AddChildIdentifier(GetChildResourceType(level), *it);
         }
       }
 
+      if (request.IsRetrieveChildrenMetadata())
+      {
+        // TODO-FIND
+        throw OrthancException(ErrorCode_NotImplemented);
+      }
 
       response.Add(resource.release());
     }
--- a/OrthancServer/Sources/Database/FindResponse.cpp	Sat May 04 11:35:34 2024 +0200
+++ b/OrthancServer/Sources/Database/FindResponse.cpp	Sat May 04 15:25:19 2024 +0200
@@ -324,6 +324,115 @@
   }
 
 
+  void FindResponse::Resource::Format(Json::Value& target,
+                                      const FindRequest& request) const
+  {
+    /**
+
+       TODO-FIND:
+
+       - Metadata / Series / ExpectedNumberOfInstances
+
+       - Metadata / Series / Status
+
+       - Metadata / Instance / FileSize
+
+       - Metadata / Instance / FileUuid
+
+       - Metadata / Instance / IndexInSeries
+
+       - Metadata / AnonymizedFrom
+
+       - Metadata / ModifiedFrom
+
+     **/
+
+    target = Json::objectValue;
+    target["ID"] = identifier_;
+    target["Type"] = GetResourceTypeText(level_, false, true);
+
+    if (request.IsRetrieveMetadata())
+    {
+      Json::Value metadata = Json::objectValue;
+
+      for (std::map<MetadataType, std::string>::const_iterator
+             it = metadata_.begin(); it != metadata_.end(); ++it)
+      {
+        metadata[EnumerationToString(it->first)] = it->second;
+      }
+
+      target["Metadata"] = metadata;
+    }
+
+    if (request.IsRetrieveLabels())
+    {
+      Json::Value labels = Json::arrayValue;
+
+      for (std::set<std::string>::const_iterator it = labels_.begin(); it != labels_.end(); ++it)
+      {
+        labels.append(*it);
+      }
+
+      target["Labels"] = labels;
+    }
+
+    if (request.IsRetrieveParentIdentifier())
+    {
+      switch (level_)
+      {
+        case ResourceType_Patient:
+          break;
+
+        case ResourceType_Study:
+          target["ParentPatient"] = GetParentIdentifier();
+          break;
+
+        case ResourceType_Series:
+          target["ParentStudy"] = GetParentIdentifier();
+          break;
+
+        case ResourceType_Instance:
+          target["ParentSeries"] = GetParentIdentifier();
+          break;
+
+        default:
+          throw OrthancException(ErrorCode_InternalError);
+      }
+    }
+
+    if (request.IsRetrieveChildrenIdentifiers())
+    {
+      Json::Value c = Json::arrayValue;
+
+      const std::set<std::string>& children = GetChildrenAtLevel(GetChildResourceType(level_)).GetIdentifiers();
+
+      for (std::set<std::string>::const_iterator
+             it = children.begin(); it != children.end(); ++it)
+      {
+        c.append(*it);
+      }
+
+      switch (level_)
+      {
+        case ResourceType_Patient:
+          target["Studies"] = c;
+          break;
+
+        case ResourceType_Study:
+          target["Series"] = c;
+          break;
+
+        case ResourceType_Series:
+          target["Instances"] = c;
+          break;
+
+        default:
+          throw OrthancException(ErrorCode_InternalError);
+      }
+    }
+  }
+
+
   FindResponse::~FindResponse()
   {
     for (size_t i = 0; i < items_.size(); i++)
--- a/OrthancServer/Sources/Database/FindResponse.h	Sat May 04 11:35:34 2024 +0200
+++ b/OrthancServer/Sources/Database/FindResponse.h	Sat May 04 15:25:19 2024 +0200
@@ -77,6 +77,11 @@
 
       ChildrenAtLevel& GetChildrenAtLevel(ResourceType level);
 
+      const ChildrenAtLevel& GetChildrenAtLevel(ResourceType level) const
+      {
+        return const_cast<Resource&>(*this).GetChildrenAtLevel(level);
+      }
+
     public:
       Resource(ResourceType level,
                const std::string& identifier) :
@@ -127,6 +132,11 @@
 
       void AddLabel(const std::string& label);
 
+      std::set<std::string>& GetLabels()
+      {
+        return labels_;
+      }
+
       const std::set<std::string>& GetLabels() const
       {
         return labels_;
@@ -135,6 +145,11 @@
       void AddMetadata(MetadataType metadata,
                        const std::string& value);
 
+      std::map<MetadataType, std::string>& GetMetadata()
+      {
+        return metadata_;
+      }
+
       const std::map<MetadataType, std::string>& GetMetadata() const
       {
         return metadata_;
@@ -154,6 +169,9 @@
 
       bool LookupAttachment(FileInfo& target,
                             FileContentType type) const;
+
+      void Format(Json::Value& target,
+                  const FindRequest& request) const;
     };
 
   private: