]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOSynchron.cpp
move directory
[creaImageIO.git] / src2 / creaImageIOSynchron.cpp
index 9ec4bae4d3fcf4b7b68469d0f88debe0158c16ec..0955ffaee260161d386f73b4391d220c5e3c314d 100644 (file)
@@ -1,8 +1,10 @@
 #include <creaImageIOSynchron.h>
 #include <creaImageIOSystem.h>
 #include <boost/filesystem.hpp>
+#include <boost/algorithm/string.hpp>
 
-namespace fs = boost::filesystem;
+
+//namespace fs = boost::filesystem;
 
 //=====================================================================
  
@@ -10,49 +12,49 @@ namespace fs = boost::filesystem;
 namespace creaImageIO
 {
 
-       //=====================================================================
-       #define QUERYSYNCDB(QUER,RES)                                           \
+    //=====================================================================
+    #define QUERYSYNCDB(QUER,RES)                                      \
     try                                                                        \
-      {                                                                        \
+    {                                                                  \
        RES = mDB->execQuery(QUER.c_str());                             \
-      }                                                                        \
+    }                                                                  \
     catch (CppSQLite3Exception& e)                                     \
     {                                                                  \
       GimmickError("SQLite query '"<<QUER<<"' Error : "                        \
                   << e.errorCode() << ":"                              \
                   << e.errorMessage() );                               \
     }                                                                                                                                     
-       //=====================================================================
-       #define UPDATESYNCDB(UP)                                                        \
-  try                                                                  \
+   //=====================================================================
+    #define UPDATESYNCDB(UP)                                           \
+    try                                                                        \
     {                                                                  \
       mDB->execDML(UP.c_str());                                                \
     }                                                                  \
-  catch (CppSQLite3Exception& e)                                       \
+    catch (CppSQLite3Exception& e)                                     \
     {                                                                  \
       GimmickError("SQLite update '"<<UP<<"' Error : "                 \
                   << e.errorCode() << ":"                              \
                   << e.errorMessage() );                               \
     }                          
-       //=====================================================================
+   //=====================================================================
 
-       Synchronizer::Synchronizer(const std::string& path)
+    Synchronizer::Synchronizer(const std::string& path)
     {
                pathDB = path + "maintenance_database.db3";
                mDB = new CppSQLite3DB;
                Initialize();
     }
 
-       //=====================================================================
-       Synchronizer::~Synchronizer()
+   //=====================================================================
+    Synchronizer::~Synchronizer()
     {
-       
+       delete mDB;
     }
 
        //=====================================================================
        void Synchronizer::Initialize()
        {    
-               if (!fs::exists(pathDB)) 
+               if (!boost::filesystem::exists(pathDB)) 
                {
                        CreateDB();
                }
@@ -72,33 +74,34 @@ namespace creaImageIO
                }
                }
                // get the ADD operations List 
-               UpdateAddList();
+               //UpdateAddList(pathDB);
        }
 
-       //=====================================================================
+   //=====================================================================
     void Synchronizer::CreateDB()
     {
                mDB->open(pathDB.c_str());
         // CREATING TABLES
        std::string command;
-           command = "CREATE TABLE ";
-           command += "ADD_OPS";
-           command += "\n(\nADD_KEY INTEGER PRIMARY KEY";
-           command += ",\nPATH text";
-               command += ",\nRECURSIVE boolean";
-               command += ",\nFILES_ADDED int";
-           command += "\n)";
-               UPDATESYNCDB(command);
+       command = "CREATE TABLE ";
+       command += "ADD_OPS";
+       command += "\n(\nADD_KEY INTEGER PRIMARY KEY";
+       command += ",\nPATH text";
+       command += ",\nRECURSIVE boolean";
+       command += ",\nFILES_ADDED int";
+       command += ",\nREFERENCEDDB text";
+       command += "\n)";
+       UPDATESYNCDB(command);
 
-               command = "CREATE TABLE ";
-           command += "IGNORED_FILES";
-           command += "\n(\nID INTEGER PRIMARY KEY";
-               command += ",\nADD_KEY integer";
-           command += ",\nPATH text";
-               command += ",\nREMOVE boolean";
-               command += ",\nTIME datetext";
-           command += "\n)";
-               UPDATESYNCDB(command);
+       command = "CREATE TABLE ";
+       command += "IGNORED_FILES";
+       command += "\n(\nID INTEGER PRIMARY KEY";
+       command += ",\nADD_KEY integer";
+       command += ",\nPATH text";
+       command += ",\nREMOVE boolean";
+       command += ",\nTIME datetext";
+       command += "\n)";
+       UPDATESYNCDB(command);
     }
 
        //=====================================================================
