comparison OrthancServer/ServerIndex.cpp @ 179:78e28d0098d9

refactoring
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 09 Nov 2012 11:30:16 +0100
parents 5739b4d10a4b
children 626777d01dc4
comparison
equal deleted inserted replaced
178:5739b4d10a4b 179:78e28d0098d9
40 40
41 #include "EmbeddedResources.h" 41 #include "EmbeddedResources.h"
42 #include "../Core/Toolbox.h" 42 #include "../Core/Toolbox.h"
43 #include "../Core/Uuid.h" 43 #include "../Core/Uuid.h"
44 #include "../Core/DicomFormat/DicomArray.h" 44 #include "../Core/DicomFormat/DicomArray.h"
45 #include "../Core/DicomFormat/DicomInstanceHasher.h"
46 #include "../Core/SQLite/Transaction.h" 45 #include "../Core/SQLite/Transaction.h"
47 #include "FromDcmtkBridge.h" 46 #include "FromDcmtkBridge.h"
48 47
49 #include <boost/lexical_cast.hpp> 48 #include <boost/lexical_cast.hpp>
50 #include <stdio.h> 49 #include <stdio.h>
205 return false; 204 return false;
206 } 205 }
207 } 206 }
208 207
209 bool ServerIndex::HasInstance(std::string& instanceUuid, 208 bool ServerIndex::HasInstance(std::string& instanceUuid,
210 const std::string& dicomInstance) 209 const DicomInstanceHasher& hasher)
211 { 210 {
212 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Instances WHERE dicomInstance=?"); 211 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Instances WHERE dicomInstance=?");
213 s.BindString(0, dicomInstance); 212 s.BindString(0, hasher.GetInstanceUid());
214 if (s.Step()) 213 if (s.Step())
215 { 214 {
216 instanceUuid = s.ColumnString(0); 215 instanceUuid = s.ColumnString(0);
217 return true; 216 return true;
218 } 217 }
232 s.Run(); 231 s.Run();
233 } 232 }
234 233
235 234
236 std::string ServerIndex::CreateInstance(const std::string& parentSeriesUuid, 235 std::string ServerIndex::CreateInstance(const std::string& parentSeriesUuid,
237 const std::string& dicomInstance, 236 const DicomInstanceHasher& hasher,
238 const DicomMap& dicomSummary, 237 const DicomMap& dicomSummary,
239 const std::string& fileUuid, 238 const std::string& fileUuid,
240 uint64_t fileSize, 239 uint64_t fileSize,
241 const std::string& jsonUuid, 240 const std::string& jsonUuid,
242 const std::string& distantAet) 241 const std::string& distantAet)
249 s2.Run(); 248 s2.Run();
250 249
251 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Instances VALUES(?, ?, ?, ?, ?, ?, ?, ?)"); 250 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Instances VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
252 s.BindString(0, instanceUuid); 251 s.BindString(0, instanceUuid);
253 s.BindString(1, parentSeriesUuid); 252 s.BindString(1, parentSeriesUuid);
254 s.BindString(2, dicomInstance); 253 s.BindString(2, hasher.GetInstanceUid());
255 s.BindString(3, fileUuid); 254 s.BindString(3, fileUuid);
256 s.BindInt64(4, fileSize); 255 s.BindInt64(4, fileSize);
257 s.BindString(5, jsonUuid); 256 s.BindString(5, jsonUuid);
258 s.BindString(6, distantAet); 257 s.BindString(6, distantAet);
259 258
285 s.BindString(0, uuid); 284 s.BindString(0, uuid);
286 s.Run(); 285 s.Run();
287 } 286 }
288 287
289 bool ServerIndex::HasSeries(std::string& seriesUuid, 288 bool ServerIndex::HasSeries(std::string& seriesUuid,
290 const std::string& dicomSeries) 289 const DicomInstanceHasher& hasher)
291 { 290 {
292 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Series WHERE dicomSeries=?"); 291 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Series WHERE dicomSeries=?");
293 s.BindString(0, dicomSeries); 292 s.BindString(0, hasher.GetSeriesUid());
294 if (s.Step()) 293 if (s.Step())
295 { 294 {
296 seriesUuid = s.ColumnString(0); 295 seriesUuid = s.ColumnString(0);
297 return true; 296 return true;
298 } 297 }
301 return false; 300 return false;
302 } 301 }
303 } 302 }
304 303
305 std::string ServerIndex::CreateSeries(const std::string& parentStudyUuid, 304 std::string ServerIndex::CreateSeries(const std::string& parentStudyUuid,
306 const std::string& dicomSeries, 305 const DicomInstanceHasher& hasher,
307 const DicomMap& dicomSummary) 306 const DicomMap& dicomSummary)
308 { 307 {
309 std::string seriesUuid = Toolbox::GenerateUuid(); 308 std::string seriesUuid = Toolbox::GenerateUuid();
310 309
311 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(?, ?)"); 310 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(?, ?)");
314 s2.Run(); 313 s2.Run();
315 314
316 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Series VALUES(?, ?, ?, ?)"); 315 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Series VALUES(?, ?, ?, ?)");
317 s.BindString(0, seriesUuid); 316 s.BindString(0, seriesUuid);
318 s.BindString(1, parentStudyUuid); 317 s.BindString(1, parentStudyUuid);
319 s.BindString(2, dicomSeries); 318 s.BindString(2, hasher.GetSeriesUid());
320 319
321 const DicomValue* expectedNumberOfInstances; 320 const DicomValue* expectedNumberOfInstances;
322 if (//(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_FRAMES)) != NULL || 321 if (//(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_FRAMES)) != NULL ||
323 (expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_SLICES)) != NULL || 322 (expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_NUMBER_OF_SLICES)) != NULL ||
324 //(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_CARDIAC_NUMBER_OF_IMAGES)) != NULL || 323 //(expectedNumberOfInstances = dicomSummary.TestAndGetValue(DICOM_TAG_CARDIAC_NUMBER_OF_IMAGES)) != NULL ||
341 340
342 return seriesUuid; 341 return seriesUuid;
343 } 342 }
344 343
345 bool ServerIndex::HasStudy(std::string& studyUuid, 344 bool ServerIndex::HasStudy(std::string& studyUuid,
346 const std::string& dicomStudy) 345 const DicomInstanceHasher& hasher)
347 { 346 {
348 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Studies WHERE dicomStudy=?"); 347 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Studies WHERE dicomStudy=?");
349 s.BindString(0, dicomStudy); 348 s.BindString(0, hasher.GetStudyUid());
350 if (s.Step()) 349 if (s.Step())
351 { 350 {
352 studyUuid = s.ColumnString(0); 351 studyUuid = s.ColumnString(0);
353 return true; 352 return true;
354 } 353 }
357 return false; 356 return false;
358 } 357 }
359 } 358 }
360 359
361 std::string ServerIndex::CreateStudy(const std::string& parentPatientUuid, 360 std::string ServerIndex::CreateStudy(const std::string& parentPatientUuid,
362 const std::string& dicomStudy, 361 const DicomInstanceHasher& hasher,
363 const DicomMap& dicomSummary) 362 const DicomMap& dicomSummary)
364 { 363 {
365 std::string studyUuid = Toolbox::GenerateUuid(); 364 std::string studyUuid = Toolbox::GenerateUuid();
366 365
367 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(?, ?)"); 366 SQLite::Statement s2(db_, SQLITE_FROM_HERE, "INSERT INTO Resources VALUES(?, ?)");
370 s2.Run(); 369 s2.Run();
371 370
372 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Studies VALUES(?, ?, ?)"); 371 SQLite::Statement s(db_, SQLITE_FROM_HERE, "INSERT INTO Studies VALUES(?, ?, ?)");
373 s.BindString(0, studyUuid); 372 s.BindString(0, studyUuid);
374 s.BindString(1, parentPatientUuid); 373 s.BindString(1, parentPatientUuid);
375 s.BindString(2, dicomStudy); 374 s.BindString(2, hasher.GetStudyUid());
376 s.Run(); 375 s.Run();
377 376
378 RecordChange("studies", studyUuid); 377 RecordChange("studies", studyUuid);
379 378
380 DicomMap dicom; 379 DicomMap dicom;
385 } 384 }
386 385
387 386
388 387
389 bool ServerIndex::HasPatient(std::string& patientUuid, 388 bool ServerIndex::HasPatient(std::string& patientUuid,
390 const std::string& dicomPatientId) 389 const DicomInstanceHasher& hasher)
391 { 390 {
392 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Patients WHERE dicomPatientId=?"); 391 SQLite::Statement s(db_, SQLITE_FROM_HERE, "SELECT uuid FROM Patients WHERE dicomPatientId=?");
393 s.BindString(0, dicomPatientId); 392 s.BindString(0,hasher.GetPatientId());
394 if (s.Step()) 393 if (s.Step())
395 { 394 {
396 patientUuid = s.ColumnString(0); 395 patientUuid = s.ColumnString(0);
397 return true; 396 return true;
398 } 397 }
400 { 399 {
401 return false; 400 return false;
402 } 401 }
403 } 402 }
404 403
405 std::string ServerIndex::CreatePatient(const std::string& patientId, 404 std::string ServerIndex::CreatePatient(const DicomInstanceHasher& hasher,
406 const DicomMap& dicomSummary) 405 const DicomMap& dicomSummary)
407 { 406 {
408 std::string patientUuid = Toolbox::GenerateUuid(); 407 std::string patientUuid = Toolbox::GenerateUuid();
409 std::string dicomPatientId = dicomSummary.GetValue(DICOM_TAG_PATIENT_ID).AsString(); 408 std::string dicomPatientId = dicomSummary.GetValue(DICOM_TAG_PATIENT_ID).AsString();
410 409
536 try 535 try
537 { 536 {
538 SQLite::Transaction t(db_); 537 SQLite::Transaction t(db_);
539 t.Begin(); 538 t.Begin();
540 539
541 if (HasInstance(instanceUuid, hasher.GetInstanceUid())) 540 if (HasInstance(instanceUuid, hasher))
542 { 541 {
543 return StoreStatus_AlreadyStored; 542 return StoreStatus_AlreadyStored;
544 // TODO: Check consistency? 543 // TODO: Check consistency?
545 } 544 }
546 545
547 std::string patientUuid; 546 std::string patientUuid;
548 if (HasPatient(patientUuid, hasher.GetPatientId())) 547 if (HasPatient(patientUuid, hasher))
549 { 548 {
550 // TODO: Check consistency? 549 // TODO: Check consistency?
551 } 550 }
552 else 551 else
553 { 552 {
554 patientUuid = CreatePatient(hasher.GetPatientId(), dicomSummary); 553 patientUuid = CreatePatient(hasher, dicomSummary);
555 } 554 }
556 555
557 std::string studyUuid; 556 std::string studyUuid;
558 if (HasStudy(studyUuid, hasher.GetStudyUid())) 557 if (HasStudy(studyUuid, hasher))
559 { 558 {
560 // TODO: Check consistency? 559 // TODO: Check consistency?
561 } 560 }
562 else 561 else
563 { 562 {
564 studyUuid = CreateStudy(patientUuid, hasher.GetStudyUid(), dicomSummary); 563 studyUuid = CreateStudy(patientUuid, hasher, dicomSummary);
565 } 564 }
566 565
567 std::string seriesUuid; 566 std::string seriesUuid;
568 if (HasSeries(seriesUuid, hasher.GetSeriesUid())) 567 if (HasSeries(seriesUuid, hasher))
569 { 568 {
570 // TODO: Check consistency? 569 // TODO: Check consistency?
571 } 570 }
572 else 571 else
573 { 572 {
574 seriesUuid = CreateSeries(studyUuid, hasher.GetSeriesUid(), dicomSummary); 573 seriesUuid = CreateSeries(studyUuid, hasher, dicomSummary);
575 } 574 }
576 575
577 instanceUuid = CreateInstance(seriesUuid, hasher.GetInstanceUid(), dicomSummary, fileUuid, 576 instanceUuid = CreateInstance(seriesUuid, hasher, dicomSummary, fileUuid,
578 uncompressedFileSize, jsonUuid, distantAet); 577 uncompressedFileSize, jsonUuid, distantAet);
579 578
580 t.Commit(); 579 t.Commit();
581 return StoreStatus_Success; 580 return StoreStatus_Success;
582 //t.Rollback(); 581 //t.Rollback();