comparison OrthancServer/ServerToolbox.cpp @ 2121:7e8889bc95c6

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 07 Nov 2016 11:28:12 +0100
parents 4b02ec79728a
children 2ecc95a239f7
comparison
equal deleted inserted replaced
2120:4b02ec79728a 2121:7e8889bc95c6
44 44
45 namespace Orthanc 45 namespace Orthanc
46 { 46 {
47 namespace ServerToolbox 47 namespace ServerToolbox
48 { 48 {
49 static const DicomTag patientIdentifiers[] =
50 {
51 DICOM_TAG_PATIENT_ID,
52 DICOM_TAG_PATIENT_NAME,
53 DICOM_TAG_PATIENT_BIRTH_DATE
54 };
55
56 static const DicomTag studyIdentifiers[] =
57 {
58 DICOM_TAG_PATIENT_ID,
59 DICOM_TAG_PATIENT_NAME,
60 DICOM_TAG_PATIENT_BIRTH_DATE,
61 DICOM_TAG_STUDY_INSTANCE_UID,
62 DICOM_TAG_ACCESSION_NUMBER,
63 DICOM_TAG_STUDY_DESCRIPTION,
64 DICOM_TAG_STUDY_DATE
65 };
66
67 static const DicomTag seriesIdentifiers[] =
68 {
69 DICOM_TAG_SERIES_INSTANCE_UID
70 };
71
72 static const DicomTag instanceIdentifiers[] =
73 {
74 DICOM_TAG_SOP_INSTANCE_UID
75 };
76
77
49 void SimplifyTags(Json::Value& target, 78 void SimplifyTags(Json::Value& target,
50 const Json::Value& source, 79 const Json::Value& source,
51 DicomToJsonFormat format) 80 DicomToJsonFormat format)
52 { 81 {
53 assert(source.isObject()); 82 assert(source.isObject());
214 ResourceType level, 243 ResourceType level,
215 const DicomMap& dicomSummary) 244 const DicomMap& dicomSummary)
216 { 245 {
217 // WARNING: The database should be locked with a transaction! 246 // WARNING: The database should be locked with a transaction!
218 247
219 LookupIdentifierQuery::StoreIdentifiers(database, resource, level, dicomSummary); 248 StoreIdentifiers(database, resource, level, dicomSummary);
220 249
221 DicomMap tags; 250 DicomMap tags;
222 251
223 switch (level) 252 switch (level)
224 { 253 {
358 << " associated with instance " << database.GetPublicId(instance); 387 << " associated with instance " << database.GetPublicId(instance);
359 throw; 388 throw;
360 } 389 }
361 } 390 }
362 } 391 }
392
393
394 void LoadIdentifiers(const DicomTag*& tags,
395 size_t& size,
396 ResourceType level)
397 {
398 switch (level)
399 {
400 case ResourceType_Patient:
401 tags = patientIdentifiers;
402 size = sizeof(patientIdentifiers) / sizeof(DicomTag);
403 break;
404
405 case ResourceType_Study:
406 tags = studyIdentifiers;
407 size = sizeof(studyIdentifiers) / sizeof(DicomTag);
408 break;
409
410 case ResourceType_Series:
411 tags = seriesIdentifiers;
412 size = sizeof(seriesIdentifiers) / sizeof(DicomTag);
413 break;
414
415 case ResourceType_Instance:
416 tags = instanceIdentifiers;
417 size = sizeof(instanceIdentifiers) / sizeof(DicomTag);
418 break;
419
420 default:
421 throw OrthancException(ErrorCode_ParameterOutOfRange);
422 }
423 }
424
425
426 std::string NormalizeIdentifier(const std::string& value)
427 {
428 std::string t;
429 t.reserve(value.size());
430
431 for (size_t i = 0; i < value.size(); i++)
432 {
433 if (value[i] == '%' ||
434 value[i] == '_')
435 {
436 t.push_back(' '); // These characters might break wildcard queries in SQL
437 }
438 else if (isascii(value[i]) &&
439 !iscntrl(value[i]) &&
440 (!isspace(value[i]) || value[i] == ' '))
441 {
442 t.push_back(value[i]);
443 }
444 }
445
446 Toolbox::ToUpperCase(t);
447
448 return Toolbox::StripSpaces(t);
449 }
450
451
452 bool IsIdentifier(const DicomTag& tag,
453 ResourceType level)
454 {
455 const DicomTag* tags;
456 size_t size;
457
458 LoadIdentifiers(tags, size, level);
459
460 for (size_t i = 0; i < size; i++)
461 {
462 if (tag == tags[i])
463 {
464 return true;
465 }
466 }
467
468 return false;
469 }
470
471
472 void StoreIdentifiers(IDatabaseWrapper& database,
473 int64_t resource,
474 ResourceType level,
475 const DicomMap& map)
476 {
477 const DicomTag* tags;
478 size_t size;
479
480 LoadIdentifiers(tags, size, level);
481
482 for (size_t i = 0; i < size; i++)
483 {
484 const DicomValue* value = map.TestAndGetValue(tags[i]);
485 if (value != NULL &&
486 !value->IsNull() &&
487 !value->IsBinary())
488 {
489 std::string s = NormalizeIdentifier(value->GetContent());
490 database.SetIdentifierTag(resource, tags[i], s);
491 }
492 }
493 }
363 } 494 }
364 } 495 }