diff OrthancServer/ServerToolbox.cpp @ 3083:683d572424b6 db-changes

IDatabaseWrapper::SetResourcesContent
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 04 Jan 2019 15:52:19 +0100
parents ce272138f15e
children c829758b9ca0
line wrap: on
line diff
--- a/OrthancServer/ServerToolbox.cpp	Fri Jan 04 13:52:34 2019 +0100
+++ b/OrthancServer/ServerToolbox.cpp	Fri Jan 04 15:52:19 2019 +0100
@@ -35,45 +35,156 @@
 #include "ServerToolbox.h"
 
 #include "../Core/DicomFormat/DicomArray.h"
+#include "../Core/DicomParsing/ParsedDicomFile.h"
 #include "../Core/FileStorage/StorageAccessor.h"
 #include "../Core/Logging.h"
 #include "../Core/OrthancException.h"
+#include "IDatabaseWrapper.h"
+#include "ServerContext.h"
 
 #include <cassert>
 
 namespace Orthanc
 {
+  static const DicomTag PATIENT_IDENTIFIERS[] = 
+  {
+    DICOM_TAG_PATIENT_ID,
+    DICOM_TAG_PATIENT_NAME,
+    DICOM_TAG_PATIENT_BIRTH_DATE
+  };
+
+  static const DicomTag STUDY_IDENTIFIERS[] = 
+  {
+    DICOM_TAG_PATIENT_ID,
+    DICOM_TAG_PATIENT_NAME,
+    DICOM_TAG_PATIENT_BIRTH_DATE,
+    DICOM_TAG_STUDY_INSTANCE_UID,
+    DICOM_TAG_ACCESSION_NUMBER,
+    DICOM_TAG_STUDY_DESCRIPTION,
+    DICOM_TAG_STUDY_DATE
+  };
+
+  static const DicomTag SERIES_IDENTIFIERS[] = 
+  {
+    DICOM_TAG_SERIES_INSTANCE_UID
+  };
+
+  static const DicomTag INSTANCE_IDENTIFIERS[] = 
+  {
+    DICOM_TAG_SOP_INSTANCE_UID
+  };
+
+
+  void ResourcesContent::Store(Compatibility::ISetResourcesContent& compatibility) const
+  {
+    for (std::list<TagValue>::const_iterator
+           it = tags_.begin(); it != tags_.end(); ++it)
+    {
+      if (it->isIdentifier_)
+      {
+        compatibility.SetIdentifierTag(it->resourceId_, it->tag_,  it->value_);
+      }
+      else
+      {
+        compatibility.SetMainDicomTag(it->resourceId_, it->tag_,  it->value_);
+      }
+    }
+
+    for (std::list<Metadata>::const_iterator
+           it = metadata_.begin(); it != metadata_.end(); ++it)
+    {
+      compatibility.SetMetadata(it->resourceId_, it->metadata_,  it->value_);
+    }
+  }
+
+
+  static void StoreMainDicomTagsInternal(ResourcesContent& target,
+                                         int64_t resource,
+                                         const DicomMap& tags)
+  {
+    DicomArray flattened(tags);
+
+    for (size_t i = 0; i < flattened.GetSize(); i++)
+    {
+      const DicomElement& element = flattened.GetElement(i);
+      const DicomTag& tag = element.GetTag();
+      const DicomValue& value = element.GetValue();
+      if (!value.IsNull() && 
+          !value.IsBinary())
+      {
+        target.AddMainDicomTag(resource, tag, element.GetValue().GetContent());
+      }
+    }
+  }
+
+
+  static void StoreIdentifiers(ResourcesContent& target,
+                               int64_t resource,
+                               ResourceType level,
+                               const DicomMap& map)
+  {
+    const DicomTag* tags;
+    size_t size;
+
+    ServerToolbox::LoadIdentifiers(tags, size, level);
+
+    for (size_t i = 0; i < size; i++)
+    {
+      // The identifiers tags are a subset of the main DICOM tags
+      assert(DicomMap::IsMainDicomTag(tags[i]));
+        
+      const DicomValue* value = map.TestAndGetValue(tags[i]);
+      if (value != NULL &&
+          !value->IsNull() &&
+          !value->IsBinary())
+      {
+        std::string s = ServerToolbox::NormalizeIdentifier(value->GetContent());
+        target.AddIdentifierTag(resource, tags[i], s);
+      }
+    }
+  }
+
+
+  void ResourcesContent::AddResource(int64_t resource,
+                                     ResourceType level,
+                                     const DicomMap& dicomSummary)
+  {
+    StoreIdentifiers(*this, resource, level, dicomSummary);
+
+    DicomMap tags;
+
+    switch (level)
+    {
+      case ResourceType_Patient:
+        dicomSummary.ExtractPatientInformation(tags);
+        break;
+
+      case ResourceType_Study:
+        // Duplicate the patient tags at the study level (new in Orthanc 0.9.5 - db v6)
+        dicomSummary.ExtractPatientInformation(tags);
+        StoreMainDicomTagsInternal(*this, resource, tags);
+
+        dicomSummary.ExtractStudyInformation(tags);
+        break;
+
+      case ResourceType_Series:
+        dicomSummary.ExtractSeriesInformation(tags);
+        break;
+
+      case ResourceType_Instance:
+        dicomSummary.ExtractInstanceInformation(tags);
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_InternalError);
+    }
+
+    StoreMainDicomTagsInternal(*this, resource, tags);
+  }
+
+
   namespace ServerToolbox
   {
-    static const DicomTag patientIdentifiers[] = 
-    {
-      DICOM_TAG_PATIENT_ID,
-      DICOM_TAG_PATIENT_NAME,
-      DICOM_TAG_PATIENT_BIRTH_DATE
-    };
-
-    static const DicomTag studyIdentifiers[] = 
-    {
-      DICOM_TAG_PATIENT_ID,
-      DICOM_TAG_PATIENT_NAME,
-      DICOM_TAG_PATIENT_BIRTH_DATE,
-      DICOM_TAG_STUDY_INSTANCE_UID,
-      DICOM_TAG_ACCESSION_NUMBER,
-      DICOM_TAG_STUDY_DESCRIPTION,
-      DICOM_TAG_STUDY_DATE
-    };
-
-    static const DicomTag seriesIdentifiers[] = 
-    {
-      DICOM_TAG_SERIES_INSTANCE_UID
-    };
-
-    static const DicomTag instanceIdentifiers[] = 
-    {
-      DICOM_TAG_SOP_INSTANCE_UID
-    };
-
-
     void SimplifyTags(Json::Value& target,
                       const Json::Value& source,
                       DicomToJsonFormat format)
@@ -138,94 +249,6 @@
     }
 
 
