changeset 1140:94c5f6623b3a

URIs to get all the children of a given resource in a single REST call
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Sep 2014 17:20:56 +0200
parents f167b672db94
children 2ff467bcfb98
files NEWS OrthancServer/OrthancRestApi/OrthancRestResources.cpp
diffstat 2 files changed, 70 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Wed Sep 10 16:38:28 2014 +0200
+++ b/NEWS	Wed Sep 10 17:20:56 2014 +0200
@@ -5,9 +5,10 @@
 -------
 
 * Creation of ZIP archives for media storage, with DICOMDIR
+* URIs to get all the children of a given resource in a single REST call
+* "/tools/lookup" URI to map DICOM UIDs to Orthanc identifiers
 * Support of index-only mode (using the "StoreDicom" option)
 * Plugins can implement a custom storage area
-* "/tools/lookup" URI to map DICOM UIDs to Orthanc identifiers
 
 Minor
 -----
--- a/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Wed Sep 10 16:38:28 2014 +0200
+++ b/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Wed Sep 10 17:20:56 2014 +0200
@@ -810,6 +810,67 @@
   }
 
 
+  template <enum ResourceType start, 
+            enum ResourceType end>
+  static void GetChildResources(RestApiGetCall& call)
+  {
+    ServerIndex& index = OrthancRestApi::GetIndex(call);
+
+    std::list<std::string> a, b, c;
+    a.push_back(call.GetUriComponent("id", ""));
+
+    ResourceType type = start;
+    while (type != end)
+    {
+      b.clear();
+
+      for (std::list<std::string>::const_iterator
+             it = a.begin(); it != a.end(); it++)
+      {
+        index.GetChildren(c, *it);
+        b.splice(b.begin(), c);
+      }
+
+      switch (type)
+      {
+        case ResourceType_Patient:
+          type = ResourceType_Study;
+          break;
+
+        case ResourceType_Study:
+          type = ResourceType_Series;
+          break;
+
+        case ResourceType_Series:
+          type = ResourceType_Instance;
+          break;
+
+        default:
+          throw OrthancException(ErrorCode_InternalError);
+      }
+
+      a.clear();
+      a.splice(a.begin(), b);
+    }
+
+    Json::Value result = Json::arrayValue;
+
+    for (std::list<std::string>::const_iterator
+           it = a.begin(); it != a.end(); it++)
+    {
+      Json::Value item;
+
+      if (OrthancRestApi::GetIndex(call).LookupResource(item, *it, end))
+      {
+        result.append(item);
+      }
+    }
+
+    call.GetOutput().AnswerJson(result);
+  }
+
+
+
   void OrthancRestApi::RegisterResources()
   {
     Register("/instances", ListResources<ResourceType_Instance>);
@@ -880,6 +941,13 @@
 
     Register("/tools/lookup", Lookup);
 
+    Register("/patients/{id}/studies", GetChildResources<ResourceType_Patient, ResourceType_Study>);
+    Register("/patients/{id}/series", GetChildResources<ResourceType_Patient, ResourceType_Series>);
+    Register("/patients/{id}/instances", GetChildResources<ResourceType_Patient, ResourceType_Instance>);
+    Register("/studies/{id}/series", GetChildResources<ResourceType_Study, ResourceType_Series>);
+    Register("/studies/{id}/instances", GetChildResources<ResourceType_Study, ResourceType_Instance>);
+    Register("/series/{id}/instances", GetChildResources<ResourceType_Series, ResourceType_Instance>);
+
     Register("/instances/{id}/content/*", GetRawContent);
   }
 }