# HG changeset patch # User Sebastien Jodogne # Date 1404825060 -7200 # Node ID 649d47854314b8079689bc9ba52fbc3175678515 # Parent 84b6d7bca6dbf3c8802c5652fe2d1baa17bd1398 proper handling of metadata in Store diff -r 84b6d7bca6db -r 649d47854314 OrthancServer/DatabaseWrapper.cpp --- a/OrthancServer/DatabaseWrapper.cpp Tue Jul 08 14:34:11 2014 +0200 +++ b/OrthancServer/DatabaseWrapper.cpp Tue Jul 08 15:11:00 2014 +0200 @@ -1045,4 +1045,21 @@ result.push_back(s.ColumnInt64(0)); } } + + + void DatabaseWrapper::GetAllMetadata(std::map& result, + int64_t id) + { + result.clear(); + + SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT type, value FROM Metadata WHERE id=?"); + s.BindInt64(0, id); + + while (s.Step()) + { + MetadataType key = static_cast(s.ColumnInt(0)); + result[key] = s.ColumnString(1); + } + } + } diff -r 84b6d7bca6db -r 649d47854314 OrthancServer/DatabaseWrapper.h --- a/OrthancServer/DatabaseWrapper.h Tue Jul 08 14:34:11 2014 +0200 +++ b/OrthancServer/DatabaseWrapper.h Tue Jul 08 15:11:00 2014 +0200 @@ -235,5 +235,8 @@ void LookupTagValue(std::list& result, const std::string& value); + + void GetAllMetadata(std::map& result, + int64_t id); }; } diff -r 84b6d7bca6db -r 649d47854314 OrthancServer/DicomInstanceToStore.h --- a/OrthancServer/DicomInstanceToStore.h Tue Jul 08 14:34:11 2014 +0200 +++ b/OrthancServer/DicomInstanceToStore.h Tue Jul 08 15:11:00 2014 +0200 @@ -138,49 +138,6 @@ }; - /*class MemoryBuffer - { - private: - const char* buffer_; - size_t size_; - - public: - MemoryBuffer() : buffer_(NULL), size_(0) - { - } - - const char* GetBuffer() const - { - return buffer_; - } - - size_t GetSize() const - { - return size_; - } - - void Assign(const char* buffer, size_t size) - { - buffer_ = buffer; - size_ = size; - } - - void Assign(const std::string& buffer) - { - size_ = buffer.size(); - - if (size_ == 0) - { - buffer_ = NULL; - } - else - { - buffer_ = &buffer[0]; - } - } - };*/ - - SmartContainer buffer_; SmartContainer parsed_; SmartContainer summary_; diff -r 84b6d7bca6db -r 649d47854314 OrthancServer/ServerContext.cpp --- a/OrthancServer/ServerContext.cpp Tue Jul 08 14:34:11 2014 +0200 +++ b/OrthancServer/ServerContext.cpp Tue Jul 08 15:11:00 2014 +0200 @@ -193,7 +193,9 @@ attachments.push_back(dicomInfo); attachments.push_back(jsonInfo); - StoreStatus status = index_.Store(dicom.GetSummary(), attachments, dicom.GetRemoteAet(), dicom.GetMetadata()); + std::map instanceMetadata; + StoreStatus status = index_.Store(instanceMetadata, dicom.GetSummary(), attachments, + dicom.GetRemoteAet(), dicom.GetMetadata()); if (status != StoreStatus_Success) { @@ -225,16 +227,20 @@ { try { +#if 1 Json::Value metadata = Json::objectValue; - for (ServerIndex::MetadataMap::const_iterator - it = dicom.GetMetadata().begin(); - it != dicom.GetMetadata().end(); ++it) + for (std::map::const_iterator + it = instanceMetadata.begin(); + it != instanceMetadata.end(); ++it) { - if (it->first.first == ResourceType_Instance) - { - metadata[EnumerationToString(it->first.second)] = it->second; - } + metadata[EnumerationToString(it->first)] = it->second; } +#else + Json::Value metadata; + index_.GetMetadata(metadata, resultPublicId); +#endif + + std::cout << metadata; ApplyOnStoredInstance(resultPublicId, simplified, metadata); } diff -r 84b6d7bca6db -r 649d47854314 OrthancServer/ServerEnumerations.h --- a/OrthancServer/ServerEnumerations.h Tue Jul 08 14:34:11 2014 +0200 +++ b/OrthancServer/ServerEnumerations.h Tue Jul 08 15:11:00 2014 +0200 @@ -32,6 +32,7 @@ #pragma once #include +#include #include "../Core/Enumerations.h" @@ -124,6 +125,8 @@ ChangeType_StableSeries = 14 }; + + void InitializeServerEnumerations(); void RegisterUserMetadata(int metadata, diff -r 84b6d7bca6db -r 649d47854314 OrthancServer/ServerIndex.cpp --- a/OrthancServer/ServerIndex.cpp Tue Jul 08 14:34:11 2014 +0200 +++ b/OrthancServer/ServerIndex.cpp Tue Jul 08 15:11:00 2014 +0200 @@ -382,14 +382,17 @@ } - StoreStatus ServerIndex::Store(const DicomMap& dicomSummary, + StoreStatus ServerIndex::Store(std::map& instanceMetadata, + const DicomMap& dicomSummary, const Attachments& attachments, const std::string& remoteAet, - MetadataMap* metadata) + const MetadataMap& metadata) { boost::mutex::scoped_lock lock(mutex_); listener_->Reset(); + instanceMetadata.clear(); + DicomInstanceHasher hasher(dicomSummary); try @@ -403,6 +406,7 @@ if (db_->LookupResource(hasher.HashInstance(), tmp, type)) { assert(type == ResourceType_Instance); + db_->GetAllMetadata(instanceMetadata, tmp); return StoreStatus_AlreadyStored; } } @@ -521,32 +525,30 @@ } // Attach the user-specified metadata - if (metadata) + for (MetadataMap::const_iterator + it = metadata.begin(); it != metadata.end(); ++it) { - for (MetadataMap::const_iterator - it = metadata->begin(); it != metadata->end(); ++it) + switch (it->first.first) { - switch (it->first.first) - { - case ResourceType_Patient: - db_->SetMetadata(patient, it->first.second, it->second); - break; + case ResourceType_Patient: + db_->SetMetadata(patient, it->first.second, it->second); + break; - case ResourceType_Study: - db_->SetMetadata(study, it->first.second, it->second); - break; + case ResourceType_Study: + db_->SetMetadata(study, it->first.second, it->second); + break; - case ResourceType_Series: - db_->SetMetadata(series, it->first.second, it->second); - break; + case ResourceType_Series: + db_->SetMetadata(series, it->first.second, it->second); + break; - case ResourceType_Instance: - db_->SetMetadata(instance, it->first.second, it->second); - break; + case ResourceType_Instance: + db_->SetMetadata(instance, it->first.second, it->second); + instanceMetadata[it->first.second] = it->second; + break; - default: - throw OrthancException(ErrorCode_ParameterOutOfRange); - } + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); } } @@ -559,24 +561,17 @@ // Attach the auto-computed metadata for the instance level, // reflecting these additions into the input metadata map db_->SetMetadata(instance, MetadataType_Instance_ReceptionDate, now); - db_->SetMetadata(instance, MetadataType_Instance_RemoteAet, remoteAet); + instanceMetadata[MetadataType_Instance_ReceptionDate] = now; - if (metadata) - { - (*metadata) [std::make_pair(ResourceType_Instance, MetadataType_Instance_ReceptionDate)] = now; - (*metadata) [std::make_pair(ResourceType_Instance, MetadataType_Instance_RemoteAet)] = remoteAet; - } + db_->SetMetadata(instance, MetadataType_Instance_RemoteAet, remoteAet); + instanceMetadata[MetadataType_Instance_RemoteAet] = remoteAet; const DicomValue* value; if ((value = dicomSummary.TestAndGetValue(DICOM_TAG_INSTANCE_NUMBER)) != NULL || (value = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGE_INDEX)) != NULL) { db_->SetMetadata(instance, MetadataType_Instance_IndexInSeries, value->AsString()); - - if (metadata) - { - (*metadata) [std::make_pair(ResourceType_Instance, MetadataType_Instance_IndexInSeries)] = value->AsString(); - } + instanceMetadata[MetadataType_Instance_IndexInSeries] = value->AsString(); } // Check whether the series of this new instance is now completed diff -r 84b6d7bca6db -r 649d47854314 OrthancServer/ServerIndex.h --- a/OrthancServer/ServerIndex.h Tue Jul 08 14:34:11 2014 +0200 +++ b/OrthancServer/ServerIndex.h Tue Jul 08 15:11:00 2014 +0200 @@ -102,11 +102,6 @@ /* in */ int64_t id, /* in */ ResourceType type); - StoreStatus Store(const DicomMap& dicomSummary, - const Attachments& attachments, - const std::string& remoteAet, - MetadataMap* metadata); - public: ServerIndex(ServerContext& context, const std::string& dbPath); @@ -129,20 +124,11 @@ // "count == 0" means no limit on the number of patients void SetMaximumPatientCount(unsigned int count); - StoreStatus Store(const DicomMap& dicomSummary, - const Attachments& attachments, - const std::string& remoteAet) - { - return Store(dicomSummary, attachments, remoteAet, NULL); - } - - StoreStatus Store(const DicomMap& dicomSummary, + StoreStatus Store(std::map& instanceMetadata, + const DicomMap& dicomSummary, const Attachments& attachments, const std::string& remoteAet, - MetadataMap& metadata) - { - return Store(dicomSummary, attachments, remoteAet, &metadata); - } + const MetadataMap& metadata); void ComputeStatistics(Json::Value& target); diff -r 84b6d7bca6db -r 649d47854314 Resources/Samples/Lua/Autorouting.lua --- a/Resources/Samples/Lua/Autorouting.lua Tue Jul 08 14:34:11 2014 +0200 +++ b/Resources/Samples/Lua/Autorouting.lua Tue Jul 08 15:11:00 2014 +0200 @@ -1,8 +1,9 @@ function OnStoredInstance(instance, tags, metadata) --PrintRecursive(tags) - PrintRecursive(metadata) - return { - { "store", instance, "pacs" }, - { "delete", instance } - } + PrintRecursive(metadata) + print(metadata['RemoteAET']) + return { + { "store", instance, "pacs" }, + { "delete", instance } + } end diff -r 84b6d7bca6db -r 649d47854314 Resources/Toolbox.lua --- a/Resources/Toolbox.lua Tue Jul 08 14:34:11 2014 +0200 +++ b/Resources/Toolbox.lua Tue Jul 08 15:11:00 2014 +0200 @@ -6,7 +6,8 @@ --]] function PrintRecursive(s, l, i) -- recursive Print (structure, limit, indent) - l = (l) or 100; i = i or ""; -- default item limit, indent string + l = (l) or 100; -- default item limit + i = i or ""; -- indent string if (l<1) then print "ERROR: Item limit reached."; return l-1 end; local ts = type(s); if (ts ~= "table") then print (i,ts,s); return l-1 end diff -r 84b6d7bca6db -r 649d47854314 UnitTestsSources/ServerIndexTests.cpp --- a/UnitTestsSources/ServerIndexTests.cpp Tue Jul 08 14:34:11 2014 +0200 +++ b/UnitTestsSources/ServerIndexTests.cpp Tue Jul 08 15:11:00 2014 +0200 @@ -579,7 +579,13 @@ instance.SetValue(DICOM_TAG_STUDY_INSTANCE_UID, "study-" + id); instance.SetValue(DICOM_TAG_SERIES_INSTANCE_UID, "series-" + id); instance.SetValue(DICOM_TAG_SOP_INSTANCE_UID, "instance-" + id); - ASSERT_EQ(StoreStatus_Success, index.Store(instance, attachments, "")); + + std::map instanceMetadata; + ServerIndex::MetadataMap metadata; + ASSERT_EQ(StoreStatus_Success, index.Store(instanceMetadata, instance, attachments, "", metadata)); + ASSERT_EQ(2, instanceMetadata.size()); + ASSERT_NE(instanceMetadata.end(), instanceMetadata.find(MetadataType_Instance_RemoteAet)); + ASSERT_NE(instanceMetadata.end(), instanceMetadata.find(MetadataType_Instance_ReceptionDate)); DicomInstanceHasher hasher(instance); ids.push_back(hasher.HashPatient());