-    static void StoreMainDicomTagsInternal(IDatabaseWrapper& database,
-                                           int64_t resource,
-                                           const DicomMap& tags)
-    {
-      DicomArray flattened(tags);
-
-      for (size_t i = 0; i < flattened.GetSize(); i++)
-      {
-        const DicomElement& element = flattened.GetElement(i);
-        const DicomTag& tag = element.GetTag();
-        const DicomValue& value = element.GetValue();
-        if (!value.IsNull() && 
-            !value.IsBinary())
-        {
-          database.SetMainDicomTag(resource, tag, element.GetValue().GetContent());
-        }
-      }
-    }
-
-
-    static void StoreIdentifiers(IDatabaseWrapper& database,
-                                 int64_t resource,
-                                 ResourceType level,
-                                 const DicomMap& map)
-    {
-      const DicomTag* tags;
-      size_t size;
-
-      LoadIdentifiers(tags, size, level);
-
-      for (size_t i = 0; i < size; i++)
-      {
-        // The identifiers tags are a subset of the main DICOM tags
-        assert(DicomMap::IsMainDicomTag(tags[i]));
-        
-        const DicomValue* value = map.TestAndGetValue(tags[i]);
-        if (value != NULL &&
-            !value->IsNull() &&
-            !value->IsBinary())
-        {
-          std::string s = NormalizeIdentifier(value->GetContent());
-          database.SetIdentifierTag(resource, tags[i], s);
-        }
-      }
-    }
-
-
-    void StoreMainDicomTags(IDatabaseWrapper& database,
-                            int64_t resource,
-                            ResourceType level,
-                            const DicomMap& dicomSummary)
-    {
-      // WARNING: The database should be locked with a transaction!
-
-      StoreIdentifiers(database, resource, level, dicomSummary);
-
-      DicomMap tags;
-
-      switch (level)
-      {
-        case ResourceType_Patient:
-          dicomSummary.ExtractPatientInformation(tags);
-          break;
-
-        case ResourceType_Study:
-          // Duplicate the patient tags at the study level (new in Orthanc 0.9.5 - db v6)
-          dicomSummary.ExtractPatientInformation(tags);
-          StoreMainDicomTagsInternal(database, resource, tags);
-
-          dicomSummary.ExtractStudyInformation(tags);
-          break;
-
-        case ResourceType_Series:
-          dicomSummary.ExtractSeriesInformation(tags);
-          break;
-
-        case ResourceType_Instance:
-          dicomSummary.ExtractInstanceInformation(tags);
-          break;
-
-        default:
-          throw OrthancException(ErrorCode_InternalError);
-      }
-
-      StoreMainDicomTagsInternal(database, resource, tags);
-    }
-
-
     bool FindOneChildInstance(int64_t& result,
                               IDatabaseWrapper& database,
                               int64_t resource,
@@ -335,7 +358,10 @@
           dicom.ExtractDicomSummary(dicomSummary);
 
           database.ClearMainDicomTags(resource);
-          StoreMainDicomTags(database, resource, level, dicomSummary);
+
+          ResourcesContent tags;
+          tags.AddResource(resource, level, dicomSummary);
+          database.SetResourcesContent(tags);
         }
         catch (OrthancException&)
         {
@@ -354,23 +380,23 @@
       switch (level)
       {
         case ResourceType_Patient:
-          tags = patientIdentifiers;
-          size = sizeof(patientIdentifiers) / sizeof(DicomTag);
+          tags = PATIENT_IDENTIFIERS;
+          size = sizeof(PATIENT_IDENTIFIERS) / sizeof(DicomTag);
           break;
 
         case ResourceType_Study:
-          tags = studyIdentifiers;
-          size = sizeof(studyIdentifiers) / sizeof(DicomTag);
+          tags = STUDY_IDENTIFIERS;
+          size = sizeof(STUDY_IDENTIFIERS) / sizeof(DicomTag);
           break;
 
         case ResourceType_Series:
-          tags = seriesIdentifiers;
-          size = sizeof(seriesIdentifiers) / sizeof(DicomTag);
+          tags = SERIES_IDENTIFIERS;
+          size = sizeof(SERIES_IDENTIFIERS) / sizeof(DicomTag);
           break;
 
         case ResourceType_Instance:
-          tags = instanceIdentifiers;
-          size = sizeof(instanceIdentifiers) / sizeof(DicomTag);
+          tags = INSTANCE_IDENTIFIERS;
+          size = sizeof(INSTANCE_IDENTIFIERS) / sizeof(DicomTag);
           break;
 
         default: