changeset 1669:a412ad57f0f9 db-changes

refactoring of sample plugins, OrthancPluginReconstructMainDicomTags
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 01 Oct 2015 11:55:25 +0200
parents de1413733c97
children 16955f8fec4d
files OrthancServer/DatabaseWrapper.cpp OrthancServer/ServerEnumerations.h OrthancServer/ServerToolbox.cpp OrthancServer/main.cpp Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/PluginsEnumerations.cpp Plugins/Engine/PluginsEnumerations.h Plugins/Include/orthanc/OrthancCPlugin.h Plugins/Samples/Common/OrthancPlugins.cmake Plugins/Samples/GdcmDecoding/CMakeLists.txt Plugins/Samples/ServeFolders/CMakeLists.txt Plugins/Samples/WebSkeleton/CMakeLists.txt
diffstat 12 files changed, 132 insertions(+), 148 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DatabaseWrapper.cpp	Wed Sep 30 17:18:39 2015 +0200
+++ b/OrthancServer/DatabaseWrapper.cpp	Thu Oct 01 11:55:25 2015 +0200
@@ -37,6 +37,7 @@
 #include "../Core/Logging.h"
 #include "../Core/Uuid.h"
 #include "EmbeddedResources.h"
+#include "ServerToolbox.h"
 
 #include <stdio.h>
 #include <boost/lexical_cast.hpp>
@@ -890,9 +891,8 @@
       // No change in the DB schema, the step from version 5 to 6 only
       // consists in reconstructing the main DICOM tags information.
       db_.BeginTransaction();
-      SetGlobalProperty(GlobalProperty_DatabaseSchemaVersion, "6");
-      SetGlobalProperty(GlobalProperty_ReconstructStudiesTags, "1");
-      SetGlobalProperty(GlobalProperty_ReconstructSeriesTags, "1");
+      Toolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Study);
+      Toolbox::ReconstructMainDicomTags(*this, storageArea, ResourceType_Series);
       db_.CommitTransaction();
       version_ = 6;
     }    
--- a/OrthancServer/ServerEnumerations.h	Wed Sep 30 17:18:39 2015 +0200
+++ b/OrthancServer/ServerEnumerations.h	Thu Oct 01 11:55:25 2015 +0200
@@ -111,11 +111,7 @@
   {
     GlobalProperty_DatabaseSchemaVersion = 1,   // Unused in the Orthanc core as of Orthanc 0.9.5
     GlobalProperty_FlushSleep = 2,
-    GlobalProperty_AnonymizationSequence = 3,
-    GlobalProperty_ReconstructPatientsTags = 4,
-    GlobalProperty_ReconstructStudiesTags = 5,
-    GlobalProperty_ReconstructSeriesTags = 6,
-    GlobalProperty_ReconstructInstancesTags = 7
+    GlobalProperty_AnonymizationSequence = 3
   };
 
   enum MetadataType
--- a/OrthancServer/ServerToolbox.cpp	Wed Sep 30 17:18:39 2015 +0200
+++ b/OrthancServer/ServerToolbox.cpp	Thu Oct 01 11:55:25 2015 +0200
@@ -235,6 +235,32 @@
     {
       // WARNING: The database should be locked with a transaction!
 
+      const char* plural = NULL;
+
+      switch (level)
+      {
+        case ResourceType_Patient:
+          plural = "patients";
+          break;
+
+        case ResourceType_Study:
+          plural = "studies";
+          break;
+
+        case ResourceType_Series:
+          plural = "series";
+          break;
+
+        case ResourceType_Instance:
+          plural = "instances";
+          break;
+
+        default:
+          throw OrthancException(ErrorCode_InternalError);
+      }
+
+      LOG(WARNING) << "Upgrade: Reconstructing the main DICOM tags of all the " << plural << "...";
+
       std::list<std::string> resources;
       database.GetAllPublicIds(resources, level);
 
--- a/OrthancServer/main.cpp	Wed Sep 30 17:18:39 2015 +0200
+++ b/OrthancServer/main.cpp	Thu Oct 01 11:55:25 2015 +0200
@@ -613,114 +613,43 @@
 }
 
 
