changeset 3073:0e9d1731b1b0 db-changes

refactoring to reuse DatabaseConstraint in separate projects
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 02 Jan 2019 13:44:18 +0100
parents 1b05fd072c57
children 495c5edce708
files OrthancServer/Search/DatabaseConstraint.cpp OrthancServer/Search/DatabaseConstraint.h OrthancServer/Search/DicomTagConstraint.cpp OrthancServer/Search/DicomTagConstraint.h OrthancServer/ServerIndex.cpp UnitTestsSources/ServerIndexTests.cpp
diffstat 6 files changed, 81 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Search/DatabaseConstraint.cpp	Wed Jan 02 11:47:58 2019 +0100
+++ b/OrthancServer/Search/DatabaseConstraint.cpp	Wed Jan 02 13:44:18 2019 +0100
@@ -35,52 +35,32 @@
 #include "DatabaseConstraint.h"
 
 #include "../../Core/OrthancException.h"
-#include "../ServerToolbox.h"
 
 namespace Orthanc
 {
-  DatabaseConstraint::DatabaseConstraint(const DicomTagConstraint& constraint,
-                                         ResourceType level,
-                                         DicomTagType tagType) :
+  DatabaseConstraint::DatabaseConstraint(ResourceType level,
+                                         const DicomTag& tag,
+                                         bool isIdentifier,
+                                         ConstraintType type,
+                                         const std::vector<std::string>& values,
+                                         bool caseSensitive,
+                                         bool mandatory) :
     level_(level),
-    tag_(constraint.GetTag()),
-    constraintType_(constraint.GetConstraintType()),
-    mandatory_(constraint.IsMandatory())
+    tag_(tag),
+    isIdentifier_(isIdentifier),
+    constraintType_(type),
+    values_(values),
+    caseSensitive_(caseSensitive),
+    mandatory_(mandatory)
   {
-    switch (tagType)
+    if (type != ConstraintType_List &&
+        values_.size() != 1)
     {
-      case DicomTagType_Identifier:
-        isIdentifier_ = true;
-        caseSensitive_ = true;
-        break;
-
-      case DicomTagType_Main:
-        isIdentifier_ = false;
-        caseSensitive_ = constraint.IsCaseSensitive();
-        break;
+      throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+  }      
 
-      default:
-        throw OrthancException(ErrorCode_InternalError);
-    }
-
-    values_.reserve(constraint.GetValues().size());
-      
-    for (std::set<std::string>::const_iterator
-           it = constraint.GetValues().begin();
-         it != constraint.GetValues().end(); ++it)
-    {
-      if (isIdentifier_)
-      {
-        values_.push_back(ServerToolbox::NormalizeIdentifier(*it));
-      }
-      else
-      {
-        values_.push_back(*it);
-      }
-    }
-  }
-
-  
+    
   const std::string& DatabaseConstraint::GetValue(size_t index) const
   {
     if (index >= values_.size())
--- a/OrthancServer/Search/DatabaseConstraint.h	Wed Jan 02 11:47:58 2019 +0100
+++ b/OrthancServer/Search/DatabaseConstraint.h	Wed Jan 02 13:44:18 2019 +0100
@@ -33,10 +33,12 @@
 
 #pragma once
 
-#include "DicomTagConstraint.h"
+#include "../../Core/DicomFormat/DicomMap.h"
+#include "../ServerEnumerations.h"
 
 namespace Orthanc
 {
+  // This class is also used by the "orthanc-databases" project
   class DatabaseConstraint
   {
   private:
@@ -49,10 +51,14 @@
     bool                      mandatory_;
 
   public:
-    DatabaseConstraint(const DicomTagConstraint& constraint,
-                       ResourceType level,
-                       DicomTagType tagType);
-
+    DatabaseConstraint(ResourceType level,
+                       const DicomTag& tag,
+                       bool isIdentifier,
+                       ConstraintType type,
+                       const std::vector<std::string>& values,
+                       bool caseSensitive,
+                       bool mandatory);
+    
     ResourceType GetLevel() const
     {
       return level_;
--- a/OrthancServer/Search/DicomTagConstraint.cpp	Wed Jan 02 11:47:58 2019 +0100
+++ b/OrthancServer/Search/DicomTagConstraint.cpp	Wed Jan 02 13:44:18 2019 +0100
@@ -339,4 +339,46 @@
         throw OrthancException(ErrorCode_InternalError);
     }
   }
+
+
+  DatabaseConstraint DicomTagConstraint::ConvertToDatabaseConstraint(ResourceType level,
+                                                                     DicomTagType tagType) const
+  {
+    bool isIdentifier, caseSensitive;
+    
+    switch (tagType)
+    {
+      case DicomTagType_Identifier:
+        isIdentifier = true;
+        caseSensitive = true;
+        break;
+
+      case DicomTagType_Main:
+        isIdentifier = false;
+        caseSensitive = IsCaseSensitive();
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
+    }
+
+    std::vector<std::string> values;
+    values.reserve(values_.size());
+      
+    for (std::set<std::string>::const_iterator
+           it = values_.begin(); it != values_.end(); ++it)
+    {
+      if (isIdentifier)
+      {
+        values.push_back(ServerToolbox::NormalizeIdentifier(*it));
+      }
+      else
+      {
+        values.push_back(*it);
+      }
+    }
+
+    return DatabaseConstraint(level, tag_, isIdentifier, constraintType_,
+                              values, caseSensitive, mandatory_);
+  }  
 }
--- a/OrthancServer/Search/DicomTagConstraint.h	Wed Jan 02 11:47:58 2019 +0100
+++ b/OrthancServer/Search/DicomTagConstraint.h	Wed Jan 02 13:44:18 2019 +0100
@@ -35,13 +35,12 @@
 
 #include "../ServerEnumerations.h"
 #include "../../Core/DicomFormat/DicomMap.h"
+#include "DatabaseConstraint.h"
 
 #include <boost/shared_ptr.hpp>
 
 namespace Orthanc
 {
-  class DatabaseConstraint;
-  
   class DicomTagConstraint : public boost::noncopyable
   {
   private:
@@ -112,5 +111,8 @@
     bool IsMatch(const DicomMap& value);
 
     std::string Format() const;
+
+    DatabaseConstraint ConvertToDatabaseConstraint(ResourceType level,
+                                                   DicomTagType tagType) const;
   };
 }
--- a/OrthancServer/ServerIndex.cpp	Wed Jan 02 11:47:58 2019 +0100
+++ b/OrthancServer/ServerIndex.cpp	Wed Jan 02 13:44:18 2019 +0100
@@ -2139,7 +2139,7 @@
     DicomTagConstraint c(tag, ConstraintType_Equal, value, true, true);
 
     std::vector<DatabaseConstraint> query;
-    query.push_back(DatabaseConstraint(c, level, DicomTagType_Identifier));
+    query.push_back(c.ConvertToDatabaseConstraint(level, DicomTagType_Identifier));
 
     {
       boost::mutex::scoped_lock lock(mutex_);
@@ -2555,8 +2555,7 @@
           level = ResourceType_Study;
         }
         
-        DatabaseConstraint c(source.GetConstraint(i), level, type);
-        target.push_back(c);
+        target.push_back(source.GetConstraint(i).ConvertToDatabaseConstraint(level, type));
       }
     }
   }
