]> Creatis software - creaImageIO.git/blob - src/creaImageIOSynchronizer.cpp
8ab3009330fca4d1bdbc9b43253d10557187b025
[creaImageIO.git] / src / creaImageIOSynchronizer.cpp
1 #include <creaImageIOSystem.h>
2 #include <creaImageIOSynchronizer.h>
3 #include "boost/filesystem.hpp"
4
5 namespace fs = boost::filesystem;
6
7 namespace creaImageIO
8 {
9
10   //==============================================================
11         Synchronizer::Synchronizer(TreeHandler * th)
12     : mHandler(th)
13   {    
14    
15   }
16   //==============================================================
17  
18   //==============================================================
19         Synchronizer::~Synchronizer()
20   {    
21    
22   }
23   //==============================================================
24
25   //==============================================================
26   std::string Synchronizer::Synchronize(bool update)
27   {
28           GimmickMessage(1,"Synchronizing "<<std::endl);
29           int id=1;
30           std::stringstream mess;
31           std::string file;
32           mHandler->GetAttribute("Image","","","FullFileName",file);
33           size_t ini=0;
34           size_t fin=0;
35           while(fin<file.size()-1)
36           {
37            fin=file.find('#',ini);
38            SynchronizeFile(update,file.substr(ini,fin-ini),mess);
39            ini=fin+1;
40           }
41           if(mess.str()=="")
42           {
43                   mess<<"Database up to date"<<std::endl;
44           }
45           GimmickMessage(1,mess.str());
46           return mess.str();
47   }
48   //==============================================================
49
50   //==============================================================
51   void Synchronizer::SynchronizeFile(bool update, std::string file, std::stringstream& mess)
52   {
53         if(!FileExists(file))
54         {
55                 if(update)
56                 {
57                         mHandler->DeleteTuple("Image","FullFileName",file);
58                         mess<<file<<" has been removed from the DB"<<std::endl;
59                 }
60                 else
61                 {
62                         mess<<file<<" State: Non existant"<<std::endl;
63                 }
64         }
65         else 
66         {
67                 AttributesMatch(update,file,mess);
68         } 
69   }
70   //==============================================================
71
72   //==============================================================
73   bool Synchronizer::FileExists(std::string file)
74   {
75           GimmickDebugMessage(4,"Verifying if file "<<file<<" exists"<<std::endl);
76           bool exists=true;
77           if ( !fs::exists( file ) )
78           {
79                   exists=false;
80           }
81           return exists;
82   }
83   //==============================================================
84
85   //==============================================================
86   void Synchronizer::AttributesMatch(bool update, std::string file, std::stringstream& mess)
87   {
88           std::map< std::string, std::string>  attr;
89           mHandler->GetTree().GetDescriptor().BuildAttributeMap(attr);
90       mReader.ReadAttributes(file,attr);
91           tree::LevelDescriptor::AttributeDescriptorListType adl= mHandler->GetTree().GetAttributeDescriptorList(mHandler->GetTree().GetNumberOfLevels()-1);    
92           tree::LevelDescriptor::AttributeDescriptorListType::const_iterator a;
93           for (a = adl.begin();a!=adl.end();++a)
94           {
95            std::string databaseVal;
96            mHandler->GetAttribute("Image","FullFileName",file,a->GetKey(),databaseVal);
97            std::string fileVal=attr.find(a->GetKey())->second;
98            if ( a->GetFlags()==0 && databaseVal.compare(fileVal)!=0 ) 
99             {
100                   if(update)
101                         {
102                                 mHandler->SetAttribute("Image",a->GetKey(),fileVal,"FullFileName", file);
103                                 mess<<file<<" has been updated in the DB"<<std::endl;
104                         }
105                         else
106                         {
107                                 mess<<file<<" State: Attributes differ"<<std::endl;
108                         }
109             }
110           }
111   }
112   //==============================================================
113
114 }