]> Creatis software - creaImageIO.git/blob - src2/creaImageIOSynchron.cpp
Added create DB.
[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();
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 += "\n)";
91                 UPDATESYNCDB(command);
92
93                 command = "CREATE TABLE ";
94             command += "IGNORED_FILES";
95             command += "\n(\nID INTEGER PRIMARY KEY";
96                 command += ",\nADD_KEY integer";
97             command += ",\nPATH text";
98                 command += ",\nREMOVE boolean";
99                 command += ",\nTIME datetext";
100             command += "\n)";
101                 UPDATESYNCDB(command);
102     }
103
104         //=====================================================================
105         void Synchronizer::CleanName(std::string& str) const
106         {
107                 size_t pos;
108                 do
109                 {
110                         pos = str.find('\\');
111                         if (pos!=-1)  
112                         {
113                                 str.replace(pos, 1, "/");
114                         }
115                 }
116                 while (pos!=-1);
117         }
118
119         //=====================================================================
120         void Synchronizer::GetFileList(std::vector<AddList> & list)
121     {
122        CleanList();
123            list=mAddList;
124     }
125
126         //=====================================================================
127         void Synchronizer::GetIgnoredFiles(const std::string& key, std::vector<std::string> &ignoreList)
128         {
129         ignoreList=GetIgnoreList(key);
130         }
131
132         //=====================================================================
133     void Synchronizer::UpdateAddList()
134     {
135         std::string query = "SELECT * FROM ADD_OPS";
136         CppSQLite3Query res;
137         QUERYSYNCDB(query, res);
138                 while (!res.eof())
139         {
140                         AddList temp = AddList(res);
141             mAddList.push_back(temp);
142                         res.nextRow();
143         }
144         }
145
146     /////////////////////////////////////////////////////////////////////////////////////////////////
147     // remove an entry of the DB
148     //@param i_table : table where do the remove
149     // @param i_key : the add_key reference (one entry to remove for ADD_OP table, many for IGNORED_FILES table
150     //@result : -
151     /////////////////////////////////////////////////////////////////////////////////////////////////
152     void Synchronizer::RemoveEntry(const std::string i_table, const std::string i_key)
153     {
154         std::string query = "DELETE  FROM " + i_table + " WHERE ADD_KEY = '" + i_key +"'";
155         UPDATESYNCDB(query);
156         }
157
158     /////////////////////////////////////////////////////////////////////////////////////////////////
159     // clean DataBase if an operation has no child anymore
160     // @result : -
161     /////////////////////////////////////////////////////////////////////////////////////////////////
162     void Synchronizer::CleanList()
163     {
164                 mAddList.clear();
165                 UpdateAddList();
166         std::vector<AddList>::iterator it_add = mAddList.begin();
167         for(;it_add <mAddList.end(); ++it_add)
168         {
169                 if(it_add->nbFiles == "0")
170                 {
171                                         RemoveEntry("ADD_OPS", it_add->key);
172                                         RemoveEntry("IGNORED_FILES", it_add->key);
173                                                 
174                 }
175         }
176                 mAddList.clear();
177                 UpdateAddList();
178     }
179
180         /////////////////////////////////////////////////////////////////////////////////////////////////
181     // Inserts a new add operation in the database
182     // @param path: the path of the directory that was added
183         // @param recursive: shows if the action was called recursively or not
184         // @param nChildren: the number of files affected by the operation
185     // @result : The operation has been added
186     /////////////////////////////////////////////////////////////////////////////////////////////////
187         void Synchronizer::InsertAddOp(const std::string& path, const std::string& recursive,   const std::string& nChildren)
188         {
189                 std::string insert;
190                 std::string pat=path.c_str();
191                 CleanName(pat);
192                 insert="INSERT INTO ADD_OPS (PATH,RECURSIVE,FILES_ADDED) VALUES('";
193                 insert+=pat+"','";
194                 insert+=recursive+"',";
195         insert+=nChildren+");";
196                 UPDATESYNCDB(insert);
197         }
198
199         /////////////////////////////////////////////////////////////////////////////////////////////////
200     // Inserts a new ignored file in the database
201         // @param add_key: the key of the add_op to which it corresponds
202     // @param path: the path of the directory that was added
203         // @param remove: shows if the file was removed or not
204         // @param time: the time in which the file was removed
205     // @result : The file has been inserted
206     /////////////////////////////////////////////////////////////////////////////////////////////////
207
208         void Synchronizer::InsertIgnoreFile(const std::string& addKey, const std::string& path, const std::string& remove, const std::string& time)
209         {
210                 std::string pat=path.c_str();
211                 CleanName(pat);
212                 std::string id=GetAttribute("ID","IGNORED_FILES","PATH",pat);
213                 if(id.compare("")==0)
214                 {
215                         std::string insert;
216                         insert="INSERT INTO IGNORED_FILES (ADD_KEY,PATH,REMOVE,TIME) VALUES('";
217                         insert+=addKey+"','";
218                         insert+=pat+"','";
219                         insert+=remove+"',";
220                 insert+=time+");";
221                         UPDATESYNCDB(insert);
222                 }
223                 else            
224                 {
225                         //Gets the add key
226                         std::string ak=GetAttribute("ADD_KEY","IGNORED_FILES","ID",id);
227                         //Sets the new add key attribute for the file
228             SetAttribute("ADD_KEY","IGNORED_FILES",addKey,"ID", id);
229                         //Sets the new remove attribute for the file
230                         SetAttribute("REMOVE","IGNORED_FILES",remove,"ID", id);
231                         //Sets the new time attribute for the file
232                         SetAttribute("TIME","IGNORED_FILES",time,"ID", id);
233                 }
234         }
235
236     /////////////////////////////////////////////////////////////////////////////////////////////////
237     // get the files name to ignore for a add operation synchronization
238     // @param : the add key
239     //@result : list (path) of ignore files
240     /////////////////////////////////////////////////////////////////////////////////////////////////
241     std::vector<std::string> Synchronizer::GetIgnoreList(const std::string &i_key)
242     {
243                 mIgnoreList.clear();
244         std::vector<std::string> i_names;
245                 std::string query = "SELECT * FROM IGNORED_FILES WHERE ADD_KEY = ";
246                 query+=i_key;
247         CppSQLite3Query res;
248         QUERYSYNCDB(query, res);
249                 while (!res.eof())
250         {
251                         RemoveList temp = RemoveList(res);
252                         if(temp.remove.compare("0")==0)
253                         {
254             mIgnoreList.push_back(temp);
255                         }
256                         res.nextRow();
257         }
258         std::vector<RemoveList>::iterator it;
259
260         for(it = mIgnoreList.begin();it != mIgnoreList.end(); ++it)
261         {
262             i_names.push_back((*it).path);
263         }
264                 return i_names;
265     }
266
267         /////////////////////////////////////////////////////////////////////////////////////////////////
268     // Gets the required attribute in the required table
269     // @param attribute: the attribute to look for
270     // @param table: the table to look in
271     // @param searchParam: the search parameter
272     // @param searchValue: the search value
273     // @result : required attribute
274     /////////////////////////////////////////////////////////////////////////////////////////////////
275     std::string Synchronizer::GetAttribute(const std::string& attribute, 
276                                                                                 const std::string& table, 
277                                                                                 const std::string& searchParam,
278                                                                                 const std::string& searchValue)
279     {
280         std::stringstream query;
281                 std::string result;
282                 std::string sVal=searchValue.c_str();
283                 CleanName(sVal);
284                 query<<"SELECT "<<attribute<<" FROM "<<table<<" WHERE "<<searchParam<<" = '"<<sVal<<"';";
285         CppSQLite3Query res;
286         QUERYSYNCDB(query.str(), res);
287                 while (!res.eof())
288         {
289                         result=res.getStringField(0);
290                         res.nextRow();
291         }
292                 return result;
293     }
294
295         /////////////////////////////////////////////////////////////////////////////////////////////////
296     // Sets the attribute value in the required table and column
297     // @param attribute: the attribute to look for
298     // @param table: the table to look in
299         // @param value: the value to set
300     // @param searchParam: the search parameter
301     // @param searchValue: the search value
302     // @result : attribute value changed
303     /////////////////////////////////////////////////////////////////////////////////////////////////
304     void Synchronizer::SetAttribute(const std::string& attribute, 
305                                                                                 const std::string& table, 
306                                                                                 const std::string& value,
307                                                                                 const std::string& searchParam,
308                                                                                 const std::string& searchValue)
309     {
310                 std::string val=value.c_str();
311                 std::string sVal=searchValue.c_str();
312                 CleanName(val);
313                 CleanName(sVal);
314                 std::string sql = "UPDATE ";
315                 sql+=table;
316                 sql+=" SET ";
317                 sql += attribute;
318                 sql += " = '";
319                 sql += val;
320                 sql += "' WHERE ";
321                 sql += searchParam;
322                 sql += " = '";
323                 sql += sVal;
324                 sql += "'";
325                 UPDATESYNCDB(sql);
326     }
327