-
-
-static bool ReconstructLevel(IDatabaseWrapper& database,
-                             IStorageArea& storageArea,
-                             bool allowDatabaseUpgrade,
-                             ResourceType level)
-{
-  GlobalProperty property;
-  const char* plural = NULL;
-
-  switch (level)
-  {
-    case ResourceType_Patient:
-      plural = "patients";
-      property = GlobalProperty_ReconstructPatientsTags;
-      break;
-
-    case ResourceType_Study:
-      plural = "studies";
-      property = GlobalProperty_ReconstructStudiesTags;
-      break;
-
-    case ResourceType_Series:
-      plural = "series";
-      property = GlobalProperty_ReconstructSeriesTags;
-      break;
-
-    case ResourceType_Instance:
-      plural = "instances";
-      property = GlobalProperty_ReconstructInstancesTags;
-      break;
-
-    default:
-      throw OrthancException(ErrorCode_InternalError);
-  }
-
-  std::string tmp;
-  if (database.LookupGlobalProperty(tmp, property) && tmp != "0")
-  {
-    if (!allowDatabaseUpgrade)
-    {
-      LOG(ERROR) << "The main DICOM tags of all the " << plural
-                 << " must be reconstructed: "
-                 << "Please run Orthanc with the \"--upgrade\" command-line option";
-      return false;
-    }
-
-    std::auto_ptr<SQLite::ITransaction> transaction(database.StartTransaction());
-    transaction->Begin();
-
-    LOG(WARNING) << "Upgrade: Reconstructing the main DICOM tags of all the " << plural << "...";
-    Toolbox::ReconstructMainDicomTags(database, storageArea, level);
-
-    database.SetGlobalProperty(property, "0");
-
-    transaction->Commit();
-  }
-
-  return true;
-}
-
-
 static bool UpgradeDatabase(IDatabaseWrapper& database,
                             IStorageArea& storageArea,
                             bool allowDatabaseUpgrade)
 {
   // Upgrade the schema of the database, if needed
   unsigned int currentVersion = database.GetDatabaseVersion();
-  if (currentVersion != ORTHANC_DATABASE_VERSION)
+  if (currentVersion == ORTHANC_DATABASE_VERSION)
   {
-    if (currentVersion > ORTHANC_DATABASE_VERSION)
-    {
-      LOG(ERROR) << "The version of the database schema (" << currentVersion
-                 << ") is too recent for this version of Orthanc. Please upgrade Orthanc.";
-      return false;
-    }
-
-    if (!allowDatabaseUpgrade)
-    {
-      LOG(ERROR) << "The database schema must be upgraded from version "
-                 << currentVersion << " to " << ORTHANC_DATABASE_VERSION 
-                 << ": Please run Orthanc with the \"--upgrade\" command-line option";
-      return false;
-    }
-
-    LOG(WARNING) << "Upgrading the database from schema version "
-                 << currentVersion << " to " << ORTHANC_DATABASE_VERSION;
-    database.Upgrade(ORTHANC_DATABASE_VERSION, storageArea);
-    
-    // Sanity check
-    currentVersion = database.GetDatabaseVersion();
-    if (ORTHANC_DATABASE_VERSION != currentVersion)
-    {
-      LOG(ERROR) << "The database schema was not properly upgraded, it is still at version " << currentVersion;
-      throw OrthancException(ErrorCode_InternalError);
-    }
+    return true;
   }
 
-
-  // Reconstruct the main DICOM tags at each level, if needed
-  if (!ReconstructLevel(database, storageArea, allowDatabaseUpgrade, ResourceType_Patient) ||
-      !ReconstructLevel(database, storageArea, allowDatabaseUpgrade, ResourceType_Study) ||
-      !ReconstructLevel(database, storageArea, allowDatabaseUpgrade, ResourceType_Series) ||
-      !ReconstructLevel(database, storageArea, allowDatabaseUpgrade, ResourceType_Instance))
+  if (currentVersion > ORTHANC_DATABASE_VERSION)
   {
+    LOG(ERROR) << "The version of the database schema (" << currentVersion
+               << ") is too recent for this version of Orthanc. Please upgrade Orthanc.";
     return false;
   }
 
+  if (!allowDatabaseUpgrade)
+  {
+    LOG(ERROR) << "The database schema must be upgraded from version "
+               << currentVersion << " to " << ORTHANC_DATABASE_VERSION 
+               << ": Please run Orthanc with the \"--upgrade\" command-line option";
+    return false;
+  }
+
+  LOG(WARNING) << "Upgrading the database from schema version "
+               << currentVersion << " to " << ORTHANC_DATABASE_VERSION;
+  database.Upgrade(ORTHANC_DATABASE_VERSION, storageArea);
+    
+  // Sanity check
+  currentVersion = database.GetDatabaseVersion();
+  if (ORTHANC_DATABASE_VERSION != currentVersion)
+  {
+    LOG(ERROR) << "The database schema was not properly upgraded, it is still at version " << currentVersion;
+    throw OrthancException(ErrorCode_InternalError);
+  }
 
   return true;
 }
