changeset 1760:51db4a25a741 db-changes

LookupIdentifier is plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 29 Oct 2015 11:09:00 +0100
parents e268412adcf1
children f4286d99ee0a
files OrthancServer/DatabaseWrapperBase.cpp Plugins/Engine/OrthancPluginDatabase.cpp Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/PluginsEnumerations.cpp Plugins/Engine/PluginsEnumerations.h Plugins/Include/orthanc/OrthancCDatabasePlugin.h Plugins/Include/orthanc/OrthancCPlugin.h Plugins/Include/orthanc/OrthancCppDatabasePlugin.h
diffstat 8 files changed, 128 insertions(+), 118 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapperBase.cpp	Wed Oct 28 12:59:18 2015 +0100
+++ b/OrthancServer/DatabaseWrapperBase.cpp	Thu Oct 29 11:09:00 2015 +0100
@@ -681,41 +681,6 @@
 
 
 
-  /**
-
-     TODO REMOVE THIS
-
-  void DatabaseWrapperBase::LookupIdentifierExact(std::list<int64_t>& target,
-                                                  ResourceType level,
-                                                  const DicomTag& tag,
-                                                  const std::string& value)
-  {
-    assert((level == ResourceType_Patient && tag == DICOM_TAG_PATIENT_ID) ||
-           (level == ResourceType_Study && tag == DICOM_TAG_STUDY_INSTANCE_UID) ||
-           (level == ResourceType_Study && tag == DICOM_TAG_ACCESSION_NUMBER) ||
-           (level == ResourceType_Series && tag == DICOM_TAG_SERIES_INSTANCE_UID) ||
-           (level == ResourceType_Instance && tag == DICOM_TAG_SOP_INSTANCE_UID));
-    
-    SQLite::Statement s(db_, SQLITE_FROM_HERE, 
-                        "SELECT d.id FROM DicomIdentifiers AS d, Resources AS r WHERE "
-                        "d.id = r.internalId AND r.resourceType=? AND d.tagGroup=? AND d.tagElement=? AND d.value=?");
-
-    s.BindInt(0, level);
-    s.BindInt(1, tag.GetGroup());
-    s.BindInt(2, tag.GetElement());
-    s.BindString(3, value);
-
-    target.clear();
-
-    while (s.Step())
-    {
-      target.push_back(s.ColumnInt64(0));
-    }
-  }
-  */
-
-
-
   void DatabaseWrapperBase::LookupIdentifier(std::list<int64_t>& target,
                                              ResourceType level,
                                              const DicomTag& tag,
--- a/Plugins/Engine/OrthancPluginDatabase.cpp	Wed Oct 28 12:59:18 2015 +0100
+++ b/Plugins/Engine/OrthancPluginDatabase.cpp	Thu Oct 29 11:09:00 2015 +0100
@@ -277,8 +277,15 @@
   void OrthancPluginDatabase::GetAllInternalIds(std::list<int64_t>& target,
                                                 ResourceType resourceType)
   {
-    // TODO
-    throw OrthancException(ErrorCode_NotImplemented);
+    if (extensions_.getAllInternalIds == NULL)
+    {
+      LOG(ERROR) << "The database plugin does not implement the GetAllInternalIds primitive";
+      throw OrthancException(ErrorCode_DatabasePlugin);
+    }
+
+    ResetAnswers();
+    CheckSuccess(extensions_.getAllInternalIds(GetContext(), payload_, Plugins::Convert(resourceType)));
+    ForwardAnswers(target);
   }
 
 
@@ -602,70 +609,27 @@
   }
 
 
