comparison OrthancServer/ParsedDicomFile.cpp @ 1979:9e0f408db796

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 22 Apr 2016 08:47:43 +0200
parents fc16ee04e71b
children ebce5f456b8e
comparison
equal deleted inserted replaced
1978:0c9c4e36c2b9 1979:9e0f408db796
581 std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, GetEncoding())); 581 std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, GetEncoding()));
582 InsertInternal(*pimpl_->file_->getDataset(), element.release()); 582 InsertInternal(*pimpl_->file_->getDataset(), element.release());
583 } 583 }
584 584
585 585
586 static void ReplaceInternal(DcmDataset& dicom, 586 static bool IsReplaceAllowed(DcmDataset& dicom,
587 std::auto_ptr<DcmElement>& element, 587 const DcmTagKey& tag,
588 DicomReplaceMode mode) 588 DicomReplaceMode mode)
589 { 589 {
590 const DcmTagKey& tag = element->getTag(); 590 if (dicom.findAndDeleteElement(tag).good())
591 591 {
592 if (!dicom.findAndDeleteElement(tag).good()) 592 // This tag was existing, it has been deleted
593 { 593 return true;
594 // This field does not exist, act wrt. the specified "mode" 594 }
595 else
596 {
597 // This tag was absent, act wrt. the specified "mode"
595 switch (mode) 598 switch (mode)
596 { 599 {
597 case DicomReplaceMode_InsertIfAbsent: 600 case DicomReplaceMode_InsertIfAbsent:
598 break; 601 return true;
599 602
600 case DicomReplaceMode_ThrowIfAbsent: 603 case DicomReplaceMode_ThrowIfAbsent:
601 throw OrthancException(ErrorCode_InexistentItem); 604 throw OrthancException(ErrorCode_InexistentItem);
602 605
603 case DicomReplaceMode_IgnoreIfAbsent: 606 case DicomReplaceMode_IgnoreIfAbsent:
604 return; 607 return false;
605 } 608
606 } 609 default:
607 610 throw OrthancException(ErrorCode_ParameterOutOfRange);
608 // Either the tag was not existing, or the replace mode was set to 611 }
609 // "InsertIfAbsent" 612 }
610 InsertInternal(dicom, element.release());
611 } 613 }
612 614
613 615
614 void ParsedDicomFile::UpdateStorageUid(const DicomTag& tag, 616 void ParsedDicomFile::UpdateStorageUid(const DicomTag& tag,
615 const std::string& utf8Value, 617 const std::string& utf8Value,
668 { 670 {
669 InvalidateCache(); 671 InvalidateCache();
670 672
671 std::auto_ptr<DcmElement> element(FromDcmtkBridge::CreateElementForTag(tag)); 673 std::auto_ptr<DcmElement> element(FromDcmtkBridge::CreateElementForTag(tag));
672 FromDcmtkBridge::FillElementWithString(*element, tag, utf8Value, false, GetEncoding()); 674 FromDcmtkBridge::FillElementWithString(*element, tag, utf8Value, false, GetEncoding());
673 ReplaceInternal(*pimpl_->file_->getDataset(), element, mode); 675
674 UpdateStorageUid(tag, utf8Value, false); 676 DcmDataset& dicom = *pimpl_->file_->getDataset();
677 if (IsReplaceAllowed(dicom, element->getTag(), mode))
678 {
679 // Either the tag was previously existing, or the replace mode
680 // was set to "InsertIfAbsent"
681 InsertInternal(dicom, element.release());
682 UpdateStorageUid(tag, utf8Value, false);
683 }
675 } 684 }
676 685
677 686
678 void ParsedDicomFile::Replace(const DicomTag& tag, 687 void ParsedDicomFile::Replace(const DicomTag& tag,
679 const Json::Value& value, 688 const Json::Value& value,
681 DicomReplaceMode mode) 690 DicomReplaceMode mode)
682 { 691 {
683 InvalidateCache(); 692 InvalidateCache();
684 693
685 std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, GetEncoding())); 694 std::auto_ptr<DcmElement> element(FromDcmtkBridge::FromJson(tag, value, decodeDataUriScheme, GetEncoding()));
686 ReplaceInternal(*pimpl_->file_->getDataset(), element, mode); 695
687 696 DcmDataset& dicom = *pimpl_->file_->getDataset();
688 if (tag == DICOM_TAG_SOP_CLASS_UID || 697 if (IsReplaceAllowed(dicom, element->getTag(), mode))
689 tag == DICOM_TAG_SOP_INSTANCE_UID) 698 {
690 { 699 // Either the tag was previously existing, or the replace mode
691 if (value.type() != Json::stringValue) 700 // was set to "InsertIfAbsent"
692 { 701 InsertInternal(dicom, element.release());
693 throw OrthancException(ErrorCode_BadParameterType); 702
694 } 703 if (tag == DICOM_TAG_SOP_CLASS_UID ||
695 704 tag == DICOM_TAG_SOP_INSTANCE_UID)
696 UpdateStorageUid(tag, value.asString(), decodeDataUriScheme); 705 {
706 if (value.type() != Json::stringValue)
707 {
708 throw OrthancException(ErrorCode_BadParameterType);
709 }
710
711 UpdateStorageUid(tag, value.asString(), decodeDataUriScheme);
712 }
697 } 713 }
698 } 714 }
699 715
700 716
701 void ParsedDicomFile::Answer(RestApiOutput& output) 717 void ParsedDicomFile::Answer(RestApiOutput& output)