@@ -117,35 +120,35 @@ namespace creaImageIO
        }
 
        //=====================================================================
-       void Synchronizer::GetFileList(std::vector<AddList> & list)
+    void Synchronizer::GetFileList(std::vector<AddList> & list, const std::string& refdb)
     {
-       CleanList();
-          list=mAddList;
+       CleanList(refdb);
+       list=mAddList;
     }
 
        //=====================================================================
-       void Synchronizer::GetIgnoredFiles(const std::string& key, std::vector<std::string> &ignoreList)
-       {
+   void Synchronizer::GetIgnoredFiles(const std::string& key, std::vector<std::string> &ignoreList)
+   {
         ignoreList=GetIgnoreList(key);
-       }
+   }
 
-       //=====================================================================
-    void Synchronizer::UpdateAddList()
+//=====================================================================
+    void Synchronizer::UpdateAddList(const std::string& refdb)
     {
-        std::string query = "SELECT * FROM ADD_OPS";
+        std::string query = "SELECT * FROM ADD_OPS WHERE REFERENCEDDB = '"+refdb+"';";
         CppSQLite3Query res;
         QUERYSYNCDB(query, res);
-               while (!res.eof())
+       while (!res.eof())
         {
-                       AddList temp = AddList(res);
+           AddList temp = AddList(res);
             mAddList.push_back(temp);
-                       res.nextRow();
+           res.nextRow();
         }
-       }
+    }
 
     /////////////////////////////////////////////////////////////////////////////////////////////////
     // remove an entry of the DB
-    //@param i_table : table where do the remove
+    //@param i_table : table where to do the remove
     // @param i_key : the add_key reference (one entry to remove for ADD_OP table, many for IGNORED_FILES table
     //@result : -
     /////////////////////////////////////////////////////////////////////////////////////////////////
@@ -153,46 +156,67 @@ namespace creaImageIO
     {
         std::string query = "DELETE  FROM " + i_table + " WHERE ADD_KEY = '" + i_key +"'";
         UPDATESYNCDB(query);
+    }
+
+       /////////////////////////////////////////////////////////////////////////////////////////////////
+    // remove several entries of the DB
+    // @param i_table : table where to do the remove
+    // @param i_attribute: attribute to match
+    // @param i_operand : operand to use
+    // @param i_val : the reference
+    //@result : -
+    /////////////////////////////////////////////////////////////////////////////////////////////////
+    void Synchronizer::RemoveEntries(const std::string i_table, 
+               const std::string i_attribute, 
+               const std::string i_operand, 
+               const std::string i_val)
+    {
+        std::stringstream query;
+       query<<"DELETE  FROM "<<i_table<<" WHERE "<<i_attribute<<" "<<i_operand<<" '"<<i_val<<"'";
+        UPDATESYNCDB(query.str());
        }
 
     /////////////////////////////////////////////////////////////////////////////////////////////////
     // clean DataBase if an operation has no child anymore
+       // @param refdb: the database segement to clean
     // @result : -
     /////////////////////////////////////////////////////////////////////////////////////////////////
-    void Synchronizer::CleanList()
+    void Synchronizer::CleanList(const std::string& refdb)
     {
-               mAddList.clear();
-               UpdateAddList();
+       mAddList.clear();
+       UpdateAddList(refdb);
         std::vector<AddList>::iterator it_add = mAddList.begin();
         for(;it_add <mAddList.end(); ++it_add)
         {
                 if(it_add->nbFiles == "0")
                 {
-                                       RemoveEntry("ADD_OPS", it_add->key);
-                                       RemoveEntry("IGNORED_FILES", it_add->key);
-                                               
+                       RemoveEntry("ADD_OPS", it_add->key);
+                       RemoveEntry("IGNORED_FILES", it_add->key);
+
                 }
         }
                mAddList.clear();
-               UpdateAddList();
+               UpdateAddList(refdb);
     }
 
-       /////////////////////////////////////////////////////////////////////////////////////////////////
+    /////////////////////////////////////////////////////////////////////////////////////////////////
     // Inserts a new add operation in the database
     // @param path: the path of the directory that was added
-       // @param recursive: shows if the action was called recursively or not
-       // @param nChildren: the number of files affected by the operation
+    // @param recursive: shows if the action was called recursively or not
+    // @param nChildren: the number of files affected by the operation
+    // @param refdb: the referenced database
     // @result : The operation has been added
     /////////////////////////////////////////////////////////////////////////////////////////////////