-  /*
-
-    TODO REMOVE THIS
-
-  void OrthancPluginDatabase::LookupIdentifierExact(std::list<int64_t>& target,
-                                                    ResourceType level,
-                                                    const DicomTag& tag,
-                                                    const std::string& value)
+  void OrthancPluginDatabase::LookupIdentifier(std::list<int64_t>& result,
+                                               ResourceType level,
+                                               const DicomTag& tag,
+                                               IdentifierConstraintType type,
+                                               const std::string& value)
   {
-    ResetAnswers();
+    if (extensions_.lookupIdentifier3 == NULL)
+    {
+      LOG(ERROR) << "The database plugin does not implement the GetAllInternalIds primitive";
+      throw OrthancException(ErrorCode_DatabasePlugin);
+    }
 
     OrthancPluginDicomTag tmp;
     tmp.group = tag.GetGroup();
     tmp.element = tag.GetElement();
     tmp.value = value.c_str();
 
-    if (extensions_.lookupIdentifierExact != NULL)
-    {
-      CheckSuccess(extensions_.lookupIdentifierExact(GetContext(), payload_, Plugins::Convert(level), &tmp));
-      ForwardAnswers(target);
-    }
-    else
-    {
-      // Emulate "lookupIdentifierExact" if unavailable
-
-      if (backend_.lookupIdentifier == NULL)
-      {
-        LOG(ERROR) << "The plugin does not have the extension \"lookupIdentifierExact\"";
-        throw OrthancException(ErrorCode_DatabasePlugin);
-      }
-
-      CheckSuccess(backend_.lookupIdentifier(GetContext(), payload_, &tmp));
-
-      if (type_ != _OrthancPluginDatabaseAnswerType_None &&
-          type_ != _OrthancPluginDatabaseAnswerType_Int64)
-      {
-        throw OrthancException(ErrorCode_DatabasePlugin);
-      }
-
-      target.clear();
-
-      if (type_ == _OrthancPluginDatabaseAnswerType_Int64)
-      {
-        for (std::list<int64_t>::const_iterator 
-               it = answerInt64_.begin(); it != answerInt64_.end(); ++it)
-        {
-          if (GetResourceType(*it) == level)
-          {
-            target.push_back(*it);
-          }
-        }
-      }
-    }
-    }*/
-
-
-  void OrthancPluginDatabase::LookupIdentifier(std::list<int64_t>& result,
-                                               ResourceType level,
-                                               const DicomTag& tag,
-                                               IdentifierConstraintType type,
-                                               const std::string& value)
-  {
-    // TODO
-    throw OrthancException(ErrorCode_NotImplemented);
+    ResetAnswers();
+    CheckSuccess(extensions_.lookupIdentifier3(GetContext(), payload_, Plugins::Convert(level),
+                                               &tmp, Plugins::Convert(type)));
+    ForwardAnswers(result);
   }
 
 
--- a/Plugins/Engine/OrthancPlugins.cpp	Wed Oct 28 12:59:18 2015 +0100
+++ b/Plugins/Engine/OrthancPlugins.cpp	Thu Oct 29 11:09:00 2015 +0100
@@ -302,6 +302,7 @@
         sizeof(int32_t) != sizeof(OrthancPluginDicomToJsonFlags) ||
         sizeof(int32_t) != sizeof(OrthancPluginDicomToJsonFormat) ||
         sizeof(int32_t) != sizeof(_OrthancPluginDatabaseAnswerType) ||
+        sizeof(int32_t) != sizeof(OrthancPluginIdentifierConstraint) ||
         static_cast<int>(OrthancPluginDicomToJsonFlags_IncludeBinary) != static_cast<int>(DicomToJsonFlags_IncludeBinary) ||
         static_cast<int>(OrthancPluginDicomToJsonFlags_IncludePrivateTags) != static_cast<int>(DicomToJsonFlags_IncludePrivateTags) ||
         static_cast<int>(OrthancPluginDicomToJsonFlags_IncludeUnknownTags) != static_cast<int>(DicomToJsonFlags_IncludeUnknownTags) ||
--- a/Plugins/Engine/PluginsEnumerations.cpp	Wed Oct 28 12:59:18 2015 +0100
+++ b/Plugins/Engine/PluginsEnumerations.cpp	Thu Oct 29 11:09:00 2015 +0100
@@ -229,6 +229,28 @@
     }
 
 
+    OrthancPluginIdentifierConstraint Convert(IdentifierConstraintType constraint)
+    {
+      switch (constraint)
+      {
+        case IdentifierConstraintType_Equal:
+          return OrthancPluginIdentifierConstraint_Equal;
+
+        case IdentifierConstraintType_GreaterOrEqual:
+          return OrthancPluginIdentifierConstraint_GreaterOrEqual;
+
+        case IdentifierConstraintType_SmallerOrEqual:
+          return OrthancPluginIdentifierConstraint_SmallerOrEqual;
+
+        case IdentifierConstraintType_Wildcard:
+          return OrthancPluginIdentifierConstraint_Wildcard;
+
+        default:
+          throw OrthancException(ErrorCode_ParameterOutOfRange);
+      }
+    }
+
+
 #if !defined(ORTHANC_ENABLE_DCMTK) || ORTHANC_ENABLE_DCMTK != 0
     DcmEVR Convert(OrthancPluginValueRepresentation vr)
     {
--- a/Plugins/Engine/PluginsEnumerations.h	Wed Oct 28 12:59:18 2015 +0100
+++ b/Plugins/Engine/PluginsEnumerations.h	Thu Oct 29 11:09:00 2015 +0100
@@ -61,6 +61,8 @@
 
     DicomToJsonFormat Convert(OrthancPluginDicomToJsonFormat format);
 
+    OrthancPluginIdentifierConstraint Convert(IdentifierConstraintType constraint);
+
 #if !defined(ORTHANC_ENABLE_DCMTK) || ORTHANC_ENABLE_DCMTK != 0
     DcmEVR Convert(OrthancPluginValueRepresentation vr);
 #endif
--- a/Plugins/Include/orthanc/OrthancCDatabasePlugin.h	Wed Oct 28 12:59:18 2015 +0100
+++ b/Plugins/Include/orthanc/OrthancCDatabasePlugin.h	Thu Oct 29 11:09:00 2015 +0100
@@ -522,7 +522,7 @@
       void* payload,
       int32_t property);
 
