changeset 2121:7e8889bc95c6

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 07 Nov 2016 11:28:12 +0100
parents 4b02ec79728a
children 2ecc95a239f7
files OrthancServer/Search/LookupIdentifierQuery.cpp OrthancServer/Search/LookupIdentifierQuery.h OrthancServer/Search/LookupResource.cpp OrthancServer/ServerToolbox.cpp OrthancServer/ServerToolbox.h UnitTestsSources/ServerIndexTests.cpp
diffstat 6 files changed, 152 insertions(+), 153 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Search/LookupIdentifierQuery.cpp	Mon Nov 07 11:19:19 2016 +0100
+++ b/OrthancServer/Search/LookupIdentifierQuery.cpp	Mon Nov 07 11:28:12 2016 +0100
@@ -43,67 +43,6 @@
 
 namespace Orthanc
 {
-  static const DicomTag patientIdentifiers[] = 
-  {
-    DICOM_TAG_PATIENT_ID,
-    DICOM_TAG_PATIENT_NAME,
-    DICOM_TAG_PATIENT_BIRTH_DATE
-  };
-
-  static const DicomTag studyIdentifiers[] = 
-  {
-    DICOM_TAG_PATIENT_ID,
-    DICOM_TAG_PATIENT_NAME,
-    DICOM_TAG_PATIENT_BIRTH_DATE,
-    DICOM_TAG_STUDY_INSTANCE_UID,
-    DICOM_TAG_ACCESSION_NUMBER,
-    DICOM_TAG_STUDY_DESCRIPTION,
-    DICOM_TAG_STUDY_DATE
-  };
-
-  static const DicomTag seriesIdentifiers[] = 
-  {
-    DICOM_TAG_SERIES_INSTANCE_UID
-  };
-
-  static const DicomTag instanceIdentifiers[] = 
-  {
-    DICOM_TAG_SOP_INSTANCE_UID
-  };
-
-
-  void LookupIdentifierQuery::LoadIdentifiers(const DicomTag*& tags,
-                                              size_t& size,
-                                              ResourceType level)
-  {
-    switch (level)
-    {
-      case ResourceType_Patient:
-        tags = patientIdentifiers;
-        size = sizeof(patientIdentifiers) / sizeof(DicomTag);
-        break;
-
-      case ResourceType_Study:
-        tags = studyIdentifiers;
-        size = sizeof(studyIdentifiers) / sizeof(DicomTag);
-        break;
-
-      case ResourceType_Series:
-        tags = seriesIdentifiers;
-        size = sizeof(seriesIdentifiers) / sizeof(DicomTag);
-        break;
-
-      case ResourceType_Instance:
-        tags = instanceIdentifiers;
-        size = sizeof(instanceIdentifiers) / sizeof(DicomTag);
-        break;
-
-      default:
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-  }
-
-
   LookupIdentifierQuery::Disjunction::~Disjunction()
   {
     for (size_t i = 0; i < disjunction_.size(); i++)
@@ -131,27 +70,6 @@
   }
 
 
-
-  bool LookupIdentifierQuery::IsIdentifier(const DicomTag& tag,
-                                           ResourceType level)
-  {
-    const DicomTag* tags;
-    size_t size;
-
-    LoadIdentifiers(tags, size, level);
-
-    for (size_t i = 0; i < size; i++)
-    {
-      if (tag == tags[i])
-      {
-        return true;
-      }
-    }
-
-    return false;
-  }
-
-
   void LookupIdentifierQuery::AddConstraint(DicomTag tag,
                                             IdentifierConstraintType type,
                                             const std::string& value)
@@ -169,56 +87,6 @@
   }
 
 
-  std::string LookupIdentifierQuery::NormalizeIdentifier(const std::string& value)
-  {
-    std::string t;
-    t.reserve(value.size());
-
-    for (size_t i = 0; i < value.size(); i++)
-    {
-      if (value[i] == '%' ||
-          value[i] == '_')
-      {
-        t.push_back(' ');  // These characters might break wildcard queries in SQL
-      }
-      else if (isascii(value[i]) &&
-               !iscntrl(value[i]) &&
-               (!isspace(value[i]) || value[i] == ' '))
-      {
-        t.push_back(value[i]);
-      }
-    }
-
-    Toolbox::ToUpperCase(t);
-
-    return Toolbox::StripSpaces(t);
-  }
-
-
-  void LookupIdentifierQuery::StoreIdentifiers(IDatabaseWrapper& database,
-                                               int64_t resource,
-                                               ResourceType level,
-                                               const DicomMap& map)
-  {
-    const DicomTag* tags;
-    size_t size;
-
-    LoadIdentifiers(tags, size, level);
-
-    for (size_t i = 0; i < size; i++)
-    {
-      const DicomValue* value = map.TestAndGetValue(tags[i]);
-      if (value != NULL &&
-          !value->IsNull() &&
-          !value->IsBinary())
-      {
-        std::string s = NormalizeIdentifier(value->GetContent());
-        database.SetIdentifierTag(resource, tags[i], s);
-      }
-    }
-  }
-
-
   void LookupIdentifierQuery::Apply(std::list<std::string>& result,
                                     IDatabaseWrapper& database)
   {
--- a/OrthancServer/Search/LookupIdentifierQuery.h	Mon Nov 07 11:19:19 2016 +0100
+++ b/OrthancServer/Search/LookupIdentifierQuery.h	Mon Nov 07 11:28:12 2016 +0100
@@ -32,7 +32,7 @@
 
 #pragma once
 
-#include "../ServerEnumerations.h"
+#include "../ServerToolbox.h"
 #include "../IDatabaseWrapper.h"
 
 #include "SetOfResources.h"
@@ -78,7 +78,7 @@
                  const std::string& value) : 
         tag_(tag),
         type_(type),
-        value_(NormalizeIdentifier(value))
+        value_(ServerToolbox::NormalizeIdentifier(value))
       {
       }
 
@@ -138,7 +138,7 @@
 
     bool IsIdentifier(const DicomTag& tag)
     {
-      return IsIdentifier(tag, level_);
+      return ServerToolbox::IsIdentifier(tag, level_);
     }
 
     void AddConstraint(DicomTag tag,
@@ -164,20 +164,6 @@
     void Apply(SetOfResources& result,
                IDatabaseWrapper& database);
 
-    static void LoadIdentifiers(const DicomTag*& tags,
-                                size_t& size,
-                                ResourceType level);
-
-    static bool IsIdentifier(const DicomTag& tag,
-                             ResourceType level);
-
-    static void StoreIdentifiers(IDatabaseWrapper& database,
-                                 int64_t resource,
-                                 ResourceType level,
-                                 const DicomMap& map);
-
-    static std::string NormalizeIdentifier(const std::string& value);
-
     void Print(std::ostream& s) const;
   };
 }