--- a/Plugins/Engine/OrthancPlugins.cpp	Wed Sep 30 17:18:39 2015 +0200
+++ b/Plugins/Engine/OrthancPlugins.cpp	Thu Oct 01 11:55:25 2015 +0200
@@ -1709,6 +1709,22 @@
         return true;
       }
 
+      case _OrthancPluginService_ReconstructMainDicomTags:
+      {
+        const _OrthancPluginReconstructMainDicomTags& p =
+          *reinterpret_cast<const _OrthancPluginReconstructMainDicomTags*>(parameters);
+
+        if (pimpl_->database_.get() == NULL)
+        {
+          LOG(ERROR) << "The service ReconstructMainDicomTags can only be invoked by custom database plugins";
+          throw OrthancException(ErrorCode_DatabasePlugin);
+        }
+
+        IStorageArea& storage = *reinterpret_cast<IStorageArea*>(p.storageArea);
+        Toolbox::ReconstructMainDicomTags(*pimpl_->database_, storage, Plugins::Convert(p.level));
+        return true;
+      }
+
       default:
       {
         // This service is unknown to the Orthanc plugin engine
--- a/Plugins/Engine/PluginsEnumerations.cpp	Wed Sep 30 17:18:39 2015 +0200
+++ b/Plugins/Engine/PluginsEnumerations.cpp	Thu Oct 01 11:55:25 2015 +0200
@@ -66,6 +66,28 @@
     }
 
 
+    ResourceType Convert(OrthancPluginResourceType type)
+    {
+      switch (type)
+      {
+        case OrthancPluginResourceType_Patient:
+          return ResourceType_Patient;
+
+        case OrthancPluginResourceType_Study:
+          return ResourceType_Study;
+
+        case OrthancPluginResourceType_Series:
+          return ResourceType_Series;
+
+        case OrthancPluginResourceType_Instance:
+          return ResourceType_Instance;
+
+        default:
+          throw OrthancException(ErrorCode_ParameterOutOfRange);
+      }
+    }
+
+
     OrthancPluginChangeType Convert(ChangeType type)
     {
       switch (type)
--- a/Plugins/Engine/PluginsEnumerations.h	Wed Sep 30 17:18:39 2015 +0200
+++ b/Plugins/Engine/PluginsEnumerations.h	Thu Oct 01 11:55:25 2015 +0200
@@ -45,6 +45,8 @@
   {
     OrthancPluginResourceType Convert(ResourceType type);
 
+    ResourceType Convert(OrthancPluginResourceType type);
+
     OrthancPluginChangeType Convert(ChangeType type);
 
     OrthancPluginPixelFormat Convert(PixelFormat format);
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Wed Sep 30 17:18:39 2015 +0200
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Oct 01 11:55:25 2015 +0200
@@ -422,6 +422,7 @@
     _OrthancPluginService_RestApiPostAfterPlugins = 3011,
     _OrthancPluginService_RestApiDeleteAfterPlugins = 3012,
     _OrthancPluginService_RestApiPutAfterPlugins = 3013,
+    _OrthancPluginService_ReconstructMainDicomTags = 3014,
 
     /* Access to DICOM instances */
     _OrthancPluginService_GetInstanceRemoteAet = 4000,
@@ -3766,6 +3767,27 @@
   }
 
 
+
+
+  typedef struct
+  {
+    OrthancPluginStorageArea*  storageArea;
+    OrthancPluginResourceType  level;
+  } _OrthancPluginReconstructMainDicomTags;
+
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode  OrthancPluginReconstructMainDicomTags(
+    OrthancPluginContext*      context,
+    OrthancPluginStorageArea*  storageArea,
+    OrthancPluginResourceType  level)
+  {
+    _OrthancPluginReconstructMainDicomTags params;
+    params.level = level;
+    params.storageArea = storageArea;
+
+    return context->InvokeService(context, _OrthancPluginService_ReconstructMainDicomTags, &params);
+  }
+
+
 #ifdef  __cplusplus
 }
 #endif
