changeset 2205:395522e46b2b

refactoring of the worklist sample using OrthancPluginCppWrapper
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 08 Dec 2016 16:16:25 +0100
parents 0158f2de8cad
children 27106f7e3759
files CMakeLists.txt Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Plugins/Samples/Common/OrthancPluginCppWrapper.h Plugins/Samples/Common/OrthancPlugins.cmake Plugins/Samples/ModalityWorklists/CMakeLists.txt Plugins/Samples/ModalityWorklists/Plugin.cpp Plugins/Samples/ServeFolders/Plugin.cpp
diffstat 7 files changed, 121 insertions(+), 146 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Thu Dec 08 14:46:13 2016 +0100
+++ b/CMakeLists.txt	Thu Dec 08 16:16:25 2016 +0100
@@ -598,6 +598,7 @@
   add_library(ModalityWorklists SHARED 
     ${BOOST_SOURCES}
     ${JSONCPP_SOURCES}
+    Plugins/Samples/Common/OrthancPluginCppWrapper.cpp
     Plugins/Samples/ModalityWorklists/Plugin.cpp
     ${MODALITY_WORKLISTS_RESOURCES}
     )
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Thu Dec 08 14:46:13 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Thu Dec 08 16:16:25 2016 +0100
@@ -276,11 +276,24 @@
   }
 
 
-  OrthancString::OrthancString(OrthancPluginContext* context,
-                               char* str) :
-    context_(context),
-    str_(str)
+  void MemoryBuffer::GetDicomQuery(const OrthancPluginWorklistQuery* query)
+  {
+    Clear();
+    Check(OrthancPluginWorklistGetDicomQuery(context_, &buffer_, query));
+  }
+
+
+  void OrthancString::Assign(char* str)
   {
+    if (str == NULL)
+    {
+      ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_InternalError);
+    }
+    else
+    {
+      Clear();
+      str_ = str;
+    }
   }
 
 
@@ -322,12 +335,24 @@
       ORTHANC_PLUGINS_THROW_EXCEPTION(OrthancPluginErrorCode_BadFileFormat);
     }
   }
+
+  
+  void OrthancString::DicomToJson(const OrthancPlugins::MemoryBuffer& dicom,
+                                  OrthancPluginDicomToJsonFormat format,
+                                  OrthancPluginDicomToJsonFlags flags,
+                                  uint32_t maxStringLength)
+  {
+    Assign(OrthancPluginDicomBufferToJson(context_, dicom.GetData(), dicom.GetSize(), 
+                                          format, flags, maxStringLength));
+  }
+
   
 
   OrthancConfiguration::OrthancConfiguration(OrthancPluginContext* context) : 
     context_(context)
   {
-    OrthancString str(context, OrthancPluginGetConfiguration(context));
+    OrthancString str(context);
+    str.Assign(OrthancPluginGetConfiguration(context));
 
     if (str.GetContent() == NULL)
     {
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Thu Dec 08 14:46:13 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Thu Dec 08 16:16:25 2016 +0100
@@ -172,6 +172,8 @@
                      OrthancPluginCreateDicomFlags flags);
 
     void ReadFile(const std::string& path);
+
+    void GetDicomQuery(const OrthancPluginWorklistQuery* query);
   };
 
 
@@ -181,16 +183,21 @@
     OrthancPluginContext*  context_;
     char*                  str_;
 
+    void Clear();
+
   public:
-    OrthancString(OrthancPluginContext* context,
-                  char* str);
+    OrthancString(OrthancPluginContext* context) :
+      context_(context),
+      str_(NULL)
+    {
+    }
 
     ~OrthancString()
     {
       Clear();
     }
 
-    void Clear();
+    void Assign(char* str);
 
     const char* GetContent() const
     {
@@ -200,6 +207,11 @@
     void ToString(std::string& target) const;
 
     void ToJson(Json::Value& target) const;
+
+    void DicomToJson(const OrthancPlugins::MemoryBuffer& dicom,
+                     OrthancPluginDicomToJsonFormat format,
+                     OrthancPluginDicomToJsonFlags flags,
+                     uint32_t maxStringLength);
   };
 
 