-    /* Use "OrthancPluginDatabaseExtensions::lookupIdentifier2" 
+    /* Use "OrthancPluginDatabaseExtensions::lookupIdentifier3" 
        instead of this function as of Orthanc 0.9.5 (db v6), can be set to NULL.
        Output: Use OrthancPluginDatabaseAnswerInt64() */
     OrthancPluginErrorCode  (*lookupIdentifier) (
@@ -664,17 +664,23 @@
       void* payload,
       int64_t id);
 
-#if 0
     /* Output: Use OrthancPluginDatabaseAnswerInt64() */
-    OrthancPluginErrorCode  (*lookupIdentifier2) (
+    OrthancPluginErrorCode  (*getAllInternalIds) (
+      /* outputs */
+      OrthancPluginDatabaseContext* context,
+      /* inputs */
+      void* payload,
+      OrthancPluginResourceType resourceType);
+
+    /* Output: Use OrthancPluginDatabaseAnswerInt64() */
+    OrthancPluginErrorCode  (*lookupIdentifier3) (
       /* outputs */
       OrthancPluginDatabaseContext* context,
       /* inputs */
       void* payload,
       OrthancPluginResourceType resourceType,
-      const OrthancPluginDicomTag* tag);
-#endif
-
+      const OrthancPluginDicomTag* tag,
+      OrthancPluginIdentifierConstraint constraint);
    } OrthancPluginDatabaseExtensions;
 
 /*<! @endcond */
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Wed Oct 28 12:59:18 2015 +0100
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Oct 29 11:09:00 2015 +0100
@@ -680,6 +680,22 @@
 
 
   /**
+   * The constraints on the DICOM identifiers that must be supported
+   * by the database plugins.
+   **/
+  typedef enum
+  {
+    OrthancPluginIdentifierConstraint_Equal,           /*!< Equal */
+    OrthancPluginIdentifierConstraint_SmallerOrEqual,  /*!< Less or equal */
+    OrthancPluginIdentifierConstraint_GreaterOrEqual,  /*!< More or equal */
+    OrthancPluginIdentifierConstraint_Wildcard,        /*!< Case-sensitive wildcard matching (with * and ?) */
+
+    _OrthancPluginIdentifierConstraint_INTERNAL = 0x7fffffff
+  } OrthancPluginIdentifierConstraint;
+
+
+
+  /**
    * @brief A memory buffer allocated by the core system of Orthanc.
    *
    * A memory buffer allocated by the core system of Orthanc. When the
@@ -892,7 +908,8 @@
         sizeof(int32_t) != sizeof(OrthancPluginImageFormat) ||
         sizeof(int32_t) != sizeof(OrthancPluginValueRepresentation) ||
         sizeof(int32_t) != sizeof(OrthancPluginDicomToJsonFormat) ||
-        sizeof(int32_t) != sizeof(OrthancPluginDicomToJsonFlags))
+        sizeof(int32_t) != sizeof(OrthancPluginDicomToJsonFlags) ||
+        sizeof(int32_t) != sizeof(OrthancPluginIdentifierConstraint))
     {
       /* Mismatch in the size of the enumerations */
       return 0;
--- a/Plugins/Include/orthanc/OrthancCppDatabasePlugin.h	Wed Oct 28 12:59:18 2015 +0100
+++ b/Plugins/Include/orthanc/OrthancCppDatabasePlugin.h	Thu Oct 29 11:09:00 2015 +0100
@@ -339,6 +339,9 @@
 
     virtual void DeleteResource(int64_t id) = 0;
 
+    virtual void GetAllInternalIds(std::list<int64_t>& target,
+                                   OrthancPluginResourceType resourceType) = 0;
+
     virtual void GetAllPublicIds(std::list<std::string>& target,
                                  OrthancPluginResourceType resourceType) = 0;
 