--- a/Plugins/Samples/Common/OrthancPlugins.cmake	Wed Sep 30 17:18:39 2015 +0200
+++ b/Plugins/Samples/Common/OrthancPlugins.cmake	Thu Oct 01 11:55:25 2015 +0200
@@ -1,34 +1,11 @@
+set(ORTHANC_ROOT ${SAMPLES_ROOT}/../..)
 include(CheckIncludeFiles)
+include(CheckIncludeFileCXX)
 include(CheckLibraryExists)
-
-
-if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
-  link_libraries(uuid)
-  SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pthread")
-  SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -pthread")
-
-elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
-  link_libraries(rpcrt4 ws2_32 secur32)
-  if (CMAKE_COMPILER_IS_GNUCXX)
-    SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libgcc -static-libstdc++")
-  endif()
-
-  CHECK_LIBRARY_EXISTS(winpthread pthread_create "" HAVE_WIN_PTHREAD)
-  if (HAVE_WIN_PTHREAD)
-    # This line is necessary to compile with recent versions of MinGW,
-    # otherwise "libwinpthread-1.dll" is not statically linked.
-    SET(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic")
-  endif()
-endif ()
-
-
-if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux" OR
-    ${CMAKE_SYSTEM_NAME} STREQUAL "kFreeBSD" OR
-    ${CMAKE_SYSTEM_NAME} STREQUAL "FreeBSD")
-  SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--version-script=${SAMPLES_ROOT}/Common/VersionScript.map -Wl,--no-undefined")
-elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
-  SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -exported_symbols_list ${CMAKE_SOURCE_DIR}/Plugins/Samples/Common/ExportedSymbols.list")
-endif()
+include(FindPythonInterp)
+include(${ORTHANC_ROOT}/Resources/CMake/AutoGeneratedCode.cmake)
+include(${ORTHANC_ROOT}/Resources/CMake/DownloadPackage.cmake)
+include(${ORTHANC_ROOT}/Resources/CMake/Compiler.cmake)
 
 
 if (CMAKE_COMPILER_IS_GNUCXX)
@@ -43,8 +20,10 @@
   link_libraries(dl rt)
 endif()
 
+
 include_directories(${SAMPLES_ROOT}/../Include/)
 
+
 if (MSVC)
   include_directories(${SAMPLES_ROOT}/../../Resources/ThirdParty/VisualStudio/)
 endif()
--- a/Plugins/Samples/GdcmDecoding/CMakeLists.txt	Wed Sep 30 17:18:39 2015 +0200
+++ b/Plugins/Samples/GdcmDecoding/CMakeLists.txt	Thu Oct 01 11:55:25 2015 +0200
@@ -3,18 +3,15 @@
 project(GdcmDecoding)
 
 SET(ALLOW_DOWNLOADS OFF CACHE BOOL "Allow CMake to download packages")
