changeset 5883:96f49b200c15 find-refactoring

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 29 Nov 2024 09:30:50 +0100
parents 1c562a107a80
children 92e5579681f2
files OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp OrthancServer/Sources/Database/StatelessDatabaseOperations.h
diffstat 2 files changed, 40 insertions(+), 89 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Thu Nov 28 11:59:51 2024 +0100
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Fri Nov 29 09:30:50 2024 +0100
@@ -580,7 +580,34 @@
   {
     ApplyInternal(NULL, &operations);
   }
-  
+
+
+  const FindResponse::Resource& StatelessDatabaseOperations::ExecuteSingleResource(FindResponse& response,
+    const FindRequest& request)
+  {
+    ExecuteFind(response, request);
+
+    if (response.GetSize() == 0)
+    {
+      throw OrthancException(ErrorCode_UnknownResource);
+    }
+    else if (response.GetSize() == 1)
+    {
+      if (response.GetResourceByIndex(0).GetLevel() != request.GetLevel())
+      {
+        throw OrthancException(ErrorCode_DatabasePlugin);
+      }
+      else
+      {
+        return response.GetResourceByIndex(0);
+      }
+    }
+    else
+    {
+      throw OrthancException(ErrorCode_DatabasePlugin);
+    }
+  }
+
 
   void StatelessDatabaseOperations::GetAllMetadata(std::map<MetadataType, std::string>& target,
                                                    const std::string& publicId,
@@ -591,32 +618,13 @@
     request.SetRetrieveMetadata(true);
 
     FindResponse response;
-    ExecuteFind(response, request);
-
-    if (response.GetSize() == 0)
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
-    else if (response.GetSize() == 1)
+    std::map<MetadataType, FindResponse::MetadataContent> metadata = ExecuteSingleResource(response, request).GetMetadata(level);
+
+    target.clear();
+    for (std::map<MetadataType, FindResponse::MetadataContent>::const_iterator
+         it = metadata.begin(); it != metadata.end(); ++it)
     {
-      if (response.GetResourceByIndex(0).GetLevel() != level)
-      {
-        throw OrthancException(ErrorCode_DatabasePlugin);
-      }
-      else
-      {
-        std::map<MetadataType, FindResponse::MetadataContent> metadata = response.GetResourceByIndex(0).GetMetadata(level);
-
-        target.clear();
-        for (std::map<MetadataType, FindResponse::MetadataContent>::const_iterator it = metadata.begin(); it != metadata.end(); ++it)
-        {
-          target[it->first] = it->second.GetValue();
-        }
-      }
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_DatabasePlugin);
+      target[it->first] = it->second.GetValue();
     }
   }
 
@@ -632,27 +640,7 @@
     request.SetRetrieveAttachments(true);
 
     FindResponse response;
-    ExecuteFind(response, request);
-
-    if (response.GetSize() == 0)
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
-    else if (response.GetSize() == 1)
-    {
-      if (response.GetResourceByIndex(0).GetLevel() != level)
-      {
-        throw OrthancException(ErrorCode_DatabasePlugin);
-      }
-      else
-      {
-        return response.GetResourceByIndex(0).LookupAttachment(attachment, revision, contentType);
-      }
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_DatabasePlugin);
-    }
+    return ExecuteSingleResource(response, request).LookupAttachment(attachment, revision, contentType);
   }
 
 
@@ -1030,27 +1018,7 @@
     request.SetRetrieveMetadataRevisions(false);  // No need to retrieve revisions
 
     FindResponse response;
-    ExecuteFind(response, request);
-
-    if (response.GetSize() == 0)
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
-    else if (response.GetSize() == 1)
-    {
-      if (response.GetResourceByIndex(0).GetLevel() != expectedType)
-      {
-        throw OrthancException(ErrorCode_DatabasePlugin);
-      }
-      else
-      {
-        return response.GetResourceByIndex(0).LookupMetadata(target, expectedType, type);
-      }
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_DatabasePlugin);
-    }
+    return ExecuteSingleResource(response, request).LookupMetadata(target, expectedType, type);
   }
 
 
@@ -1066,27 +1034,7 @@
     request.SetRetrieveMetadataRevisions(true);  // We are asked to retrieve revisions
 
     FindResponse response;
-    ExecuteFind(response, request);
-
-    if (response.GetSize() == 0)
-    {
-      throw OrthancException(ErrorCode_UnknownResource);
-    }
-    else if (response.GetSize() == 1)
-    {
-      if (response.GetResourceByIndex(0).GetLevel() != expectedType)
-      {
-        throw OrthancException(ErrorCode_DatabasePlugin);
-      }
-      else
-      {
-        return response.GetResourceByIndex(0).LookupMetadata(target, revision, expectedType, type);
-      }
-    }
-    else
-    {
-      throw OrthancException(ErrorCode_DatabasePlugin);
-    }
+    return ExecuteSingleResource(response, request).LookupMetadata(target, revision, expectedType, type);
   }
 
 
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Thu Nov 28 11:59:51 2024 +0100
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.h	Fri Nov 29 09:30:50 2024 +0100
@@ -491,6 +491,9 @@
     void ApplyInternal(IReadOnlyOperations* readOperations,
                        IReadWriteOperations* writeOperations);
 
+    const FindResponse::Resource &ExecuteSingleResource(FindResponse &response,
+                                                        const FindRequest &request);
+
   protected:
     void StandaloneRecycling(MaxStorageMode maximumStorageMode,
                              uint64_t maximumStorageSize,