comparison OrthancServer/Sources/Database/FindResponse.cpp @ 5833:58c549b881ae find-refactoring-clean

merged find-refactoring -> find-refactoring-clean
author Alain Mazy <am@orthanc.team>
date Wed, 09 Oct 2024 11:01:11 +0200
parents f75596b224e0 f96abfe08946
children
comparison
equal deleted inserted replaced
5754:f75596b224e0 5833:58c549b881ae
481 return false; 481 return false;
482 } 482 }
483 } 483 }
484 484
485 485
486 const std::string& FindResponse::Resource::GetOneInstanceIdentifier() const 486 void FindResponse::Resource::SetOneInstanceMetadataAndAttachments(const std::string& instancePublicId,
487 { 487 const std::map<MetadataType, std::string>& metadata,
488 const std::set<std::string>& instances = GetChildrenInformation(ResourceType_Instance).GetIdentifiers(); 488 const std::map<FileContentType, FileInfo>& attachments)
489 489 {
490 if (instances.size() == 0) 490 if (hasOneInstanceMetadataAndAttachments_)
491 { 491 {
492 throw OrthancException(ErrorCode_BadSequenceOfCalls); // HasOneInstanceIdentifier() should have been called 492 throw OrthancException(ErrorCode_BadSequenceOfCalls);
493 } 493 }
494 else 494 else if (instancePublicId.empty())
495 { 495 {
496 return *instances.begin(); 496 throw OrthancException(ErrorCode_ParameterOutOfRange);
497 }
498 else
499 {
500 hasOneInstanceMetadataAndAttachments_ = true;
501 oneInstancePublicId_ = instancePublicId;
502 oneInstanceMetadata_ = metadata;
503 oneInstanceAttachments_ = attachments;
504 }
505 }
506
507
508 void FindResponse::Resource::SetOneInstancePublicId(const std::string& instancePublicId)
509 {
510 SetOneInstanceMetadataAndAttachments(instancePublicId, std::map<MetadataType, std::string>(),
511 std::map<FileContentType, FileInfo>());
512 }
513
514
515 void FindResponse::Resource::AddOneInstanceMetadata(MetadataType metadata,
516 const std::string& value)
517 {
518 if (hasOneInstanceMetadataAndAttachments_)
519 {
520 if (oneInstanceMetadata_.find(metadata) == oneInstanceMetadata_.end())
521 {
522 oneInstanceMetadata_[metadata] = value;
523 }
524 else
525 {
526 throw OrthancException(ErrorCode_BadSequenceOfCalls, "Metadata already exists");
527 }
528 }
529 else
530 {
531 throw OrthancException(ErrorCode_BadSequenceOfCalls);
532 }
533 }
534
535
536 void FindResponse::Resource::AddOneInstanceAttachment(const FileInfo& attachment)
537 {
538 if (hasOneInstanceMetadataAndAttachments_)
539 {
540 if (oneInstanceAttachments_.find(attachment.GetContentType()) == oneInstanceAttachments_.end())
541 {
542 oneInstanceAttachments_[attachment.GetContentType()] = attachment;
543 }
544 else
545 {
546 throw OrthancException(ErrorCode_BadSequenceOfCalls, "Attachment already exists");
547 }
548 }
549 else
550 {
551 throw OrthancException(ErrorCode_BadSequenceOfCalls);
552 }
553 }
554
555
556 const std::string& FindResponse::Resource::GetOneInstancePublicId() const
557 {
558 if (hasOneInstanceMetadataAndAttachments_)
559 {
560 return oneInstancePublicId_;
561 }
562 else
563 {
564 throw OrthancException(ErrorCode_BadSequenceOfCalls);
565 }
566 }
567
568
569 const std::map<MetadataType, std::string>& FindResponse::Resource::GetOneInstanceMetadata() const
570 {
571 if (hasOneInstanceMetadataAndAttachments_)
572 {
573 return oneInstanceMetadata_;
574 }
575 else
576 {
577 throw OrthancException(ErrorCode_BadSequenceOfCalls);
578 }
579 }
580
581
582 const std::map<FileContentType, FileInfo>& FindResponse::Resource::GetOneInstanceAttachments() const
583 {
584 if (hasOneInstanceMetadataAndAttachments_)
585 {
586 return oneInstanceAttachments_;
587 }
588 else
589 {
590 throw OrthancException(ErrorCode_BadSequenceOfCalls);
497 } 591 }
498 } 592 }
499 593
500 594
501 static void DebugDicomMap(Json::Value& target, 595 static void DebugDicomMap(Json::Value& target,
540 u.append(static_cast<Json::UInt64>(info.GetUncompressedSize())); 634 u.append(static_cast<Json::UInt64>(info.GetUncompressedSize()));
541 target[EnumerationToString(info.GetContentType())] = u; 635 target[EnumerationToString(info.GetContentType())] = u;
542 } 636 }
543 637
544 638
639 static void DebugAttachments(Json::Value& target,
640 const std::map<FileContentType, FileInfo>& attachments)
641 {
642 target = Json::objectValue;
643 for (std::map<FileContentType, FileInfo>::const_iterator it = attachments.begin();
644 it != attachments.end(); ++it)
645 {
646 if (it->first != it->second.GetContentType())
647 {
648 throw OrthancException(ErrorCode_DatabasePlugin);
649 }
650 else
651 {
652 DebugAddAttachment(target, it->second);
653 }
654 }
655 }
656
657
545 static void DebugSetOfStrings(Json::Value& target, 658 static void DebugSetOfStrings(Json::Value& target,
546 const std::set<std::string>& values) 659 const std::set<std::string>& values)
547 { 660 {
548 target = Json::arrayValue; 661 target = Json::arrayValue;
549 for (std::set<std::string>::const_iterator it = values.begin(); it != values.end(); ++it) 662 for (std::set<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
631 DebugSetOfStrings(target["Labels"], labels_); 744 DebugSetOfStrings(target["Labels"], labels_);
632 } 745 }
633 746
634 if (request.IsRetrieveAttachments()) 747 if (request.IsRetrieveAttachments())
635 { 748 {
636 Json::Value v = Json::objectValue; 749 DebugAttachments(target["Attachments"], attachments_);
637 for (std::map<FileContentType, FileInfo>::const_iterator it = attachments_.begin(); 750 }
638 it != attachments_.end(); ++it) 751
639 { 752 if (request.GetLevel() != ResourceType_Instance &&
640 if (it->first != it->second.GetContentType()) 753 request.IsRetrieveOneInstanceMetadataAndAttachments())
641 { 754 {
642 throw OrthancException(ErrorCode_DatabasePlugin); 755 DebugMetadata(target["OneInstance"]["Metadata"], GetOneInstanceMetadata());
643 } 756 DebugAttachments(target["OneInstance"]["Attachments"], GetOneInstanceAttachments());
644 else
645 {
646 DebugAddAttachment(v, it->second);
647 }
648 }
649 target["Attachments"] = v;
650 }
651
652 if (request.IsRetrieveOneInstanceIdentifier())
653 {
654 target["OneInstance"] = GetOneInstanceIdentifier();
655 } 757 }
656 } 758 }
657 759
658 760
659 FindResponse::~FindResponse() 761 FindResponse::~FindResponse()