]> Creatis software - creaImageIO.git/commitdiff
Added synchronization classes and methods.
authorcaballero <caballero>
Fri, 10 Apr 2009 14:34:15 +0000 (14:34 +0000)
committercaballero <caballero>
Fri, 10 Apr 2009 14:34:15 +0000 (14:34 +0000)
src2/CMakeLists.txt
src2/creaImageIOGimmick.cpp
src2/creaImageIOGimmick.h
src2/creaImageIOGimmickView.cpp
src2/creaImageIOSQLiteTreeHandler.cpp
src2/creaImageIOSQLiteTreeHandler.h
src2/creaImageIOSynchronizer.cpp [new file with mode: 0644]
src2/creaImageIOSynchronizer.h [new file with mode: 0644]
src2/creaImageIOTreeHandler.h

index b3c1c0a9dd989fcc458e1c16d78926d6a4e23de7..83bab4a5091cd2639a5c05ae964530eba64a694b 100644 (file)
@@ -33,6 +33,7 @@ SET( SRCS
   
   # 
   creaImageIOGimmick
+  creaImageIOSynchronizer
 
   # Abstract views
   creaImageIOGimmickView
@@ -50,7 +51,7 @@ SET( SRCS
 
   #  Viewer
   creaImageIOWxViewer
-creaImageIOImagePointerHolder.h
+  creaImageIOImagePointerHolder.h
 
 
 )
index ced62fa28882e1d5ca1b9e6f6fd49dc631a3a642..8d1583ccac4ddd4fd75b62d39535e32e8cd05e8f 100644 (file)
@@ -34,7 +34,7 @@ namespace creaImageIO
     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
@@ -202,13 +202,32 @@ namespace creaImageIO
     GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
                   <<recurse<<std::endl);
 
-    mImageAdder.SetTreeHandler(GetTreeHandler(d));
+       TreeHandler * handler=GetTreeHandler(d);
+    mImageAdder.SetTreeHandler(handler);
     mImageAdder.AddDirectory(f,recurse);
+       //Synchronize(true, handler);
     
   }
 
   //========================================================================
 
+  //========================================================================
+  void Gimmick::Synchronize(bool update, TreeHandler* handler)
+  {
+         GimmickMessage(4,"Synchronizing. Update:"<<update<<std::endl);
+         if(mSynchronizer==0)
+         {
+                 mSynchronizer=new Synchronizer(handler);
+         }
+         else
+         {
+                 mSynchronizer->SetTreeHandler(handler);
+         }
+         mSynchronizer->Synchronize(update);
+
+  }
+
   //========================================================================
   /// 
   void Gimmick::Print(const std::string& d)
index cdb76764fcb99f143b7ccac4c6d4c827f691151e..93d982a99c10a12d940665891ec4c957f1596f35 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <creaImageIOSQLiteTreeHandler.h>
 #include <creaImageIOTreeHandlerImageAdder.h>
+#include <creaImageIOSynchronizer.h>
 
 namespace creaImageIO
 {
@@ -89,6 +90,10 @@ namespace creaImageIO
     void AddDir(const std::string& handler, const std::string& path, 
                bool recurse);
 
+       ///Synchronizes the loaded data with the database. If remove is true the database will be updated, otherwise 
+       ///only a warning sign will be issued
+       void Synchronize(bool update, TreeHandler* handler);
+
     /// Prints the tree handled by the handler
     void Print(const std::string& handler);
 
@@ -114,6 +119,7 @@ namespace creaImageIO
   private:
     SQLiteTreeHandler* mLocalDatabase;
     TreeHandlerMapType mTreeHandlerMap;
+       Synchronizer* mSynchronizer;
 
     std::string mCurrentDirectory;
     std::string mHomeDirectory;
index 8c0884c3e4926406e2d2387ba39612e42ad942fd..40bd219cd5fbcedc1869a7fe152be09cf2e62b50 100644 (file)
@@ -162,7 +162,10 @@ namespace creaImageIO
   {
          ImageExtent * extent= (ImageExtent*)&ie;
          mExtent[2]+=(*extent).Get(2);
+         if(mExtent[2]>1)
+         {
          SetDimension(3);
+         }
   }
 
   //======================================================================
index 17025b4604cbc4161236d0ae94dfa61701212380..8dcdd1795dcda95800944e9934bddd3f7c33200f 100644 (file)
@@ -188,6 +188,24 @@ namespace creaImageIO
     return DBSetAttribute(n,key,value);
   }
   //===================================================================== 