-       void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive,   const std::string& nChildren)
+       void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive, const std::string& nChildren, const std::string& refdb)
        {
                std::string insert;
                std::string pat=path.c_str();
                CleanName(pat);
-               insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED) VALUES('";
+               insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED,REFERENCEDDB) VALUES('";
                insert+=pat+"','";
                insert+=recursive+"',";
-       insert+=nChildren+");";
+               insert+=nChildren+",'";
+               insert+=refdb+"');";
                UPDATESYNCDB(insert);
        }
 
@@ -205,11 +229,11 @@ namespace creaImageIO
     // @result : The file has been inserted
     /////////////////////////////////////////////////////////////////////////////////////////////////
 
-       void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time)
+       void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time, const std::string& refdb )
        {
                std::string pat=path.c_str();
                CleanName(pat);
-               std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat);
+               std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat,refdb);
                if(id.compare("")==0)
                {
                        std::string insert;
@@ -217,19 +241,35 @@ namespace creaImageIO
                        insert+=addKey+"','";
                        insert+=pat+"','";
                        insert+=remove+"',";
-               insert+=time+");";
+                       insert+=time+");";
                        UPDATESYNCDB(insert);
                }
-               else            
+               else
                {
                        //Gets the add key
-                       std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id);
-                       //Sets the new add key attribute for the file
-            SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id);
-                       //Sets the new remove attribute for the file
-                       SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id);
-                       //Sets the new time attribute for the file
-                       SetAttribute("TIME","IGNORED_FILES",time,"ID", id);
+                       std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id,refdb);
+                       //gets the parent database to check if the file has been added to the current database
+                       std::string parentDB=GetAttribute("*","ADD_OPS","ADD_KEY",ak,refdb);
+                       //If there is no such entry, add it
+                       if(parentDB.compare("")==0)
+                       {
+                               std::string insert;
+                               insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
+                               insert+=addKey+"','";
+                               insert+=pat+"','";
+                               insert+=remove+"',";
+                               insert+=time+");";
+                               UPDATESYNCDB(insert);
+                       }
+                       else
+                       {
+                               //Sets the new add key attribute for the file
+                               SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id,refdb);
+                               //Sets the new remove attribute for the file
+                               SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id,refdb);
+                               //Sets the new time attribute for the file
+                               SetAttribute("TIME","IGNORED_FILES",time,"ID", id,refdb);
+                       }
                }
        }
 
@@ -240,20 +280,20 @@ namespace creaImageIO
     /////////////////////////////////////////////////////////////////////////////////////////////////
     std::vector<std::string> Synchronizer::GetIgnoreList(const std::string &i_key)
     {
-               mIgnoreList.clear();
+        mIgnoreList.clear();
         std::vector<std::string> i_names;
-               std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
-               query+=i_key;
+       std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
+       query+=i_key;
         CppSQLite3Query res;
         QUERYSYNCDB(query, res);
-               while (!res.eof())
+       while (!res.eof())
         {
-                       RemoveList temp = RemoveList(res);
-                       if(temp.remove.compare("0")==0)
-                       {
-            mIgnoreList.push_back(temp);
-                       }
-                       res.nextRow();
+               RemoveList temp = RemoveList(res);
+               if(temp.remove.compare("0")==0)
+               {
+                   mIgnoreList.push_back(temp);
+               }
+               res.nextRow();
         }
         std::vector<RemoveList>::iterator it;
 
@@ -261,10 +301,10 @@ namespace creaImageIO
         {
             i_names.push_back((*it).path);
         }
-               return i_names;
+       return i_names;
     }
 
-       /////////////////////////////////////////////////////////////////////////////////////////////////
+    /////////////////////////////////////////////////////////////////////////////////////////////////
     // Gets the required attribute in the required table
     // @param attribute: the attribute to look for
     // @param table: the table to look in
@@ -273,55 +313,139 @@ namespace creaImageIO
     // @result : required attribute
     /////////////////////////////////////////////////////////////////////////////////////////////////
     std::string Synchronizer::GetAttribute(const std::string& attribute, 
-                                                                               const std::string& table, 
-                                                                               const std::string& searchParam,
-                                                                               const std::string& searchValue)
+                                       const std::string& table, 
+                                       const std::string& searchParam,
+                                       const std::string& searchValue, 
+                                       const std::string& refdb)
     {
         std::stringstream query;
-               std::string result;
-               std::string sVal=searchValue.c_str();
-               CleanName(sVal);
-               query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal<<"';";
+       std::string result;
+       std::string sVal=searchValue.c_str();
+       CleanName(sVal);
+       query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal;
+       if(table.compare("ADD_OPS")==0)
+       {
+               query<<"' AND REFERENCEDDB = '"<<refdb<<"';";
+       }
+       else
+       {
+               query<<"';";
+       }
         CppSQLite3Query res;
         QUERYSYNCDB(query.str(), res);
-               while (!res.eof())
+       while (!res.eof())
         {
-                       result=res.getStringField(0);
-                       res.nextRow();
+               result=res.getStringField(0);
+               res.nextRow();
         }
-               return result;
+       return result;
     }
 
