comparison OrthancServer/Sources/Database/StatelessDatabaseOperations.cpp @ 5221:d0f7c742d397 db-protobuf

started implementation of labels
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 03 Apr 2023 20:53:14 +0200
parents df39c7583a49
children 3a61fd50f804
comparison
equal deleted inserted replaced
5220:df39c7583a49 5221:d0f7c742d397
935 } 935 }
936 936
937 currentInternalId = currentParentId; 937 currentInternalId = currentParentId;
938 } 938 }
939 } 939 }
940 }
941
942 if (expandFlags & ExpandResourceDbFlags_IncludeLabels)
943 {
944 transaction.ListLabels(target.labels_, internalId);
940 } 945 }
941 946
942 std::string tmp; 947 std::string tmp;
943 948
944 if (LookupStringMetadata(tmp, target.metadata_, MetadataType_AnonymizedFrom)) 949 if (LookupStringMetadata(tmp, target.metadata_, MetadataType_AnonymizedFrom))
3517 Operations operations(newRevision, attachment, publicId, maximumStorageSize, maximumPatients, 3522 Operations operations(newRevision, attachment, publicId, maximumStorageSize, maximumPatients,
3518 hasOldRevision, oldRevision, oldMD5); 3523 hasOldRevision, oldRevision, oldMD5);
3519 Apply(operations); 3524 Apply(operations);
3520 return operations.GetStatus(); 3525 return operations.GetStatus();
3521 } 3526 }
3527
3528
3529 void StatelessDatabaseOperations::ListLabels(std::set<std::string>& target,
3530 const std::string& publicId,
3531 ResourceType level)
3532 {
3533 class Operations : public ReadOnlyOperationsT3<std::set<std::string>&, const std::string&, ResourceType>
3534 {
3535 public:
3536 virtual void ApplyTuple(ReadOnlyTransaction& transaction,
3537 const Tuple& tuple) ORTHANC_OVERRIDE
3538 {
3539 ResourceType type;
3540 int64_t id;
3541 if (!transaction.LookupResource(id, type, tuple.get<1>()) ||
3542 tuple.get<2>() != type)
3543 {
3544 throw OrthancException(ErrorCode_UnknownResource);
3545 }
3546 else
3547 {
3548 transaction.ListLabels(tuple.get<0>(), id);
3549 }
3550 }
3551 };
3552
3553 Operations operations;
3554 operations.Apply(*this, target, publicId, level);
3555 }
3556
3557
3558 void StatelessDatabaseOperations::ModifyLabel(const std::string& publicId,
3559 ResourceType level,
3560 const std::string& label,
3561 LabelOperation operation)
3562 {
3563 class Operations : public IReadWriteOperations
3564 {
3565 private:
3566 const std::string& publicId_;
3567 ResourceType level_;
3568 const std::string& label_;
3569 LabelOperation operation_;
3570
3571 public:
3572 Operations(const std::string& publicId,
3573 ResourceType level,
3574 const std::string& label,
3575 LabelOperation operation) :
3576 publicId_(publicId),
3577 level_(level),
3578 label_(label),
3579 operation_(operation)
3580 {
3581 }
3582
3583 virtual void Apply(ReadWriteTransaction& transaction) ORTHANC_OVERRIDE
3584 {
3585 ResourceType type;
3586 int64_t id;
3587 if (!transaction.LookupResource(id, type, publicId_) ||
3588 level_ != type)
3589 {
3590 throw OrthancException(ErrorCode_UnknownResource);
3591 }
3592 else
3593 {
3594 switch (operation_)
3595 {
3596 case LabelOperation_Add:
3597 transaction.AddLabel(id, label_);
3598 break;
3599
3600 case LabelOperation_Remove:
3601 transaction.RemoveLabel(id, label_);
3602 break;
3603
3604 default:
3605 throw OrthancException(ErrorCode_ParameterOutOfRange);
3606 }
3607 }
3608 }
3609 };
3610
3611 if (!Toolbox::IsAsciiString(label))
3612 {
3613 throw OrthancException(ErrorCode_ParameterOutOfRange, "A label must only contain ASCII characters");
3614 }
3615
3616 Operations operations(publicId, level, label, operation);
3617 Apply(operations);
3618 }
3522 } 3619 }