changeset 5244:72dfa0ac84eb db-protobuf

lookup for labels in orthanc explorer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 06 Apr 2023 16:55:55 +0200
parents f00811d14348
children b36f82260f41
files OrthancServer/OrthancExplorer/explorer.html OrthancServer/OrthancExplorer/explorer.js OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp OrthancServer/Sources/Search/DatabaseLookup.cpp OrthancServer/Sources/ServerToolbox.cpp OrthancServer/Sources/ServerToolbox.h OrthancServer/UnitTestsSources/ServerIndexTests.cpp
diffstat 7 files changed, 84 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/OrthancExplorer/explorer.html	Thu Apr 06 16:04:23 2023 +0200
+++ b/OrthancServer/OrthancExplorer/explorer.html	Thu Apr 06 16:55:55 2023 +0200
@@ -81,6 +81,11 @@
           <input type="text" name="lookup-study-description" id="lookup-study-description" value="" />
         </div>
 
+        <div data-role="fieldcontain" id="lookup-study-labels-div">
+          <label for="lookup-study-labels">Labels:</label>
+          <input type="text" name="lookup-study-description" id="lookup-study-labels" value="" />
+        </div>
+
         <div data-role="fieldcontain">
           <label for="lookup-study-date">Study Date:</label>
           <select name="lookup-study-date" id="lookup-study-date">
--- a/OrthancServer/OrthancExplorer/explorer.js	Thu Apr 06 16:04:23 2023 +0200
+++ b/OrthancServer/OrthancExplorer/explorer.js	Thu Apr 06 16:55:55 2023 +0200
@@ -485,6 +485,14 @@
       } else {
         $('.warning-insecure').hide();
       }
+
+      // New in Orthanc 1.12.0
+      if ('HasLabels' in s &&
+          s.HasLabels) {
+        $('#lookup-study-labels-div').show();
+      } else {
+        $('#lookup-study-labels-div').hide();
+      }
     }
   });
 });
@@ -556,6 +564,10 @@
       else if (input.id == 'lookup-study-date-specific') {
         // Ignore
       }
+      else if (input.id == 'lookup-study-labels') {
+        // New in Orthanc 1.12.0
+        lookup['WithLabels'] = input.value.split(' ');
+      }
       else {
         console.error('Unknown lookup field: ' + input.id);
       }
--- a/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Thu Apr 06 16:04:23 2023 +0200
+++ b/OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp	Thu Apr 06 16:55:55 2023 +0200
@@ -1918,6 +1918,15 @@
   }
 
 
+  static void CheckValidLabels(const std::set<std::string>& labels)
+  {
+    for (std::set<std::string>::const_iterator it = labels.begin(); it != labels.end(); ++it)
+    {
+      ServerToolbox::CheckValidLabel(*it);
+    }
+  }
+  
+
   void StatelessDatabaseOperations::ApplyLookupResources(std::vector<std::string>& resourcesId,
                                                          std::vector<std::string>* instancesId,
                                                          const DatabaseLookup& lookup,
@@ -1967,6 +1976,9 @@
       throw OrthancException(ErrorCode_NotImplemented, "The database backend doesn't support labels");
     }
 
+    CheckValidLabels(withLabels);
+    CheckValidLabels(withoutLabels);
+
     std::vector<DatabaseConstraint> normalized;
     NormalizeLookup(normalized, lookup, queryLevel);
 
@@ -3627,10 +3639,7 @@
       }
     };
 
-    if (!Toolbox::IsAsciiString(label))
-    {
-      throw OrthancException(ErrorCode_ParameterOutOfRange, "A label must only contain ASCII characters");
-    }
+    ServerToolbox::CheckValidLabel(label);
     
     Operations operations(publicId, level, label, operation);
     Apply(operations);
--- a/OrthancServer/Sources/Search/DatabaseLookup.cpp	Thu Apr 06 16:04:23 2023 +0200
+++ b/OrthancServer/Sources/Search/DatabaseLookup.cpp	Thu Apr 06 16:55:55 2023 +0200
@@ -372,12 +372,9 @@
 
   void DatabaseLookup::AddWithLabel(const std::string& label)
   {
-    if (label.empty())
+    if (!label.empty())
     {
-      throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-    else
-    {
+      ServerToolbox::CheckValidLabel(label);
       withLabels_.insert(label);
     }
   }
@@ -385,12 +382,9 @@
 
   void DatabaseLookup::AddWithoutLabel(const std::string& label)
   {
-    if (label.empty())
+    if (!label.empty())
     {
-      throw OrthancException(ErrorCode_ParameterOutOfRange);
-    }
-    else
-    {
+      ServerToolbox::CheckValidLabel(label);
       withoutLabels_.insert(label);
     }
   }
--- a/OrthancServer/Sources/ServerToolbox.cpp	Thu Apr 06 16:04:23 2023 +0200
+++ b/OrthancServer/Sources/ServerToolbox.cpp	Thu Apr 06 16:55:55 2023 +0200
@@ -287,5 +287,40 @@
         }
       }
     }
+
+    
+    bool IsValidLabel(const std::string& label)
+    {
+      if (label.empty())
+      {
+        return false;
+      }
+      
+      for (size_t i = 0; i < label.size(); i++)
+      {
+        if (!(label[i] == '.' ||
+              label[i] == '_' ||
+              label[i] == '-' ||
+              (label[i] >= 'a' && label[i] <= 'z') ||
+              (label[i] >= 'A' && label[i] <= 'Z') ||
+              (label[i] >= '0' && label[i] <= '9')))
+        {
+          return false;
+        }
+      }
+
+      return true;
+    }
+
+
+    void CheckValidLabel(const std::string& label)
+    {
+      if (!IsValidLabel(label))
+      {
+        throw OrthancException(ErrorCode_ParameterOutOfRange,
+                               "A label must be a non-empty, alphanumeric string, "
+                               "possibly with '.', '_', or '-' characters, but got: " + label);
+      }
+    }
   }
 }
--- a/OrthancServer/Sources/ServerToolbox.h	Thu Apr 06 16:04:23 2023 +0200
+++ b/OrthancServer/Sources/ServerToolbox.h	Thu Apr 06 16:55:55 2023 +0200
@@ -56,5 +56,9 @@
     void ReconstructResource(ServerContext& context,
                              const std::string& resource,
                              bool reconstructFiles);
+
+    bool IsValidLabel(const std::string& label);
+
+    void CheckValidLabel(const std::string& label);
   }
 }
--- a/OrthancServer/UnitTestsSources/ServerIndexTests.cpp	Thu Apr 06 16:04:23 2023 +0200
+++ b/OrthancServer/UnitTestsSources/ServerIndexTests.cpp	Thu Apr 06 16:55:55 2023 +0200
@@ -1040,3 +1040,14 @@
     }
   }
 }
+
+
+TEST(ServerToolbox, ValidLabels)
+{
+  ASSERT_TRUE(ServerToolbox::IsValidLabel("abcdefghijklmnopqrstuvwxyz"
+                                          "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+                                          "0123456789-_."));
+  ASSERT_FALSE(ServerToolbox::IsValidLabel(""));
+  ASSERT_FALSE(ServerToolbox::IsValidLabel(" "));
+  ASSERT_FALSE(ServerToolbox::IsValidLabel("&"));
+}