-       /////////////////////////////////////////////////////////////////////////////////////////////////
+    /////////////////////////////////////////////////////////////////////////////////////////////////
     // Sets the attribute value in the required table and column
     // @param attribute: the attribute to look for
     // @param table: the table to look in
-       // @param value: the value to set
+    // @param value: the value to set
     // @param searchParam: the search parameter
     // @param searchValue: the search value
     // @result : attribute value changed
     /////////////////////////////////////////////////////////////////////////////////////////////////
     void Synchronizer::SetAttribute(const std::string& attribute, 
-                                                                               const std::string& table, 
-                                                                               const std::string& value,
-                                                                               const std::string& searchParam,
-                                                                               const std::string& searchValue)
+                               const std::string& table, 
+                               const std::string& value,
+                               const std::string& searchParam,
+                               const std::string& searchValue,
+                               const std::string& refdb)
     {
-               std::string val=value.c_str();
-               std::string sVal=searchValue.c_str();
-               CleanName(val);
-               CleanName(sVal);
-               std::string sql = "UPDATE ";
-               sql+=table;
-               sql+=" SET ";
-               sql += attribute;
-               sql += " = '";
-               sql += val;
-               sql += "' WHERE ";
-               sql += searchParam;
-               sql += " = '";
-               sql += sVal;
-               sql += "'";
-               UPDATESYNCDB(sql);
+       std::string val=value.c_str();
+       std::string sVal=searchValue.c_str();
+       CleanName(val);
+       CleanName(sVal);
+       std::string sql = "UPDATE ";
+       sql+=table;
+       sql+=" SET ";
+       sql += attribute;
+       sql += " = '";
+       sql += val;
+       sql += "' WHERE ";
+       sql += searchParam;
+       sql += " = '";
+       sql += sVal;
+       if(table.compare("ADD_OPS")==0)
+       {
+               sql += "' AND REFERENCEDDB = '";
+               sql += refdb;
+       }
+       sql += "';";
+       UPDATESYNCDB(sql);
     }
-} 
\ No newline at end of file
+
+
+
+/////////////////////////////////////////////////////////////////////////////////////////////////
+    // get the files name to ignore for a add operation synchronization
+    // @param : the add key
+    //@result : list (path) of ignore files
+    /////////////////////////////////////////////////////////////////////////////////////////////////
+       void Synchronizer::GetList(const std::string i_db)
+    {
+               mList.clear();
+        std::vector<std::string> i_names;
+               std::vector<std::string> keys;
+               CppSQLite3Query res;
+               std::string query ="SELECT ADD_KEY, REFERENCEDDB FROM  ADD_OPS";
+               QUERYSYNCDB(query, res);
+               keys.clear();
+               while (!res.eof())
+        {
+                       std::string key(res.getStringField(0));
+                       std::string db(res.getStringField(1));
+                       if (db == i_db)
+                       {
+                               keys.push_back(key);
+                       }
+                       res.nextRow();
+               }
+               query = "SELECT PATH, REMOVE FROM IGNORED_FILES WHERE";
+               if(keys.size() > 0)
+               {
+                       for (int i=0; i < keys.size(); i++)
+                       {
+                               query += " ADD_KEY = " + keys[i];
+                               query += " AND";
+                       }
+                       query = query.substr(0,query.size() - 4);
+               }
+               else 
+               {
+                       query += " ADD_KEY = -1";
+               }
+        QUERYSYNCDB(query, res);
+               while (!res.eof())
+        {
+               std::string file(res.getStringField(0));
+               std::string ignore(res.getStringField(1));
+               mList[file] = ignore == "0"? true : false;
+               res.nextRow();
+        }
+    }
+
+       bool Synchronizer::isIndexed(const std::string filename)
+       {
+               bool valid = true;
+               std::string name(filename);
+               boost::algorithm::replace_all( name,"\\" , "/");
+               std::map <std::string, bool>::iterator it_list = mList.begin();
+               for(;it_list != mList.end(); it_list++)
+               {
+                       if(it_list->first == name)
+                       {
+                               valid = false;
+                               break;
+                       }
+               }
+               return valid;
+       }
+}
+
+