--- a/Plugins/Samples/Common/OrthancPlugins.cmake	Thu Dec 08 14:46:13 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPlugins.cmake	Thu Dec 08 16:16:25 2016 +0100
@@ -29,4 +29,4 @@
 endif()
 
 
-add_definitions(-DORTHANC_HAS_EXCEPTION=0)
+add_definitions(-DHAS_ORTHANC_EXCEPTION=0)
--- a/Plugins/Samples/ModalityWorklists/CMakeLists.txt	Thu Dec 08 14:46:13 2016 +0100
+++ b/Plugins/Samples/ModalityWorklists/CMakeLists.txt	Thu Dec 08 16:16:25 2016 +0100
@@ -16,6 +16,7 @@
 
 add_library(ModalityWorklists SHARED 
   Plugin.cpp
+  ../Common/OrthancPluginCppWrapper.cpp
   ${JSONCPP_SOURCES}
   ${BOOST_SOURCES}
   )
--- a/Plugins/Samples/ModalityWorklists/Plugin.cpp	Thu Dec 08 14:46:13 2016 +0100
+++ b/Plugins/Samples/ModalityWorklists/Plugin.cpp	Thu Dec 08 16:16:25 2016 +0100
@@ -18,7 +18,7 @@
  **/
 
 
-#include <orthanc/OrthancCPlugin.h>
+#include "../Common/OrthancPluginCppWrapper.h"
 
 #include <boost/filesystem.hpp>
 #include <json/value.h>
@@ -31,23 +31,6 @@
 static std::string folder_;
 
 
-static bool ReadFile(std::string& result,
-                     const std::string& path)
-{
-  OrthancPluginMemoryBuffer tmp;
-  if (OrthancPluginReadFile(context_, &tmp, path.c_str()))
-  {
-    return false;
-  }
-  else
-  {
-    result.assign(reinterpret_cast<const char*>(tmp.data), tmp.size);
-    OrthancPluginFreeMemoryBuffer(context_, &tmp);
-    return true;
-  }
-}
-
-
 /**
  * This is the main function for matching a DICOM worklist against a query.
  **/
