comparison Framework/Plugins/DatabaseBackendAdapterV4.cpp @ 376:59bba5fbb425 db-protobuf

cont
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 28 Mar 2023 19:15:33 +0200
parents 824d70ce85ff
children 02fe4606f5e4
comparison
equal deleted inserted replaced
375:824d70ce85ff 376:59bba5fbb425
44 namespace OrthancDatabases 44 namespace OrthancDatabases
45 { 45 {
46 static bool isBackendInUse_ = false; // Only for sanity checks 46 static bool isBackendInUse_ = false; // Only for sanity checks
47 47
48 48
49 static Orthanc::DatabasePluginMessages::ResourceType Convert(OrthancPluginResourceType resourceType)
50 {
51 switch (resourceType)
52 {
53 case OrthancPluginResourceType_Patient:
54 return Orthanc::DatabasePluginMessages::RESOURCE_PATIENT;
55
56 case OrthancPluginResourceType_Study:
57 return Orthanc::DatabasePluginMessages::RESOURCE_STUDY;
58
59 case OrthancPluginResourceType_Series:
60 return Orthanc::DatabasePluginMessages::RESOURCE_SERIES;
61
62 case OrthancPluginResourceType_Instance:
63 return Orthanc::DatabasePluginMessages::RESOURCE_INSTANCE;
64
65 default:
66 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
67 }
68 }
69
70
71 static OrthancPluginResourceType Convert(Orthanc::DatabasePluginMessages::ResourceType resourceType)
72 {
73 switch (resourceType)
74 {
75 case Orthanc::DatabasePluginMessages::RESOURCE_PATIENT:
76 return OrthancPluginResourceType_Patient;
77
78 case Orthanc::DatabasePluginMessages::RESOURCE_STUDY:
79 return OrthancPluginResourceType_Study;
80
81 case Orthanc::DatabasePluginMessages::RESOURCE_SERIES:
82 return OrthancPluginResourceType_Series;
83
84 case Orthanc::DatabasePluginMessages::RESOURCE_INSTANCE:
85 return OrthancPluginResourceType_Instance;
86
87 default:
88 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
89 }
90 }
91
92
49 class Output : public IDatabaseBackendOutput 93 class Output : public IDatabaseBackendOutput
50 { 94 {
51 private: 95 private:
52 Orthanc::DatabasePluginMessages::DeleteAttachment::Response* deleteAttachment_; 96 Orthanc::DatabasePluginMessages::DeleteAttachment::Response* deleteAttachment_;
97 Orthanc::DatabasePluginMessages::DeleteResource::Response* deleteResource_;
98 Orthanc::DatabasePluginMessages::GetChanges::Response* getChanges_;
99 Orthanc::DatabasePluginMessages::GetExportedResources::Response* getExportedResources_;
100 Orthanc::DatabasePluginMessages::GetLastChange::Response* getLastChange_;
53 101
54 void Clear() 102 void Clear()
55 { 103 {
56 deleteAttachment_ = NULL; 104 deleteAttachment_ = NULL;
105 deleteResource_ = NULL;
106 getChanges_ = NULL;
107 getExportedResources_ = NULL;
108 getLastChange_ = NULL;
57 } 109 }
58 110
59 public: 111 public:
60 Output(Orthanc::DatabasePluginMessages::DeleteAttachment::Response& deleteAttachment) 112 Output(Orthanc::DatabasePluginMessages::DeleteAttachment::Response& deleteAttachment)
61 { 113 {
62 Clear(); 114 Clear();
63 deleteAttachment_ = &deleteAttachment; 115 deleteAttachment_ = &deleteAttachment;
116 }
117
118 Output(Orthanc::DatabasePluginMessages::DeleteResource::Response& deleteResource)
119 {
120 Clear();
121 deleteResource_ = &deleteResource;
122 }
123
124 Output(Orthanc::DatabasePluginMessages::GetChanges::Response& getChanges)
125 {
126 Clear();
127 getChanges_ = &getChanges;
128 }
129
130 Output(Orthanc::DatabasePluginMessages::GetExportedResources::Response& getExportedResources)
131 {
132 Clear();
133 getExportedResources_ = &getExportedResources;
134 }
135
136 Output(Orthanc::DatabasePluginMessages::GetLastChange::Response& getLastChange)
137 {
138 Clear();
139 getLastChange_ = &getLastChange;
64 } 140 }
65 141
66 virtual void SignalDeletedAttachment(const std::string& uuid, 142 virtual void SignalDeletedAttachment(const std::string& uuid,
67 int32_t contentType, 143 int32_t contentType,
68 uint64_t uncompressedSize, 144 uint64_t uncompressedSize,
70 int32_t compressionType, 146 int32_t compressionType,
71 uint64_t compressedSize, 147 uint64_t compressedSize,
72 const std::string& compressedHash) ORTHANC_OVERRIDE 148 const std::string& compressedHash) ORTHANC_OVERRIDE
73 { 149 {
74 Orthanc::DatabasePluginMessages::FileInfo* attachment; 150 Orthanc::DatabasePluginMessages::FileInfo* attachment;
151
75 if (deleteAttachment_ != NULL) 152 if (deleteAttachment_ != NULL)
76 { 153 {
77 if (deleteAttachment_->has_deleted_attachment()) 154 if (deleteAttachment_->has_deleted_attachment())
78 { 155 {
79 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); 156 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
80 } 157 }
81 158
82 attachment = deleteAttachment_->mutable_deleted_attachment(); 159 attachment = deleteAttachment_->mutable_deleted_attachment();
160 }
161 else if (deleteResource_ != NULL)
162 {
163 attachment = deleteResource_->add_deleted_attachments();
83 } 164 }
84 else 165 else
85 { 166 {
86 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); 167 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
87 } 168 }
96 } 177 }
97 178
98 virtual void SignalDeletedResource(const std::string& publicId, 179 virtual void SignalDeletedResource(const std::string& publicId,
99 OrthancPluginResourceType resourceType) ORTHANC_OVERRIDE 180 OrthancPluginResourceType resourceType) ORTHANC_OVERRIDE
100 { 181 {
101 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); 182 if (deleteResource_ != NULL)
183 {
184 Orthanc::DatabasePluginMessages::DeleteResource_Response_Resource* resource = deleteResource_->add_deleted_resources();
185 resource->set_level(Convert(resourceType));
186 resource->set_public_id(publicId);
187 }
188 else
189 {
190 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
191 }
102 } 192 }
103 193
104 virtual void SignalRemainingAncestor(const std::string& ancestorId, 194 virtual void SignalRemainingAncestor(const std::string& ancestorId,
105 OrthancPluginResourceType ancestorType) ORTHANC_OVERRIDE 195 OrthancPluginResourceType ancestorType) ORTHANC_OVERRIDE
106 { 196 {
107 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); 197 if (deleteResource_ != NULL)
198 {
199 if (deleteResource_->is_remaining_ancestor())
200 {
201 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
202 }
203 else
204 {
205 deleteResource_->set_is_remaining_ancestor(true);
206 deleteResource_->mutable_remaining_ancestor()->set_level(Convert(ancestorType));
207 deleteResource_->mutable_remaining_ancestor()->set_public_id(ancestorId);
208 }
209 }
210 else
211 {
212 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
213 }
108 } 214 }
109 215
110 virtual void AnswerAttachment(const std::string& uuid, 216 virtual void AnswerAttachment(const std::string& uuid,
111 int32_t contentType, 217 int32_t contentType,
112 uint64_t uncompressedSize, 218 uint64_t uncompressedSize,
122 int32_t changeType, 228 int32_t changeType,
123 OrthancPluginResourceType resourceType, 229 OrthancPluginResourceType resourceType,
124 const std::string& publicId, 230 const std::string& publicId,
125 const std::string& date) ORTHANC_OVERRIDE 231 const std::string& date) ORTHANC_OVERRIDE
126 { 232 {
127 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); 233 Orthanc::DatabasePluginMessages::ServerIndexChange* change;
234
235 if (getChanges_ != NULL)
236 {
237 change = getChanges_->add_changes();
238 }
239 else if (getLastChange_ != NULL)
240 {
241 if (getLastChange_->exists())
242 {
243 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
244 }
245
246 getLastChange_->set_exists(true);
247 change = getLastChange_->mutable_change();
248 }
249 else
250 {
251 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
252 }
253
254 change->set_seq(seq);
255 change->set_change_type(changeType);
256 change->set_resource_type(Convert(resourceType));
257 change->set_public_id(publicId);
258 change->set_date(date);
128 } 259 }
129 260
130 virtual void AnswerDicomTag(uint16_t group, 261 virtual void AnswerDicomTag(uint16_t group,
131 uint16_t element, 262 uint16_t element,
132 const std::string& value) ORTHANC_OVERRIDE 263 const std::string& value) ORTHANC_OVERRIDE
142 const std::string& patientId, 273 const std::string& patientId,
143 const std::string& studyInstanceUid, 274 const std::string& studyInstanceUid,
144 const std::string& seriesInstanceUid, 275 const std::string& seriesInstanceUid,
145 const std::string& sopInstanceUid) ORTHANC_OVERRIDE 276 const std::string& sopInstanceUid) ORTHANC_OVERRIDE
146 { 277 {
147 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); 278 if (getExportedResources_ != NULL)
279 {
280 Orthanc::DatabasePluginMessages::ExportedResource* resource = getExportedResources_->add_resources();
281 resource->set_seq(seq);
282 resource->set_resource_type(Convert(resourceType));
283 resource->set_public_id(publicId);
284 resource->set_modality(modality);
285 resource->set_date(date);
286 resource->set_patient_id(patientId);
287 resource->set_study_instance_uid(studyInstanceUid);
288 resource->set_series_instance_uid(seriesInstanceUid);
289 resource->set_sop_instance_uid(sopInstanceUid);
290 }
291 else
292 {
293 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
294 }
148 } 295 }
149 296
150 virtual void AnswerMatchingResource(const std::string& resourceId) ORTHANC_OVERRIDE 297 virtual void AnswerMatchingResource(const std::string& resourceId) ORTHANC_OVERRIDE
151 { 298 {
152 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); 299 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
289 transaction.GetBackend().DeleteMetadata( 436 transaction.GetBackend().DeleteMetadata(
290 transaction.GetManager(), request.delete_metadata().id(), request.delete_metadata().type()); 437 transaction.GetManager(), request.delete_metadata().id(), request.delete_metadata().type());
291 break; 438 break;
292 } 439 }
293 440
441 case Orthanc::DatabasePluginMessages::OPERATION_DELETE_RESOURCE:
442 {
443 response.mutable_delete_resource()->set_is_remaining_ancestor(false);
444
445 Output output(*response.mutable_delete_resource());
446 transaction.GetBackend().DeleteResource(output, transaction.GetManager(), request.delete_resource().id());
447 break;
448 }
449
450 case Orthanc::DatabasePluginMessages::OPERATION_GET_ALL_METADATA:
451 {
452 typedef std::map<int32_t, std::string> Values;
453
454 Values values;
455 transaction.GetBackend().GetAllMetadata(values, transaction.GetManager(), request.get_all_metadata().id());
456
457 for (Values::const_iterator it = values.begin(); it != values.end(); ++it)
458 {
459 Orthanc::DatabasePluginMessages::GetAllMetadata_Response_Metadata* metadata = response.mutable_get_all_metadata()->add_metadata();
460 metadata->set_type(it->first);
461 metadata->set_value(it->second);
462 }
463
464 break;
465 }
466
467 case Orthanc::DatabasePluginMessages::OPERATION_GET_ALL_PUBLIC_IDS:
468 {
469 std::list<std::string> values;
470 transaction.GetBackend().GetAllPublicIds(values, transaction.GetManager(), Convert(request.get_all_public_ids().resource_type()));
471
472 for (std::list<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
473 {
474 response.mutable_get_all_public_ids()->add_ids(*it);
475 }
476
477 break;
478 }
479
480 case Orthanc::DatabasePluginMessages::OPERATION_GET_ALL_PUBLIC_IDS_WITH_LIMITS:
481 {
482 std::list<std::string> values;
483 transaction.GetBackend().GetAllPublicIds(values, transaction.GetManager(),
484 Convert(request.get_all_public_ids_with_limits().resource_type()),
485 request.get_all_public_ids_with_limits().since(),
486 request.get_all_public_ids_with_limits().limit());
487
488 for (std::list<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
489 {
490 response.mutable_get_all_public_ids_with_limits()->add_ids(*it);
491 }
492
493 break;
494 }
495
496 case Orthanc::DatabasePluginMessages::OPERATION_GET_CHANGES:
497 {
498 Output output(*response.mutable_get_changes());
499
500 bool done;
501 transaction.GetBackend().GetChanges(output, done, transaction.GetManager(),
502 request.get_changes().since(),
503 request.get_changes().limit());
504
505 response.mutable_get_changes()->set_done(done);
506 break;
507 }
508
509 case Orthanc::DatabasePluginMessages::OPERATION_GET_CHILDREN_INTERNAL_ID:
510 {
511 std::list<int64_t> values;
512 transaction.GetBackend().GetChildrenInternalId(values, transaction.GetManager(), request.get_children_internal_id().id());
513
514 for (std::list<int64_t>::const_iterator it = values.begin(); it != values.end(); ++it)
515 {
516 response.mutable_get_children_internal_id()->add_ids(*it);
517 }
518
519 break;
520 }
521
522 case Orthanc::DatabasePluginMessages::OPERATION_GET_CHILDREN_PUBLIC_ID:
523 {
524 std::list<std::string> values;
525 transaction.GetBackend().GetChildrenPublicId(values, transaction.GetManager(), request.get_children_public_id().id());
526
527 for (std::list<std::string>::const_iterator it = values.begin(); it != values.end(); ++it)
528 {
529 response.mutable_get_children_public_id()->add_ids(*it);
530 }
531
532 break;
533 }
534
535 case Orthanc::DatabasePluginMessages::OPERATION_GET_EXPORTED_RESOURCES:
536 {
537 Output output(*response.mutable_get_exported_resources());
538
539 bool done;
540 transaction.GetBackend().GetExportedResources(output, done, transaction.GetManager(),
541 request.get_exported_resources().since(),
542 request.get_exported_resources().limit());
543
544 response.mutable_get_exported_resources()->set_done(done);
545 break;
546 }
547
548 case Orthanc::DatabasePluginMessages::OPERATION_GET_LAST_CHANGE:
549 {
550 response.mutable_get_last_change()->set_exists(false);
551
552 Output output(*response.mutable_get_last_change());
553 transaction.GetBackend().GetLastChange(output, transaction.GetManager());
554 break;
555 }
556
294 default: 557 default:
295 LOG(ERROR) << "Not implemented transaction operation from protobuf: " << request.operation(); 558 LOG(ERROR) << "Not implemented transaction operation from protobuf: " << request.operation();
296 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); 559 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
297 } 560 }
298 } 561 }