--- a/OrthancServer/Search/LookupResource.cpp	Mon Nov 07 11:19:19 2016 +0100
+++ b/OrthancServer/Search/LookupResource.cpp	Mon Nov 07 11:28:12 2016 +0100
@@ -46,7 +46,7 @@
     const DicomTag* tags = NULL;
     size_t size;
     
-    LookupIdentifierQuery::LoadIdentifiers(tags, size, level);
+    ServerToolbox::LoadIdentifiers(tags, size, level);
     
     for (size_t i = 0; i < size; i++)
     {
--- a/OrthancServer/ServerToolbox.cpp	Mon Nov 07 11:19:19 2016 +0100
+++ b/OrthancServer/ServerToolbox.cpp	Mon Nov 07 11:28:12 2016 +0100
@@ -46,6 +46,35 @@
 {
   namespace ServerToolbox
   {
+    static const DicomTag patientIdentifiers[] = 
+    {
+      DICOM_TAG_PATIENT_ID,
+      DICOM_TAG_PATIENT_NAME,
+      DICOM_TAG_PATIENT_BIRTH_DATE
+    };
+
+    static const DicomTag studyIdentifiers[] = 
+    {
+      DICOM_TAG_PATIENT_ID,
+      DICOM_TAG_PATIENT_NAME,
+      DICOM_TAG_PATIENT_BIRTH_DATE,
+      DICOM_TAG_STUDY_INSTANCE_UID,
+      DICOM_TAG_ACCESSION_NUMBER,
+      DICOM_TAG_STUDY_DESCRIPTION,
+      DICOM_TAG_STUDY_DATE
+    };
+
+    static const DicomTag seriesIdentifiers[] = 
+    {
+      DICOM_TAG_SERIES_INSTANCE_UID
+    };
+
+    static const DicomTag instanceIdentifiers[] = 
+    {
+      DICOM_TAG_SOP_INSTANCE_UID
+    };
+
+
     void SimplifyTags(Json::Value& target,
                       const Json::Value& source,
                       DicomToJsonFormat format)
@@ -216,7 +245,7 @@
     {
       // WARNING: The database should be locked with a transaction!
 
-      LookupIdentifierQuery::StoreIdentifiers(database, resource, level, dicomSummary);
+      StoreIdentifiers(database, resource, level, dicomSummary);
 
       DicomMap tags;
 
@@ -360,5 +389,107 @@
         }
       }
     }
+
+
+    void LoadIdentifiers(const DicomTag*& tags,
+                         size_t& size,
+                         ResourceType level)
+    {
+      switch (level)
+      {
+        case ResourceType_Patient:
+          tags = patientIdentifiers;
+          size = sizeof(patientIdentifiers) / sizeof(DicomTag);
+          break;
+
+        case ResourceType_Study:
+          tags = studyIdentifiers;
+          size = sizeof(studyIdentifiers) / sizeof(DicomTag);
+          break;
+
+        case ResourceType_Series:
+          tags = seriesIdentifiers;
+          size = sizeof(seriesIdentifiers) / sizeof(DicomTag);
+          break;
+
+        case ResourceType_Instance:
+          tags = instanceIdentifiers;
+          size = sizeof(instanceIdentifiers) / sizeof(DicomTag);
+          break;
+
+        default:
+          throw OrthancException(ErrorCode_ParameterOutOfRange);
+      }
+    }
+
+
+    std::string NormalizeIdentifier(const std::string& value)
+    {
+      std::string t;
+      t.reserve(value.size());
+
+      for (size_t i = 0; i < value.size(); i++)
+      {
+        if (value[i] == '%' ||
+            value[i] == '_')
+        {
+          t.push_back(' ');  // These characters might break wildcard queries in SQL
+        }
+        else if (isascii(value[i]) &&
+                 !iscntrl(value[i]) &&
+                 (!isspace(value[i]) || value[i] == ' '))
+        {
+          t.push_back(value[i]);
+        }
+      }
+
+      Toolbox::ToUpperCase(t);
+
+      return Toolbox::StripSpaces(t);
+    }
+
+
+    bool IsIdentifier(const DicomTag& tag,
+                      ResourceType level)
+    {
+      const DicomTag* tags;
+      size_t size;
+
+      LoadIdentifiers(tags, size, level);
+
+      for (size_t i = 0; i < size; i++)
+      {
+        if (tag == tags[i])
+        {
+          return true;
+        }
+      }
+
+      return false;
+    }
+
+
+    void StoreIdentifiers(IDatabaseWrapper& database,
+                          int64_t resource,
+                          ResourceType level,
+                          const DicomMap& map)
+    {
+      const DicomTag* tags;
+      size_t size;
+
+      LoadIdentifiers(tags, size, level);
+
+      for (size_t i = 0; i < size; i++)
+      {
+        const DicomValue* value = map.TestAndGetValue(tags[i]);
+        if (value != NULL &&
+            !value->IsNull() &&
+            !value->IsBinary())
+        {
+          std::string s = NormalizeIdentifier(value->GetContent());
+          database.SetIdentifierTag(resource, tags[i], s);
+        }
+      }
+    }
   }
 }