-SET(USE_SYSTEM_BOOST ON CACHE BOOL "Use the system version of Boost")
-SET(USE_SYSTEM_GOOGLE_LOG ON CACHE BOOL "Use the system version of Google Log")
+SET(STATIC_BUILD OFF CACHE BOOL "Static build of the third-party libraries (necessary for Windows)")
 
-set(ORTHANC_ROOT ${CMAKE_SOURCE_DIR}/../../..)
-set(SAMPLES_ROOT ${CMAKE_SOURCE_DIR}/..)
+SET(USE_SYSTEM_BOOST ON CACHE BOOL "Use the system version of Boost")
+SET(USE_SYSTEM_JSONCPP ON CACHE BOOL "Use the system version of JsonCpp")
 
-include(CheckIncludeFiles)
-include(CheckIncludeFileCXX)
+set(SAMPLES_ROOT ${CMAKE_SOURCE_DIR}/..)
 include(${CMAKE_SOURCE_DIR}/../Common/OrthancPlugins.cmake)
-include(${ORTHANC_ROOT}/Resources/CMake/DownloadPackage.cmake)
+
 include(${ORTHANC_ROOT}/Resources/CMake/BoostConfiguration.cmake)
-include(${ORTHANC_ROOT}/Resources/CMake/GoogleLogConfiguration.cmake)
 include(${ORTHANC_ROOT}/Resources/CMake/JsonCppConfiguration.cmake)
 
 find_package(GDCM REQUIRED)
--- a/Plugins/Samples/ServeFolders/CMakeLists.txt	Wed Sep 30 17:18:39 2015 +0200
+++ b/Plugins/Samples/ServeFolders/CMakeLists.txt	Thu Oct 01 11:55:25 2015 +0200
@@ -5,16 +5,12 @@
 SET(SERVE_FOLDERS_VERSION "0.0" CACHE STRING "Version of the plugin")
 SET(STATIC_BUILD OFF CACHE BOOL "Static build of the third-party libraries (necessary for Windows)")
 SET(ALLOW_DOWNLOADS OFF CACHE BOOL "Allow CMake to download packages")
+
 SET(USE_SYSTEM_JSONCPP ON CACHE BOOL "Use the system version of JsonCpp")
 SET(USE_SYSTEM_BOOST ON CACHE BOOL "Use the system version of boost")
 
-set(ORTHANC_ROOT ${CMAKE_SOURCE_DIR}/../../../)
 set(SAMPLES_ROOT ${CMAKE_SOURCE_DIR}/..)
-
-include(CheckIncludeFiles)
-include(CheckIncludeFileCXX)
 include(${CMAKE_SOURCE_DIR}/../Common/OrthancPlugins.cmake)
-include(${ORTHANC_ROOT}/Resources/CMake/DownloadPackage.cmake)
 include(${ORTHANC_ROOT}/Resources/CMake/JsonCppConfiguration.cmake)
 include(${ORTHANC_ROOT}/Resources/CMake/BoostConfiguration.cmake)
 
@@ -24,7 +20,6 @@
   ${BOOST_SOURCES}
   )
 
-
 message("Setting the version of the plugin to ${SERVE_FOLDERS_VERSION}")
 add_definitions(-DSERVE_FOLDERS_VERSION="${SERVE_FOLDERS_VERSION}")
 
--- a/Plugins/Samples/WebSkeleton/CMakeLists.txt	Wed Sep 30 17:18:39 2015 +0200
+++ b/Plugins/Samples/WebSkeleton/CMakeLists.txt	Thu Oct 01 11:55:25 2015 +0200
@@ -5,11 +5,11 @@
 SET(STANDALONE_BUILD ON CACHE BOOL "Standalone build (all the resources are embedded, necessary for releases)")
 SET(RESOURCES_ROOT ${CMAKE_SOURCE_DIR}/StaticResources)
 
-include(Framework/Framework.cmake)
-
 set(SAMPLES_ROOT ${CMAKE_SOURCE_DIR}/..)
 include(${SAMPLES_ROOT}/Common/OrthancPlugins.cmake)
 
+include(Framework/Framework.cmake)
+
 add_library(WebSkeleton SHARED 
   ${AUTOGENERATED_SOURCES}
   )