changeset 2206:27106f7e3759

more refactoring of the worklist sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 08 Dec 2016 16:29:34 +0100
parents 395522e46b2b
children 6dc3bdb4088b
files Plugins/Samples/Common/OrthancPluginCppWrapper.cpp Plugins/Samples/Common/OrthancPluginCppWrapper.h Plugins/Samples/ModalityWorklists/Plugin.cpp
diffstat 3 files changed, 35 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Thu Dec 08 16:16:25 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Thu Dec 08 16:29:34 2016 +0100
@@ -337,13 +337,14 @@
   }
 
   
-  void OrthancString::DicomToJson(const OrthancPlugins::MemoryBuffer& dicom,
-                                  OrthancPluginDicomToJsonFormat format,
-                                  OrthancPluginDicomToJsonFlags flags,
-                                  uint32_t maxStringLength)
+  void MemoryBuffer::DicomToJson(Json::Value& target,
+                                 OrthancPluginDicomToJsonFormat format,
+                                 OrthancPluginDicomToJsonFlags flags,
+                                 uint32_t maxStringLength)
   {
-    Assign(OrthancPluginDicomBufferToJson(context_, dicom.GetData(), dicom.GetSize(), 
-                                          format, flags, maxStringLength));
+    OrthancString str(context_);
+    str.Assign(OrthancPluginDicomBufferToJson(context_, GetData(), GetSize(), format, flags, maxStringLength));
+    str.ToJson(target);
   }
 
   
--- a/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Thu Dec 08 16:16:25 2016 +0100
+++ b/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Thu Dec 08 16:29:34 2016 +0100
@@ -174,6 +174,11 @@
     void ReadFile(const std::string& path);
 
     void GetDicomQuery(const OrthancPluginWorklistQuery* query);
+
+    void DicomToJson(Json::Value& target,
+                     OrthancPluginDicomToJsonFormat format,
+                     OrthancPluginDicomToJsonFlags flags,
+                     uint32_t maxStringLength);
   };
 
 
@@ -197,6 +202,8 @@
       Clear();
     }
 
+    // This transfers ownership, warning: The string must have been
+    // allocated by the Orthanc core
     void Assign(char* str);
 
     const char* GetContent() const
@@ -207,11 +214,6 @@
     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/ModalityWorklists/Plugin.cpp	Thu Dec 08 16:16:25 2016 +0100
+++ b/Plugins/Samples/ModalityWorklists/Plugin.cpp	Thu Dec 08 16:29:34 2016 +0100
@@ -34,9 +34,9 @@
 /**
  * This is the main function for matching a DICOM worklist against a query.
  **/
-static OrthancPluginErrorCode  MatchWorklist(OrthancPluginWorklistAnswers*     answers,
-                                             const OrthancPluginWorklistQuery* query,
-                                             const std::string& path)
+static void  MatchWorklist(OrthancPluginWorklistAnswers*     answers,
+                           const OrthancPluginWorklistQuery* query,
+                           const std::string& path)
 {
   OrthancPlugins::MemoryBuffer dicom(context_);
   dicom.ReadFile(path);
@@ -44,35 +44,15 @@
   if (OrthancPluginWorklistIsMatch(context_, query, dicom.GetData(), dicom.GetSize()))
   {
     // This DICOM file matches the worklist query, add it to the answers
-    return OrthancPluginWorklistAddAnswer
+    OrthancPluginErrorCode code = OrthancPluginWorklistAddAnswer
       (context_, answers, query, dicom.GetData(), dicom.GetSize());
-  }
-  else
-  {
-    // This DICOM file does not match
-    return OrthancPluginErrorCode_Success;
-  }
-}
-
-
 
-static void GetQueryDicom(Json::Value& value,
-                          const OrthancPluginWorklistQuery* query)
-{
-  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 void ToLowerCase(std::string& s)
-{
-  std::transform(s.begin(), s.end(), s.begin(), tolower);
+    if (code != OrthancPluginErrorCode_Success)
+    {
+      OrthancPlugins::LogError(context_, "Error while adding an answer to a worklist request");
+      ORTHANC_PLUGINS_THROW_EXCEPTION(code);
+    }
+  }
 }
 
 
@@ -86,12 +66,15 @@
     namespace fs = boost::filesystem;  
 
     {
-      Json::Value json;
-      GetQueryDicom(json, query);
+      OrthancPlugins::MemoryBuffer dicom(context_);
+      dicom.GetDicomQuery(query);
 
-      std::string msg = ("Received worklist query from remote modality " + 
-                         std::string(remoteAet) + ":\n" + json.toStyledString());
-      OrthancPluginLogInfo(context_, msg.c_str());
+      Json::Value json;
+      dicom.DicomToJson(json, OrthancPluginDicomToJsonFormat_Short, 
+                        static_cast<OrthancPluginDicomToJsonFlags>(0), 0);
+
+      OrthancPlugins::LogInfo(context_, "Received worklist query from remote modality " + 
+                              std::string(remoteAet) + ":\n" + json.toStyledString());
     }
 
     fs::path source(folder_);
@@ -107,24 +90,18 @@
             type == fs::reparse_file)   // cf. BitBucket issue #11
         {
           std::string extension = fs::extension(it->path());
-          ToLowerCase(extension);
+          std::transform(extension.begin(), extension.end(), extension.begin(), tolower);  // Convert to lowercase
 
           if (extension == ".wl")
           {
-            OrthancPluginErrorCode error = MatchWorklist(answers, query, it->path().string());
-            if (error)
-            {
-              OrthancPluginLogError(context_, "Error while adding an answer to a worklist request");
-              return error;
-            }
+            MatchWorklist(answers, query, it->path().string());
           }
         }
       }
     }
     catch (fs::filesystem_error&)
     {
-      std::string description = std::string("Inexistent folder while scanning for worklists: ") + source.string();
-      OrthancPluginLogError(context_, description.c_str());
+      OrthancPlugins::LogError(context_, "Inexistent folder while scanning for worklists: " + source.string());
       return OrthancPluginErrorCode_DirectoryExpected;
     }