]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSynchron.cpp
136c29e7fcd190756e4edb5eeb226b132c50d355
[creaImageIO.git] / src2 / creaImageIOSynchron.cpp
1 #include <creaImageIOSynchron.h>
2 #include <creaImageIOSystem.h>
3 #include <boost/filesystem.hpp>
4
5 namespace fs = boost::filesystem;
6
7 //=====================================================================
8  
9
10 namespace creaImageIO
11 {
12
13         //=====================================================================
14         #define QUERYSYNCDB(QUER,RES)                                           \
15     try                                                                 \
16       {                                                                 \
17         RES = mDB->execQuery(QUER.c_str());                             \
18       }                                                                 \
19     catch (CppSQLite3Exception& e)                                      \
20     {                                                                   \
21       GimmickError("SQLite query '"<<QUER<<"' Error : "                 \
22                    << e.errorCode() << ":"                              \
23                    << e.errorMessage() );                               \
24     }                                                                                                                                      
25         //=====================================================================
26         #define UPDATESYNCDB(UP)                                                        \
27   try                                                                   \
28     {                                                                   \
29       mDB->execDML(UP.c_str());                                         \
30     }                                                                   \
31   catch (CppSQLite3Exception& e)                                        \
32     {                                                                   \
33       GimmickError("SQLite update '"<<UP<<"' Error : "                  \
34                    << e.errorCode() << ":"                              \
35                    << e.errorMessage() );                               \
36     }                           
37         //=====================================================================
38
39         Synchronizer::Synchronizer(const std::string& path)
40     {
41                 pathDB = path + "maintenance_database.db3";
42                 mDB = new CppSQLite3DB;
43                 Initialize();
44     }
45
46         //=====================================================================
47         Synchronizer::~Synchronizer()
48     {
49        
50     }
51
52         //=====================================================================
53         void Synchronizer::Initialize()
54         {    
55                 if (!fs::exists(pathDB)) 
56                 {
57                         CreateDB();
58                 }
59     
60                 // OPENING
61                 else
62                 {
63                 try
64                 {
65                 mDB->open(pathDB.c_str());
66                 }
67                 catch (CppSQLite3Exception& e)
68                 {
69                 GimmickError("Opening '"<<pathDB<<"' : "
70                      << e.errorCode() << ":" 
71                      << e.errorMessage());
72                 }
73                 }
74                 // get the ADD operations List 
75                 //UpdateAddList(pathDB);
76         }
77
78         //=====================================================================
79     void Synchronizer::CreateDB()
80     {
81                 mDB->open(pathDB.c_str());
82         // CREATING TABLES
83         std::string command;
84             command = "CREATE TABLE ";
85             command += "ADD_OPS";
86             command += "\n(\nADD_KEY INTEGER PRIMARY KEY";
87             command += ",\nPATH text";
88                 command += ",\nRECURSIVE boolean";
89                 command += ",\nFILES_ADDED int";
90                 command += ",\nREFERENCEDDB text";
91             command += "\n)";
92                 UPDATESYNCDB(command);
93
94                 command = "CREATE TABLE ";
95             command += "IGNORED_FILES";
96             command += "\n(\nID INTEGER PRIMARY KEY";
97                 command += ",\nADD_KEY integer";
98             command += ",\nPATH text";
99                 command += ",\nREMOVE boolean";
100                 command += ",\nTIME datetext";
101             command += "\n)";
102                 UPDATESYNCDB(command);
103     }
104
105         //=====================================================================
106         void Synchronizer::CleanName(std::string& str) const
107         {
108                 size_t pos;
109                 do
110                 {
111                         pos = str.find('\\');
112                         if (pos!=-1)  
113                         {
114                                 str.replace(pos, 1, "/");
115                         }
116                 }
117                 while (pos!=-1);
118         }
119
120         //=====================================================================
121         void Synchronizer::GetFileList(std::vector<AddList> & list, const std::string& refdb)
122     {
123        CleanList(refdb);
124            list=mAddList;
125     }
126
127         //=====================================================================
128         void Synchronizer::GetIgnoredFiles(const std::string& key, std::vector<std::string> &ignoreList)
129         {
130         ignoreList=GetIgnoreList(key);
131         }
132
133         //=====================================================================
134         void Synchronizer::UpdateAddList(const std::string& refdb)
135     {
136         std::string query = "SELECT * FROM ADD_OPS WHERE REFERENCEDDB = '"+refdb+"';";
137         CppSQLite3Query res;
138         QUERYSYNCDB(query, res);
139                 while (!res.eof())
140         {
141                         AddList temp = AddList(res);
142             mAddList.push_back(temp);
143                         res.nextRow();
144         }
145         }
146
147     /////////////////////////////////////////////////////////////////////////////////////////////////
148     // remove an entry of the DB
149     //@param i_table : table where do the remove
150     // @param i_key : the add_key reference (one entry to remove for ADD_OP table, many for IGNORED_FILES table
151     //@result : -
152     /////////////////////////////////////////////////////////////////////////////////////////////////
153     void Synchronizer::RemoveEntry(const std::string i_table, const std::string i_key)
154     {
155         std::string query = "DELETE  FROM " + i_table + " WHERE ADD_KEY = '" + i_key +"'";
156         UPDATESYNCDB(query);
157         }
158
159     /////////////////////////////////////////////////////////////////////////////////////////////////
160     // clean DataBase if an operation has no child anymore
161         // @param refdb: the database segement to clean
162     // @result : -
163     /////////////////////////////////////////////////////////////////////////////////////////////////
164     void Synchronizer::CleanList(const std::string& refdb)
165     {
166                 mAddList.clear();
167                 UpdateAddList(refdb);
168         std::vector<AddList>::iterator it_add = mAddList.begin();
169         for(;it_add <mAddList.end(); ++it_add)
170         {
171                 if(it_add->nbFiles == "0")
172                 {
173                                         RemoveEntry("ADD_OPS", it_add->key);
174                                         RemoveEntry("IGNORED_FILES", it_add->key);
175                                                 
176                 }
177         }
178                 mAddList.clear();
179                 UpdateAddList(refdb);
180     }
181
182         /////////////////////////////////////////////////////////////////////////////////////////////////
183     // Inserts a new add operation in the database
184     // @param path: the path of the directory that was added
185         // @param recursive: shows if the action was called recursively or not
186         // @param nChildren: the number of files affected by the operation
187         // @param refdb: the referenced database
188     // @result : The operation has been added
189     /////////////////////////////////////////////////////////////////////////////////////////////////
190         void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive,   const std::string& nChildren, const std::string& refdb)
191         {
192                 std::string insert;
193                 std::string pat=path.c_str();
194                 CleanName(pat);
195                 insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED,REFERENCEDDB) VALUES('";
196                 insert+=pat+"','";
197                 insert+=recursive+"',";
198         insert+=nChildren+",'";
199                 insert+=refdb+"');";
200                 UPDATESYNCDB(insert);
201         }
202
203         /////////////////////////////////////////////////////////////////////////////////////////////////
204     // Inserts a new ignored file in the database
205         // @param add_key: the key of the add_op to which it corresponds
206     // @param path: the path of the directory that was added
207         // @param remove: shows if the file was removed or not
208         // @param time: the time in which the file was removed
209     // @result : The file has been inserted
210     /////////////////////////////////////////////////////////////////////////////////////////////////
211
212         void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time, const std::string& refdb )
213         {
214                 std::string pat=path.c_str();
215                 CleanName(pat);
216                 std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat,refdb);
217                 if(id.compare("")==0)
218                 {
219                         std::string insert;
220                         insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
221                         insert+=addKey+"','";
222                         insert+=pat+"','";
223                         insert+=remove+"',";
224                 insert+=time+");";
225                         UPDATESYNCDB(insert);
226                 }
227                 else            
228                 {
229                         //Gets the add key
230                         std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id,refdb);
231                         //gets the parent database to check if the file has been added to the current database
232                         std::string parentDB=GetAttribute("*","ADD_OPS","ADD_KEY",ak,refdb);
233                         //If there is no such entry, add it
234                         if(parentDB.compare("")==0)
235                         {
236                                 std::string insert;
237                                 insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
238                                 insert+=addKey+"','";
239                                 insert+=pat+"','";
240                                 insert+=remove+"',";
241                         insert+=time+");";
242                                 UPDATESYNCDB(insert);
243                         }
244                         else
245                         {
246                                 //Sets the new add key attribute for the file
247                                 SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id,refdb);
248                                 //Sets the new remove attribute for the file
249                                 SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id,refdb);
250                                 //Sets the new time attribute for the file
251                                 SetAttribute("TIME","IGNORED_FILES",time,"ID", id,refdb);
252                         }
253                 }
254         }
255
256     /////////////////////////////////////////////////////////////////////////////////////////////////
257     // get the files name to ignore for a add operation synchronization
258     // @param : the add key
259     //@result : list (path) of ignore files
260     /////////////////////////////////////////////////////////////////////////////////////////////////
261     std::vector<std::string> Synchronizer::GetIgnoreList(const std::string &i_key)
262     {
263                 mIgnoreList.clear();
264         std::vector<std::string> i_names;
265                 std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
266                 query+=i_key;
267         CppSQLite3Query res;
268         QUERYSYNCDB(query, res);
269                 while (!res.eof())
270         {
271                         RemoveList temp = RemoveList(res);
272                         if(temp.remove.compare("0")==0)
273                         {
274             mIgnoreList.push_back(temp);
275                         }
276                         res.nextRow();
277         }
278         std::vector<RemoveList>::iterator it;
279
280         for(it = mIgnoreList.begin();it != mIgnoreList.end(); ++it)
281         {
282             i_names.push_back((*it).path);
283         }
284                 return i_names;
285     }
286
287         /////////////////////////////////////////////////////////////////////////////////////////////////
288     // Gets the required attribute in the required table
289     // @param attribute: the attribute to look for
290     // @param table: the table to look in
291     // @param searchParam: the search parameter
292     // @param searchValue: the search value
293     // @result : required attribute
294     /////////////////////////////////////////////////////////////////////////////////////////////////
295     std::string Synchronizer::GetAttribute(const std::string& attribute, 
296                                                                                 const std::string& table, 
297                                                                                 const std::string& searchParam,
298                                                                                 const std::string& searchValue, 
299                                                                                 const std::string& refdb)
300     {
301         std::stringstream query;
302                 std::string result;
303                 std::string sVal=searchValue.c_str();
304                 CleanName(sVal);
305                 query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal;
306                 if(table.compare("ADD_OPS")==0)
307                 {
308                         query<<"' AND REFERENCEDDB = '"<<refdb<<"';";
309                 }
310                 else
311                 {
312                         query<<"';";
313                 }
314         CppSQLite3Query res;
315         QUERYSYNCDB(query.str(), res);
316                 while (!res.eof())
317         {
318                         result=res.getStringField(0);
319                         res.nextRow();
320         }
321                 return result;
322     }
323
324         /////////////////////////////////////////////////////////////////////////////////////////////////
325     // Sets the attribute value in the required table and column
326     // @param attribute: the attribute to look for
327     // @param table: the table to look in
328         // @param value: the value to set
329     // @param searchParam: the search parameter
330     // @param searchValue: the search value
331     // @result : attribute value changed
332     /////////////////////////////////////////////////////////////////////////////////////////////////
333     void Synchronizer::SetAttribute(const std::string& attribute, 
334                                                                                 const std::string& table, 
335                                                                                 const std::string& value,
336                                                                                 const std::string& searchParam,
337                                                                                 const std::string& searchValue,
338                                                                                 const std::string& refdb)
339     {
340                 std::string val=value.c_str();
341                 std::string sVal=searchValue.c_str();
342                 CleanName(val);
343                 CleanName(sVal);
344                 std::string sql = "UPDATE ";
345                 sql+=table;
346                 sql+=" SET ";
347                 sql += attribute;
348                 sql += " = '";
349                 sql += val;
350                 sql += "' WHERE ";
351                 sql += searchParam;
352                 sql += " = '";
353                 sql += sVal;
354                 if(table.compare("ADD_OPS")==0)
355                 {
356                         sql += "' AND REFERENCEDDB = '";
357                         sql += refdb;
358                 }
359                 sql += "';";
360                 UPDATESYNCDB(sql);
361     }
362