diff Plugin/OrthancResource.cpp @ 1:d5d3cb00556a

initial release
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 22 Mar 2017 16:13:52 +0100
parents
children c44013681a51
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Plugin/OrthancResource.cpp	Wed Mar 22 16:13:52 2017 +0100
@@ -0,0 +1,286 @@
+/**
+ * Advanced authorization plugin for Orthanc
+ * Copyright (C) 2017 Osimis, Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#include "OrthancResource.h"
+
+#include "../Resources/Orthanc/Plugins/Samples/Common/OrthancPluginCppWrapper.h"
+
+namespace OrthancPlugins
+{
+  void OrthancResource::GetDicomUidInternal(std::string& result,
+                                            Orthanc::ResourceType level,
+                                            const Json::Value& content)
+  {
+    std::string uidTag;
+        
+    switch (level)
+    {
+      case Orthanc::ResourceType_Patient:
+        uidTag = "PatientID";
+        break;
+
+      case Orthanc::ResourceType_Study:
+        uidTag = "StudyInstanceUID";
+        break;
+
+      case Orthanc::ResourceType_Series:
+        uidTag = "SeriesInstanceUID";
+        break;
+
+      case Orthanc::ResourceType_Instance:
+        uidTag = "SOPInstanceUID";
+        break;
+
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+    }
+
+    static const char* MAIN_DICOM_TAGS = "MainDicomTags";
+      
+    if (content.type() != Json::objectValue ||
+        !content.isMember(MAIN_DICOM_TAGS))
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
+    }
+
+    const Json::Value& mainDicomTags = content[MAIN_DICOM_TAGS];
+    if (mainDicomTags.type() != Json::objectValue ||
+        (mainDicomTags.isMember(uidTag) &&
+         mainDicomTags[uidTag].type() != Json::stringValue))
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
+    }
+
+    if (!mainDicomTags.isMember(uidTag))
+    {
+      result.clear();
+    }
+    else
+    {
+      result = mainDicomTags[uidTag].asString();
+    }
+  }
+
+
+  Orthanc::ResourceType OrthancResource::GetLevel() const
+  {
+    if (IsValid())
+    {
+      return level_;
+    }
+    else
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+    }
+  }
+  
+
+  const std::string& OrthancResource::GetIdentifier() const
+  {
+    if (IsValid())
+    {
+      return id_;
+    }
+    else
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+    }
+  }
+
+  
+  bool OrthancResource::GetContent(Json::Value& content,
+                                   OrthancPluginContext* context) const
+  {
+    if (!IsValid())
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
+    }
+
+    std::string uri;
+    switch (level_)
+    {
+      case Orthanc::ResourceType_Patient:
+        uri = "patients";
+        break;
+
+      case Orthanc::ResourceType_Study:
+        uri = "studies";
+        break;
+
+      case Orthanc::ResourceType_Series:
+        uri = "series";
+        break;
+
+      case Orthanc::ResourceType_Instance:
+        uri = "instances";
+        break;
+
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+    }
+
+    uri = "/" + uri + "/" + id_;
+        
+    return RestApiGet(content, context, uri, false /* ignore plugins */);
+  }
+  
+
+  bool OrthancResource::GetDicomUid(std::string& dicomUid /* out */,
+                                    OrthancPluginContext* context) const
+  {
+    Json::Value content;
+        
+    if (!GetContent(content, context))
+    {
+      return false;
+    }
+    else
+    {
+      GetDicomUidInternal(dicomUid, level_, content);
+      return true;
+    }
+  }
+  
+    
+  bool OrthancResource::GetHierarchy(std::string& dicomUid /* out */,
+                                     OrthancResource& parent /* out */,
+                                     std::list<OrthancResource>& children /* out */,
+                                     OrthancPluginContext* context) const
+  {
+    Json::Value content;
+        
+    if (!GetContent(content, context))
+    {
+      return false;
+    }
+
+    std::string parentKey, childrenKey;
+        
+    switch (level_)
+    {
+      case Orthanc::ResourceType_Patient:
+        childrenKey = "Studies";
+        break;
+
+      case Orthanc::ResourceType_Study:
+        parentKey = "ParentPatient";
+        childrenKey = "Series";
+        break;
+
+      case Orthanc::ResourceType_Series:
+        parentKey = "ParentStudy";
+        childrenKey = "Instances";
+        break;
+
+      case Orthanc::ResourceType_Instance:
+        parentKey = "ParentSeries";
+        break;
+
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+    }
+
+    GetDicomUidInternal(dicomUid, level_, content);
+      
+    if (content.type() != Json::objectValue)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
+    }
+
+    if (!parentKey.empty())
+    {
+      if (!content.isMember(parentKey) ||
+          content[parentKey].type() != Json::stringValue)
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
+      }
+
+      parent = OrthancResource(Orthanc::GetParentResourceType(level_),
+                               content[parentKey].asString());
+    }
+
+    children.clear();
+        
+    if (!childrenKey.empty())
+    {
+      if (!content.isMember(childrenKey) ||
+          content[childrenKey].type() != Json::arrayValue)
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
+      }
+
+      Orthanc::ResourceType childrenType = Orthanc::GetChildResourceType(level_);
+
+      for (Json::Value::ArrayIndex i = 0; i < content[childrenKey].size(); i++)
+      {
+        const Json::Value& child = content[childrenKey][i];
+
+        if (child.type() != Json::stringValue)
+        {
+          throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
+        }
+
+        children.push_back(OrthancResource(childrenType, child.asString()));
+      }
+    }
+        
+    return true;
+  }
+
+
+  bool OrthancResource::LookupOrthancId(std::string& result,
+                                        OrthancPluginContext* context,
+                                        Orthanc::ResourceType level,
+                                        const std::string& dicomUid)
+  {
+    OrthancString s(context);
+
+    switch (level)
+    {
+      case Orthanc::ResourceType_Patient:
+        s.Assign(OrthancPluginLookupPatient(context, dicomUid.c_str()));
+        break;
+
+      case Orthanc::ResourceType_Study:
+        s.Assign(OrthancPluginLookupStudy(context, dicomUid.c_str()));
+        break;
+
+      case Orthanc::ResourceType_Series:
+        s.Assign(OrthancPluginLookupSeries(context, dicomUid.c_str()));
+        break;
+
+      case Orthanc::ResourceType_Instance:
+        s.Assign(OrthancPluginLookupInstance(context, dicomUid.c_str()));
+        break;
+
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+    }
+
+    if (s.GetContent() == NULL)
+    {
+      // Inexisting resource
+      return false;
+    }
+    else
+    {
+      result.assign(s.GetContent());
+      return true;
+    }
+  }
+}