+   //===================================================================== 
+  /// Sets an attribute
+  void SQLiteTreeHandler::SetAttribute(const std::string& levelDescriptor, 
+                             const std::string& key,
+                             const std::string& value,
+                                 const std::string& searchParam, 
+                                 const std::string& searchVal)
+  {
+       DBSetAttribute(levelDescriptor,key,value,searchParam, searchVal);
+  }
+  //===================================================================== 
+  /// Deletes a tuple
+  void SQLiteTreeHandler::DeleteTuple(std::string levelDescriptor, 
+                                                               std::string key, std::string value)
+  {
+    DBDelete(levelDescriptor,key,value);
+  }
+  //===================================================================== 
 
 
 
@@ -858,10 +876,30 @@ namespace creaImageIO
     //    sql += " LIMIT 1";
     UPDATEDB(sql);
   }
-  //===================================================================== 
 
+  //===================================================================== 
+  /// Sets an attribute of a Node
+  void SQLiteTreeHandler::DBSetAttribute(const std::string& levelDescriptor, 
+                             const std::string& key,
+                             const std::string& value,
+                                 const std::string& searchParam, 
+                                 const std::string& searchVal)
+  {
 
-  //=====================================================================
+    std::string sql = "UPDATE ";
+    sql += levelDescriptor;
+    sql += " SET ";
+    sql += key;
+    sql += " = '";
+    sql += value;
+    sql += "' WHERE ";
+       sql += searchParam;
+       sql += " = '";
+    sql += searchVal;
+       sql += "'";
+    UPDATEDB(sql);
+  }
+   //=====================================================================
   void SQLiteTreeHandler::DBRecursiveRemoveNode(Node* node)
   {
        
@@ -887,6 +925,52 @@ namespace creaImageIO
       }
   }
 
+  //=====================================================================
+  void SQLiteTreeHandler::DBDelete(std::string levelDescriptor, std::string key, std::string value)
+  {
+       
+    std::stringstream query;
+       query<<"DELETE FROM "<<levelDescriptor<<" WHERE "<<key<<"='"<<value<<"';";
+    UPDATEDB(query.str());
+       GimmickMessage(1," Deleting: Query: "<<query.str()<<std::endl);
+  }
+
+
+  //===================================================================== 
+  void SQLiteTreeHandler::GetAttribute(std::string levelDescriptor,
+                                                                          std::string searchParam, 
+                                                                          std::string searchVal, 
+                                                                          std::string key, 
+                                                                          std::string& result) 
+  { 
+       std::stringstream out;
+       std::stringstream results;
+       out<<"SELECT "<<key<<" FROM "<<levelDescriptor;
+       if(searchParam!="")
+       {
+               out<<" WHERE "<<searchParam<<"='"<<searchVal<<"'";
+       }
+       
+       CppSQLite3Query q;
+       QUERYDB(out.str(),q);
+       
+       
+       while (!q.eof())
+         {
+           for (int fld = 0; fld < q.numFields(); fld++)
+             {
+                         results<<q.getStringField(fld);
+                         if(searchParam=="")
+                         {
+                                 results<<"#";
+                         }
+             }
+           q.nextRow();
+         }
+       result=results.str();
+      
+  }
   //===================================================================== 
   unsigned int SQLiteTreeHandler::GetNumberOfChildren(tree::Node* n) 
   { 
index cb100c0f208c7df075db30a25a85784a33531806..0f45f7093503360e45ff9d0300c6b9dc79019e9e 100644 (file)
@@ -70,6 +70,16 @@ namespace creaImageIO
     virtual unsigned int GetNumberOfChildren(tree::Node* n);
     //====================================================================
 
+       //====================================================================
+    /// Returns the attribute requested. Useful for synchronization.
+       virtual void GetAttribute(std::string levelDescriptor,
+                                                                          std::string searchParam, 
+                                                                          std::string searchVal, 
+                                                                          std::string key, 
+                                                                          std::string& result);
+    //====================================================================
+
+
     //====================================================================
     /// Recursively loads the children of node 'parent' until maxlevel 
     // is reached.
@@ -100,6 +110,15 @@ namespace creaImageIO
     virtual bool SetAttribute(tree::Node*, 
                              const std::string& key,
                              const std::string& value);
+       // Sets an attribute
+    virtual void SetAttribute(const std::string& levelDescriptor, 
+                             const std::string& key,
+                             const std::string& value,
+                                 const std::string& searchParam, 
+                                 const std::string& searchVal);
+       //Deletes the tuple that matches the parameters given
+       virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value);
     //====================================================================
     
 
