diff OrthancServer/OrthancRestApi/OrthancRestModalities.cpp @ 3737:f29843323daf storage-commitment

accessing storage commitment reports from REST API
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 10 Mar 2020 20:33:01 +0100
parents 4fc24b69446a
children bff4da769f6f
line wrap: on
line diff
--- a/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp	Tue Mar 10 17:43:49 2020 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestModalities.cpp	Tue Mar 10 20:33:01 2020 +0100
@@ -46,6 +46,7 @@
 #include "../ServerJobs/DicomMoveScuJob.h"
 #include "../ServerJobs/OrthancPeerStoreJob.h"
 #include "../ServerToolbox.h"
+#include "../StorageCommitmentReports.h"
 
 
 namespace Orthanc
@@ -1305,7 +1306,9 @@
   }
 
 
-  static void StorageCommitment(RestApiPostCall& call)
+  // Storage commitment SCU ---------------------------------------------------
+
+  static void StorageCommitmentScu(RestApiPostCall& call)
   {
     ServerContext& context = OrthancRestApi::GetContext(call);
 
@@ -1364,20 +1367,27 @@
         }
       }
 
-      const std::string transaction = Toolbox::GenerateDicomPrivateUniqueIdentifier();
+      const std::string transactionUid = Toolbox::GenerateDicomPrivateUniqueIdentifier();
 
       {
-        const std::string& localAet = context.GetDefaultLocalApplicationEntityTitle();
         const RemoteModalityParameters remote =
           MyGetModalityUsingSymbolicName(call.GetUriComponent("id", ""));
 
+        const std::string& remoteAet = remote.GetApplicationEntityTitle();
+        const std::string& localAet = context.GetDefaultLocalApplicationEntityTitle();
+        
+        // Create a "pending" storage commitment report BEFORE the
+        // actual SCU call in order to avoid race conditions
+        context.GetStorageCommitmentReports().Store(
+          transactionUid, new StorageCommitmentReports::Report(remoteAet));
+
         DicomUserConnection scu(localAet, remote);
-        scu.RequestStorageCommitment(transaction, sopClassUids, sopInstanceUids);
+        scu.RequestStorageCommitment(transactionUid, sopClassUids, sopInstanceUids);
       }
 
       Json::Value result = Json::objectValue;
-      result["ID"] = transaction;
-      result["Path"] = "/storage-commitment/" + transaction;
+      result["ID"] = transactionUid;
+      result["Path"] = "/storage-commitment/" + transactionUid;
       call.GetOutput().AnswerJson(result);
     }
     else
@@ -1388,6 +1398,31 @@
   }
 
 
+  static void GetStorageCommitmentReport(RestApiGetCall& call)
+  {
+    ServerContext& context = OrthancRestApi::GetContext(call);
+
+    const std::string& transactionUid = call.GetUriComponent("id", "");
+
+    {
+      StorageCommitmentReports::Accessor accessor(
+        context.GetStorageCommitmentReports(), transactionUid);
+
+      if (accessor.IsValid())
+      {
+        Json::Value json;
+        accessor.GetReport().Format(json);
+        call.GetOutput().AnswerJson(json);
+      }
+      else
+      {
+        throw OrthancException(ErrorCode_InexistentItem,
+                               "No storage commitment transaction with UID: " + transactionUid);
+      }
+    }
+  }
+  
+
   void OrthancRestApi::RegisterModalities()
   {
     Register("/modalities", ListModalities);
@@ -1432,6 +1467,7 @@
 
     Register("/modalities/{id}/find-worklist", DicomFindWorklist);
 
-    Register("/modalities/{id}/storage-commitment", StorageCommitment);
+    Register("/modalities/{id}/storage-commitment", StorageCommitmentScu);
+    Register("/storage-commitment/{id}", GetStorageCommitmentReport);
   }
 }