ctkDICOMDatabase.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. void ctkDICOMDatabase::loadInstanceHeader (QString sopInstanceUID)
  353. {
  354. Q_D(ctkDICOMDatabase);
  355. QSqlQuery query(d->Database);
  356. query.prepare ( "SELECT Filename FROM Images WHERE SOPInstanceUID=?");
  357. query.bindValue ( 0, sopInstanceUID );
  358. query.exec();
  359. if (query.next())
  360. {
  361. QString fileName = query.value(0).toString();
  362. this->loadFileHeader(fileName);
  363. }
  364. return;
  365. }
  366. //------------------------------------------------------------------------------
  367. void ctkDICOMDatabase::loadFileHeader (QString fileName)
  368. {
  369. Q_D(ctkDICOMDatabase);
  370. d->LoadedHeader.clear();
  371. DcmFileFormat fileFormat;
  372. OFCondition status = fileFormat.loadFile(fileName.toLatin1().data());
  373. if (status.good())
  374. {
  375. DcmDataset *dataset = fileFormat.getDataset();
  376. DcmStack stack;
  377. while (dataset->nextObject(stack, true) == EC_Normal)
  378. {
  379. DcmObject *dO = stack.top();
  380. QString tag = QString("%1,%2").arg(
  381. dO->getGTag(),4,16,QLatin1Char('0')).arg(
  382. dO->getETag(),4,16,QLatin1Char('0'));
  383. std::ostringstream s;
  384. dO->print(s);
  385. d->LoadedHeader[tag] = QString(s.str().c_str());
  386. }
  387. }
  388. return;
  389. }
  390. //------------------------------------------------------------------------------
  391. QStringList ctkDICOMDatabase::headerKeys ()
  392. {
  393. Q_D(ctkDICOMDatabase);
  394. return (d->LoadedHeader.keys());
  395. }
  396. //------------------------------------------------------------------------------
  397. QString ctkDICOMDatabase::headerValue (QString key)
  398. {
  399. Q_D(ctkDICOMDatabase);
  400. return (d->LoadedHeader[key]);
  401. }
  402. //------------------------------------------------------------------------------
  403. QString ctkDICOMDatabase::instanceValue(QString sopInstanceUID, QString tag)
  404. {
  405. unsigned short group, element;
  406. this->tagToGroupElement(tag, group, element);
  407. return( this->instanceValue(sopInstanceUID, group, element) );
  408. }
  409. //------------------------------------------------------------------------------
  410. QString ctkDICOMDatabase::instanceValue(const QString sopInstanceUID, const unsigned short group, const unsigned short element)
  411. {
  412. QString filePath = this->fileForInstance(sopInstanceUID);
  413. if (filePath != "" )
  414. {
  415. return( this->fileValue(filePath, group, element) );
  416. }
  417. else
  418. {
  419. return ("");
  420. }
  421. }
  422. //------------------------------------------------------------------------------
  423. QString ctkDICOMDatabase::fileValue(const QString fileName, QString tag)
  424. {
  425. unsigned short group, element;
  426. this->tagToGroupElement(tag, group, element);
  427. return( this->fileValue(fileName, group, element) );
  428. }
  429. //------------------------------------------------------------------------------
  430. QString ctkDICOMDatabase::fileValue(const QString fileName, const unsigned short group, const unsigned short element)
  431. {
  432. // here is where the real lookup happens
  433. // - for now we create a ctkDICOMDataset and extract the value from there
  434. // - then we convert to the appropriate type of string
  435. //
  436. //As an optimization we could consider
  437. // - check if we are currently looking at the dataset for this fileName
  438. // - if so, are we looking for a group/element that is past the last one
  439. // accessed
  440. // -- if so, keep looking for the requested group/element
  441. // -- if not, start again from the begining
  442. ctkDICOMDataset dataset;
  443. dataset.InitializeFromFile(fileName);
  444. DcmTagKey tagKey(group, element);
  445. return( dataset.GetAllElementValuesAsString(tagKey) );
  446. }
  447. //------------------------------------------------------------------------------
  448. bool ctkDICOMDatabase::tagToGroupElement(const QString tag, unsigned short& group, unsigned short& element)
  449. {
  450. QStringList groupElement = tag.split(",");
  451. bool groupOK, elementOK;
  452. group = groupElement[0].toUInt(&groupOK, 16);
  453. element = groupElement[1].toUInt(&elementOK, 16);
  454. return( groupOK && elementOK );
  455. }
  456. //------------------------------------------------------------------------------
  457. void ctkDICOMDatabase::insert( DcmDataset *dataset, bool storeFile, bool generateThumbnail)
  458. {
  459. if (!dataset)
  460. {
  461. return;
  462. }
  463. ctkDICOMDataset ctkDataset;
  464. ctkDataset.InitializeFromDataset(dataset, false /* do not take ownership */);
  465. this->insert(ctkDataset,storeFile,generateThumbnail);
  466. }
  467. void ctkDICOMDatabase::insert( const ctkDICOMDataset& ctkDataset, bool storeFile, bool generateThumbnail)
  468. {
  469. Q_D(ctkDICOMDatabase);
  470. d->insert(ctkDataset, QString(), storeFile, generateThumbnail);
  471. }
  472. //------------------------------------------------------------------------------
  473. void ctkDICOMDatabase::insert ( const QString& filePath, bool storeFile, bool generateThumbnail, bool createHierarchy, const QString& destinationDirectoryName)
  474. {
  475. Q_D(ctkDICOMDatabase);
  476. Q_UNUSED(createHierarchy);
  477. Q_UNUSED(destinationDirectoryName);
  478. /// first we check if the file is already in the database
  479. if (fileExistsAndUpToDate(filePath))
  480. {
  481. logger.debug( "File " + filePath + " already added.");
  482. return;
  483. }
  484. logger.debug( "Processing " + filePath );
  485. std::string filename = filePath.toStdString();
  486. DcmFileFormat fileformat;
  487. ctkDICOMDataset ctkDataset;
  488. ctkDataset.InitializeFromFile(filePath);
  489. if ( ctkDataset.IsInitialized() )
  490. {
  491. d->insert( ctkDataset, filePath, storeFile, generateThumbnail );
  492. }
  493. else
  494. {
  495. logger.warn(QString("Could not read DICOM file:") + filePath);
  496. }
  497. }
  498. //------------------------------------------------------------------------------
  499. int ctkDICOMDatabasePrivate::insertPatient(const ctkDICOMDataset& ctkDataset)
  500. {
  501. int dbPatientID;
  502. // Check if patient is already present in the db
  503. // TODO: maybe add birthdate check for extra safety
  504. QString patientID(ctkDataset.GetElementAsString(DCM_PatientID) );
  505. QString patientsName(ctkDataset.GetElementAsString(DCM_PatientName) );
  506. QString patientsBirthDate(ctkDataset.GetElementAsString(DCM_PatientBirthDate) );
  507. QSqlQuery checkPatientExistsQuery(Database);
  508. checkPatientExistsQuery.prepare ( "SELECT * FROM Patients WHERE PatientID = ? AND PatientsName = ?" );
  509. checkPatientExistsQuery.bindValue ( 0, patientID );
  510. checkPatientExistsQuery.bindValue ( 1, patientsName );
  511. loggedExec(checkPatientExistsQuery);
  512. if (checkPatientExistsQuery.next())
  513. {
  514. // we found him
  515. dbPatientID = checkPatientExistsQuery.value(checkPatientExistsQuery.record().indexOf("UID")).toInt();
  516. qDebug() << "Found patient in the database as UId: " << dbPatientID;
  517. }
  518. else
  519. {
  520. // Insert it
  521. QString patientsBirthTime(ctkDataset.GetElementAsString(DCM_PatientBirthTime) );
  522. QString patientsSex(ctkDataset.GetElementAsString(DCM_PatientSex) );
  523. QString patientsAge(ctkDataset.GetElementAsString(DCM_PatientAge) );
  524. QString patientComments(ctkDataset.GetElementAsString(DCM_PatientComments) );
  525. QSqlQuery insertPatientStatement ( Database );
  526. insertPatientStatement.prepare ( "INSERT INTO Patients ('UID', 'PatientsName', 'PatientID', 'PatientsBirthDate', 'PatientsBirthTime', 'PatientsSex', 'PatientsAge', 'PatientsComments' ) values ( NULL, ?, ?, ?, ?, ?, ?, ? )" );
  527. insertPatientStatement.bindValue ( 0, patientsName );
  528. insertPatientStatement.bindValue ( 1, patientID );
  529. insertPatientStatement.bindValue ( 2, patientsBirthDate );
  530. insertPatientStatement.bindValue ( 3, patientsBirthTime );
  531. insertPatientStatement.bindValue ( 4, patientsSex );
  532. // TODO: shift patient's age to study,
  533. // since this is not a patient level attribute in images
  534. // insertPatientStatement.bindValue ( 5, patientsAge );
  535. insertPatientStatement.bindValue ( 6, patientComments );
  536. loggedExec(insertPatientStatement);
  537. dbPatientID = insertPatientStatement.lastInsertId().toInt();
  538. logger.debug ( "New patient inserted: " + QString().setNum ( dbPatientID ) );
  539. qDebug() << "New patient inserted as : " << dbPatientID;
  540. }
  541. return dbPatientID;
  542. }
  543. void ctkDICOMDatabasePrivate::insertStudy(const ctkDICOMDataset& ctkDataset, int dbPatientID)
  544. {
  545. QString studyInstanceUID(ctkDataset.GetElementAsString(DCM_StudyInstanceUID) );
  546. QSqlQuery checkStudyExistsQuery (Database);
  547. checkStudyExistsQuery.prepare ( "SELECT * FROM Studies WHERE StudyInstanceUID = ?" );
  548. checkStudyExistsQuery.bindValue ( 0, studyInstanceUID );
  549. checkStudyExistsQuery.exec();
  550. if(!checkStudyExistsQuery.next())
  551. {
  552. QString studyID(ctkDataset.GetElementAsString(DCM_StudyID) );
  553. QString studyDate(ctkDataset.GetElementAsString(DCM_StudyDate) );
  554. QString studyTime(ctkDataset.GetElementAsString(DCM_StudyTime) );
  555. QString accessionNumber(ctkDataset.GetElementAsString(DCM_AccessionNumber) );
  556. QString modalitiesInStudy(ctkDataset.GetElementAsString(DCM_ModalitiesInStudy) );
  557. QString institutionName(ctkDataset.GetElementAsString(DCM_InstitutionName) );
  558. QString performingPhysiciansName(ctkDataset.GetElementAsString(DCM_PerformingPhysicianName) );
  559. QString referringPhysician(ctkDataset.GetElementAsString(DCM_ReferringPhysicianName) );
  560. QString studyDescription(ctkDataset.GetElementAsString(DCM_StudyDescription) );
  561. QSqlQuery insertStudyStatement ( Database );
  562. insertStudyStatement.prepare ( "INSERT INTO Studies ( 'StudyInstanceUID', 'PatientsUID', 'StudyID', 'StudyDate', 'StudyTime', 'AccessionNumber', 'ModalitiesInStudy', 'InstitutionName', 'ReferringPhysician', 'PerformingPhysiciansName', 'StudyDescription' ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" );
  563. insertStudyStatement.bindValue ( 0, studyInstanceUID );
  564. insertStudyStatement.bindValue ( 1, dbPatientID );
  565. insertStudyStatement.bindValue ( 2, studyID );
  566. insertStudyStatement.bindValue ( 3, QDate::fromString ( studyDate, "yyyyMMdd" ) );
  567. insertStudyStatement.bindValue ( 4, studyTime );
  568. insertStudyStatement.bindValue ( 5, accessionNumber );
  569. insertStudyStatement.bindValue ( 6, modalitiesInStudy );
  570. insertStudyStatement.bindValue ( 7, institutionName );
  571. insertStudyStatement.bindValue ( 8, referringPhysician );
  572. insertStudyStatement.bindValue ( 9, performingPhysiciansName );
  573. insertStudyStatement.bindValue ( 10, studyDescription );
  574. if ( !insertStudyStatement.exec() )
  575. {
  576. logger.error ( "Error executing statament: " + insertStudyStatement.lastQuery() + " Error: " + insertStudyStatement.lastError().text() );
  577. }
  578. else
  579. {
  580. LastStudyInstanceUID = studyInstanceUID;
  581. }
  582. }
  583. }
  584. void ctkDICOMDatabasePrivate::insertSeries(const ctkDICOMDataset& ctkDataset, QString studyInstanceUID)
  585. {
  586. QString seriesInstanceUID(ctkDataset.GetElementAsString(DCM_SeriesInstanceUID) );
  587. QSqlQuery checkSeriesExistsQuery (Database);
  588. checkSeriesExistsQuery.prepare ( "SELECT * FROM Series WHERE SeriesInstanceUID = ?" );
  589. checkSeriesExistsQuery.bindValue ( 0, seriesInstanceUID );
  590. logger.warn ( "Statement: " + checkSeriesExistsQuery.lastQuery() );
  591. loggedExec(checkSeriesExistsQuery);
  592. if(!checkSeriesExistsQuery.next())
  593. {
  594. QString seriesDate(ctkDataset.GetElementAsString(DCM_SeriesDate) );
  595. QString seriesTime(ctkDataset.GetElementAsString(DCM_SeriesTime) );
  596. QString seriesDescription(ctkDataset.GetElementAsString(DCM_SeriesDescription) );
  597. QString bodyPartExamined(ctkDataset.GetElementAsString(DCM_BodyPartExamined) );
  598. QString frameOfReferenceUID(ctkDataset.GetElementAsString(DCM_FrameOfReferenceUID) );
  599. QString contrastAgent(ctkDataset.GetElementAsString(DCM_ContrastBolusAgent) );
  600. QString scanningSequence(ctkDataset.GetElementAsString(DCM_ScanningSequence) );
  601. long seriesNumber(ctkDataset.GetElementAsInteger(DCM_SeriesNumber) );
  602. long acquisitionNumber(ctkDataset.GetElementAsInteger(DCM_AcquisitionNumber) );
  603. long echoNumber(ctkDataset.GetElementAsInteger(DCM_EchoNumbers) );
  604. long temporalPosition(ctkDataset.GetElementAsInteger(DCM_TemporalPositionIdentifier) );
  605. QSqlQuery insertSeriesStatement ( Database );
  606. insertSeriesStatement.prepare ( "INSERT INTO Series ( 'SeriesInstanceUID', 'StudyInstanceUID', 'SeriesNumber', 'SeriesDate', 'SeriesTime', 'SeriesDescription', 'BodyPartExamined', 'FrameOfReferenceUID', 'AcquisitionNumber', 'ContrastAgent', 'ScanningSequence', 'EchoNumber', 'TemporalPosition' ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )" );
  607. insertSeriesStatement.bindValue ( 0, seriesInstanceUID );
  608. insertSeriesStatement.bindValue ( 1, studyInstanceUID );
  609. insertSeriesStatement.bindValue ( 2, static_cast<int>(seriesNumber) );
  610. insertSeriesStatement.bindValue ( 3, seriesDate );
  611. insertSeriesStatement.bindValue ( 4, QDate::fromString ( seriesTime, "yyyyMMdd" ) );
  612. insertSeriesStatement.bindValue ( 5, seriesDescription );
  613. insertSeriesStatement.bindValue ( 6, bodyPartExamined );
  614. insertSeriesStatement.bindValue ( 7, frameOfReferenceUID );
  615. insertSeriesStatement.bindValue ( 8, static_cast<int>(acquisitionNumber) );
  616. insertSeriesStatement.bindValue ( 9, contrastAgent );
  617. insertSeriesStatement.bindValue ( 10, scanningSequence );
  618. insertSeriesStatement.bindValue ( 11, static_cast<int>(echoNumber) );
  619. insertSeriesStatement.bindValue ( 12, static_cast<int>(temporalPosition) );
  620. if ( !insertSeriesStatement.exec() )
  621. {
  622. logger.error ( "Error executing statament: "
  623. + insertSeriesStatement.lastQuery()
  624. + " Error: " + insertSeriesStatement.lastError().text() );
  625. LastSeriesInstanceUID = "";
  626. }
  627. else
  628. {
  629. LastSeriesInstanceUID = seriesInstanceUID;
  630. }
  631. }
  632. }
  633. void ctkDICOMDatabasePrivate::insert( const ctkDICOMDataset& ctkDataset, const QString& filePath, bool storeFile, bool generateThumbnail)
  634. {
  635. Q_Q(ctkDICOMDatabase);
  636. QMutexLocker lock(&insertMutex);
  637. // Check to see if the file has already been loaded
  638. // TODO:
  639. // It could make sense to actually remove the dataset and re-add it. This needs the remove
  640. // method we still have to write.
  641. //
  642. QString sopInstanceUID ( ctkDataset.GetElementAsString(DCM_SOPInstanceUID) );
  643. QSqlQuery fileExists ( Database );
  644. fileExists.prepare("SELECT InsertTimestamp,Filename FROM Images WHERE SOPInstanceUID == :sopInstanceUID");
  645. fileExists.bindValue(":sopInstanceUID",sopInstanceUID);
  646. bool success = fileExists.exec();
  647. if (!success)
  648. {
  649. logger.error("SQLITE ERROR: " + fileExists.lastError().driverText());
  650. return;
  651. }
  652. QString databaseFilename(fileExists.value(1).toString());
  653. QDateTime fileLastModified(QFileInfo(databaseFilename).lastModified());
  654. QDateTime databaseInsertTimestamp(QDateTime::fromString(fileExists.value(0).toString(),Qt::ISODate));
  655. qDebug() << "inserting filePath: " << filePath;
  656. if (databaseFilename == "")
  657. {
  658. qDebug() << "database filename for " << sopInstanceUID << " is empty - we should insert on top of it";
  659. }
  660. else
  661. {
  662. qDebug() << "database filename for " << sopInstanceUID << " is: " << databaseFilename;
  663. qDebug() << "modified date is: " << fileLastModified;
  664. qDebug() << "db insert date is: " << databaseInsertTimestamp;
  665. if ( fileExists.next() && fileLastModified < databaseInsertTimestamp )
  666. {
  667. logger.debug ( "File " + databaseFilename + " already added" );
  668. return;
  669. }
  670. }
  671. //If the following fields can not be evaluated, cancel evaluation of the DICOM file
  672. QString patientsName(ctkDataset.GetElementAsString(DCM_PatientName) );
  673. QString studyInstanceUID(ctkDataset.GetElementAsString(DCM_StudyInstanceUID) );
  674. QString seriesInstanceUID(ctkDataset.GetElementAsString(DCM_SeriesInstanceUID) );
  675. QString patientID(ctkDataset.GetElementAsString(DCM_PatientID) );
  676. if ( patientsName.isEmpty() && !patientID.isEmpty() )
  677. { // Use patient id as name if name is empty - can happen on anonymized datasets
  678. // see: http://www.na-mic.org/Bug/view.php?id=1643
  679. patientsName = patientID;
  680. }
  681. if ( patientsName.isEmpty() || studyInstanceUID.isEmpty() || patientID.isEmpty() )
  682. {
  683. logger.error("Dataset is missing necessary information!");
  684. return;
  685. }
  686. // store the file if the database is not in memomry
  687. // TODO: if we are called from insert(file) we
  688. // have to do something else
  689. //
  690. QString filename = filePath;
  691. if ( storeFile && !q->isInMemory() && !seriesInstanceUID.isEmpty() )
  692. {
  693. // QString studySeriesDirectory = studyInstanceUID + "/" + seriesInstanceUID;
  694. QString destinationDirectoryName = q->databaseDirectory() + "/dicom/";
  695. QDir destinationDir(destinationDirectoryName);
  696. filename = destinationDirectoryName +
  697. studyInstanceUID + "/" +
  698. seriesInstanceUID + "/" +
  699. sopInstanceUID;
  700. destinationDir.mkpath(studyInstanceUID + "/" +
  701. seriesInstanceUID);
  702. if(filePath.isEmpty())
  703. {
  704. logger.debug ( "Saving file: " + filename );
  705. if ( !ctkDataset.SaveToFile( filename) )
  706. {
  707. logger.error ( "Error saving file: " + filename );
  708. return;
  709. }
  710. }
  711. else
  712. {
  713. // we're inserting an existing file
  714. QFile currentFile( filePath );
  715. currentFile.copy(filename);
  716. logger.debug( "Copy file from: " + filePath );
  717. logger.debug( "Copy file to : " + filename );
  718. }
  719. }
  720. //The dbPatientID is a unique number within the database,
  721. //generated by the sqlite autoincrement
  722. //The patientID is the (non-unique) DICOM patient id
  723. int dbPatientID = LastPatientUID;
  724. if ( patientID != "" && patientsName != "" )
  725. {
  726. //Speed up: Check if patient is the same as in last file;
  727. // very probable, as all images belonging to a study have the same patient
  728. QString patientsBirthDate(ctkDataset.GetElementAsString(DCM_PatientBirthDate) );
  729. if ( LastPatientID != patientID
  730. || LastPatientsBirthDate != patientsBirthDate
  731. || LastPatientsName != patientsName )
  732. { QString seriesInstanceUID(ctkDataset.GetElementAsString(DCM_SeriesInstanceUID) );
  733. qDebug() << "This looks like a different patient from last insert: " << patientID;
  734. // Ok, something is different from last insert, let's insert him if he's not
  735. // already in the db.
  736. dbPatientID = insertPatient( ctkDataset );
  737. /// keep this for the next image
  738. LastPatientUID = dbPatientID;
  739. LastPatientID = patientID;
  740. LastPatientsBirthDate = patientsBirthDate;
  741. LastPatientsName = patientsName;
  742. }
  743. qDebug() << "Going to insert this instance with dbPatientID: " << dbPatientID;
  744. // Patient is in now. Let's continue with the study
  745. if ( studyInstanceUID != "" && LastStudyInstanceUID != studyInstanceUID )
  746. {
  747. insertStudy(ctkDataset,dbPatientID);
  748. }
  749. if ( seriesInstanceUID != "" && seriesInstanceUID != LastSeriesInstanceUID )
  750. {
  751. insertSeries(ctkDataset, studyInstanceUID);
  752. }
  753. // TODO: what to do with imported files
  754. //
  755. if ( !filename.isEmpty() && !seriesInstanceUID.isEmpty() )
  756. {
  757. QSqlQuery checkImageExistsQuery (Database);
  758. checkImageExistsQuery.prepare ( "SELECT * FROM Images WHERE Filename = ?" );
  759. checkImageExistsQuery.bindValue ( 0, filename );
  760. checkImageExistsQuery.exec();
  761. if(!checkImageExistsQuery.next())
  762. {
  763. QSqlQuery insertImageStatement ( Database );
  764. insertImageStatement.prepare ( "INSERT INTO Images ( 'SOPInstanceUID', 'Filename', 'SeriesInstanceUID', 'InsertTimestamp' ) VALUES ( ?, ?, ?, ? )" );
  765. insertImageStatement.bindValue ( 0, sopInstanceUID );
  766. insertImageStatement.bindValue ( 1, filename );
  767. insertImageStatement.bindValue ( 2, seriesInstanceUID );
  768. insertImageStatement.bindValue ( 3, QDateTime::currentDateTime() );
  769. insertImageStatement.exec();
  770. }
  771. }
  772. if( generateThumbnail && thumbnailGenerator && !seriesInstanceUID.isEmpty() )
  773. {
  774. QString studySeriesDirectory = studyInstanceUID + "/" + seriesInstanceUID;
  775. //Create thumbnail here
  776. QString thumbnailPath = q->databaseDirectory() +
  777. "/thumbs/" + studyInstanceUID + "/" + seriesInstanceUID
  778. + "/" + sopInstanceUID + ".png";
  779. QFileInfo thumbnailInfo(thumbnailPath);
  780. if( !(thumbnailInfo.exists()
  781. && (thumbnailInfo.lastModified() > QFileInfo(filename).lastModified())))
  782. {
  783. QDir(q->databaseDirectory() + "/thumbs/").mkpath(studySeriesDirectory);
  784. DicomImage dcmImage(QDir::toNativeSeparators(filename).toAscii());
  785. thumbnailGenerator->generateThumbnail(&dcmImage, thumbnailPath);
  786. }
  787. }
  788. if (q->isInMemory())
  789. {
  790. emit q->databaseChanged();
  791. }
  792. }
  793. }
  794. bool ctkDICOMDatabase::fileExistsAndUpToDate(const QString& filePath)
  795. {
  796. Q_D(ctkDICOMDatabase);
  797. bool result(false);
  798. QSqlQuery check_filename_query(database());
  799. check_filename_query.prepare("SELECT InsertTimestamp FROM Images WHERE Filename == ?");
  800. check_filename_query.bindValue(0,filePath);
  801. d->loggedExec(check_filename_query);
  802. if (
  803. check_filename_query.next() &&
  804. QFileInfo(filePath).lastModified() < QDateTime::fromString(check_filename_query.value(0).toString(),Qt::ISODate)
  805. )
  806. {
  807. result = true;
  808. }
  809. check_filename_query.finish();
  810. return result;
  811. }
  812. bool ctkDICOMDatabase::isOpen() const
  813. {
  814. Q_D(const ctkDICOMDatabase);
  815. return d->Database.isOpen();
  816. }
  817. bool ctkDICOMDatabase::isInMemory() const
  818. {
  819. Q_D(const ctkDICOMDatabase);
  820. return d->DatabaseFileName == ":memory:";
  821. }
  822. bool ctkDICOMDatabase::removeSeries(const QString& seriesInstanceUID)
  823. {
  824. Q_D(ctkDICOMDatabase);
  825. // get all images from series
  826. QSqlQuery fileExists ( d->Database );
  827. fileExists.prepare("SELECT Filename, SOPInstanceUID, StudyInstanceUID FROM Images,Series WHERE Series.SeriesInstanceUID = Images.SeriesInstanceUID AND Images.SeriesInstanceUID = :seriesID");
  828. fileExists.bindValue(":seriesID",seriesInstanceUID);
  829. bool success = fileExists.exec();
  830. if (!success)
  831. {
  832. logger.error("SQLITE ERROR: " + fileExists.lastError().driverText());
  833. return false;
  834. }
  835. QList< QPair<QString,QString> > removeList;
  836. while ( fileExists.next() )
  837. {
  838. QString dbFilePath = fileExists.value(fileExists.record().indexOf("Filename")).toString();
  839. QString sopInstanceUID = fileExists.value(fileExists.record().indexOf("SOPInstanceUID")).toString();
  840. QString studyInstanceUID = fileExists.value(fileExists.record().indexOf("StudyInstanceUID")).toString();
  841. QString internalFilePath = studyInstanceUID + "/" + seriesInstanceUID + "/" + sopInstanceUID;
  842. removeList << qMakePair(dbFilePath,internalFilePath);
  843. }
  844. QSqlQuery fileRemove ( d->Database );
  845. fileRemove.prepare("DELETE FROM Images WHERE SeriesInstanceUID == :seriesID");
  846. fileRemove.bindValue(":seriesID",seriesInstanceUID);
  847. logger.debug("SQLITE: removing seriesInstanceUID " + seriesInstanceUID);
  848. success = fileRemove.exec();
  849. if (!success)
  850. {
  851. logger.error("SQLITE ERROR: could not remove seriesInstanceUID " + seriesInstanceUID);
  852. logger.error("SQLITE ERROR: " + fileRemove.lastError().driverText());
  853. }
  854. QPair<QString,QString> fileToRemove;
  855. foreach (fileToRemove, removeList)
  856. {
  857. QString dbFilePath = fileToRemove.first;
  858. QString thumbnailToRemove = databaseDirectory() + "/thumbs/" + fileToRemove.second + ".png";
  859. // check that the file is below our internal storage
  860. if (dbFilePath.startsWith( databaseDirectory() + "/dicom/"))
  861. {
  862. if (!dbFilePath.endsWith(fileToRemove.second))
  863. {
  864. logger.error("Database inconsistency detected during delete!");
  865. continue;
  866. }
  867. if (QFile( dbFilePath ).remove())
  868. {
  869. logger.debug("Removed file " + dbFilePath );
  870. }
  871. else
  872. {
  873. logger.warn("Failed to remove file " + dbFilePath );
  874. }
  875. }
  876. if (QFile( thumbnailToRemove ).remove())
  877. {
  878. logger.debug("Removed thumbnail " + thumbnailToRemove);
  879. }
  880. else
  881. {
  882. logger.warn("Failed to remove thumbnail " + thumbnailToRemove);
  883. }
  884. }
  885. this->cleanup();
  886. d->LastSeriesInstanceUID = "";
  887. return true;
  888. }
  889. bool ctkDICOMDatabase::cleanup()
  890. {
  891. Q_D(ctkDICOMDatabase);
  892. QSqlQuery seriesCleanup ( d->Database );
  893. seriesCleanup.exec("DELETE FROM Series WHERE ( SELECT COUNT(*) FROM Images WHERE Images.SeriesInstanceUID = Series.SeriesInstanceUID ) = 0;");
  894. seriesCleanup.exec("DELETE FROM Studies WHERE ( SELECT COUNT(*) FROM Series WHERE Series.StudyInstanceUID = Studies.StudyInstanceUID ) = 0;");
  895. seriesCleanup.exec("DELETE FROM Patients WHERE ( SELECT COUNT(*) FROM Studies WHERE Studies.PatientsUID = Patients.UID ) = 0;");
  896. return true;
  897. }
  898. bool ctkDICOMDatabase::removeStudy(const QString& studyInstanceUID)
  899. {
  900. Q_D(ctkDICOMDatabase);
  901. QSqlQuery seriesForStudy( d->Database );
  902. seriesForStudy.prepare("SELECT SeriesInstanceUID FROM Series WHERE StudyInstanceUID = :studyID");
  903. seriesForStudy.bindValue(":studyID", studyInstanceUID);
  904. bool success = seriesForStudy.exec();
  905. if (!success)
  906. {
  907. logger.error("SQLITE ERROR: " + seriesForStudy.lastError().driverText());
  908. return false;
  909. }
  910. bool result = true;
  911. while ( seriesForStudy.next() )
  912. {
  913. QString seriesInstanceUID = seriesForStudy.value(seriesForStudy.record().indexOf("SeriesInstanceUID")).toString();
  914. if ( ! this->removeSeries(seriesInstanceUID) )
  915. {
  916. result = false;
  917. }
  918. }
  919. d->LastStudyInstanceUID = "";
  920. return result;
  921. }
  922. bool ctkDICOMDatabase::removePatient(const QString& patientID)
  923. {
  924. Q_D(ctkDICOMDatabase);
  925. QSqlQuery studiesForPatient( d->Database );
  926. studiesForPatient.prepare("SELECT StudyInstanceUID FROM Studies WHERE PatientsUID = :patientsID");
  927. studiesForPatient.bindValue(":patientsID", patientID);
  928. bool success = studiesForPatient.exec();
  929. if (!success)
  930. {
  931. logger.error("SQLITE ERROR: " + studiesForPatient.lastError().driverText());
  932. return false;
  933. }
  934. bool result = true;
  935. while ( studiesForPatient.next() )
  936. {
  937. QString studyInstanceUID = studiesForPatient.value(studiesForPatient.record().indexOf("StudyInstanceUID")).toString();
  938. if ( ! this->removeStudy(studyInstanceUID) )
  939. {
  940. result = false;
  941. }
  942. }
  943. d->LastPatientID = "";
  944. d->LastPatientsName = "";
  945. d->LastPatientsBirthDate = "";
  946. d->LastPatientUID = -1;
  947. return result;
  948. }