ctkDICOMDatabase.cpp 44 KB

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