changeset 5589:b51ee994cd6f find-refactoring

renamed FindResponse::Item as FindResponse::Resource
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 03 May 2024 17:26:06 +0200
parents 1b0fc6685f57
children 8b32213af23e
files OrthancServer/Sources/Database/Compatibility/GenericFind.cpp OrthancServer/Sources/Database/FindResponse.cpp OrthancServer/Sources/Database/FindResponse.h OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp OrthancServer/Sources/Database/StatelessDatabaseOperations.h OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerContext.h
diffstat 9 files changed, 94 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Fri May 03 17:20:27 2024 +0200
+++ b/OrthancServer/Sources/Database/Compatibility/GenericFind.cpp	Fri May 03 17:26:06 2024 +0200
@@ -59,7 +59,7 @@
 
         for (std::list<std::string>::const_iterator it = ids.begin(); it != ids.end(); ++it)
         {
-          response.Add(new FindResponse::Item(request.GetLevel(), *it));
+          response.Add(new FindResponse::Resource(request.GetLevel(), *it));
         }
       }
       else
@@ -74,12 +74,12 @@
 
       for (size_t i = 0; i < response.GetSize(); i++)
       {
-        const FindResponse::Item& item = response.GetItem(i);
+        const FindResponse::Resource& resource = response.GetResource(i);
 
         if (request.HasResponseContent(FindRequest::ResponseContent_MainDicomTags))
         {
           DicomMap tmp;
-          item.GetDicomTagsAtLevel(tmp, request.GetLevel());
+          resource.GetDicomTagsAtLevel(tmp, request.GetLevel());
           if (tmp.GetSize() == 0)
           {
             throw OrthancException(ErrorCode_InternalError);
--- a/OrthancServer/Sources/Database/FindResponse.cpp	Fri May 03 17:20:27 2024 +0200
+++ b/OrthancServer/Sources/Database/FindResponse.cpp	Fri May 03 17:26:06 2024 +0200
@@ -152,7 +152,7 @@
   }
 
 
-  FindResponse::DicomTagsAtLevel& FindResponse::Item::GetDicomTagsAtLevel(ResourceType level)
+  FindResponse::DicomTagsAtLevel& FindResponse::Resource::GetDicomTagsAtLevel(ResourceType level)
   {
     switch (level)
     {
@@ -174,7 +174,7 @@
   }
 
 
-  FindResponse::ChildrenAtLevel& FindResponse::Item::GetChildrenAtLevel(ResourceType level)
+  FindResponse::ChildrenAtLevel& FindResponse::Resource::GetChildrenAtLevel(ResourceType level)
   {
     switch (level)
     {
@@ -217,8 +217,8 @@
   }
 
 
-  void FindResponse::Item::AddMetadata(MetadataType metadata,
-                                       const std::string& value)
+  void FindResponse::Resource::AddMetadata(MetadataType metadata,
+                                           const std::string& value)
   {
     if (metadata_.find(metadata) != metadata_.end())
     {
@@ -231,8 +231,8 @@
   }
 
 
-  bool FindResponse::Item::LookupMetadata(std::string& value,
-                                          MetadataType metadata) const
+  bool FindResponse::Resource::LookupMetadata(std::string& value,
+                                              MetadataType metadata) const
   {
     std::map<MetadataType, std::string>::const_iterator found = metadata_.find(metadata);
 
@@ -248,7 +248,7 @@
   }
 
 
-  void FindResponse::Item::ListMetadata(std::set<MetadataType>& target) const
+  void FindResponse::Resource::ListMetadata(std::set<MetadataType>& target) const
   {
     target.clear();
 
@@ -258,7 +258,7 @@
     }
   }
 
-  const std::string& FindResponse::Item::GetParentIdentifier() const
+  const std::string& FindResponse::Resource::GetParentIdentifier() const
   {
     if (level_ == ResourceType_Patient)
     {
@@ -275,7 +275,7 @@
   }
 
 
-  void FindResponse::Item::SetParentIdentifier(const std::string& id)
+  void FindResponse::Resource::SetParentIdentifier(const std::string& id)
   {
     if (level_ == ResourceType_Patient)
     {
@@ -292,7 +292,7 @@
   }
 
 
-  bool FindResponse::Item::HasParentIdentifier() const
+  bool FindResponse::Resource::HasParentIdentifier() const
   {
     if (level_ == ResourceType_Patient)
     {
@@ -305,7 +305,7 @@
   }
 
 
-  void FindResponse::Item::AddLabel(const std::string& label)
+  void FindResponse::Resource::AddLabel(const std::string& label)
   {
     if (labels_.find(label) == labels_.end())
     {
@@ -318,7 +318,7 @@
   }
 
 
-  void FindResponse::Item::AddAttachment(const FileInfo& attachment)
+  void FindResponse::Resource::AddAttachment(const FileInfo& attachment)
   {
     if (attachments_.find(attachment.GetContentType()) == attachments_.end())
     {
@@ -331,7 +331,7 @@
   }
 
 
-  bool FindResponse::Item::LookupAttachment(FileInfo& target, FileContentType type) const
+  bool FindResponse::Resource::LookupAttachment(FileInfo& target, FileContentType type) const
   {
     std::map<FileContentType, FileInfo>::const_iterator it = attachments_.find(type);
     if (it != attachments_.end())
@@ -356,9 +356,9 @@
   }
 
 
-  void FindResponse::Add(Item* item /* takes ownership */)
+  void FindResponse::Add(Resource* item /* takes ownership */)
   {
-    std::unique_ptr<Item> protection(item);
+    std::unique_ptr<Resource> protection(item);
 
     if (item == NULL)
     {
@@ -386,7 +386,7 @@
   }
 
 
-  const FindResponse::Item& FindResponse::GetItem(size_t index) const
+  const FindResponse::Resource& FindResponse::GetResource(size_t index) const
   {
     if (index >= items_.size())
     {
@@ -400,7 +400,7 @@
   }
 
 
-  FindResponse::Item& FindResponse::GetItem(const std::string& id)
+  FindResponse::Resource& FindResponse::GetResource(const std::string& id)
   {
     Index::const_iterator found = index_.find(id);
 
--- a/OrthancServer/Sources/Database/FindResponse.h	Fri May 03 17:20:27 2024 +0200
+++ b/OrthancServer/Sources/Database/FindResponse.h	Fri May 03 17:26:06 2024 +0200
@@ -80,7 +80,7 @@
 
 
   public:
-    class Item : public boost::noncopyable
+    class Resource : public boost::noncopyable
     {
     private:
       ResourceType                          level_;
@@ -102,8 +102,8 @@
       ChildrenAtLevel& GetChildrenAtLevel(ResourceType level);
 
     public:
-      explicit Item(ResourceType level,
-                    const std::string& identifier) :
+      explicit Resource(ResourceType level,
+                        const std::string& identifier) :
         level_(level),
         identifier_(identifier)
       {
@@ -146,7 +146,7 @@
       void GetDicomTagsAtLevel(DicomMap& target,
                                ResourceType level) const
       {
-        const_cast<Item&>(*this).GetDicomTagsAtLevel(level).Fill(target);
+        const_cast<Resource&>(*this).GetDicomTagsAtLevel(level).Fill(target);
       }
 
       void AddChildIdentifier(ResourceType level,
@@ -157,7 +157,7 @@
 
       const std::set<std::string>& GetChildrenIdentifiers(ResourceType level) const
       {
-        return const_cast<Item&>(*this).GetChildrenAtLevel(level).GetIdentifiers();
+        return const_cast<Resource&>(*this).GetChildrenAtLevel(level).GetIdentifiers();
       }
 
       void AddLabel(const std::string& label);
@@ -192,31 +192,31 @@
     };
 
   private:
-    typedef std::map<std::string, Item*>  Index;
+    typedef std::map<std::string, Resource*>  Index;
 
-    std::deque<Item*>  items_;
-    Index              index_;
+    std::deque<Resource*>  items_;
+    Index                  index_;
 
   public:
     ~FindResponse();
 
-    void Add(Item* item /* takes ownership */);
+    void Add(Resource* item /* takes ownership */);
 
     size_t GetSize() const
     {
       return items_.size();
     }
 
-    const Item& GetItem(size_t index) const;
+    const Resource& GetResource(size_t index) const;
 
-    Item& GetItem(const std::string& id);
+    Resource& GetResource(const std::string& id);
 
-    const Item& GetItem(const std::string& id) const
+    const Resource& GetResource(const std::string& id) const
     {
-      return const_cast<FindResponse&>(*this).GetItem(id);
+      return const_cast<FindResponse&>(*this).GetResource(id);
     }
 
-    bool HasItem(const std::string& id) const
+    bool HasResource(const std::string& id) const
     {
       return (index_.find(id) != index_.end());
     }
--- a/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Fri May 03 17:20:27 2024 +0200
+++ b/OrthancServer/Sources/Database/SQLiteDatabaseWrapper.cpp	Fri May 03 17:26:06 2024 +0200
@@ -1173,7 +1173,7 @@
 
           while (statement.Step())
           {
-            response.Add(new FindResponse::Item(request.GetLevel(), statement.ColumnString(0)));
+            response.Add(new FindResponse::Resource(request.GetLevel(), statement.ColumnString(0)));
           }
         }
         else
@@ -1195,7 +1195,7 @@
             while (statement.Step())
             {
               const std::string resourceId = statement.ColumnString(0);
-              response.Add(new FindResponse::Item(request.GetLevel(), resourceId));
+              response.Add(new FindResponse::Resource(request.GetLevel(), resourceId));
             }
           }
 
@@ -1211,86 +1211,86 @@
             while (statement.Step())
             {
               const std::string& resourceId = statement.ColumnString(0);
-              assert(response.HasItem(resourceId));
-              response.GetItem(resourceId).AddStringDicomTag(request.GetLevel(),
-                                                             statement.ColumnInt(1),
-                                                             statement.ColumnInt(2),
-                                                             statement.ColumnString(3));
+              assert(response.HasResource(resourceId));
+              response.GetResource(resourceId).AddStringDicomTag(request.GetLevel(),
+                                                                 statement.ColumnInt(1),
+                                                                 statement.ColumnInt(2),
+                                                                 statement.ColumnString(3));
             }
           }
 
           if (request.HasResponseContent(FindRequest::ResponseContent_Children))
           {
             SQLite::Statement statement(db_, SQLITE_FROM_HERE, 
-                "SELECT filtered.publicId, childLevel.publicId AS childPublicId "
-                "FROM Resources as currentLevel "
-                "    INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = currentLevel.internalId "
-                "    INNER JOIN Resources childLevel ON childLevel.parentId = currentLevel.internalId");
+                                        "SELECT filtered.publicId, childLevel.publicId AS childPublicId "
+                                        "FROM Resources as currentLevel "
+                                        "    INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = currentLevel.internalId "
+                                        "    INNER JOIN Resources childLevel ON childLevel.parentId = currentLevel.internalId");
             formatter.Bind(statement);
 
             while (statement.Step())
             {
               const std::string& resourceId = statement.ColumnString(0);
-              assert(response.HasItem(resourceId));
-              response.GetItem(resourceId).AddChildIdentifier(GetChildResourceType(request.GetLevel()), statement.ColumnString(1));
+              assert(response.HasResource(resourceId));
+              response.GetResource(resourceId).AddChildIdentifier(GetChildResourceType(request.GetLevel()), statement.ColumnString(1));
             }
           }
 
           if (request.HasResponseContent(FindRequest::ResponseContent_Parent))
           {
             SQLite::Statement statement(db_, SQLITE_FROM_HERE, 
-                "SELECT filtered.publicId, parentLevel.publicId AS parentPublicId "
-                "FROM Resources as currentLevel "
-                "    INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = currentLevel.internalId "
-                "    INNER JOIN Resources parentLevel ON currentLevel.parentId = parentLevel.internalId");
+                                        "SELECT filtered.publicId, parentLevel.publicId AS parentPublicId "
+                                        "FROM Resources as currentLevel "
+                                        "    INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = currentLevel.internalId "
+                                        "    INNER JOIN Resources parentLevel ON currentLevel.parentId = parentLevel.internalId");
 
             while (statement.Step())
             {
               const std::string& resourceId = statement.ColumnString(0);
               const std::string& parentId = statement.ColumnString(1);
-              assert(response.HasItem(resourceId));
-              response.GetItem(resourceId).SetParentIdentifier(parentId);
+              assert(response.HasResource(resourceId));
+              response.GetResource(resourceId).SetParentIdentifier(parentId);
             }
           }
 
           if (request.HasResponseContent(FindRequest::ResponseContent_Metadata))
           {
             SQLite::Statement statement(db_, SQLITE_FROM_HERE, 
-                "SELECT filtered.publicId, metadata.type, metadata.value "
-                "FROM Metadata "
-                "  INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = Metadata.id");
+                                        "SELECT filtered.publicId, metadata.type, metadata.value "
+                                        "FROM Metadata "
+                                        "  INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = Metadata.id");
 
             while (statement.Step())
             {
               const std::string& resourceId = statement.ColumnString(0);
-              assert(response.HasItem(resourceId));
-              response.GetItem(resourceId).AddMetadata(static_cast<MetadataType>(statement.ColumnInt(1)),
-                                                       statement.ColumnString(2));
+              assert(response.HasResource(resourceId));
+              response.GetResource(resourceId).AddMetadata(static_cast<MetadataType>(statement.ColumnInt(1)),
+                                                           statement.ColumnString(2));
             }
           }
 
           if (request.HasResponseContent(FindRequest::ResponseContent_Labels))
           {
             SQLite::Statement statement(db_, SQLITE_FROM_HERE, 
-                "SELECT filtered.publicId, label "
-                "FROM Labels "
-                "  INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = Labels.id");
+                                        "SELECT filtered.publicId, label "
+                                        "FROM Labels "
+                                        "  INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = Labels.id");
 
             while (statement.Step())
             {
               const std::string& resourceId = statement.ColumnString(0);
-              assert(response.HasItem(resourceId));
-              response.GetItem(resourceId).AddLabel(statement.ColumnString(1));
+              assert(response.HasResource(resourceId));
+              response.GetResource(resourceId).AddLabel(statement.ColumnString(1));
             }
           }
 
           if (request.HasResponseContent(FindRequest::ResponseContent_Attachments))
           {
             SQLite::Statement statement(db_, SQLITE_FROM_HERE, 
-                "SELECT filtered.publicId, uuid, fileType, uncompressedSize, compressionType, compressedSize, "
-                "       uncompressedMD5, compressedMD5 "
-                "FROM AttachedFiles "
-                "  INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = AttachedFiles.id");
+                                        "SELECT filtered.publicId, uuid, fileType, uncompressedSize, compressionType, compressedSize, "
+                                        "       uncompressedMD5, compressedMD5 "
+                                        "FROM AttachedFiles "
+                                        "  INNER JOIN FilteredResourcesIds filtered ON filtered.internalId = AttachedFiles.id");
 
             while (statement.Step())
             {
@@ -1303,8 +1303,8 @@
                                              statement.ColumnInt64(5),
                                              statement.ColumnString(7));
 
-              assert(response.HasItem(resourceId));
-              response.GetItem(resourceId).AddAttachment(attachment);
+              assert(response.HasResource(resourceId));
+              response.GetResource(resourceId).AddAttachment(attachment);
             };
           }
 
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Fri May 03 17:20:27 2024 +0200
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Fri May 03 17:26:06 2024 +0200
@@ -3864,22 +3864,27 @@
   // TODO-FIND: we reuse the ExpandedResource class to reuse Serialization code from ExpandedResource
   // But, finally, we might just get rid of ExpandedResource and replace it by FindResponse
   ExpandedResource::ExpandedResource(const FindRequest& request,
-                                     const FindResponse::Item& item) :
-    id_(item.GetIdentifier()),
+                                     const FindResponse::Resource& resource) :
+    id_(resource.GetIdentifier()),
     level_(request.GetLevel()),
     isStable_(false),
     expectedNumberOfInstances_(0),
     fileSize_(0),
     indexInSeries_(0)
   {
+    if (request.GetLevel() != resource.GetLevel())
+    {
+      throw OrthancException(ErrorCode_InternalError);
+    }
+
     if (request.HasResponseContent(FindRequest::ResponseContent_MainDicomTags))
     {
-      item.GetDicomTagsAtLevel(tags_, request.GetLevel());
+      resource.GetDicomTagsAtLevel(tags_, request.GetLevel());
     }
 
     if (request.HasResponseContent(FindRequest::ResponseContent_Children))
     {
-      const std::set<std::string>& s = item.GetChildrenIdentifiers(GetChildResourceType(request.GetLevel()));
+      const std::set<std::string>& s = resource.GetChildrenIdentifiers(GetChildResourceType(request.GetLevel()));
       for (std::set<std::string>::const_iterator it = s.begin(); it != s.end(); ++it)
       {
         childrenIds_.push_back(*it);
@@ -3888,39 +3893,39 @@
 
     if (request.HasResponseContent(FindRequest::ResponseContent_Parent))
     {
-      parentId_ = item.GetParentIdentifier();
+      parentId_ = resource.GetParentIdentifier();
     }
 
     if (request.HasResponseContent(FindRequest::ResponseContent_Metadata))
     {
-      metadata_ = item.GetMetadata();
+      metadata_ = resource.GetMetadata();
       std::string value;
-      if (item.LookupMetadata(value, MetadataType_MainDicomTagsSignature))
+      if (resource.LookupMetadata(value, MetadataType_MainDicomTagsSignature))
       {
         mainDicomTagsSignature_ = value;
       }
-      if (item.LookupMetadata(value, MetadataType_AnonymizedFrom))
+      if (resource.LookupMetadata(value, MetadataType_AnonymizedFrom))
       {
         anonymizedFrom_ = value;
       }
-      if (item.LookupMetadata(value, MetadataType_ModifiedFrom))
+      if (resource.LookupMetadata(value, MetadataType_ModifiedFrom))
       {
         modifiedFrom_ = value;
       }
-      if (item.LookupMetadata(value, MetadataType_LastUpdate))
+      if (resource.LookupMetadata(value, MetadataType_LastUpdate))
       {
         lastUpdate_ = value;
       }
       if (request.GetLevel() == ResourceType_Series)
       {
-        if (item.LookupMetadata(value, MetadataType_Series_ExpectedNumberOfInstances))
+        if (resource.LookupMetadata(value, MetadataType_Series_ExpectedNumberOfInstances))
         {
           expectedNumberOfInstances_ = boost::lexical_cast<int>(value);
         }
       }
       if (request.GetLevel() == ResourceType_Instance)
       {
-        if (item.LookupMetadata(value, MetadataType_Instance_IndexInSeries))
+        if (resource.LookupMetadata(value, MetadataType_Instance_IndexInSeries))
         {
           indexInSeries_ = boost::lexical_cast<int>(value);
         }
@@ -3929,13 +3934,13 @@
 
     if (request.HasResponseContent(FindRequest::ResponseContent_Labels))
     {
-      labels_ = item.GetLabels();
+      labels_ = resource.GetLabels();
     }
 
     if (request.HasResponseContent(FindRequest::ResponseContent_Attachments))
     {
       FileInfo attachment;
-      if (item.LookupAttachment(attachment, FileContentType_Dicom))
+      if (resource.LookupAttachment(attachment, FileContentType_Dicom))
       {
         fileSize_ = attachment.GetUncompressedSize();
         fileUuid_ = attachment.GetUuid();
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Fri May 03 17:20:27 2024 +0200
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Fri May 03 17:26:06 2024 +0200
@@ -81,7 +81,7 @@
     }
     
     ExpandedResource(const FindRequest& request,
-                     const FindResponse::Item& item);
+                     const FindResponse::Resource& resource);
 
     void SetResource(ResourceType level,
                      const std::string& id)
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Fri May 03 17:20:27 2024 +0200
+++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp	Fri May 03 17:26:06 2024 +0200
@@ -315,14 +315,14 @@
       {
         for (size_t i = 0; i < response.GetSize(); i++)
         {
-          answer.append(response.GetItem(i).GetIdentifier());
+          answer.append(response.GetResource(i).GetIdentifier());
         }
       }
       else
       {
         for (size_t i = 0; i < response.GetSize(); i++)
         {
-          context.AppendFindResponse(answer, request, response.GetItem(i), format, requestedTags, true /* allowStorageAccess */);
+          context.AppendFindResponse(answer, request, response.GetResource(i), format, requestedTags, true /* allowStorageAccess */);
         }
       }
 
--- a/OrthancServer/Sources/ServerContext.cpp	Fri May 03 17:20:27 2024 +0200
+++ b/OrthancServer/Sources/ServerContext.cpp	Fri May 03 17:26:06 2024 +0200
@@ -2719,7 +2719,7 @@
 
   void ServerContext::AppendFindResponse(Json::Value& target,
                                          const FindRequest& request,
-                                         const FindResponse::Item& item,
+                                         const FindResponse::Resource& item,
                                          DicomToJsonFormat format,
                                          const std::set<DicomTag>& requestedTags,
                                          bool allowStorageAccess)
--- a/OrthancServer/Sources/ServerContext.h	Fri May 03 17:20:27 2024 +0200
+++ b/OrthancServer/Sources/ServerContext.h	Fri May 03 17:26:06 2024 +0200
@@ -609,7 +609,7 @@
 
     void AppendFindResponse(Json::Value& target,
                             const FindRequest& request,
-                            const FindResponse::Item& item,
+                            const FindResponse::Resource& resource,
                             DicomToJsonFormat format,
                             const std::set<DicomTag>& requestedTags,
                             bool allowStorageAccess);