@@ -55,18 +38,14 @@
                                              const OrthancPluginWorklistQuery* query,
                                              const std::string& path)
 {
-  std::string dicom;
-  if (!ReadFile(dicom, path))
-  {
-    // Cannot read this file, ignore this error
-    return OrthancPluginErrorCode_Success;
-  }
+  OrthancPlugins::MemoryBuffer dicom(context_);
+  dicom.ReadFile(path);
 
-  if (OrthancPluginWorklistIsMatch(context_, query, dicom.c_str(), dicom.size()))
+  if (OrthancPluginWorklistIsMatch(context_, query, dicom.GetData(), dicom.GetSize()))
   {
     // This DICOM file matches the worklist query, add it to the answers
     return OrthancPluginWorklistAddAnswer
-      (context_, answers, query, dicom.c_str(), dicom.size());
+      (context_, answers, query, dicom.GetData(), dicom.GetSize());
   }
   else
   {
@@ -77,42 +56,20 @@
 
 
 
-static bool ConvertToJson(Json::Value& result,
-                          char* content)
+static void GetQueryDicom(Json::Value& value,
+                          const OrthancPluginWorklistQuery* query)
 {
-  if (content == NULL)
-  {
-    return false;
-  }
-  else
-  {
-    Json::Reader reader;
-    bool success = reader.parse(content, content + strlen(content), result);
-    OrthancPluginFreeString(context_, content);
-    return success;
-  }
+  OrthancPlugins::MemoryBuffer dicom(context_);
+  dicom.GetDicomQuery(query);
+
+  OrthancPlugins::OrthancString str(context_);
+  str.DicomToJson(dicom, OrthancPluginDicomToJsonFormat_Short, 
+                  static_cast<OrthancPluginDicomToJsonFlags>(0), 0);
+
+  str.ToJson(value);
 }
 
 
-static bool GetQueryDicom(Json::Value& value,
-                          const OrthancPluginWorklistQuery* query)
-{
-  OrthancPluginMemoryBuffer dicom;
-  if (OrthancPluginWorklistGetDicomQuery(context_, &dicom, query))
-  {
-    return false;
-  }
-
-  char* json = OrthancPluginDicomBufferToJson(context_, reinterpret_cast<const char*>(dicom.data),
-                                              dicom.size, 
-                                              OrthancPluginDicomToJsonFormat_Short, 
-                                              static_cast<OrthancPluginDicomToJsonFlags>(0), 0);
-  OrthancPluginFreeMemoryBuffer(context_, &dicom);
-
-  return ConvertToJson(value, json);
-}
-                          
-
 static void ToLowerCase(std::string& s)
 {
   std::transform(s.begin(), s.end(), s.begin(), tolower);
@@ -124,59 +81,62 @@
                                 const char*                       remoteAet,
                                 const char*                       calledAet)
 {
-  namespace fs = boost::filesystem;  
-
-  Json::Value json;
-
-  if (!GetQueryDicom(json, query))
-  {
-    return OrthancPluginErrorCode_InternalError;
-  }
-
-  {
-    std::string msg = ("Received worklist query from remote modality " + 
-                       std::string(remoteAet) + ":\n" + json.toStyledString());
-    OrthancPluginLogInfo(context_, msg.c_str());
-  }
-
-  fs::path source(folder_);
-  fs::directory_iterator end;
-
   try
   {
-    for (fs::directory_iterator it(source); it != end; ++it)
+    namespace fs = boost::filesystem;  
+
     {
-      fs::file_type type(it->status().type());
+      Json::Value json;
+      GetQueryDicom(json, query);
 
-      if (type == fs::regular_file ||
-          type == fs::reparse_file)   // cf. BitBucket issue #11
+      std::string msg = ("Received worklist query from remote modality " + 
+                         std::string(remoteAet) + ":\n" + json.toStyledString());
+      OrthancPluginLogInfo(context_, msg.c_str());
+    }
+
+    fs::path source(folder_);
+    fs::directory_iterator end;
+
+    try
+    {
+      for (fs::directory_iterator it(source); it != end; ++it)
       {
-        std::string extension = fs::extension(it->path());
-        ToLowerCase(extension);
+        fs::file_type type(it->status().type());
 
-        if (extension == ".wl")
+        if (type == fs::regular_file ||
+            type == fs::reparse_file)   // cf. BitBucket issue #11
         {
-          OrthancPluginErrorCode error = MatchWorklist(answers, query, it->path().string());
-          if (error)
+          std::string extension = fs::extension(it->path());
+          ToLowerCase(extension);
+
+          if (extension == ".wl")
           {
-            OrthancPluginLogError(context_, "Error while adding an answer to a worklist request");
-            return error;
+            OrthancPluginErrorCode error = MatchWorklist(answers, query, it->path().string());
+            if (error)
+            {
+              OrthancPluginLogError(context_, "Error while adding an answer to a worklist request");
+              return error;
+            }
           }
         }
       }
     }
+    catch (fs::filesystem_error&)
+    {
+      std::string description = std::string("Inexistent folder while scanning for worklists: ") + source.string();
+      OrthancPluginLogError(context_, description.c_str());
+      return OrthancPluginErrorCode_DirectoryExpected;
+    }
+
+    // Uncomment the following line if too many answers are to be returned
+    // OrthancPluginMarkWorklistAnswersIncomplete(context_, answers);
+
+    return OrthancPluginErrorCode_Success;
   }
-  catch (fs::filesystem_error&)
+  catch (OrthancPlugins::PluginException& e)
   {
-    std::string description = std::string("Inexistent folder while scanning for worklists: ") + source.string();
-    OrthancPluginLogError(context_, description.c_str());
-    return OrthancPluginErrorCode_DirectoryExpected;
+    return e.GetErrorCode();
   }
-
-  // Uncomment the following line if too many answers are to be returned
-  // OrthancPluginMarkWorklistAnswersIncomplete(context_, answers);
-
-  return OrthancPluginErrorCode_Success;
 }
 
 
@@ -185,8 +145,6 @@
   ORTHANC_PLUGINS_API int32_t OrthancPluginInitialize(OrthancPluginContext* c)
   {
     context_ = c;
-    OrthancPluginLogWarning(context_, "Sample worklist plugin is initializing");
-    OrthancPluginSetDescription(context_, "Serve DICOM modality worklists from a folder with Orthanc.");
 
     /* Check the version of the Orthanc core */
     if (OrthancPluginCheckVersion(c) == 0)
@@ -201,55 +159,31 @@
       return -1;
     }
 
-    Json::Value configuration;
-    if (!ConvertToJson(configuration, OrthancPluginGetConfiguration(context_)))
-    {
-      OrthancPluginLogError(context_, "Cannot access the configuration of the worklist server");
-      return -1;
-    }
+    OrthancPlugins::LogWarning(context_, "Sample worklist plugin is initializing");
+    OrthancPluginSetDescription(context_, "Serve DICOM modality worklists from a folder with Orthanc.");
 
-    bool enabled = false;
+    OrthancPlugins::OrthancConfiguration configuration(context_);
+
+    OrthancPlugins::OrthancConfiguration worklists;
+    configuration.GetSection(worklists, "Worklists");
 
-    if (configuration.isMember("Worklists"))
+    bool enabled = worklists.GetBooleanValue("Enable", false);
+    if (enabled)
     {
-      const Json::Value& config = configuration["Worklists"];
-      if (!config.isMember("Enable") ||
-          config["Enable"].type() != Json::booleanValue)
+      if (worklists.LookupStringValue(folder_, "Database"))
       {
-        OrthancPluginLogError(context_, "The configuration option \"Worklists.Enable\" must contain a Boolean");
-        return -1;
+        OrthancPlugins::LogWarning(context_, "The database of worklists will be read from folder: " + folder_);
+        OrthancPluginRegisterWorklistCallback(context_, Callback);
       }
       else
       {
-        enabled = config["Enable"].asBool();
-        if (enabled)
-        {
-          if (!config.isMember("Database") ||
-              config["Database"].type() != Json::stringValue)
-          {
-            OrthancPluginLogError(context_, "The configuration option \"Worklists.Database\" must contain a path");
-            return -1;
-          }
-
-          folder_ = config["Database"].asString();
-        }
-        else
-        {
-          OrthancPluginLogWarning(context_, "Worklists server is disabled by the configuration file");
-        }
+        OrthancPlugins::LogError(context_, "The configuration option \"Worklists.Database\" must contain a path");
+        return -1;
       }
     }
     else
     {
-      OrthancPluginLogWarning(context_, "Worklists server is disabled, no suitable configuration section was provided");
-    }
-
-    if (enabled)
-    {
-      std::string message = "The database of worklists will be read from folder: " + folder_;
-      OrthancPluginLogWarning(context_, message.c_str());
-
-      OrthancPluginRegisterWorklistCallback(context_, Callback);
+      OrthancPlugins::LogWarning(context_, "Worklists server is disabled by the configuration file");
     }
 
     return 0;
--- a/Plugins/Samples/ServeFolders/Plugin.cpp	Thu Dec 08 14:46:13 2016 +0100
+++ b/Plugins/Samples/ServeFolders/Plugin.cpp	Thu Dec 08 16:16:25 2016 +0100
@@ -121,7 +121,9 @@
 {
   if (generateETag_)
   {
-    OrthancPlugins::OrthancString md5(context_, OrthancPluginComputeMd5(context_, content, size));
+    OrthancPlugins::OrthancString md5(context_);
+    md5.Assign(OrthancPluginComputeMd5(context_, content, size));
+
     std::string etag = "\"" + std::string(md5.GetContent()) + "\"";
     OrthancPluginSetHttpHeader(context_, output, "ETag", etag.c_str());
   }