ctkDICOMDatabase.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. #include <stdexcept>
  15. // Qt includes
  16. #include <QSqlQuery>
  17. #include <QSqlRecord>
  18. #include <QSqlError>
  19. #include <QVariant>
  20. #include <QDate>
  21. #include <QStringList>
  22. #include <QSet>
  23. #include <QFile>
  24. #include <QDirIterator>
  25. #include <QFileInfo>
  26. #include <QDebug>
  27. #include <QFileSystemWatcher>
  28. #include <QMutexLocker>
  29. // ctkDICOM includes
  30. #include "ctkDICOMDatabase.h"
  31. #include "ctkDICOMAbstractThumbnailGenerator.h"
  32. #include "ctkDICOMDataset.h"
  33. #include "ctkLogger.h"
  34. // DCMTK includes
  35. #include <dcmtk/dcmdata/dcfilefo.h>
  36. #include <dcmtk/dcmdata/dcfilefo.h>
  37. #include <dcmtk/dcmdata/dcdeftag.h>
  38. #include <dcmtk/dcmdata/dcdatset.h>
  39. #include <dcmtk/ofstd/ofcond.h>
  40. #include <dcmtk/ofstd/ofstring.h>
  41. #include <dcmtk/ofstd/ofstd.h> /* for class OFStandard */
  42. #include <dcmtk/dcmdata/dcddirif.h> /* for class DicomDirInterface */
  43. #include <dcmimage.h>
  44. #include <dcmtk/dcmjpeg/djdecode.h> /* for dcmjpeg decoders */
  45. #include <dcmtk/dcmjpeg/djencode.h> /* for dcmjpeg encoders */
  46. #include <dcmtk/dcmdata/dcrledrg.h> /* for DcmRLEDecoderRegistration */
  47. #include <dcmtk/dcmdata/dcrleerg.h> /* for DcmRLEEncoderRegistration */
  48. //------------------------------------------------------------------------------
  49. static ctkLogger logger("org.commontk.dicom.DICOMDatabase" );
  50. //------------------------------------------------------------------------------
  51. //------------------------------------------------------------------------------
  52. class ctkDICOMDatabasePrivate
  53. {
  54. Q_DECLARE_PUBLIC(ctkDICOMDatabase);
  55. protected:
  56. ctkDICOMDatabase* const q_ptr;
  57. public:
  58. ctkDICOMDatabasePrivate(ctkDICOMDatabase&);
  59. ~ctkDICOMDatabasePrivate();
  60. void init(QString databaseFile);
  61. void registerCompressionLibraries();
  62. bool executeScript(const QString script);
  63. ///
  64. /// \brief runs a query and prints debug output of status
  65. ///
  66. bool loggedExec(QSqlQuery& query);
  67. bool loggedExec(QSqlQuery& query, const QString& queryString);
  68. // dataset must be set always
  69. // filePath has to be set if this is an import of an actual file
  70. void insert ( const ctkDICOMDataset& ctkDataset, const QString& filePath, bool storeFile = true, bool generateThumbnail = true);
  71. /// Name of the database file (i.e. for SQLITE the sqlite file)
  72. QString DatabaseFileName;
  73. QString LastError;
  74. QSqlDatabase Database;
  75. QMap<QString, QString> LoadedHeader;
  76. ctkDICOMAbstractThumbnailGenerator* thumbnailGenerator;
  77. /// these are for optimizing the import of image sequences
  78. /// since most information are identical for all slices
  79. QString LastPatientID;
  80. QString LastPatientsName;
  81. QString LastPatientsBirthDate;
  82. QString LastStudyInstanceUID;
  83. QString LastSeriesInstanceUID;
  84. int LastPatientUID;
  85. /// parallel inserts are not allowed (yet)
  86. QMutex insertMutex;
  87. int insertPatient(const ctkDICOMDataset& ctkDataset);
  88. void insertStudy(const ctkDICOMDataset& ctkDataset, int dbPatientID);
  89. void insertSeries( const ctkDICOMDataset& ctkDataset, QString studyInstanceUID);
  90. };
  91. //------------------------------------------------------------------------------
  92. // ctkDICOMDatabasePrivate methods
  93. //------------------------------------------------------------------------------
  94. ctkDICOMDatabasePrivate::ctkDICOMDatabasePrivate(ctkDICOMDatabase& o): q_ptr(&o)
  95. {
  96. this->thumbnailGenerator = NULL;
  97. this->LastPatientUID = -1;
  98. }
  99. //------------------------------------------------------------------------------
  100. void ctkDICOMDatabasePrivate::init(QString databaseFilename)
  101. {
  102. Q_Q(ctkDICOMDatabase);
  103. q->openDatabase(databaseFilename);
  104. }
  105. //------------------------------------------------------------------------------
  106. void ctkDICOMDatabasePrivate::registerCompressionLibraries(){
  107. logger.debug("Register compression libraries");
  108. // Register the JPEG libraries in case we need them
  109. // (registration only happens once, so it's okay to call repeatedly)
  110. // register global JPEG decompression codecs
  111. DJDecoderRegistration::registerCodecs();
  112. // register global JPEG compression codecs
  113. DJEncoderRegistration::registerCodecs();
  114. // register RLE compression codec
  115. DcmRLEEncoderRegistration::registerCodecs();
  116. // register RLE decompression codec
  117. DcmRLEDecoderRegistration::registerCodecs();
  118. }
  119. //------------------------------------------------------------------------------
  120. ctkDICOMDatabasePrivate::~ctkDICOMDatabasePrivate()
  121. {
  122. }
  123. //------------------------------------------------------------------------------
  124. bool ctkDICOMDatabasePrivate::loggedExec(QSqlQuery& query)
  125. {
  126. return (loggedExec(query, QString("")));
  127. }
  128. //------------------------------------------------------------------------------
  129. bool ctkDICOMDatabasePrivate::loggedExec(QSqlQuery& query, const QString& queryString)
  130. {
  131. bool success;
  132. if (queryString.compare(""))
  133. {
  134. success = query.exec(queryString);
  135. }
  136. else
  137. {
  138. success = query.exec();
  139. }
  140. if (!success)
  141. {
  142. QSqlError sqlError = query.lastError();
  143. logger.debug( "SQL failed\n Bad SQL: " + query.lastQuery());
  144. logger.debug( "Error text: " + sqlError.text());
  145. }
  146. else
  147. {
  148. logger.debug( "SQL worked!\n SQL: " + query.lastQuery());
  149. }
  150. return (success);
  151. }
  152. //------------------------------------------------------------------------------
  153. void ctkDICOMDatabase::openDatabase(const QString databaseFile, const QString& connectionName )
  154. {
  155. Q_D(ctkDICOMDatabase);
  156. d->DatabaseFileName = databaseFile;
  157. d->Database = QSqlDatabase::addDatabase("QSQLITE", connectionName);
  158. d->Database.setDatabaseName(databaseFile);
  159. if ( ! (d->Database.open()) )
  160. {
  161. d->LastError = d->Database.lastError().text();
  162. return;
  163. }
  164. if ( d->Database.tables().empty() )
  165. {
  166. if (!initializeDatabase())
  167. {
  168. d->LastError = QString("Unable to initialize DICOM database!");
  169. return;
  170. }
  171. }
  172. if (!isInMemory())
  173. {
  174. QFileSystemWatcher* watcher = new QFileSystemWatcher(QStringList(databaseFile),this);
  175. connect(watcher, SIGNAL(fileChanged(QString)),this, SIGNAL (databaseChanged()) );
  176. }
  177. }
  178. //------------------------------------------------------------------------------
  179. // ctkDICOMDatabase methods
  180. //------------------------------------------------------------------------------
  181. ctkDICOMDatabase::ctkDICOMDatabase(QString databaseFile)
  182. : d_ptr(new ctkDICOMDatabasePrivate(*this))
  183. {
  184. Q_D(ctkDICOMDatabase);
  185. d->registerCompressionLibraries();
  186. d->init(databaseFile);
  187. }
  188. ctkDICOMDatabase::ctkDICOMDatabase(QObject* parent)
  189. : d_ptr(new ctkDICOMDatabasePrivate(*this))
  190. {
  191. Q_UNUSED(parent);
  192. Q_D(ctkDICOMDatabase);
  193. d->registerCompressionLibraries();
  194. }
  195. //------------------------------------------------------------------------------
  196. ctkDICOMDatabase::~ctkDICOMDatabase()
  197. {
  198. }
  199. //----------------------------------------------------------------------------
  200. //------------------------------------------------------------------------------
  201. const QString ctkDICOMDatabase::lastError() const {
  202. Q_D(const ctkDICOMDatabase);
  203. return d->LastError;
  204. }
  205. //------------------------------------------------------------------------------
  206. const QString ctkDICOMDatabase::databaseFilename() const {
  207. Q_D(const ctkDICOMDatabase);
  208. return d->DatabaseFileName;
  209. }
  210. //------------------------------------------------------------------------------
  211. const QString ctkDICOMDatabase::databaseDirectory() const {
  212. QString databaseFile = databaseFilename();
  213. if (!QFileInfo(databaseFile).isAbsolute())
  214. {
  215. databaseFile.prepend(QDir::currentPath() + "/");
  216. }
  217. return QFileInfo ( databaseFile ).absoluteDir().path();
  218. }
  219. //------------------------------------------------------------------------------
  220. const QSqlDatabase& ctkDICOMDatabase::database() const {
  221. Q_D(const ctkDICOMDatabase);
  222. return d->Database;
  223. }
  224. //------------------------------------------------------------------------------
  225. void ctkDICOMDatabase::setThumbnailGenerator(ctkDICOMAbstractThumbnailGenerator *generator){
  226. Q_D(ctkDICOMDatabase);
  227. d->thumbnailGenerator = generator;
  228. }
  229. //------------------------------------------------------------------------------
  230. ctkDICOMAbstractThumbnailGenerator* ctkDICOMDatabase::thumbnailGenerator(){
  231. Q_D(const ctkDICOMDatabase);
  232. return d->thumbnailGenerator;
  233. }
  234. //------------------------------------------------------------------------------
  235. bool ctkDICOMDatabasePrivate::executeScript(const QString script) {
  236. QFile scriptFile(script);
  237. scriptFile.open(QIODevice::ReadOnly);
  238. if ( !scriptFile.isOpen() )
  239. {
  240. qDebug() << "Script file " << script << " could not be opened!\n";
  241. return false;
  242. }
  243. QString sqlCommands( QTextStream(&scriptFile).readAll() );
  244. sqlCommands.replace( '\n', ' ' );
  245. sqlCommands.remove( '\r' );
  246. sqlCommands.replace("; ", ";\n");
  247. QStringList sqlCommandsLines = sqlCommands.split('\n');
  248. QSqlQuery query(Database);
  249. for (QStringList::iterator it = sqlCommandsLines.begin(); it != sqlCommandsLines.end()-1; ++it)
  250. {
  251. if (! (*it).startsWith("--") )
  252. {
  253. qDebug() << *it << "\n";
  254. query.exec(*it);
  255. if (query.lastError().type())
  256. {
  257. qDebug() << "There was an error during execution of the statement: " << (*it);
  258. qDebug() << "Error message: " << query.lastError().text();
  259. return false;
  260. }
  261. }
  262. }
  263. return true;
  264. }
  265. //------------------------------------------------------------------------------
  266. bool ctkDICOMDatabase::initializeDatabase(const char* sqlFileName)
  267. {
  268. Q_D(ctkDICOMDatabase);
  269. return d->executeScript(sqlFileName);
  270. }
  271. //------------------------------------------------------------------------------
  272. void ctkDICOMDatabase::closeDatabase()
  273. {
  274. Q_D(ctkDICOMDatabase);
  275. d->Database.close();
  276. }
  277. //------------------------------------------------------------------------------
  278. QStringList ctkDICOMDatabase::patients()
  279. {
  280. Q_D(ctkDICOMDatabase);
  281. QSqlQuery query(d->Database);
  282. query.prepare ( "SELECT UID FROM Patients" );
  283. query.exec();
  284. QStringList result;
  285. while (query.next())
  286. {
  287. result << query.value(0).toString();
  288. }
  289. return( result );
  290. }
  291. //------------------------------------------------------------------------------
  292. QStringList ctkDICOMDatabase::studiesForPatient(QString dbPatientID)
  293. {
  294. Q_D(ctkDICOMDatabase);
  295. QSqlQuery query(d->Database);
  296. query.prepare ( "SELECT StudyInstanceUID FROM Studies WHERE PatientsUID = ?" );
  297. query.bindValue ( 0, dbPatientID );
  298. query.exec();
  299. QStringList result;
  300. while (query.next())
  301. {
  302. result << query.value(0).toString();
  303. }
  304. return( result );
  305. }
  306. //------------------------------------------------------------------------------
  307. QStringList ctkDICOMDatabase::seriesForStudy(QString studyUID)
  308. {
  309. Q_D(ctkDICOMDatabase);
  310. QSqlQuery query(d->Database);
  311. query.prepare ( "SELECT SeriesInstanceUID FROM Series WHERE StudyInstanceUID=?");
  312. query.bindValue ( 0, studyUID );
  313. query.exec();
  314. QStringList result;
  315. while (query.next())
  316. {
  317. result << query.value(0).toString();
  318. }
  319. return( result );
  320. }
  321. //------------------------------------------------------------------------------
  322. QStringList ctkDICOMDatabase::filesForSeries(QString seriesUID)
  323. {
  324. Q_D(ctkDICOMDatabase);
  325. QSqlQuery query(d->Database);
  326. query.prepare ( "SELECT Filename FROM Images WHERE SeriesInstanceUID=?");
  327. query.bindValue ( 0, seriesUID );
  328. query.exec();
  329. QStringList result;
  330. while (query.next())
  331. {
  332. result << query.value(0).toString();
  333. }
  334. return( result );
  335. }
  336. //------------------------------------------------------------------------------
  337. QString ctkDICOMDatabase::fileForInstance(QString sopInstanceUID)
  338. {
  339. Q_D(ctkDICOMDatabase);
  340. QSqlQuery query(d->Database);
  341. query.prepare ( "SELECT Filename FROM Images WHERE SOPInstanceUID=?");
  342. query.bindValue ( 0, sopInstanceUID );
  343. query.exec();
  344. QString result;
  345. if (query.next())
  346. {
  347. result = query.value(0).toString();
  348. }
  349. return( result );
  350. }
  351. //------------------------------------------------------------------------------
  352. QStringList ctkDICOMDatabase::allFiles()
  353. {
  354. Q_D(ctkDICOMDatabase);
  355. /// get all filenames from the database
  356. QSqlQuery allFilesQuery(d->Database);
  357. QStringList allFileNames;
  358. allFilesQuery.prepare("SELECT Filename from Images;");
  359. allFilesQuery.exec();
  360. while (allFilesQuery.next())
  361. {
  362. allFileNames << allFilesQuery.value(0).toString();
  363. }
  364. return allFileNames;
  365. }
  366. //------------------------------------------------------------------------------
  367. void ctkDICOMDatabase::loadInstanceHeader (QString sopInstanceUID)
  368. {
  369. Q_D(ctkDICOMDatabase);
  370. QSqlQuery query(d->Database);
  371. query.prepare ( "SELECT Filename FROM Images WHERE SOPInstanceUID=?");
  372. query.bindValue ( 0, sopInstanceUID );
  373. query.exec();
  374. if (query.next())
  375. {
  376. QString fileName = query.value(0).toString();
  377. this->loadFileHeader(fileName);
  378. }
  379. return;
  380. }
  381. //------------------------------------------------------------------------------
  382. void ctkDICOMDatabase::loadFileHeader (QString fileName)
  383. {
  384. Q_D(ctkDICOMDatabase);
  385. d->LoadedHeader.clear();
  386. DcmFileFormat fileFormat;
  387. OFCondition status = fileFormat.loadFile(fileName.toLatin1().data());
  388. if (status.good())
  389. {
  390. DcmDataset *dataset = fileFormat.getDataset();
  391. DcmStack stack;
  392. while (dataset->nextObject(stack, true) == EC_Normal)
  393. {
  394. DcmObject *dO = stack.top();
  395. QString tag = QString("%1,%2").arg(
  396. dO->getGTag(),4,16,QLatin1Char('0')).arg(
  397. dO->getETag(),4,16,QLatin1Char('0'));
  398. std::ostringstream s;
  399. dO->print(s);
  400. d->LoadedHeader[tag] = QString(s.str().c_str());
  401. }
  402. }
  403. return;
  404. }
  405. //------------------------------------------------------------------------------
  406. QStringList ctkDICOMDatabase::headerKeys ()
  407. {
  408. Q_D(ctkDICOMDatabase);
  409. return (d->LoadedHeader.keys());
  410. }
  411. //------------------------------------------------------------------------------
  412. QString ctkDICOMDatabase::headerValue (QString key)
  413. {
  414. Q_D(ctkDICOMDatabase);
  415. return (d->LoadedHeader[key]);
  416. }
  417. //------------------------------------------------------------------------------
  418. QString ctkDICOMDatabase::instanceValue(QString sopInstanceUID, QString tag)
  419. {
  420. unsigned short group, element;
  421. this->tagToGroupElement(tag, group, element);
  422. return( this->instanceValue(sopInstanceUID, group, element) );
  423. }
  424. //------------------------------------------------------------------------------
  425. QString ctkDICOMDatabase::instanceValue(const QString sopInstanceUID, const unsigned short group, const unsigned short element)
  426. {
  427. QString filePath = this->fileForInstance(sopInstanceUID);
  428. if (filePath != "" )
  429. {
  430. return( this->fileValue(filePath, group, element) );
  431. }
  432. else
  433. {
  434. return ("");
  435. }
  436. }
  437. //------------------------------------------------------------------------------
  438. QString ctkDICOMDatabase::fileValue(const QString fileName, QString tag)
  439. {
  440. unsigned short group, element;
  441. this->tagToGroupElement(tag, group, element);
  442. return( this->fileValue(fileName, group, element) );
  443. }
  444. //------------------------------------------------------------------------------
  445. QString ctkDICOMDatabase::fileValue(const QString fileName, const unsigned short group, const unsigned short element)
  446. {
  447. // here is where the real lookup happens
  448. // - for now we create a ctkDICOMDataset and extract the value from there
  449. // - then we convert to the appropriate type of string
  450. //
  451. //As an optimization we could consider
  452. // - check if we are currently looking at the dataset for this fileName
  453. // - if so, are we looking for a group/element that is past the last one
  454. // accessed
  455. // -- if so, keep looking for the requested group/element
  456. // -- if not, start again from the begining
  457. ctkDICOMDataset dataset;
  458. dataset.InitializeFromFile(fileName);
  459. DcmTagKey tagKey(group, element);
  460. return( dataset.GetAllElementValuesAsString(tagKey) );
  461. }
  462. //------------------------------------------------------------------------------
  463. bool ctkDICOMDatabase::tagToGroupElement(const QString tag, unsigned short& group, unsigned short& element)
  464. {
  465. QStringList groupElement = tag.split(",");
  466. bool groupOK, elementOK;
  467. group = groupElement[0].toUInt(&groupOK, 16);
  468. element = groupElement[1].toUInt(&elementOK, 16);
  469. return( groupOK && elementOK );
  470. }
  471. //------------------------------------------------------------------------------
  472. void ctkDICOMDatabase::insert( DcmDataset *dataset, bool storeFile, bool generateThumbnail)
  473. {
  474. if (!dataset)
  475. {
  476. return;
  477. }
  478. ctkDICOMDataset ctkDataset;
  479. ctkDataset.InitializeFromDataset(dataset, false /* do not take ownership */);
  480. this->insert(ctkDataset,storeFile,generateThumbnail);
  481. }
  482. void ctkDICOMDatabase::insert( const ctkDICOMDataset& ctkDataset, bool storeFile, bool generateThumbnail)
  483. {
  484. Q_D(ctkDICOMDatabase);
  485. d->insert(ctkDataset, QString(), storeFile, generateThumbnail);
  486. }
  487. //------------------------------------------------------------------------------
  488. void ctkDICOMDatabase::insert ( const QString& filePath, bool storeFile, bool generateThumbnail, bool createHierarchy, const QString& destinationDirectoryName)
  489. {
  490. Q_D(ctkDICOMDatabase);
  491. Q_UNUSED(createHierarchy);
  492. Q_UNUSED(destinationDirectoryName);
  493. /// first we check if the file is already in the database
  494. if (fileExistsAndUpToDate(filePath))
  495. {
  496. logger.debug( "File " + filePath + " already added.");
  497. return;
  498. }
  499. logger.debug( "Processing " + filePath );
  500. std::string filename = filePath.toStdString();
  501. DcmFileFormat fileformat;
  502. ctkDICOMDataset ctkDataset;
  503. ctkDataset.InitializeFromFile(filePath);
  504. if ( ctkDataset.IsInitialized() )
  505. {
  506. d->insert( ctkDataset, filePath, storeFile, generateThumbnail );
  507. }
  508. else
  509. {
  510. logger.warn(QString("Could not read DICOM file:") + filePath);
  511. }
  512. }
  513. //------------------------------------------------------------------------------
  514. int ctkDICOMDatabasePrivate::insertPatient(const ctkDICOMDataset& ctkDataset)
  515. {
  516. int dbPatientID;
  517. // Check if patient is already present in the db
  518. // TODO: maybe add birthdate check for extra safety
  519. QString patientID(ctkDataset.GetElementAsString(DCM_PatientID) );
  520. QString patientsName(ctkDataset.GetElementAsString(DCM_PatientName) );
  521. QString patientsBirthDate(ctkDataset.GetElementAsString(DCM_PatientBirthDate) );
  522. QSqlQuery checkPatientExistsQuery(Database);
  523. checkPatientExistsQuery.prepare ( "SELECT * FROM Patients WHERE PatientID = ? AND PatientsName = ?" );
  524. checkPatientExistsQuery.bindValue ( 0, patientID );
  525. checkPatientExistsQuery.bindValue ( 1, patientsName );
  526. loggedExec(checkPatientExistsQuery);
  527. if (checkPatientExistsQuery.next())
  528. {
  529. // we found him
  530. dbPatientID = checkPatientExistsQuery.value(checkPatientExistsQuery.record().indexOf("UID")).toInt();
  531. qDebug() << "Found patient in the database as UId: " << dbPatientID;
  532. }
  533. else
  534. {
  535. // Insert it
  536. QString patientsBirthTime(ctkDataset.GetElementAsString(DCM_PatientBirthTime) );
  537. QString patientsSex(ctkDataset.GetElementAsString(DCM_PatientSex) );
  538. QString patientsAge(ctkDataset.GetElementAsString(DCM_PatientAge) );
  539. QString patientComments(ctkDataset.GetElementAsString(DCM_PatientComments) );
  540. QSqlQuery insertPatientStatement ( Database );
  541. insertPatientStatement.prepare ( "INSERT INTO Patients ('UID', 'PatientsName', 'PatientID', 'PatientsBirthDate', 'PatientsBirthTime', 'PatientsSex', 'PatientsAge', 'PatientsComments' ) values ( NULL, ?, ?, ?, ?, ?, ?, ? )" );
  542. insertPatientStatement.bindValue ( 0, patientsName );
  543. insertPatientStatement.bindValue ( 1, patientID );
  544. insertPatientStatement.bindValue ( 2, patientsBirthDate );
  545. insertPatientStatement.bindValue ( 3, patientsBirthTime );
  546. insertPatientStatement.bindValue ( 4, patientsSex );
  547. // TODO: shift patient's age to study,
  548. // since this is not a patient level attribute in images
  549. // insertPatientStatement.bindValue ( 5, patientsAge );
  550. insertPatientStatement.bindValue ( 6, patientComments );
  551. loggedExec(insertPatientStatement);
  552. dbPatientID = insertPatientStatement.lastInsertId().toInt();
  553. logger.debug ( "New patient inserted: " + QString().setNum ( dbPatientID ) );
  554. qDebug() << "New patient inserted as : " << dbPatientID;
  555. }
  556. return dbPatientID;
  557. }
  558. void ctkDICOMDatabasePrivate::insertStudy(const ctkDICOMDataset& ctkDataset, int dbPatientID)
  559. {
  560. QString studyInstanceUID(ctkDataset.GetElementAsString(DCM_StudyInstanceUID) );
  561. QSqlQuery checkStudyExistsQuery (Database);
  562. checkStudyExistsQuery.prepare ( "SELECT * FROM Studies WHERE StudyInstanceUID = ?" );
  563. checkStudyExistsQuery.bindValue ( 0, studyInstanceUID );
  564. checkStudyExistsQuery.exec();
  565. if(!checkStudyExistsQuery.next())
  566. {
  567. QString studyID(ctkDataset.GetElementAsString(DCM_StudyID) );
  568. QString studyDate(ctkDataset.GetElementAsString(DCM_StudyDate) );
  569. QString studyTime(ctkDataset.GetElementAsString(DCM_StudyTime) );
  570. QString accessionNumber(ctkDataset.GetElementAsString(DCM_AccessionNumber) );
  571. QString modalitiesInStudy(ctkDataset.GetElementAsString(DCM_ModalitiesInStudy) );
  572. QString institutionName(ctkDataset.GetElementAsString(DCM_InstitutionName) );
  573. QString performingPhysiciansName(ctkDataset.GetElementAsString(DCM_PerformingPhysicianName) );
  574. QString referringPhysician(ctkDataset.GetElementAsString(DCM_ReferringPhysicianName) );
  575. QString studyDescription(ctkDataset.GetElementAsString(DCM_StudyDescription) );
  576. QSqlQuery insertStudyStatement ( Database );
  577. insertStudyStatement.prepare ( "INSERT INTO Studies ( 'StudyInstanceUID', 'PatientsUID', 'StudyID', 'StudyDate', 'StudyTime', 'AccessionNumber', 'ModalitiesInStudy', 'InstitutionName', 'ReferringPhysician', 'PerformingPhysiciansName', 'StudyDescription' ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" );
  578. insertStudyStatement.bindValue ( 0, studyInstanceUID );
  579. insertStudyStatement.bindValue ( 1, dbPatientID );
  580. insertStudyStatement.bindValue ( 2, studyID );
  581. insertStudyStatement.bindValue ( 3, QDate::fromString ( studyDate, "yyyyMMdd" ) );
  582. insertStudyStatement.bindValue ( 4, studyTime );
  583. insertStudyStatement.bindValue ( 5, accessionNumber );
  584. insertStudyStatement.bindValue ( 6, modalitiesInStudy );
  585. insertStudyStatement.bindValue ( 7, institutionName );
  586. insertStudyStatement.bindValue ( 8, referringPhysician );
  587. insertStudyStatement.bindValue ( 9, performingPhysiciansName );
  588. insertStudyStatement.bindValue ( 10, studyDescription );
  589. if ( !insertStudyStatement.exec() )
  590. {
  591. logger.error ( "Error executing statament: " + insertStudyStatement.lastQuery() + " Error: " + insertStudyStatement.lastError().text() );
  592. }
  593. else
  594. {
  595. LastStudyInstanceUID = studyInstanceUID;
  596. }
  597. }
  598. }
  599. void ctkDICOMDatabasePrivate::insertSeries(const ctkDICOMDataset& ctkDataset, QString studyInstanceUID)
  600. {
  601. QString seriesInstanceUID(ctkDataset.GetElementAsString(DCM_SeriesInstanceUID) );
  602. QSqlQuery checkSeriesExistsQuery (Database);
  603. checkSeriesExistsQuery.prepare ( "SELECT * FROM Series WHERE SeriesInstanceUID = ?" );
  604. checkSeriesExistsQuery.bindValue ( 0, seriesInstanceUID );
  605. logger.warn ( "Statement: " + checkSeriesExistsQuery.lastQuery() );
  606. loggedExec(checkSeriesExistsQuery);
  607. if(!checkSeriesExistsQuery.next())
  608. {
  609. QString seriesDate(ctkDataset.GetElementAsString(DCM_SeriesDate) );
  610. QString seriesTime(ctkDataset.GetElementAsString(DCM_SeriesTime) );
  611. QString seriesDescription(ctkDataset.GetElementAsString(DCM_SeriesDescription) );
  612. QString bodyPartExamined(ctkDataset.GetElementAsString(DCM_BodyPartExamined) );
  613. QString frameOfReferenceUID(ctkDataset.GetElementAsString(DCM_FrameOfReferenceUID) );
  614. QString contrastAgent(ctkDataset.GetElementAsString(DCM_ContrastBolusAgent) );
  615. QString scanningSequence(ctkDataset.GetElementAsString(DCM_ScanningSequence) );
  616. long seriesNumber(ctkDataset.GetElementAsInteger(DCM_SeriesNumber) );
  617. long acquisitionNumber(ctkDataset.GetElementAsInteger(DCM_AcquisitionNumber) );
  618. long echoNumber(ctkDataset.GetElementAsInteger(DCM_EchoNumbers) );
  619. long temporalPosition(ctkDataset.GetElementAsInteger(DCM_TemporalPositionIdentifier) );
  620. QSqlQuery insertSeriesStatement ( Database );
  621. insertSeriesStatement.prepare ( "INSERT INTO Series ( 'SeriesInstanceUID', 'StudyInstanceUID', 'SeriesNumber', 'SeriesDate', 'SeriesTime', 'SeriesDescription', 'BodyPartExamined', 'FrameOfReferenceUID', 'AcquisitionNumber', 'ContrastAgent', 'ScanningSequence', 'EchoNumber', 'TemporalPosition' ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" );
  622. insertSeriesStatement.bindValue ( 0, seriesInstanceUID );
  623. insertSeriesStatement.bindValue ( 1, studyInstanceUID );
  624. insertSeriesStatement.bindValue ( 2, static_cast<int>(seriesNumber) );
  625. insertSeriesStatement.bindValue ( 3, seriesDate );
  626. insertSeriesStatement.bindValue ( 4, QDate::fromString ( seriesTime, "yyyyMMdd" ) );
  627. insertSeriesStatement.bindValue ( 5, seriesDescription );
  628. insertSeriesStatement.bindValue ( 6, bodyPartExamined );
  629. insertSeriesStatement.bindValue ( 7, frameOfReferenceUID );
  630. insertSeriesStatement.bindValue ( 8, static_cast<int>(acquisitionNumber) );
  631. insertSeriesStatement.bindValue ( 9, contrastAgent );
  632. insertSeriesStatement.bindValue ( 10, scanningSequence );
  633. insertSeriesStatement.bindValue ( 11, static_cast<int>(echoNumber) );
  634. insertSeriesStatement.bindValue ( 12, static_cast<int>(temporalPosition) );
  635. if ( !insertSeriesStatement.exec() )
  636. {
  637. logger.error ( "Error executing statament: "
  638. + insertSeriesStatement.lastQuery()
  639. + " Error: " + insertSeriesStatement.lastError().text() );
  640. LastSeriesInstanceUID = "";
  641. }
  642. else
  643. {
  644. LastSeriesInstanceUID = seriesInstanceUID;
  645. }
  646. }
  647. }
  648. void ctkDICOMDatabasePrivate::insert( const ctkDICOMDataset& ctkDataset, const QString& filePath, bool storeFile, bool generateThumbnail)
  649. {
  650. Q_Q(ctkDICOMDatabase);
  651. QMutexLocker lock(&insertMutex);
  652. // Check to see if the file has already been loaded
  653. // TODO:
  654. // It could make sense to actually remove the dataset and re-add it. This needs the remove
  655. // method we still have to write.
  656. //
  657. QString sopInstanceUID ( ctkDataset.GetElementAsString(DCM_SOPInstanceUID) );
  658. QSqlQuery fileExists ( Database );
  659. fileExists.prepare("SELECT InsertTimestamp,Filename FROM Images WHERE SOPInstanceUID == :sopInstanceUID");
  660. fileExists.bindValue(":sopInstanceUID",sopInstanceUID);
  661. bool success = fileExists.exec();
  662. if (!success)
  663. {
  664. logger.error("SQLITE ERROR: " + fileExists.lastError().driverText());
  665. return;
  666. }
  667. QString databaseFilename(fileExists.value(1).toString());
  668. QDateTime fileLastModified(QFileInfo(databaseFilename).lastModified());
  669. QDateTime databaseInsertTimestamp(QDateTime::fromString(fileExists.value(0).toString(),Qt::ISODate));
  670. qDebug() << "inserting filePath: " << filePath;
  671. if (databaseFilename == "")
  672. {
  673. qDebug() << "database filename for " << sopInstanceUID << " is empty - we should insert on top of it";
  674. }
  675. else
  676. {
  677. qDebug() << "database filename for " << sopInstanceUID << " is: " << databaseFilename;
  678. qDebug() << "modified date is: " << fileLastModified;
  679. qDebug() << "db insert date is: " << databaseInsertTimestamp;
  680. if ( fileExists.next() && fileLastModified < databaseInsertTimestamp )
  681. {
  682. logger.debug ( "File " + databaseFilename + " already added" );
  683. return;
  684. }
  685. }
  686. //If the following fields can not be evaluated, cancel evaluation of the DICOM file
  687. QString patientsName(ctkDataset.GetElementAsString(DCM_PatientName) );
  688. QString studyInstanceUID(ctkDataset.GetElementAsString(DCM_StudyInstanceUID) );
  689. QString seriesInstanceUID(ctkDataset.GetElementAsString(DCM_SeriesInstanceUID) );
  690. QString patientID(ctkDataset.GetElementAsString(DCM_PatientID) );
  691. if ( patientsName.isEmpty() && !patientID.isEmpty() )
  692. { // Use patient id as name if name is empty - can happen on anonymized datasets
  693. // see: http://www.na-mic.org/Bug/view.php?id=1643
  694. patientsName = patientID;
  695. }
  696. if ( patientsName.isEmpty() || studyInstanceUID.isEmpty() || patientID.isEmpty() )
  697. {
  698. logger.error("Dataset is missing necessary information!");
  699. return;
  700. }
  701. // store the file if the database is not in memomry
  702. // TODO: if we are called from insert(file) we
  703. // have to do something else
  704. //
  705. QString filename = filePath;
  706. if ( storeFile && !q->isInMemory() && !seriesInstanceUID.isEmpty() )
  707. {
  708. // QString studySeriesDirectory = studyInstanceUID + "/" + seriesInstanceUID;
  709. QString destinationDirectoryName = q->databaseDirectory() + "/dicom/";
  710. QDir destinationDir(destinationDirectoryName);
  711. filename = destinationDirectoryName +
  712. studyInstanceUID + "/" +
  713. seriesInstanceUID + "/" +
  714. sopInstanceUID;
  715. destinationDir.mkpath(studyInstanceUID + "/" +
  716. seriesInstanceUID);
  717. if(filePath.isEmpty())
  718. {
  719. logger.debug ( "Saving file: " + filename );
  720. if ( !ctkDataset.SaveToFile( filename) )
  721. {
  722. logger.error ( "Error saving file: " + filename );
  723. return;
  724. }
  725. }
  726. else
  727. {
  728. // we're inserting an existing file
  729. QFile currentFile( filePath );
  730. currentFile.copy(filename);
  731. logger.debug( "Copy file from: " + filePath );
  732. logger.debug( "Copy file to : " + filename );
  733. }
  734. }
  735. //The dbPatientID is a unique number within the database,
  736. //generated by the sqlite autoincrement
  737. //The patientID is the (non-unique) DICOM patient id
  738. int dbPatientID = LastPatientUID;
  739. if ( patientID != "" && patientsName != "" )
  740. {
  741. //Speed up: Check if patient is the same as in last file;
  742. // very probable, as all images belonging to a study have the same patient
  743. QString patientsBirthDate(ctkDataset.GetElementAsString(DCM_PatientBirthDate) );
  744. if ( LastPatientID != patientID
  745. || LastPatientsBirthDate != patientsBirthDate
  746. || LastPatientsName != patientsName )
  747. { QString seriesInstanceUID(ctkDataset.GetElementAsString(DCM_SeriesInstanceUID) );
  748. qDebug() << "This looks like a different patient from last insert: " << patientID;
  749. // Ok, something is different from last insert, let's insert him if he's not
  750. // already in the db.
  751. dbPatientID = insertPatient( ctkDataset );
  752. /// keep this for the next image
  753. LastPatientUID = dbPatientID;
  754. LastPatientID = patientID;
  755. LastPatientsBirthDate = patientsBirthDate;
  756. LastPatientsName = patientsName;
  757. }
  758. qDebug() << "Going to insert this instance with dbPatientID: " << dbPatientID;
  759. // Patient is in now. Let's continue with the study
  760. if ( studyInstanceUID != "" && LastStudyInstanceUID != studyInstanceUID )
  761. {
  762. insertStudy(ctkDataset,dbPatientID);
  763. }
  764. if ( seriesInstanceUID != "" && seriesInstanceUID != LastSeriesInstanceUID )
  765. {
  766. insertSeries(ctkDataset, studyInstanceUID);
  767. }
  768. // TODO: what to do with imported files
  769. //
  770. if ( !filename.isEmpty() && !seriesInstanceUID.isEmpty() )
  771. {
  772. QSqlQuery checkImageExistsQuery (Database);
  773. checkImageExistsQuery.prepare ( "SELECT * FROM Images WHERE Filename = ?" );
  774. checkImageExistsQuery.bindValue ( 0, filename );
  775. checkImageExistsQuery.exec();
  776. if(!checkImageExistsQuery.next())
  777. {
  778. QSqlQuery insertImageStatement ( Database );
  779. insertImageStatement.prepare ( "INSERT INTO Images ( 'SOPInstanceUID', 'Filename', 'SeriesInstanceUID', 'InsertTimestamp' ) VALUES ( ?, ?, ?, ? )" );
  780. insertImageStatement.bindValue ( 0, sopInstanceUID );
  781. insertImageStatement.bindValue ( 1, filename );
  782. insertImageStatement.bindValue ( 2, seriesInstanceUID );
  783. insertImageStatement.bindValue ( 3, QDateTime::currentDateTime() );
  784. insertImageStatement.exec();
  785. }
  786. }
  787. if( generateThumbnail && thumbnailGenerator && !seriesInstanceUID.isEmpty() )
  788. {
  789. QString studySeriesDirectory = studyInstanceUID + "/" + seriesInstanceUID;
  790. //Create thumbnail here
  791. QString thumbnailPath = q->databaseDirectory() +
  792. "/thumbs/" + studyInstanceUID + "/" + seriesInstanceUID
  793. + "/" + sopInstanceUID + ".png";
  794. QFileInfo thumbnailInfo(thumbnailPath);
  795. if( !(thumbnailInfo.exists()
  796. && (thumbnailInfo.lastModified() > QFileInfo(filename).lastModified())))
  797. {
  798. QDir(q->databaseDirectory() + "/thumbs/").mkpath(studySeriesDirectory);
  799. DicomImage dcmImage(QDir::toNativeSeparators(filename).toAscii());
  800. thumbnailGenerator->generateThumbnail(&dcmImage, thumbnailPath);
  801. }
  802. }
  803. if (q->isInMemory())
  804. {
  805. emit q->databaseChanged();
  806. }
  807. }
  808. }
  809. bool ctkDICOMDatabase::fileExistsAndUpToDate(const QString& filePath)
  810. {
  811. Q_D(ctkDICOMDatabase);
  812. bool result(false);
  813. QSqlQuery check_filename_query(database());
  814. check_filename_query.prepare("SELECT InsertTimestamp FROM Images WHERE Filename == ?");
  815. check_filename_query.bindValue(0,filePath);
  816. d->loggedExec(check_filename_query);
  817. if (
  818. check_filename_query.next() &&
  819. QFileInfo(filePath).lastModified() < QDateTime::fromString(check_filename_query.value(0).toString(),Qt::ISODate)
  820. )
  821. {
  822. result = true;
  823. }
  824. check_filename_query.finish();
  825. return result;
  826. }
  827. bool ctkDICOMDatabase::isOpen() const
  828. {
  829. Q_D(const ctkDICOMDatabase);
  830. return d->Database.isOpen();
  831. }
  832. bool ctkDICOMDatabase::isInMemory() const
  833. {
  834. Q_D(const ctkDICOMDatabase);
  835. return d->DatabaseFileName == ":memory:";
  836. }
  837. bool ctkDICOMDatabase::removeSeries(const QString& seriesInstanceUID)
  838. {
  839. Q_D(ctkDICOMDatabase);
  840. // get all images from series
  841. QSqlQuery fileExists ( d->Database );
  842. fileExists.prepare("SELECT Filename, SOPInstanceUID, StudyInstanceUID FROM Images,Series WHERE Series.SeriesInstanceUID = Images.SeriesInstanceUID AND Images.SeriesInstanceUID = :seriesID");
  843. fileExists.bindValue(":seriesID",seriesInstanceUID);
  844. bool success = fileExists.exec();
  845. if (!success)
  846. {
  847. logger.error("SQLITE ERROR: " + fileExists.lastError().driverText());
  848. return false;
  849. }
  850. QList< QPair<QString,QString> > removeList;
  851. while ( fileExists.next() )
  852. {
  853. QString dbFilePath = fileExists.value(fileExists.record().indexOf("Filename")).toString();
  854. QString sopInstanceUID = fileExists.value(fileExists.record().indexOf("SOPInstanceUID")).toString();
  855. QString studyInstanceUID = fileExists.value(fileExists.record().indexOf("StudyInstanceUID")).toString();
  856. QString internalFilePath = studyInstanceUID + "/" + seriesInstanceUID + "/" + sopInstanceUID;
  857. removeList << qMakePair(dbFilePath,internalFilePath);
  858. }
  859. QSqlQuery fileRemove ( d->Database );
  860. fileRemove.prepare("DELETE FROM Images WHERE SeriesInstanceUID == :seriesID");
  861. fileRemove.bindValue(":seriesID",seriesInstanceUID);
  862. logger.debug("SQLITE: removing seriesInstanceUID " + seriesInstanceUID);
  863. success = fileRemove.exec();
  864. if (!success)
  865. {
  866. logger.error("SQLITE ERROR: could not remove seriesInstanceUID " + seriesInstanceUID);
  867. logger.error("SQLITE ERROR: " + fileRemove.lastError().driverText());
  868. }
  869. QPair<QString,QString> fileToRemove;
  870. foreach (fileToRemove, removeList)
  871. {
  872. QString dbFilePath = fileToRemove.first;
  873. QString thumbnailToRemove = databaseDirectory() + "/thumbs/" + fileToRemove.second + ".png";
  874. // check that the file is below our internal storage
  875. if (dbFilePath.startsWith( databaseDirectory() + "/dicom/"))
  876. {
  877. if (!dbFilePath.endsWith(fileToRemove.second))
  878. {
  879. logger.error("Database inconsistency detected during delete!");
  880. continue;
  881. }
  882. if (QFile( dbFilePath ).remove())
  883. {
  884. logger.debug("Removed file " + dbFilePath );
  885. }
  886. else
  887. {
  888. logger.warn("Failed to remove file " + dbFilePath );
  889. }
  890. }
  891. if (QFile( thumbnailToRemove ).remove())
  892. {
  893. logger.debug("Removed thumbnail " + thumbnailToRemove);
  894. }
  895. else
  896. {
  897. logger.warn("Failed to remove thumbnail " + thumbnailToRemove);
  898. }
  899. }
  900. this->cleanup();
  901. d->LastSeriesInstanceUID = "";
  902. return true;
  903. }
  904. bool ctkDICOMDatabase::cleanup()
  905. {
  906. Q_D(ctkDICOMDatabase);
  907. QSqlQuery seriesCleanup ( d->Database );
  908. seriesCleanup.exec("DELETE FROM Series WHERE ( SELECT COUNT(*) FROM Images WHERE Images.SeriesInstanceUID = Series.SeriesInstanceUID ) = 0;");
  909. seriesCleanup.exec("DELETE FROM Studies WHERE ( SELECT COUNT(*) FROM Series WHERE Series.StudyInstanceUID = Studies.StudyInstanceUID ) = 0;");
  910. seriesCleanup.exec("DELETE FROM Patients WHERE ( SELECT COUNT(*) FROM Studies WHERE Studies.PatientsUID = Patients.UID ) = 0;");
  911. return true;
  912. }
  913. bool ctkDICOMDatabase::removeStudy(const QString& studyInstanceUID)
  914. {
  915. Q_D(ctkDICOMDatabase);
  916. QSqlQuery seriesForStudy( d->Database );
  917. seriesForStudy.prepare("SELECT SeriesInstanceUID FROM Series WHERE StudyInstanceUID = :studyID");
  918. seriesForStudy.bindValue(":studyID", studyInstanceUID);
  919. bool success = seriesForStudy.exec();
  920. if (!success)
  921. {
  922. logger.error("SQLITE ERROR: " + seriesForStudy.lastError().driverText());
  923. return false;
  924. }
  925. bool result = true;
  926. while ( seriesForStudy.next() )
  927. {
  928. QString seriesInstanceUID = seriesForStudy.value(seriesForStudy.record().indexOf("SeriesInstanceUID")).toString();
  929. if ( ! this->removeSeries(seriesInstanceUID) )
  930. {
  931. result = false;
  932. }
  933. }
  934. d->LastStudyInstanceUID = "";
  935. return result;
  936. }
  937. bool ctkDICOMDatabase::removePatient(const QString& patientID)
  938. {
  939. Q_D(ctkDICOMDatabase);
  940. QSqlQuery studiesForPatient( d->Database );
  941. studiesForPatient.prepare("SELECT StudyInstanceUID FROM Studies WHERE PatientsUID = :patientsID");
  942. studiesForPatient.bindValue(":patientsID", patientID);
  943. bool success = studiesForPatient.exec();
  944. if (!success)
  945. {
  946. logger.error("SQLITE ERROR: " + studiesForPatient.lastError().driverText());
  947. return false;
  948. }
  949. bool result = true;
  950. while ( studiesForPatient.next() )
  951. {
  952. QString studyInstanceUID = studiesForPatient.value(studiesForPatient.record().indexOf("StudyInstanceUID")).toString();
  953. if ( ! this->removeStudy(studyInstanceUID) )
  954. {
  955. result = false;
  956. }
  957. }
  958. d->LastPatientID = "";
  959. d->LastPatientsName = "";
  960. d->LastPatientsBirthDate = "";
  961. d->LastPatientUID = -1;
  962. return result;
  963. }