# HG changeset patch # User Sebastien Jodogne # Date 1443785165 -7200 # Node ID 0bbcfd9695e5014638c5861995623d53700f011e # Parent 4c5a85c3ff439904580a31d2d3163dc7800e3db1 UpgradeDatabase in the sample plugin diff -r 4c5a85c3ff43 -r 0bbcfd9695e5 OrthancServer/ServerToolbox.cpp --- a/OrthancServer/ServerToolbox.cpp Fri Oct 02 12:20:49 2015 +0200 +++ b/OrthancServer/ServerToolbox.cpp Fri Oct 02 13:26:05 2015 +0200 @@ -33,9 +33,10 @@ #include "PrecompiledHeadersServer.h" #include "ServerToolbox.h" +#include "../Core/DicomFormat/DicomArray.h" +#include "../Core/FileStorage/StorageAccessor.h" #include "../Core/Logging.h" #include "../Core/OrthancException.h" -#include "../Core/DicomFormat/DicomArray.h" #include "ParsedDicomFile.h" #include @@ -286,8 +287,10 @@ } // Read and parse the content of the DICOM file + StorageAccessor accessor(storageArea); + std::string content; - storageArea.Read(content, attachment.GetUuid(), FileContentType_Dicom); + accessor.Read(content, attachment); ParsedDicomFile dicom(content); @@ -306,7 +309,7 @@ case ResourceType_Study: Toolbox::SetMainDicomTags(database, resource, ResourceType_Study, dicomSummary, true); - // Duplicate the patient tags at the study level + // Duplicate the patient tags at the study level (new in Orthanc 0.9.5 - db v6) Toolbox::SetMainDicomTags(database, resource, ResourceType_Patient, dicomSummary, false); break; diff -r 4c5a85c3ff43 -r 0bbcfd9695e5 Plugins/Engine/OrthancPluginDatabase.cpp --- a/Plugins/Engine/OrthancPluginDatabase.cpp Fri Oct 02 12:20:49 2015 +0200 +++ b/Plugins/Engine/OrthancPluginDatabase.cpp Fri Oct 02 13:26:05 2015 +0200 @@ -692,7 +692,7 @@ void OrthancPluginDatabase::ClearMainDicomTags(int64_t id) { - if (extensions_.clearMainDicomTags != NULL) + if (extensions_.clearMainDicomTags == NULL) { LOG(ERROR) << "Your custom index plugin does not implement the ClearMainDicomTags() extension"; throw OrthancException(ErrorCode_DatabasePlugin); @@ -848,9 +848,23 @@ { if (extensions_.upgradeDatabase != NULL) { - CheckSuccess(extensions_.upgradeDatabase( - payload_, targetVersion, - reinterpret_cast(&storageArea))); + Transaction transaction(backend_, payload_, errorDictionary_); + transaction.Begin(); + + OrthancPluginErrorCode code = extensions_.upgradeDatabase( + payload_, targetVersion, + reinterpret_cast(&storageArea)); + + if (code == OrthancPluginErrorCode_Success) + { + transaction.Commit(); + } + else + { + transaction.Rollback(); + errorDictionary_.LogError(code, true); + throw OrthancException(static_cast(code)); + } } } diff -r 4c5a85c3ff43 -r 0bbcfd9695e5 Plugins/Engine/OrthancPlugins.cpp --- a/Plugins/Engine/OrthancPlugins.cpp Fri Oct 02 12:20:49 2015 +0200 +++ b/Plugins/Engine/OrthancPlugins.cpp Fri Oct 02 13:26:05 2015 +0200 @@ -1722,6 +1722,7 @@ IStorageArea& storage = *reinterpret_cast(p.storageArea); Toolbox::ReconstructMainDicomTags(*pimpl_->database_, storage, Plugins::Convert(p.level)); + return true; } diff -r 4c5a85c3ff43 -r 0bbcfd9695e5 Plugins/Include/orthanc/OrthancCPlugin.h --- a/Plugins/Include/orthanc/OrthancCPlugin.h Fri Oct 02 12:20:49 2015 +0200 +++ b/Plugins/Include/orthanc/OrthancCPlugin.h Fri Oct 02 13:26:05 2015 +0200 @@ -3775,6 +3775,22 @@ OrthancPluginResourceType level; } _OrthancPluginReconstructMainDicomTags; + /** + * @brief Reconstruct the main DICOM tags. + * + * This function requests the Orthanc core to reconstruct the main + * DICOM tags of all the resources of the given type. This function + * can only be used as a part of the upgrade of a custom database + * back-end + * (cf. OrthancPlugins::IDatabaseBackend::UpgradeDatabase). A + * database transaction will be automatically setup. + * + * @param context The Orthanc plugin context, as received by OrthancPluginInitialize(). + * @param storageArea The storage area. + * @param level The type of the resources of interest. + * @return 0 if success, other value if error. + * @ingroup Callbacks + **/ ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode OrthancPluginReconstructMainDicomTags( OrthancPluginContext* context, OrthancPluginStorageArea* storageArea, diff -r 4c5a85c3ff43 -r 0bbcfd9695e5 Plugins/Include/orthanc/OrthancCppDatabasePlugin.h --- a/Plugins/Include/orthanc/OrthancCppDatabasePlugin.h Fri Oct 02 12:20:49 2015 +0200 +++ b/Plugins/Include/orthanc/OrthancCppDatabasePlugin.h Fri Oct 02 13:26:05 2015 +0200 @@ -122,6 +122,11 @@ { } + OrthancPluginContext* GetContext() + { + return context_; + } + void LogError(const std::string& message) { OrthancPluginLogError(context_, message.c_str()); @@ -456,6 +461,11 @@ virtual uint32_t GetDatabaseVersion() = 0; + /** + * Upgrade the database to the specified version of the database + * schema. The upgrade script is allowed to make calls to + * OrthancPluginReconstructMainDicomTags(). + **/ virtual void UpgradeDatabase(uint32_t targetVersion, OrthancPluginStorageArea* storageArea) = 0; diff -r 4c5a85c3ff43 -r 0bbcfd9695e5 Plugins/Samples/DatabasePlugin/Database.cpp --- a/Plugins/Samples/DatabasePlugin/Database.cpp Fri Oct 02 12:20:49 2015 +0200 +++ b/Plugins/Samples/DatabasePlugin/Database.cpp Fri Oct 02 13:26:05 2015 +0200 @@ -540,4 +540,21 @@ void Database::UpgradeDatabase(uint32_t targetVersion, OrthancPluginStorageArea* storageArea) { + if (targetVersion == 6) + { + OrthancPluginErrorCode code = OrthancPluginReconstructMainDicomTags(GetOutput().GetContext(), storageArea, + OrthancPluginResourceType_Study); + if (code == OrthancPluginErrorCode_Success) + { + code = OrthancPluginReconstructMainDicomTags(GetOutput().GetContext(), storageArea, + OrthancPluginResourceType_Series); + } + + if (code != OrthancPluginErrorCode_Success) + { + throw OrthancPlugins::DatabaseException(code); + } + + base_.SetGlobalProperty(Orthanc::GlobalProperty_DatabaseSchemaVersion, "6"); + } }