creaImageIOSynchronizer.cpp

Go to the documentation of this file.
00001 #include <creaImageIOSystem.h>
00002 #include <creaImageIOSynchronizer.h>
00003 #include "boost/filesystem.hpp"
00004 
00005 namespace fs = boost::filesystem;
00006 
00007 namespace creaImageIO
00008 {
00009 
00010   //==============================================================
00011         Synchronizer::Synchronizer(TreeHandler * th)
00012     : mHandler(th)
00013   {    
00014    
00015   }
00016   //==============================================================
00017  
00018   //==============================================================
00019         Synchronizer::~Synchronizer()
00020   {    
00021    
00022   }
00023   //==============================================================
00024 
00025   //==============================================================
00026   std::string Synchronizer::Synchronize(bool update)
00027   {
00028           GimmickMessage(1,"Synchronizing "<<std::endl);
00029           int id=1;
00030           std::stringstream mess;
00031           std::string file;
00032           mHandler->GetAttribute("Image","","","FullFileName",file);
00033           size_t ini=0;
00034           size_t fin=0;
00035           while(fin<file.size()-1)
00036           {
00037            fin=file.find('#',ini);
00038            SynchronizeFile(update,file.substr(ini,fin-ini),mess);
00039            ini=fin+1;
00040           }
00041           if(mess.str()=="")
00042           {
00043                   mess<<"Database up to date"<<std::endl;
00044           }
00045           GimmickMessage(1,mess.str());
00046           return mess.str();
00047   }
00048   //==============================================================
00049 
00050   //==============================================================
00051   void Synchronizer::SynchronizeFile(bool update, std::string file, std::stringstream& mess)
00052   {
00053         if(!FileExists(file))
00054         {
00055                 if(update)
00056                 {
00057                         mHandler->DeleteTuple("Image","FullFileName",file);
00058                         mess<<file<<" has been removed from the DB"<<std::endl;
00059                 }
00060                 else
00061                 {
00062                         mess<<file<<" State: Non existant"<<std::endl;
00063                 }
00064         }
00065         else 
00066         {
00067                 AttributesMatch(update,file,mess);
00068         } 
00069   }
00070   //==============================================================
00071 
00072   //==============================================================
00073   bool Synchronizer::FileExists(std::string file)
00074   {
00075           GimmickDebugMessage(4,"Verifying if file "<<file<<" exists"<<std::endl);
00076           bool exists=true;
00077           if ( !fs::exists( file ) )
00078           {
00079                   exists=false;
00080           }
00081           return exists;
00082   }
00083   //==============================================================
00084 
00085   //==============================================================
00086   void Synchronizer::AttributesMatch(bool update, std::string file, std::stringstream& mess)
00087   {
00088           std::map< std::string, std::string>  attr;
00089           mHandler->GetTree().GetDescriptor().BuildAttributeMap(attr);
00090       mReader.ReadAttributes(file,attr);
00091           tree::LevelDescriptor::AttributeDescriptorListType adl= mHandler->GetTree().GetAttributeDescriptorList(mHandler->GetTree().GetNumberOfLevels()-1);    
00092           tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
00093           for (a = adl.begin();a!=adl.end();++a)
00094           {
00095            std::string databaseVal;
00096            mHandler->GetAttribute("Image","FullFileName",file,a->GetKey(),databaseVal);
00097            std::string fileVal=attr.find(a->GetKey())->second;
00098            if ( a->GetFlags()==0 && databaseVal.compare(fileVal)!=0 ) 
00099             {
00100                   if(update)
00101                         {
00102                                 mHandler->SetAttribute("Image",a->GetKey(),fileVal,"FullFileName", file);
00103                                 mess<<file<<" has been updated in the DB"<<std::endl;
00104                         }
00105                         else
00106                         {
00107                                 mess<<file<<" State: Attributes differ"<<std::endl;
00108                         }
00109             }
00110           }
00111   }
00112   //==============================================================
00113 
00114 }