changeset 1281:8dac11c78d71

URIs to get all the parents of a given resource in a single REST call
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 03 Feb 2015 10:53:35 +0100
parents d6a65dc6d0ac
children 7bccdd221e2b
files NEWS OrthancServer/OrthancRestApi/OrthancRestResources.cpp
diffstat 2 files changed, 49 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Tue Feb 03 10:25:56 2015 +0100
+++ b/NEWS	Tue Feb 03 10:53:35 2015 +0100
@@ -4,6 +4,7 @@
 General
 -------
 
+* URIs to get all the parents of a given resource in a single REST call
 * Support of HTTP proxy
 * Instances without PatientID are now allowed
 * Support of Tudor DICOM in Query/Retrieve
--- a/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Tue Feb 03 10:25:56 2015 +0100
+++ b/OrthancServer/OrthancRestApi/OrthancRestResources.cpp	Tue Feb 03 10:53:35 2015 +0100
@@ -912,6 +912,47 @@
 
 
 
+  template <enum ResourceType start, 
+            enum ResourceType end>
+  static void GetParentResource(RestApiGetCall& call)
+  {
+    assert(start > end);
+
+    ServerIndex& index = OrthancRestApi::GetIndex(call);
+    
+    std::string current = call.GetUriComponent("id", "");
+    ResourceType currentType = start;
+    while (currentType > end)
+    {
+      std::string parent;
+      if (!index.LookupParent(parent, current))
+      {
+        // Error that could happen if the resource gets deleted by
+        // another concurrent call
+        return;
+      }
+      
+      current = parent;
+      switch (currentType)
+      {
+        case ResourceType_Instance:  currentType = ResourceType_Series; break;
+        case ResourceType_Series:    currentType = ResourceType_Study; break;
+        case ResourceType_Study:     currentType = ResourceType_Patient; break;
+        default:                     throw OrthancException(ErrorCode_InternalError);
+      }
+    }
+
+    assert(currentType == end);
+
+    Json::Value result;
+    if (index.LookupResource(result, current, end))
+    {
+      call.GetOutput().AnswerJson(result);
+    }
+  }
+
+
+
   void OrthancRestApi::RegisterResources()
   {
     Register("/instances", ListResources<ResourceType_Instance>);
@@ -989,6 +1030,13 @@
     Register("/studies/{id}/instances", GetChildResources<ResourceType_Study, ResourceType_Instance>);
     Register("/series/{id}/instances", GetChildResources<ResourceType_Series, ResourceType_Instance>);
 
+    Register("/studies/{id}/patient", GetParentResource<ResourceType_Study, ResourceType_Patient>);
+    Register("/series/{id}/patient", GetParentResource<ResourceType_Series, ResourceType_Patient>);
+    Register("/series/{id}/study", GetParentResource<ResourceType_Series, ResourceType_Study>);
+    Register("/instances/{id}/patient", GetParentResource<ResourceType_Instance, ResourceType_Patient>);
+    Register("/instances/{id}/study", GetParentResource<ResourceType_Instance, ResourceType_Study>);
+    Register("/instances/{id}/series", GetParentResource<ResourceType_Instance, ResourceType_Series>);
+
     Register("/patients/{id}/instances-tags", GetChildInstancesTags);
     Register("/studies/{id}/instances-tags", GetChildInstancesTags);
     Register("/series/{id}/instances-tags", GetChildInstancesTags);