@@ -403,17 +406,12 @@
     virtual bool LookupGlobalProperty(std::string& target /*out*/,
                                       int32_t property) = 0;
 
-    /**
-     * "Identifiers" are necessarily one of the following tags:
-     * PatientID (0x0010, 0x0020), StudyInstanceUID (0x0020, 0x000d),
-     * SeriesInstanceUID (0x0020, 0x000e), SOPInstanceUID (0x0008,
-     * 0x0018) or AccessionNumber (0x0008, 0x0050).
-     **/
-    virtual void LookupIdentifierExact(std::list<int64_t>& target /*out*/,
-                                       OrthancPluginResourceType resourceType,
-                                       uint16_t group,
-                                       uint16_t element,
-                                       const char* value) = 0;
+    virtual void LookupIdentifier(std::list<int64_t>& target /*out*/,
+                                  OrthancPluginResourceType resourceType,
+                                  uint16_t group,
+                                  uint16_t element,
+                                  OrthancPluginIdentifierConstraint constraint,
+                                  const char* value) = 0;
 
     virtual bool LookupMetadata(std::string& target /*out*/,
                                 int64_t id,
@@ -684,6 +682,39 @@
     }
 
 
+    static OrthancPluginErrorCode  GetAllInternalIds(OrthancPluginDatabaseContext* context,
+                                                     void* payload,
+                                                     OrthancPluginResourceType resourceType)
+    {
+      IDatabaseBackend* backend = reinterpret_cast<IDatabaseBackend*>(payload);
+      backend->GetOutput().SetAllowedAnswers(DatabaseBackendOutput::AllowedAnswers_None);
+
+      try
+      {
+        std::list<int64_t> target;
+        backend->GetAllInternalIds(target, resourceType);
+
+        for (std::list<int64_t>::const_iterator
+               it = target.begin(); it != target.end(); ++it)
+        {
+          OrthancPluginDatabaseAnswerInt64(backend->GetOutput().context_,
+                                           backend->GetOutput().database_, *it);
+        }
+
+        return OrthancPluginErrorCode_Success;
+      }
+      catch (std::runtime_error& e)
+      {
+        LogError(backend, e);
+        return OrthancPluginErrorCode_DatabasePlugin;
+      }
+      catch (DatabaseException& e)
+      {
+        return e.GetErrorCode();
+      }
+    }
+
+
     static OrthancPluginErrorCode  GetAllPublicIds(OrthancPluginDatabaseContext* context,
                                                    void* payload,
                                                    OrthancPluginResourceType resourceType)
@@ -1296,10 +1327,11 @@
     }
 
 
-    static OrthancPluginErrorCode  LookupIdentifierExact(OrthancPluginDatabaseContext* context,
-                                                         void* payload,
-                                                         OrthancPluginResourceType resourceType,
-                                                         const OrthancPluginDicomTag* tag)
+    static OrthancPluginErrorCode  LookupIdentifier3(OrthancPluginDatabaseContext* context,
+                                                     void* payload,
+                                                     OrthancPluginResourceType resourceType,
+                                                     const OrthancPluginDicomTag* tag,
+                                                     OrthancPluginIdentifierConstraint constraint)
     {
       IDatabaseBackend* backend = reinterpret_cast<IDatabaseBackend*>(payload);
       backend->GetOutput().SetAllowedAnswers(DatabaseBackendOutput::AllowedAnswers_None);
@@ -1307,7 +1339,7 @@
       try
       {
         std::list<int64_t> target;
-        backend->LookupIdentifierExact(target, resourceType, tag->group, tag->element, tag->value);
+        backend->LookupIdentifier(target, resourceType, tag->group, tag->element, constraint, tag->value);
 
         for (std::list<int64_t>::const_iterator
                it = target.begin(); it != target.end(); ++it)
@@ -1848,7 +1880,8 @@
       extensions.getDatabaseVersion = GetDatabaseVersion;
       extensions.upgradeDatabase = UpgradeDatabase;
       extensions.clearMainDicomTags = ClearMainDicomTags;
-      extensions.lookupIdentifierExact = LookupIdentifierExact;   // New in Orthanc 0.9.5 (db v6)
+      extensions.getAllInternalIds = GetAllInternalIds;   // New in Orthanc 0.9.5 (db v6)
+      extensions.lookupIdentifier3 = LookupIdentifier3;   // New in Orthanc 0.9.5 (db v6)
 
       OrthancPluginDatabaseContext* database = OrthancPluginRegisterDatabaseBackendV2(context, &params, &extensions, &backend);
       if (!context)