Selaa lähdekoodia

Fix for http://www.na-mic.org/Bug/view.php?id=2301

When two different studies for the same subject are being
inserted in the database, be sure to use the correct patientUID
value during the optimization or else the study won't be correctly
linked to the patient and won't be accessible via the database
(and won't show up in a GUI that relies on the database).
Steve Pieper 13 vuotta sitten
vanhempi
commit
96ec6fa986
1 muutettua tiedostoa jossa 27 lisäystä ja 9 poistoa
  1. 27 9
      Libs/DICOM/Core/ctkDICOMDatabase.cpp

+ 27 - 9
Libs/DICOM/Core/ctkDICOMDatabase.cpp

@@ -515,14 +515,27 @@ void ctkDICOMDatabasePrivate::insert( const ctkDICOMDataset& ctkDataset, const Q
     logger.error("SQLITE ERROR: " + fileExists.lastError().driverText());
     return;
   }
-  qDebug() << "filename is: " << fileExists.value(1).toString();
-  qDebug() << "modified date is: " << QFileInfo(fileExists.value(1).toString()).lastModified();
-  qDebug() << "db mod date is: " << QDateTime::fromString(fileExists.value(0).toString(),Qt::ISODate);
-  if ( fileExists.next() && QFileInfo(fileExists.value(1).toString()).lastModified() < QDateTime::fromString(fileExists.value(0).toString(),Qt::ISODate) )
-  {
-    logger.debug ( "File " + fileExists.value(1).toString() + " already added" );
-    return;
-  }
+
+  QString databaseFilename(fileExists.value(1).toString());
+  QDateTime fileLastModified(QFileInfo(databaseFilename).lastModified());
+  QDateTime databaseInsertTimestamp(QDateTime::fromString(fileExists.value(0).toString(),Qt::ISODate));
+
+  qDebug() << "inserting filePath: " << filePath;
+  if (databaseFilename == "")
+    {
+    qDebug() << "database filename for " << sopInstanceUID << " is empty - we should insert on top of it";
+    }
+  else
+    {
+    qDebug() << "database filename for " << sopInstanceUID << " is: " << databaseFilename;
+    qDebug() << "modified date is: " << fileLastModified;
+    qDebug() << "db insert date is: " << databaseInsertTimestamp;
+    if ( fileExists.next() && fileLastModified < databaseInsertTimestamp )
+      {
+      logger.debug ( "File " + databaseFilename + " already added" );
+      return;
+      }
+    }
 
   //If the following fields can not be evaluated, cancel evaluation of the DICOM file
   QString patientsName(ctkDataset.GetElementAsString(DCM_PatientName) );
@@ -611,7 +624,7 @@ void ctkDICOMDatabasePrivate::insert( const ctkDICOMDataset& ctkDataset, const Q
   //The dbPatientID  is a unique number within the database, 
   //generated by the sqlite autoincrement
   //The patientID  is the (non-unique) DICOM patient id
-  int dbPatientID = -1;
+  int dbPatientID = lastPatientUID;
 
   if ( patientID != "" && patientsName != "" )
     {
@@ -621,6 +634,7 @@ void ctkDICOMDatabasePrivate::insert( const ctkDICOMDataset& ctkDataset, const Q
           || lastPatientsBirthDate != patientsBirthDate 
           || lastPatientsName != patientsName )
       {
+      qDebug() << "This looks like a different patient from last insert: " << patientID;
       // Ok, something is different from last insert, let's insert him if he's not
       // already in the db.
       //
@@ -636,6 +650,7 @@ void ctkDICOMDatabasePrivate::insert( const ctkDICOMDataset& ctkDataset, const Q
       {
         // we found him
         dbPatientID = checkPatientExistsQuery.value(checkPatientExistsQuery.record().indexOf("UID")).toInt();
+        qDebug() << "Found patient in the database as UId: " << dbPatientID;
       }
       else
         {
@@ -654,6 +669,7 @@ void ctkDICOMDatabasePrivate::insert( const ctkDICOMDataset& ctkDataset, const Q
         loggedExec(insertPatientStatement);
         dbPatientID = insertPatientStatement.lastInsertId().toInt();
         logger.debug ( "New patient inserted: " + QString().setNum ( dbPatientID ) );
+        qDebug() << "New patient inserted as : " << dbPatientID;
         }
       /// keep this for the next image
       lastPatientUID = dbPatientID;
@@ -662,6 +678,8 @@ void ctkDICOMDatabasePrivate::insert( const ctkDICOMDataset& ctkDataset, const Q
       lastPatientsName = patientsName;
       }
 
+    qDebug() << "Going to insert this instance with dbPatientID: " << dbPatientID;
+
     // Patient is in now. Let's continue with the study
 
     if ( studyInstanceUID != "" && lastStudyInstanceUID != studyInstanceUID )