]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOSynchron.cpp
bug correction on addfiles .
[creaImageIO.git] / src2 / creaImageIOSynchron.cpp
index 9ec4bae4d3fcf4b7b68469d0f88debe0158c16ec..06785fe8e94bc32d55335409f95fea662b040920 100644 (file)
@@ -1,8 +1,9 @@
 #include <creaImageIOSynchron.h>
 #include <creaImageIOSystem.h>
 #include <boost/filesystem.hpp>
+#include <boost/algorithm/string.hpp>
+//namespace fs = boost::filesystem;
 
-namespace fs = boost::filesystem;
 
 //=====================================================================
  
@@ -11,7 +12,7 @@ namespace creaImageIO
 {
 
        //=====================================================================
-       #define QUERYSYNCDB(QUER,RES)                                           \
+       #define QUERYSYNCDB(QUER,RES)                                   \
     try                                                                        \
       {                                                                        \
        RES = mDB->execQuery(QUER.c_str());                             \
@@ -23,7 +24,7 @@ namespace creaImageIO
                   << e.errorMessage() );                               \
     }                                                                                                                                     
        //=====================================================================
-       #define UPDATESYNCDB(UP)                                                        \
+       #define UPDATESYNCDB(UP)                                        \
   try                                                                  \
     {                                                                  \
       mDB->execDML(UP.c_str());                                                \
@@ -52,7 +53,7 @@ namespace creaImageIO
        //=====================================================================
        void Synchronizer::Initialize()
        {    
-               if (!fs::exists(pathDB)) 
+               if (!boost::filesystem::exists(pathDB)) 
                {
                        CreateDB();
                }
@@ -72,7 +73,7 @@ namespace creaImageIO
                }
                }
                // get the ADD operations List 
-               UpdateAddList();
+               //UpdateAddList(pathDB);
        }
 
        //=====================================================================
@@ -87,6 +88,7 @@ namespace creaImageIO
            command += ",\nPATH text";
                command += ",\nRECURSIVE boolean";
                command += ",\nFILES_ADDED int";
+               command += ",\nREFERENCEDDB text";
            command += "\n)";
                UPDATESYNCDB(command);
 
@@ -117,9 +119,9 @@ namespace creaImageIO
        }
 
        //=====================================================================
-       void Synchronizer::GetFileList(std::vector<AddList> & list)
+       void Synchronizer::GetFileList(std::vector<AddList> & list, const std::string& refdb)
     {
-       CleanList();
+       CleanList(refdb);
           list=mAddList;
     }
 
@@ -130,9 +132,9 @@ namespace creaImageIO
        }
 
        //=====================================================================
-    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())
@@ -145,7 +147,7 @@ namespace creaImageIO
 
     /////////////////////////////////////////////////////////////////////////////////////////////////
     // 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 : -
     /////////////////////////////////////////////////////////////////////////////////////////////////
@@ -155,14 +157,33 @@ namespace creaImageIO
         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();
+               UpdateAddList(refdb);
         std::vector<AddList>::iterator it_add = mAddList.begin();
         for(;it_add <mAddList.end(); ++it_add)
         {
@@ -174,7 +195,7 @@ namespace creaImageIO
                 }
         }
                mAddList.clear();
-               UpdateAddList();
+               UpdateAddList(refdb);
     }
 
        /////////////////////////////////////////////////////////////////////////////////////////////////
@@ -182,17 +203,19 @@ namespace creaImageIO
     // @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 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 +228,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;
@@ -223,13 +246,29 @@ namespace creaImageIO
                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);
+                       }
                }
        }
 
@@ -273,15 +312,24 @@ 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<<"';";
+               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())
@@ -302,10 +350,11 @@ namespace creaImageIO
     // @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();
@@ -321,7 +370,53 @@ namespace creaImageIO
                sql += searchParam;
                sql += " = '";
                sql += sVal;
-               sql += "'";
+               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()
+    {
+               mList.clear();
+        std::vector<std::string> i_names;
+               std::string query = "SELECT PATH, REMOVE FROM IGNORED_FILES";
+               CppSQLite3Query res;
+        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;
+       }
+}
\ No newline at end of file