Mercurial > hg > orthanc
comparison OrthancServer/ServerIndex.cpp @ 432:2b09d4ee86c6
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 15 May 2013 13:36:08 +0200 |
parents | 16b52fb8d034 |
children | aa50783f9550 |
comparison
equal
deleted
inserted
replaced
431:16b52fb8d034 | 432:2b09d4ee86c6 |
---|---|
305 | 305 |
306 try | 306 try |
307 { | 307 { |
308 Transaction t(*this); | 308 Transaction t(*this); |
309 | 309 |
310 int64_t patient, study, series, instance; | |
311 ResourceType type; | |
312 bool isNewSeries = false; | |
313 | |
314 // Do nothing if the instance already exists | 310 // Do nothing if the instance already exists |
315 if (db_->LookupResource(hasher.HashInstance(), patient, type)) | 311 { |
316 { | 312 ResourceType type; |
317 assert(type == ResourceType_Instance); | 313 int64_t tmp; |
318 return StoreStatus_AlreadyStored; | 314 if (db_->LookupResource(hasher.HashInstance(), tmp, type)) |
315 { | |
316 assert(type == ResourceType_Instance); | |
317 return StoreStatus_AlreadyStored; | |
318 } | |
319 } | 319 } |
320 | 320 |
321 // Ensure there is enough room in the storage for the new instance | 321 // Ensure there is enough room in the storage for the new instance |
322 uint64_t instanceSize = 0; | 322 uint64_t instanceSize = 0; |
323 for (Attachments::const_iterator it = attachments.begin(); | 323 for (Attachments::const_iterator it = attachments.begin(); |
327 } | 327 } |
328 | 328 |
329 Recycle(instanceSize, hasher.HashPatient()); | 329 Recycle(instanceSize, hasher.HashPatient()); |
330 | 330 |
331 // Create the instance | 331 // Create the instance |
332 instance = db_->CreateResource(hasher.HashInstance(), ResourceType_Instance); | 332 int64_t instance = db_->CreateResource(hasher.HashInstance(), ResourceType_Instance); |
333 | 333 |
334 DicomMap dicom; | 334 DicomMap dicom; |
335 dicomSummary.ExtractInstanceInformation(dicom); | 335 dicomSummary.ExtractInstanceInformation(dicom); |
336 db_->SetMainDicomTags(instance, dicom); | 336 db_->SetMainDicomTags(instance, dicom); |
337 | 337 |
338 // Create the patient/study/series/instance hierarchy | 338 // Detect up to which level the patient/study/series/instance |
339 if (!db_->LookupResource(hasher.HashSeries(), series, type)) | 339 // hierarchy must be created |
340 { | 340 int64_t patient = -1, study = -1, series = -1; |
341 // This is a new series | 341 bool isNewPatient = false; |
342 isNewSeries = true; | 342 bool isNewStudy = false; |
343 bool isNewSeries = false; | |
344 | |
345 { | |
346 ResourceType dummy; | |
347 | |
348 if (db_->LookupResource(hasher.HashSeries(), series, dummy)) | |
349 { | |
350 assert(dummy == ResourceType_Series); | |
351 // The patient, the study and the series already exist | |
352 | |
353 bool ok = (db_->LookupResource(hasher.HashPatient(), patient, dummy) && | |
354 db_->LookupResource(hasher.HashStudy(), study, dummy)); | |
355 assert(ok); | |
356 } | |
357 else if (db_->LookupResource(hasher.HashStudy(), study, dummy)) | |
358 { | |
359 assert(dummy == ResourceType_Study); | |
360 | |
361 // New series: The patient and the study already exist | |
362 isNewSeries = true; | |
363 | |
364 bool ok = db_->LookupResource(hasher.HashPatient(), patient, dummy); | |
365 assert(ok); | |
366 } | |
367 else if (db_->LookupResource(hasher.HashPatient(), patient, dummy)) | |
368 { | |
369 assert(dummy == ResourceType_Patient); | |
370 | |
371 // New study and series: The patient already exist | |
372 isNewStudy = true; | |
373 isNewSeries = true; | |
374 } | |
375 else | |
376 { | |
377 // New patient, study and series: Nothing exists | |
378 isNewPatient = true; | |
379 isNewStudy = true; | |
380 isNewSeries = true; | |
381 } | |
382 } | |
383 | |
384 // Create the series if needed | |
385 if (isNewSeries) | |
386 { | |
343 series = db_->CreateResource(hasher.HashSeries(), ResourceType_Series); | 387 series = db_->CreateResource(hasher.HashSeries(), ResourceType_Series); |
344 dicomSummary.ExtractSeriesInformation(dicom); | 388 dicomSummary.ExtractSeriesInformation(dicom); |
345 db_->SetMainDicomTags(series, dicom); | 389 db_->SetMainDicomTags(series, dicom); |
346 db_->AttachChild(series, instance); | 390 } |
347 | 391 |
348 if (!db_->LookupResource(hasher.HashStudy(), study, type)) | 392 // Create the study if needed |
349 { | 393 if (isNewStudy) |
350 // This is a new study | 394 { |
351 study = db_->CreateResource(hasher.HashStudy(), ResourceType_Study); | 395 study = db_->CreateResource(hasher.HashStudy(), ResourceType_Study); |
352 dicomSummary.ExtractStudyInformation(dicom); | 396 dicomSummary.ExtractStudyInformation(dicom); |
353 db_->SetMainDicomTags(study, dicom); | 397 db_->SetMainDicomTags(study, dicom); |
354 db_->AttachChild(study, series); | 398 } |
355 | 399 |
356 if (!db_->LookupResource(hasher.HashPatient(), patient, type)) | 400 // Create the patient if needed |
357 { | 401 if (isNewPatient) |
358 // This is a new patient | 402 { |
359 patient = db_->CreateResource(hasher.HashPatient(), ResourceType_Patient); | 403 patient = db_->CreateResource(hasher.HashPatient(), ResourceType_Patient); |
360 dicomSummary.ExtractPatientInformation(dicom); | 404 dicomSummary.ExtractPatientInformation(dicom); |
361 db_->SetMainDicomTags(patient, dicom); | 405 db_->SetMainDicomTags(patient, dicom); |
362 db_->AttachChild(patient, study); | 406 } |
363 } | 407 |
364 else | 408 // Create the parent-to-child links |
365 { | 409 db_->AttachChild(series, instance); |
366 assert(type == ResourceType_Patient); | 410 |
367 db_->AttachChild(patient, study); | 411 if (isNewSeries) |
368 } | 412 { |
369 } | 413 db_->AttachChild(study, series); |
370 else | 414 } |
371 { | 415 |
372 assert(type == ResourceType_Study); | 416 if (isNewStudy) |
373 db_->AttachChild(study, series); | 417 { |
374 } | 418 db_->AttachChild(patient, study); |
375 } | 419 } |
376 else | 420 |
377 { | 421 // Sanity checks |
378 assert(type == ResourceType_Series); | 422 assert(patient != -1); |
379 db_->AttachChild(series, instance); | 423 assert(study != -1); |
380 } | 424 assert(series != -1); |
425 assert(instance != -1); | |
381 | 426 |
382 // Attach the files to the newly created instance | 427 // Attach the files to the newly created instance |
383 for (Attachments::const_iterator it = attachments.begin(); | 428 for (Attachments::const_iterator it = attachments.begin(); |
384 it != attachments.end(); it++) | 429 it != attachments.end(); it++) |
385 { | 430 { |
386 db_->AddAttachment(instance, *it); | 431 db_->AddAttachment(instance, *it); |
387 } | 432 } |
388 | 433 |
389 // Attach the metadata | 434 // Attach the metadata |
390 db_->SetMetadata(instance, MetadataType_Instance_ReceptionDate, Toolbox::GetNowIsoString()); | 435 std::string now = Toolbox::GetNowIsoString(); |
436 db_->SetMetadata(instance, MetadataType_Instance_ReceptionDate, now); | |
437 db_->SetMetadata(series, MetadataType_LastUpdate, now); | |
438 db_->SetMetadata(study, MetadataType_LastUpdate, now); | |
439 db_->SetMetadata(patient, MetadataType_LastUpdate, now); | |
440 | |
391 db_->SetMetadata(instance, MetadataType_Instance_RemoteAet, remoteAet); | 441 db_->SetMetadata(instance, MetadataType_Instance_RemoteAet, remoteAet); |
392 | 442 |
393 const DicomValue* value; | 443 const DicomValue* value; |
394 if ((value = dicomSummary.TestAndGetValue(DICOM_TAG_INSTANCE_NUMBER)) != NULL || | 444 if ((value = dicomSummary.TestAndGetValue(DICOM_TAG_INSTANCE_NUMBER)) != NULL || |
395 (value = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGE_INDEX)) != NULL) | 445 (value = dicomSummary.TestAndGetValue(DICOM_TAG_IMAGE_INDEX)) != NULL) |