#
creaImageIOGimmick
+ creaImageIOSynchronizer
# Abstract views
creaImageIOGimmickView
# Viewer
creaImageIOWxViewer
-creaImageIOImagePointerHolder.h
+ creaImageIOImagePointerHolder.h
)
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
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)
#include <creaImageIOSQLiteTreeHandler.h>
#include <creaImageIOTreeHandlerImageAdder.h>
+#include <creaImageIOSynchronizer.h>
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);
private:
SQLiteTreeHandler* mLocalDatabase;
TreeHandlerMapType mTreeHandlerMap;
+ Synchronizer* mSynchronizer;
std::string mCurrentDirectory;
std::string mHomeDirectory;
{
ImageExtent * extent= (ImageExtent*)&ie;
mExtent[2]+=(*extent).Get(2);
+ if(mExtent[2]>1)
+ {
SetDimension(3);
+ }
}
//======================================================================
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);
+ }
+ //=====================================================================
// 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)
{
}
}
+ //=====================================================================
+ 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)
{
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.
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);
+
//====================================================================
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);
--- /dev/null
+#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
--- /dev/null
+#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
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.
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){}
+
//====================================================================