--- a/OrthancServer/ServerToolbox.h	Mon Nov 07 11:19:19 2016 +0100
+++ b/OrthancServer/ServerToolbox.h	Mon Nov 07 11:28:12 2016 +0100
@@ -60,5 +60,19 @@
     void ReconstructMainDicomTags(IDatabaseWrapper& database,
                                   IStorageArea& storageArea,
                                   ResourceType level);
+
+    void LoadIdentifiers(const DicomTag*& tags,
+                         size_t& size,
+                         ResourceType level);
+
+    bool IsIdentifier(const DicomTag& tag,
+                      ResourceType level);
+
+    void StoreIdentifiers(IDatabaseWrapper& database,
+                          int64_t resource,
+                          ResourceType level,
+                          const DicomMap& map);
+
+    std::string NormalizeIdentifier(const std::string& value);
   }
 }
--- a/UnitTestsSources/ServerIndexTests.cpp	Mon Nov 07 11:19:19 2016 +0100
+++ b/UnitTestsSources/ServerIndexTests.cpp	Mon Nov 07 11:28:12 2016 +0100
@@ -833,6 +833,6 @@
 
 TEST(LookupIdentifierQuery, NormalizeIdentifier)
 {
-  ASSERT_EQ("H^L.LO", LookupIdentifierQuery::NormalizeIdentifier("   Hé^l.LO  %_  "));
-  ASSERT_EQ("1.2.840.113619.2.176.2025", LookupIdentifierQuery::NormalizeIdentifier("   1.2.840.113619.2.176.2025  "));
+  ASSERT_EQ("H^L.LO", ServerToolbox::NormalizeIdentifier("   Hé^l.LO  %_  "));
+  ASSERT_EQ("1.2.840.113619.2.176.2025", ServerToolbox::NormalizeIdentifier("   1.2.840.113619.2.176.2025  "));
 }