]> Creatis software - creaImageIO.git/blobdiff - src2/creaImageIOTreeHandlerImageAdder.cpp
clean-up
[creaImageIO.git] / src2 / creaImageIOTreeHandlerImageAdder.cpp
index 679e156df04b130d4ec728bf67b33eca5969edd4..290da65474741d59812ad591a705f3445e1601e6 100644 (file)
@@ -48,19 +48,23 @@ namespace creaImageIO
        
     unsigned int nbf = filenames.size(); 
     std::vector<std::string>::const_iterator i;
+       mSynchronizer->GetList();
     for (i=filenames.begin();i!=filenames.end();++i)
       {
-       mTimestampHandler->AddFile((*i), fs::last_write_time(*i), time(0));
+       
        mProgress.IncNumberScannedFiles();
        if (IsHandledFile(*i)) 
          {
            mProgress.IncNumberHandledFiles();
-               mSynchronizer->InsertAddOp((*i),"0","1");
-               std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","ADD_OPS","PATH",(*i));
-               std::stringstream removedOn;
-               removedOn<<time(0);
-               mSynchronizer->InsertIgnoreFile(addKey,(*i),"0",removedOn.str());
-           AddFile(*i);
+               if(mSynchronizer->isIndexed(*i))
+               {
+                       mSynchronizer->InsertAddOp((*i),"0","1",mCurrentDB);
+                       std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","ADD_OPS","PATH",(*i),mCurrentDB);
+                       std::stringstream removedOn;
+                       removedOn<<time(0);
+                       mSynchronizer->InsertIgnoreFile(addKey,(*i),"0",removedOn.str(),mCurrentDB);
+                       AddFile(*i);
+               }
          }
        mProgressSignal(mProgress);
        if (mProgress.GetStop()) break;
@@ -77,13 +81,16 @@ namespace creaImageIO
        
        std::stringstream rec;
        rec<<recurse;
-       mSynchronizer->InsertAddOp(directory,rec.str(),"0");
-       std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","ADD_OPS","PATH",directory);
+       mSynchronizer->InsertAddOp(directory,rec.str(),"0",mCurrentDB);
+       std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","ADD_OPS","PATH",directory,mCurrentDB);
+       mTreeHandler->BeginTransaction();
+       mSynchronizer->GetList();
        AddDirectoryRecursor( directory, recurse, addKey );
        
        int nFiles=GetProgress().GetNumberAddedFiles();
        files<<nFiles;
-       mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",files.str(),"ADD_KEY",addKey);
+       mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",files.str(),"ADD_KEY",addKey,mCurrentDB);
+       mTreeHandler->EndTransaction();
     GimmickDebugMessage(3,mProgress<<std::endl);
   }
 
@@ -104,7 +111,7 @@ namespace creaImageIO
   }
   //=====================================================================
 
-  void TreeHandlerImageAdder::RemoveFile( const tree::Node*& node)
+  void TreeHandlerImageAdder::RemoveFile( tree::Node* node)
   {
                int n=node->GetNumberOfChildren();
                if(n>0)
@@ -115,16 +122,16 @@ namespace creaImageIO
                {
                  std::string path=node->GetAttribute("FullFileName");
                  //Gets the add key
-                 std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","IGNORED_FILES","PATH",path);
+                 std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","IGNORED_FILES","PATH",path,mCurrentDB);
                  //Gets the number of files added
-                 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",addKey)).c_str());
+                 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",addKey,mCurrentDB)).c_str());
                  files=files-1;
                  std::stringstream out;
                  out<<files;
                  //Sets the new number of files
-                 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",out.str(),"ADD_KEY",addKey);
+                 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",out.str(),"ADD_KEY",addKey,mCurrentDB);
                  //Sets the file as removed
-                 mSynchronizer->SetAttribute("REMOVE","IGNORED_FILES","1","PATH",path);
+                 mSynchronizer->SetAttribute("REMOVE","IGNORED_FILES","1","PATH = '"+path+"' AND ADD_KEY",addKey,mCurrentDB);
                }
   }
 