@@ -154,11 +173,26 @@ namespace creaImageIO
                        const std::string& value);
     //======================================================================
     //======================================================================
-
+       /// Sets an attribute and updates the database
+       void DBSetAttribute(const std::string& levelDescriptor, 
+                             const std::string& key,
+                             const std::string& value,
+                                 const std::string& searchParam, 
+                                 const std::string& searchVal);
+    //======================================================================
+    //======================================================================
+       
     /// Inserts the Node in the database
     void DBInsert(tree::Node* n);
     //======================================================================
 
+       
+       //======================================================================
+
+    /// Deletes the tuple that matches the value specified in the given key and that belongs to the given level
+       void DBDelete(std::string levelDescriptor, std::string key, std::string value);
+    //======================================================================
+
 
        /// Recursively Removes the nodes whose parent is given as a parameter
     void DBRecursiveRemoveNode(tree::Node* node);
diff --git a/src2/creaImageIOSynchronizer.cpp b/src2/creaImageIOSynchronizer.cpp
new file mode 100644 (file)
index 0000000..8ab3009
--- /dev/null
@@ -0,0 +1,114 @@
+#include <creaImageIOSystem.h>
+#include <creaImageIOSynchronizer.h>
+#include "boost/filesystem.hpp"
+
+namespace fs = boost::filesystem;
+
+namespace creaImageIO
+{
+
+  //==============================================================
+       Synchronizer::Synchronizer(TreeHandler * th)
+    : mHandler(th)
+  {    
+   
+  }
+  //==============================================================
+  //==============================================================
+       Synchronizer::~Synchronizer()
+  {    
+   
+  }
+  //==============================================================
+
+  //==============================================================
+  std::string Synchronizer::Synchronize(bool update)
+  {
+         GimmickMessage(1,"Synchronizing "<<std::endl);
+         int id=1;
+         std::stringstream mess;
+         std::string file;
+         mHandler->GetAttribute("Image","","","FullFileName",file);
+         size_t ini=0;
+         size_t fin=0;
+         while(fin<file.size()-1)
+         {
+          fin=file.find('#',ini);
+          SynchronizeFile(update,file.substr(ini,fin-ini),mess);
+          ini=fin+1;
+         }
+         if(mess.str()=="")
+         {
+                 mess<<"Database up to date"<<std::endl;
+         }
+         GimmickMessage(1,mess.str());
+         return mess.str();
+  }
+  //==============================================================
+
+  //==============================================================
+  void Synchronizer::SynchronizeFile(bool update, std::string file, std::stringstream& mess)
+  {
+       if(!FileExists(file))
+       {
+               if(update)
+               {
+                       mHandler->DeleteTuple("Image","FullFileName",file);
+                       mess<<file<<" has been removed from the DB"<<std::endl;
+               }
+               else
+               {
+                       mess<<file<<" State: Non existant"<<std::endl;
+               }
+       }
+       else 
+       {
+               AttributesMatch(update,file,mess);
+       } 
+  }
+  //==============================================================
+
+  //==============================================================
+  bool Synchronizer::FileExists(std::string file)
+  {
+         GimmickDebugMessage(4,"Verifying if file "<<file<<" exists"<<std::endl);
+         bool exists=true;
+         if ( !fs::exists( file ) )
+         {
+                 exists=false;
+         }
+         return exists;
+  }
+  //==============================================================
+
+  //==============================================================
+  void Synchronizer::AttributesMatch(bool update, std::string file, std::stringstream& mess)
+  {
+         std::map< std::string, std::string>  attr;
+         mHandler->GetTree().GetDescriptor().BuildAttributeMap(attr);
+      mReader.ReadAttributes(file,attr);
+         tree::LevelDescriptor::AttributeDescriptorListType adl= mHandler->GetTree().GetAttributeDescriptorList(mHandler->GetTree().GetNumberOfLevels()-1);    
+         tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
+         for (a = adl.begin();a!=adl.end();++a)
+         {
+          std::string databaseVal;
+          mHandler->GetAttribute("Image","FullFileName",file,a->GetKey(),databaseVal);
+          std::string fileVal=attr.find(a->GetKey())->second;
+          if ( a->GetFlags()==0 && databaseVal.compare(fileVal)!=0 ) 
+           {
+                 if(update)
+                       {
+                               mHandler->SetAttribute("Image",a->GetKey(),fileVal,"FullFileName", file);
+                               mess<<file<<" has been updated in the DB"<<std::endl;
+                       }
+                       else
+                       {
+                               mess<<file<<" State: Attributes differ"<<std::endl;
+                       }
+           }
+         }
+  }
+  //==============================================================
+
+}
\ No newline at end of file
diff --git a/src2/creaImageIOSynchronizer.h b/src2/creaImageIOSynchronizer.h
new file mode 100644 (file)
index 0000000..fa3ae37
--- /dev/null
@@ -0,0 +1,43 @@
+#ifndef __creaImageIOSynchronizer_h_INCLUDED__
+#define __creaImageIOSynchronizer_h_INCLUDED__
+
+#include <creaImageIOSQLiteTreeHandler.h>
+#include <creaImageIOImageReader.h>
+
+namespace creaImageIO
+{
+
+//=======================================================================
+/// Synchronizes a given database with disk
+class Synchronizer
+  {
+  public:
+      /// Ctor
+      Synchronizer(TreeHandler* th);
+      /// Dtor
+      ~Synchronizer();
+         ///Sets the tree handler to use in order to synchronize with a given database
+         void SetTreeHandler(TreeHandler * handler){mHandler=handler;}
+         ///Synchronizes the database in the current tree handler with disk by doing the passed action. If it is true,
+         ///the database will be updated, otherwise a warning message will be returned.
+         std::string Synchronize(bool update);
+         ///Checks if the file given as a parameter exists in the drive
+         bool FileExists(std::string file);
+         ///Checks if the attributes of the node given as a parameter matchwith it correspondent file in disk 
+         ///NB: This method doesn't check the existence of the file, so FileExists should be called before.
+         void AttributesMatch(bool update, std::string file, std::stringstream& mess);
+
+
+  private:
+         ///The tree handler
+         TreeHandler* mHandler;
+         ///The image reader
+         ImageReader mReader;
+         ///Synchronizes the given file, doing the action required and returning the result on the string supplied
+         void SynchronizeFile(bool update, std::string file, std::stringstream& message);
+
+  };
+} // EO namespace creaImageIO
+
+// EOF
+#endif 
\ No newline at end of file
index 16f599cddf52f69460a8b53082817499e51362f3..5a7cecd32f644aa3f9cb1fb47a70b7417b824cb6 100644 (file)
@@ -79,6 +79,15 @@ namespace creaImageIO
     virtual unsigned int GetNumberOfChildren(tree::Node* n) { return 0; }
     //====================================================================
 
+       //====================================================================
+    /// Returns the attribute requested. Useful for synchronization.
+       virtual void GetAttribute(std::string levelDescriptor,
+                                                                          std::string searchParam, 
+                                                                          std::string searchVal, 
+                                                                          std::string key, 
+                                                                          std::string& result){}
+    //====================================================================
+
     //====================================================================
     /// Recursively loads the children of node 'parent' until maxlevel 
     // is reached.
@@ -112,6 +121,15 @@ namespace creaImageIO
     virtual bool SetAttribute(tree::Node*, 
                              const std::string& key,
                              const std::string& value) { return false; }
+       // Sets an attribute
+    virtual void SetAttribute(const std::string& levelDescriptor, 
+                             const std::string& key,
+                             const std::string& value,
+                                 const std::string& searchParam, 
+                                 const std::string& searchVal){}
+       //Deletes the tuple that matches the parameters given
+       virtual void DeleteTuple(std::string levelDescriptor, std::string key, std::string value){}
     //====================================================================