changeset 1979:9e0f408db796

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 22 Apr 2016 08:47:43 +0200
parents 0c9c4e36c2b9
children ebce5f456b8e
files OrthancServer/ParsedDicomFile.cpp
diffstat 1 files changed, 38 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/ParsedDicomFile.cpp	Fri Apr 15 21:42:10 2016 +0200
+++ b/OrthancServer/ParsedDicomFile.cpp	Fri Apr 22 08:47:43 2016 +0200
@@ -583,31 +583,33 @@
   }
 
 
-  static void ReplaceInternal(DcmDataset& dicom,
-                              std::auto_ptr<DcmElement>& element,
-                              DicomReplaceMode mode)
+  static bool IsReplaceAllowed(DcmDataset& dicom,
+                               const DcmTagKey& tag,
+                               DicomReplaceMode mode)
   {
-    const DcmTagKey& tag = element->getTag();
-
-    if (!dicom.findAndDeleteElement(tag).good())
+    if (dicom.findAndDeleteElement(tag).good())
     {
-      // This field does not exist, act wrt. the specified "mode"
+      // This tag was existing, it has been deleted
+      return true;
+    }
+    else
+    {
+      // This tag was absent, act wrt. the specified "mode"
       switch (mode)
       {
         case DicomReplaceMode_InsertIfAbsent:
-          break;
+          return true;
 
         case DicomReplaceMode_ThrowIfAbsent:
           throw OrthancException(ErrorCode_InexistentItem);
 
         case DicomReplaceMode_IgnoreIfAbsent:
-          return;
+          return false;
+
+        default:
+          throw OrthancException(ErrorCode_ParameterOutOfRange);
       }
     }
-
-    // Either the tag was not existing, or the replace mode was set to
-    // "InsertIfAbsent"
-    InsertInternal(dicom, element.release());
   }
 
 
@@ -670,8 +672,15 @@
 
     std::auto_ptr<DcmElement> element(FromDcmtkBridge::CreateElementForTag(tag));
     FromDcmtkBridge::FillElementWithString(*element, tag, utf8Value, false, GetEncoding());
-    ReplaceInternal(*pimpl_->file_->getDataset(), element, mode);
-    UpdateStorageUid(tag, utf8Value, false);
+
+    DcmDataset& dicom = *pimpl_->file_->getDataset();
+    if (IsReplaceAllowed(dicom, element->getTag(), mode))
+    {
+      // Either the tag was previously existing, or the replace mode
+      // was set to "InsertIfAbsent"
+      InsertInternal(dicom, element.release());
+      UpdateStorageUid(tag, utf8Value, false);
+    }
   }
 
     
@@ -683,17 +692,24 @@
     InvalidateCache();
 
     std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, GetEncoding()));
-    ReplaceInternal(*pimpl_->file_->getDataset(), element, mode);
 
-    if (tag == DICOM_TAG_SOP_CLASS_UID ||
-        tag == DICOM_TAG_SOP_INSTANCE_UID)
+    DcmDataset& dicom = *pimpl_->file_->getDataset();
+    if (IsReplaceAllowed(dicom, element->getTag(), mode))
     {
-      if (value.type() != Json::stringValue)
+      // Either the tag was previously existing, or the replace mode
+      // was set to "InsertIfAbsent"
+      InsertInternal(dicom, element.release());
+
+      if (tag == DICOM_TAG_SOP_CLASS_UID ||
+          tag == DICOM_TAG_SOP_INSTANCE_UID)
       {
-        throw OrthancException(ErrorCode_BadParameterType);
+        if (value.type() != Json::stringValue)
+        {
+          throw OrthancException(ErrorCode_BadParameterType);
+        }
+
+        UpdateStorageUid(tag, value.asString(), decodeDataUriScheme);
       }
-
-      UpdateStorageUid(tag, value.asString(), decodeDataUriScheme);
     }
   }