--- a/UnitTestsSources/ServerIndexTests.cpp	Wed Jan 02 11:47:58 2019 +0100
+++ b/UnitTestsSources/ServerIndexTests.cpp	Wed Jan 02 13:44:18 2019 +0100
@@ -260,7 +260,7 @@
       DicomTagConstraint c(tag, type, value, true, true);
       
       std::vector<DatabaseConstraint> lookup;
-      lookup.push_back(DatabaseConstraint(c, level, DicomTagType_Identifier));
+      lookup.push_back(c.ConvertToDatabaseConstraint(level, DicomTagType_Identifier));
       
       index_->ApplyLookupResources(result, NULL, lookup, level, 0 /* no limit */);
     }
@@ -280,8 +280,8 @@
       DicomTagConstraint c2(tag, type2, value2, true, true);
       
       std::vector<DatabaseConstraint> lookup;
-      lookup.push_back(DatabaseConstraint(c1, level, DicomTagType_Identifier));
-      lookup.push_back(DatabaseConstraint(c2, level, DicomTagType_Identifier));
+      lookup.push_back(c1.ConvertToDatabaseConstraint(level, DicomTagType_Identifier));
+      lookup.push_back(c2.ConvertToDatabaseConstraint(level, DicomTagType_Identifier));
       
       index_->ApplyLookupResources(result, NULL, lookup, level, 0 /* no limit */);
     }