]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOGimmick.cpp
Added Add DataBase event.
[creaImageIO.git] / src2 / creaImageIOGimmick.cpp
index 01e49bd352d5a29bc655e57420fb98b71c89c57b..2d759d0b29a2862547278e8aac7a379ae0e51d4f 100644 (file)
@@ -30,48 +30,19 @@ namespace creaImageIO
   //==============================================================
   void Gimmick::Initialize()
   {
+         std::string i_nameDB = "Local database";
     // Create the UserSettings dir if does not exist
     CreateUserSettingsDirectory();
     // Sets the current directory to the home dir
     mCurrentDirectory =  GetHomeDirectory();
        mSynchronizer=0;
 
-    // Create local database handler
-    mLocalDatabase = new SQLiteTreeHandler(GetLocalDatabasePath());
-    // Add it to the TreeHandlerMap
-    mTreeHandlerMap["Local database"] = mLocalDatabase;
+       std::string dbpath = GetLocalDatabasePath();
     // Create or open local database
-    if (! boost::filesystem::exists( GetLocalDatabasePath() ) )
-      {
-       std::string mess = "Local database '";
-       mess += GetLocalDatabasePath();
-       mess += "' does not exist : creating it";
-       GimmickMessage(1,mess<<std::endl);
-       
-       // CREATING DEFAULT DB STRUCTURE
-//     mLocalDatabase->GetTree().GetDescriptor().CreateDefault();
-       mLocalDatabase->GetTree().GetDescriptor().createDescriptorfromFile(mCurrentDirectory + "\\.gimmick\\localdatabase_Descriptor.txt");
-       
-       if ( ! mLocalDatabase->Create(true) )
-         {
-           GimmickError("ERROR CREATING '"<<GetLocalDatabasePath()<<"'");
-         }
-       mLocalDatabase->SetAttribute(0,"Name","Local database");
-      }
-    else 
-      {
-       /// Open and test it
-       GimmickMessage(1,"Opening local database '"
-                      <<GetLocalDatabasePath()<<"' "
-                      <<std::endl);
-       if ( ! mLocalDatabase->Open(true) )
-         {
-           GimmickError("ERROR OPENING '"<<GetLocalDatabasePath()<<"'");
-         }
-       
-      }
-
-
+       mLocalDatabase = createDB( mCurrentDirectory + "\\.gimmick\\localdatabase_Descriptor.txt", dbpath);
+    // Add it to the TreeHandlerMap
+    mTreeHandlerMap[i_nameDB] = mLocalDatabase;
+    
        // Creates files and directories database
     mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
     // Create or open local database
@@ -102,13 +73,66 @@ namespace creaImageIO
       }
 
   }
-  //================================================================
+
+   ///////////////////////////////////////////////////////////////////////
+   // add DB to TreeHandler Map                                                                                        //
+   // @param i_name : DB name                                                                              //
+   // @param i_location : DB location                                                              //
+   // return : -                                                                                                               //
+  ///////////////////////////////////////////////////////////////////////
+       void Gimmick::addDB(std::string &i_name, std::string &i_location)
+       {
+               mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
+       }
+
+  ///////////////////////////////////////////////////////////////////////////
+  // create a DB from a attributes descriptor file for medical images      //
+  // @param i_locDesc : location of descriptor file                                               //
+  // @param i_locDB : location of DB                                                                      //
+  // return : the SQLiteTreeHandler object on DB                                                  //
+       /////////////////////////////////////////////////////////////////////////
+  SQLiteTreeHandler *Gimmick::createDB(std::string &i_locDesc, std::string &i_locDB)
+  {
+      SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
+    // Create or open local database
+    if (! boost::filesystem::exists(i_locDB) )
+     {
+         std::string mess = "Local database '";
+         mess += i_locDB;
+         mess += "' does not exist : creating it";
+         GimmickMessage(1,mess<<std::endl);
+         
+                // CREATING DB STRUCTURE
+         sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
+         if ( ! sqlTreeH->Create(true) )
+                {
+                       GimmickError("ERROR CREATING '"<<i_locDB<<"'");
+         }
+         sqlTreeH->SetAttribute(0,"Name","i_name");
+        }
+        else 
+        {
+               /// Open and test it
+               GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
+        if ( !sqlTreeH->Open(true) )
+               {
+                       GimmickError("ERROR OPENING '"<<i_locDB<<"'");
+               }
+      }
+        return sqlTreeH;
+  }
 
 
   //==============================================================
   void Gimmick::Finalize()
   {
-    delete mLocalDatabase;
+         // delete SQLiteTreeHandler Object
+          for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
+           it!= mTreeHandlerMap.end(); ++it)
+          {
+                  delete it->second;
+          }
        delete mTimestampDatabase;
   }
   //==============================================================