@@ -144,21 +151,22 @@ namespace creaImageIO
                {
                   std::string path=(*it)->GetAttribute("FullFileName");
                  //Gets the add key
-                 std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","IGNORED_FILES","PATH",path);
+                 std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","IGNORED_FILES","PATH",path,mCurrentDB);
                  //Gets the number of files added
-                 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",addKey)).c_str());
+                 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",addKey,mCurrentDB)).c_str());
                  files=files-1;
                  std::stringstream out;
                  out<<files;
                  //Sets the new number of files
-                 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",out.str(),"ADD_KEY",addKey);
+                 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",out.str(),"ADD_KEY",addKey,mCurrentDB);
                  //Sets the file as removed
-                 mSynchronizer->SetAttribute("REMOVE","IGNORED_FILES","1","PATH",path);
+                 mSynchronizer->SetAttribute("REMOVE","IGNORED_FILES","1","PATH = '"+path+"' AND ADD_KEY",addKey,mCurrentDB);
                }
        
          }
   }
 
   //=====================================================================
   void TreeHandlerImageAdder::AddDirectoryRecursor(const std::string &dirpath, 
                                                   bool recursive,
@@ -170,11 +178,12 @@ namespace creaImageIO
     if ( !fs::exists( dirpath ) ) return;
        time_t lastModif=fs::last_write_time(dirpath);
 
+       
     fs::directory_iterator end_itr; // default construction yields past-the-end
     for ( fs::directory_iterator itr( dirpath );
          itr != end_itr;
          ++itr )
-      {
+       {
        // If is directory & recurse : do recurse
        if ( fs::is_directory(itr->status()) )
          {
@@ -186,7 +195,8 @@ namespace creaImageIO
        else 
          {
                std::string parent_id;
-               bool valid=mTimestampHandler->AddDirectory(dirpath, itr->string(), lastModif, time(0));
+               // tTest if directory (and only it) exists or not.
+               bool valid = mSynchronizer->isIndexed(itr->string());//true;//=mTimestampHandler->AddDirectory(dirpath, itr->string(), lastModif, time(0),mCurrentDB);
                if(valid)
                {
                        mProgress.IncNumberScannedFiles();
@@ -195,19 +205,19 @@ namespace creaImageIO
                        mProgress.IncNumberHandledFiles();
                        AddFile( itr->string() );
                        mTreeHandler->GetTopLevelNodeId("FullFileName",itr->string(),parent_id);
-                       mTimestampHandler->SetAttribute("TopLevelNodeId",parent_id,"PATH",itr->string());
                        std::stringstream removedOn;
                        removedOn<<time(0);
-                       mSynchronizer->InsertIgnoreFile(addKey, itr->string(),"0",removedOn.str());
+                       mSynchronizer->InsertIgnoreFile(addKey, itr->string(),"0",removedOn.str(),mCurrentDB);
                        }
+                       
                        mProgressSignal(mProgress);
                        if (mProgress.GetStop()) 
                        {
                        //itr = end_itr;
                        break;
                        }
+                       
                }
-               mTimestampHandler->SetAttribute("TopLevelNodeId",parent_id,"PATH",dirpath);
          }
       }
        
@@ -279,7 +289,7 @@ namespace creaImageIO
          std::vector<AddList>::iterator iter;
 
          //Gets the list of added files
-         mSynchronizer->GetFileList(fileList);
+         mSynchronizer->GetFileList(fileList,mCurrentDB);
 
          std::vector<std::string>::iterator i;
          //Actions to take if the user doesn't want to repair
@@ -348,19 +358,18 @@ namespace creaImageIO
                        //For the new files, add them
                        for (i=newFiles.begin();i!=newFiles.end();++i)
                        {
-                       mTimestampHandler->AddFile((*i), fs::last_write_time(*i), time(0));
                        if (IsHandledFile(*i)) 
                        {
                                std::stringstream removedOn;
                                removedOn<<time(0);
-                               mSynchronizer->InsertIgnoreFile((*iter).key,(*i),"0",removedOn.str());
+                               mSynchronizer->InsertIgnoreFile((*iter).key,(*i),"0",removedOn.str(),mCurrentDB);
                                //Gets the number of files added
-                               int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",(*iter).key)).c_str());
+                               int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",(*iter).key,mCurrentDB)).c_str());
                                files=files+1;
                                std::stringstream out;
                                out<<files;
                                //Sets the new number of files
-                               mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",out.str(),"ADD_KEY",(*iter).key);
+                               mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",out.str(),"ADD_KEY",(*iter).key,mCurrentDB);
                                AddFile(*i);
                        }
                        }
@@ -408,7 +417,7 @@ namespace creaImageIO
           std::string databaseVal;
           mTreeHandler->GetAttribute("Image","FullFileName",file,a->GetKey(),databaseVal);
           std::string fileVal=attr.find(a->GetKey())->second;
-          if ( a->GetFlags()==0 && databaseVal.compare(fileVal)!=0 
+          if ( a->GetFlags()==0 && databaseVal == fileVal
            {
                        if(repair)
                        {
@@ -444,8 +453,152 @@ namespace creaImageIO
          }
   }
   
+  void TreeHandlerImageAdder::SaveAs(const std::vector<std::string>& filenames, std::vector<vtkImageData *> i_images)
+  {
+         std::vector<std::string>::const_iterator it_file = filenames.begin();
+         std::vector<vtkImageData *>::iterator it_image = i_images.begin();
+       /*  mWriter.CanWrite(".jpeg");
+         for(; it_file != filenames.end(); ++it_file, ++it_image)
+                 mWriter.WriteImage(it_file->c_str(), (vtkImageData &)it_image);*/
+  }
+
+  //=======================================================================
+  void TreeHandlerImageAdder::FindNodePartial(tree::Node* parent, int level, const std::string& searchParam, const std::string& searchVal, tree::Node*& node)
+  {
+         if(level>1)
+         {
+                 std::vector<tree::Node*>::iterator iter;
+                 for(iter=parent->GetChildrenList().begin();iter!=parent->GetChildrenList().end() && node==0 ;++iter)
+                 {
+                         FindNodePartial(*iter,level-1,searchParam,searchVal,node);
+                 }
+         }
+         else
+         {
+                 if(parent->GetAttribute(searchParam).find(searchVal)<9000)
+                 {
+                         node=parent;
+                         return;
+                 }
+
+         }
+  }
+  
+  //=======================================================================
+
+  void TreeHandlerImageAdder::CopyFiles(const std::vector<std::string>& filenames, const std::string directory  )
+  {
+         std::vector<std::string>::const_iterator i;
+         if(!boost::filesystem::exists(directory))
+         {
+         boost::filesystem::create_directory(boost::filesystem::path(directory));
+         mSynchronizer->InsertAddOp(directory,"0","0",mCurrentDB);
+         }
+         std::string addKey=mSynchronizer->GetAttribute("ADD_KEY","ADD_OPS","PATH",directory,mCurrentDB);
+         size_t last;
+         std::vector<std::string> newNames;
+         for(i=filenames.begin();i!=filenames.end();++i)
+         {
+                 std::string dir=directory.c_str();
+                 if(boost::filesystem::exists(*i) && (*i).find(dir)==std::string::npos)
+                 {
+                 std::string path=*i;
+                 last=(*i).find_last_of('/');
+                 std::string f="\\"+(*i).substr(last+1);
+       
+                 int p=1;
+                 std::stringstream out;
+                 out<<directory<<f;
+                 while(boost::filesystem::exists(out.str()))
+                 {
+                         out.str("");
+                         out<<directory<<f.substr(0,f.size()-4)<<"("<<p<<")"<<f.substr(f.size()-4);
+                         p++;
+                 }
+                 std::string result=out.str();
+                 boost::filesystem::copy_file((*i),result);
+
+                 //To update image database
+                 mTreeHandler->SetAttribute("Image","FullFileName",result,"FullFileName", (*i));
+                 
+                 //To update maintenance database
+                 //1.Add the new path and increase number of children on new operation.
+                 std::stringstream removedOn;
+                 removedOn<<time(0);
+                 //Inserts the file
+                 mSynchronizer->InsertIgnoreFile(addKey, result,"0",removedOn.str(),mCurrentDB);
+                 //Gets the number of files added
+                 int files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",addKey,mCurrentDB)).c_str());
+                 files=files+1;
+                 std::stringstream fil;
+                 fil<<files;
+                 //Sets the new number of files
+                 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",fil.str(),"ADD_KEY",addKey,mCurrentDB);
+                 fil.str("");
+
+                 //2.Set the old path as removed and decrease number of children on old operation.
+                 //Gets the old add key
+                 std::string oldAddKey=mSynchronizer->GetAttribute("ADD_KEY","IGNORED_FILES","PATH",path,mCurrentDB);
+                 //Sets the file as removed
+                 mSynchronizer->SetAttribute("REMOVE","IGNORED_FILES","1","PATH = '"+path+"' AND ADD_KEY",oldAddKey,mCurrentDB);
+                 //Gets the number of files added
+                 files=atoi((mSynchronizer->GetAttribute("FILES_ADDED","ADD_OPS","ADD_KEY",oldAddKey,mCurrentDB)).c_str());
+                 files=files-1;
+                 fil<<files;
+                 //Sets the new number of files
+                 mSynchronizer->SetAttribute("FILES_ADDED","ADD_OPS",fil.str(),"ADD_KEY",oldAddKey,mCurrentDB);
+                 
+                 }
+
+         }
+  }
+
   //=======================================================================
 
+  void TreeHandlerImageAdder::DeleteDriveFromMainDB(const std::string& drive)
+  {
+         //Delete from local database and others
+         tree::Node* node=0;
+         mTreeHandler->LoadChildren(NULL,4);
+         FindNodePartial(mTreeHandler->GetTree().GetChildrenList()[0],3,"FullFileName",drive,node);
+         while(node!=0)
+         {
+         mTreeHandler->Remove(node);
+         node=0;
+         mTreeHandler->LoadChildren(NULL,4);
+         FindNodePartial(mTreeHandler->GetTree().GetChildrenList()[0],3,"FullFileName",drive,node); 
+         }
+  }
+
+   //=======================================================================
+
+  void TreeHandlerImageAdder::DeleteDriveFromOtherDB(const std::string& drive)
+  {
+         //Delete from maintenance
+         mSynchronizer->RemoveEntries("ADD_OPS", "PATH", "LIKE", drive+"%");
+         mSynchronizer->RemoveEntries("IGNORED_FILES", "PATH", "LIKE", drive+"%");
+  }
+
+  //=======================================================================
+  void TreeHandlerImageAdder::EditField(tree::Node* node, const std::string& name, const std::string& key, const std::string& val)
+  {
+         node->SetAttribute(key,val);
+         mTreeHandler->SetAttribute(node,key,val);
+  }
+
+  //=======================================================================
+  void TreeHandlerImageAdder::GetAttributes(const std::vector<std::string>& params, 
+         const std::string& filename, 
+         std::vector<std::string>& results)
+  {
+         std::vector<std::string>::const_iterator i;
+         std::string result;
+         for(i=params.begin();i!=params.end();i++)
+         {
+                 mTreeHandler->GetAttribute("Image","FullFileName",filename,*i,result);
+                 results.push_back(result);
+         }
+  }
 
  
 }