Public Member Functions | Protected Member Functions | Private Attributes

creaImageIO::TimestampDatabaseHandler Class Reference

Concrete TreeHandler which manages a Tree stored in a sqlite database. More...

#include <creaImageIOTimestampDatabaseHandler.h>

Collaboration diagram for creaImageIO::TimestampDatabaseHandler:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 TimestampDatabaseHandler (const std::string &filename)
 Ctor with database file name.
virtual ~TimestampDatabaseHandler ()
 Dtor.
const std::string & GetFileName () const
 Returns the sqlite db file name.
bool Open ()
 Opens an existing 'source'.
bool Close ()
 Closes the 'source'.
bool Create ()
 Creates a new 'source'.
bool Destroy ()
 Destroys the 'source'.
std::string IsIndexed (const std::string &path, const std::string &refdb)
 Returns the id of the path if it's indexed, blank otherwise.
bool AddDirectory (const std::string &parent, const std::string &path, const time_t lastModif, const time_t lastRead, const std::string &refdb)
 Sets the current path's parent.
void AddFile (const std::string &path, const time_t lastModif, const time_t lastRead, const std::string &refdb)
 Adds a new file to the database without a parent.
void AddFile (const std::string &parentId, const std::string &path, const time_t lastModif, const time_t lastRead, const std::string &refdb)
 Adds a new file to the database with a parent.
void SetAttribute (const std::string &attName, const std::string &attValue, const std::string &searchParam, const std::string &searchValue)
 Sets the attribute to the value passed as parameter where the searchParameter is searchValue.
void RemoveNode (const std::string &searchAtt, const tree::Node *node, const std::string &refdb)
 Removes the given node.
void RemoveFile (const std::string &searchAtt, const std::string &searchVal, const std::string &refdb)
 Removes the filename with the given pathname.
void CleanPath (std::string &str) const
 Cleans the path name.
bool CheckTimestamp (const std::string pathId, const time_t lastModif, const std::string &refdb)
 Checks the timestamp in the database and compares it with the given one.
void RemoveEntries (const std::string i_table, const std::string i_attribute, const std::string i_operand, const std::string i_val)
 Removes the entries that match the given parameters.

Protected Member Functions

bool DBOpen ()
 Open the database.
bool DBCreate ()
 Creates a new database on disk and the tables.
void DBRemove (const std::string &searchAtt, const std::string &searchVal, const std::string &refdb)

Private Attributes

CppSQLite3DBmDB
 The DB.
std::string mFileName
 The physical location associated to the DicomDatabase (directory, db file...).

Detailed Description

Concrete TreeHandler which manages a Tree stored in a sqlite database.

Definition at line 13 of file creaImageIOTimestampDatabaseHandler.h.


Constructor & Destructor Documentation

creaImageIO::TimestampDatabaseHandler::TimestampDatabaseHandler ( const std::string &  filename  ) 

Ctor with database file name.

Definition at line 17 of file creaImageIOTimestampDatabaseHandler.cpp.

References GimmickMessage, mDB, and CppSQLite3DB::SQLiteVersion().

    : mFileName(filename)
  {
    mDB = new CppSQLite3DB;
    GimmickMessage(1,"SQLite version : "
                   <<std::string(mDB->SQLiteVersion())<< std::endl);
  }

Here is the call graph for this function:

creaImageIO::TimestampDatabaseHandler::~TimestampDatabaseHandler (  )  [virtual]

Dtor.

Definition at line 27 of file creaImageIOTimestampDatabaseHandler.cpp.

References mDB.

  {
    delete mDB;
  }


Member Function Documentation

bool creaImageIO::TimestampDatabaseHandler::AddDirectory ( const std::string &  parent,
const std::string &  path,
const time_t  lastModif,
const time_t  lastRead,
const std::string &  refdb 
)

Sets the current path's parent.

Definition at line 194 of file creaImageIOTimestampDatabaseHandler.cpp.

References AddFile(), CheckTimestamp(), CleanPath(), IsIndexed(), and SetAttribute().

  {
         bool valid=false;
         std::string par=parent.c_str();
         std::string pat=path.c_str();
         CleanPath(par);
         CleanPath(pat);

         std::string pathId=IsIndexed(pat,refdb);
         //Case: It is a root parent
         if(parent.compare("")==0)
         {
                 if(pathId.compare("")==0)
                 {
                        AddFile(pat,lastModif,lastRead,refdb);
                        valid=true;
                 }
                 else
                 {
                         valid=CheckTimestamp(pathId, lastModif, refdb);
                 }
         }
         else 
         {
                 std::string parentId=IsIndexed(par,refdb);
                 //Case: Parent is not in database
                 if(parentId.compare("")==0)
                {
                        AddFile(par,lastModif,lastRead,refdb);
                        parentId=IsIndexed(par,refdb);
                }

                //Case path is not in database
                if(pathId.compare("")==0)
                {
                    AddFile(parentId,pat,lastModif,lastRead,refdb);
                        valid=true;
                }
                //Parent and path are in the database
                else
                {
                        SetAttribute("PARENT_ID",parentId,"ID", pathId);
                        valid=CheckTimestamp(pathId, lastModif, refdb);
                }
         }
         return valid;
        
  }

Here is the call graph for this function:

void creaImageIO::TimestampDatabaseHandler::AddFile ( const std::string &  path,
const time_t  lastModif,
const time_t  lastRead,
const std::string &  refdb 
)

Adds a new file to the database without a parent.

Definition at line 249 of file creaImageIOTimestampDatabaseHandler.cpp.

References UPDATETIMESTAMPDB.

Referenced by AddDirectory().

  {
        std::stringstream out;
        out<<"INSERT INTO FILES (PARENT_ID,PATH,LastModified,LastRead,ReferencedDB) VALUES(0,'"<<path<<"',";
        out<<lastModif<<","<<lastRead<<",'"<<refdb<<"');";
    UPDATETIMESTAMPDB(out.str());
        
  }

Here is the caller graph for this function:

void creaImageIO::TimestampDatabaseHandler::AddFile ( const std::string &  parentId,
const std::string &  path,
const time_t  lastModif,
const time_t  lastRead,
const std::string &  refdb 
)

Adds a new file to the database with a parent.

Definition at line 260 of file creaImageIOTimestampDatabaseHandler.cpp.

References UPDATETIMESTAMPDB.

  {
        std::stringstream out;
        out<<"INSERT INTO FILES (PARENT_ID,PATH,LastModified,LastRead,ReferencedDB) VALUES("<<parentId<<",'"<<path<<"',";
        out<<lastModif<<","<<lastRead<<",'"<<refdb<<"');";
    UPDATETIMESTAMPDB(out.str());
  }

bool creaImageIO::TimestampDatabaseHandler::CheckTimestamp ( const std::string  pathId,
const time_t  lastModif,
const std::string &  refdb 
)

Checks the timestamp in the database and compares it with the given one.

Definition at line 395 of file creaImageIOTimestampDatabaseHandler.cpp.

References CppSQLite3Query::eof(), CppSQLite3Query::getFloatField(), CppSQLite3Query::nextRow(), CppSQLite3Query::numFields(), QUERYTIMESTAMPDB, and SetAttribute().

Referenced by AddDirectory().

  {
        std::string sel="SELECT LastModified FROM FILES WHERE ID='"+pathId+"' AND REFERENCEDDB='"+refdb+"';";
        CppSQLite3Query q;
        QUERYTIMESTAMPDB(sel,q);
        double timestamp;
        
        while (!q.eof())
          {
            for (int fld = 0; fld < q.numFields(); fld++)
              {
                          timestamp=q.getFloatField(fld);
              }
            q.nextRow();
          }

          
          std::stringstream lm;
          lm<<lastModif;
          double modif=atof((lm.str()).c_str());
          if(timestamp<modif)
          {
                  SetAttribute("LastModified",lm.str(),"ID",pathId);
                  return true;
          }
          return false;  
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::TimestampDatabaseHandler::CleanPath ( std::string &  str  )  const

Cleans the path name.

Definition at line 179 of file creaImageIOTimestampDatabaseHandler.cpp.

Referenced by AddDirectory(), IsIndexed(), and SetAttribute().

  {
         size_t pos;
         do
     {
         pos = str.find('\\');
         if ((int)pos!=-1)  
                 {
                         str.replace(pos, 1, "/");
                 }
     }
     while ((int)pos!=-1);
  }

Here is the caller graph for this function:

bool creaImageIO::TimestampDatabaseHandler::Close (  ) 

Closes the 'source'.

Definition at line 47 of file creaImageIOTimestampDatabaseHandler.cpp.

  {
    return true;
  }

bool creaImageIO::TimestampDatabaseHandler::Create (  ) 

Creates a new 'source'.

Definition at line 39 of file creaImageIOTimestampDatabaseHandler.cpp.

References DBCreate().

  {
    return DBCreate();
  }

Here is the call graph for this function:

bool creaImageIO::TimestampDatabaseHandler::DBCreate (  )  [protected]

Creates a new database on disk and the tables.

Definition at line 127 of file creaImageIOTimestampDatabaseHandler.cpp.

References CppSQLite3Exception::errorCode(), CppSQLite3Exception::errorMessage(), GetFileName(), GimmickError, GimmickMessage, mDB, CppSQLite3DB::open(), and UPDATETIMESTAMPDB.

Referenced by Create().

  {
    GimmickMessage(1,"Creating SQLite database '"<<GetFileName()
                   <<"' ... "<<std::endl);

    if (boost::filesystem::exists(GetFileName())) 
      {
        GimmickError(GetFileName()<<"' : "
                     << "file already exists");
        return false;
      }
    
    // OPENING
    try
      {
        mDB->open(GetFileName().c_str());
      }
    catch (CppSQLite3Exception& e)
      {
        GimmickError(e.errorCode() << ":" 
                     << e.errorMessage() <<std::endl);
        return false;
      }
    
     
    // CREATING TABLES
    
    std::string command;
  
    
            command = "CREATE TABLE ";
            command += "FILES";
            command += "\n(\nID INTEGER PRIMARY KEY";
                command += ",\nPARENT_ID int not null"; 
            command += ",\nPATH text";
                command += ",\nLastModified datetext";
                command += ",\nLastRead datetext";
                command += ",\nTopLevelNodeId text";
                command += ",\nReferencedDB text";
                command += ",\nconstraint FK_PARENT foreign key (PARENT_ID) references ";
                command += "FILES";
                command += "(ID) on delete restrict on update restrict";
              
            command += "\n)";
            UPDATETIMESTAMPDB(command);
            
    return true;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

bool creaImageIO::TimestampDatabaseHandler::DBOpen (  )  [protected]

Open the database.

Definition at line 98 of file creaImageIOTimestampDatabaseHandler.cpp.

References CppSQLite3Exception::errorCode(), CppSQLite3Exception::errorMessage(), GetFileName(), GimmickDebugMessage, GimmickError, GimmickMessage, mDB, and CppSQLite3DB::open().

Referenced by Open().

  {
    GimmickMessage(1,"Opening SQLite database '"<<GetFileName()
                   <<"' ... "<<std::endl);
    // OPENING FILE
    if (!boost::filesystem::exists(GetFileName())) 
      {
        return false;
      }

    try
      {
        mDB->open(GetFileName().c_str());
      }
    catch (CppSQLite3Exception& e)
      {
        GimmickError("Opening '"<<GetFileName()<<"' : "
                     << e.errorCode() << ":" 
                     << e.errorMessage());
        return false;
      }

    GimmickDebugMessage(1,"Opening SQLite database '"<<GetFileName()
                   <<"' ... OK"<<std::endl);
    return true;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::TimestampDatabaseHandler::DBRemove ( const std::string &  searchAtt,
const std::string &  searchVal,
const std::string &  refdb 
) [protected]

Definition at line 387 of file creaImageIOTimestampDatabaseHandler.cpp.

References UPDATETIMESTAMPDB.

Referenced by RemoveFile(), and RemoveNode().

  {
       
    std::string query = "DELETE FROM FILES WHERE "+searchAtt+"='"+ searchVal + "' AND REFERENCEDDB='"+refdb+"';";
    UPDATETIMESTAMPDB(query);
  }

Here is the caller graph for this function:

bool creaImageIO::TimestampDatabaseHandler::Destroy (  ) 

Destroys the 'source'.

Definition at line 55 of file creaImageIOTimestampDatabaseHandler.cpp.

  {
    return false;
  }

const std::string& creaImageIO::TimestampDatabaseHandler::GetFileName (  )  const [inline]

Returns the sqlite db file name.

Definition at line 25 of file creaImageIOTimestampDatabaseHandler.h.

Referenced by DBCreate(), and DBOpen().

{ return mFileName; }

Here is the caller graph for this function:

std::string creaImageIO::TimestampDatabaseHandler::IsIndexed ( const std::string &  path,
const std::string &  refdb 
)

Returns the id of the path if it's indexed, blank otherwise.

Definition at line 273 of file creaImageIOTimestampDatabaseHandler.cpp.

References CleanPath(), CppSQLite3Query::eof(), CppSQLite3Query::getStringField(), CppSQLite3Query::nextRow(), CppSQLite3Query::numFields(), and QUERYTIMESTAMPDB.

Referenced by AddDirectory().

  {
        std::string pat=path.c_str();
        CleanPath(pat);
        std::stringstream out;
        std::stringstream result;
        out<<"SELECT ID FROM FILES WHERE PATH='"<<pat<<"' AND REFERENCEDDB='"<<refdb<<"';";
                
        CppSQLite3Query q;
        QUERYTIMESTAMPDB(out.str(),q);
        
        
        while (!q.eof())
          {
            for (int fld = 0; fld < q.numFields(); fld++)
              {
                          result<<q.getStringField(fld);
              }
            q.nextRow();
          }

          return result.str();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

bool creaImageIO::TimestampDatabaseHandler::Open (  ) 

Opens an existing 'source'.

Definition at line 33 of file creaImageIOTimestampDatabaseHandler.cpp.

References DBOpen().

  {
    return DBOpen();
  }

Here is the call graph for this function:

void creaImageIO::TimestampDatabaseHandler::RemoveEntries ( const std::string  i_table,
const std::string  i_attribute,
const std::string  i_operand,
const std::string  i_val 
)

Removes the entries that match the given parameters.

Definition at line 424 of file creaImageIOTimestampDatabaseHandler.cpp.

References UPDATETIMESTAMPDB.

    {
        std::stringstream query;
                query<<"DELETE  FROM "<<i_table<<" WHERE "<<i_attribute<<" "<<i_operand<<" '"<<i_val<<"'";
        UPDATETIMESTAMPDB(query.str());
        }

void creaImageIO::TimestampDatabaseHandler::RemoveFile ( const std::string &  searchAtt,
const std::string &  searchVal,
const std::string &  refdb 
)

Removes the filename with the given pathname.

Definition at line 345 of file creaImageIOTimestampDatabaseHandler.cpp.

References DBRemove(), CppSQLite3Query::eof(), CppSQLite3Query::getStringField(), CppSQLite3Query::nextRow(), CppSQLite3Query::numFields(), and QUERYTIMESTAMPDB.

Referenced by RemoveNode().

  {
          
          std::stringstream result;
          std::string sel="SELECT PARENT_ID FROM FILES WHERE "+searchAtt+"='"+searchVal+"' AND REFERENCEDDB='"+refdb+"';";
                
          CppSQLite3Query q;
          QUERYTIMESTAMPDB(sel,q);
        
          while (!q.eof())
          {
            for (int fld = 0; fld < q.numFields(); fld++)
              {
                          result<<q.getStringField(fld);
              }
            q.nextRow();
          }
          DBRemove(searchAtt,searchVal,refdb);
          
                  int nChildren=0;
                  sel="SELECT ID FROM FILES WHERE PARENT_ID='"+result.str()+"'";
                  CppSQLite3Query q2;
                  QUERYTIMESTAMPDB(sel,q2);
                  while (!q2.eof())
                        {
                                nChildren++;
                                q2.nextRow();
                        }
                        if(nChildren<1)
                        {
                                if(!result.str().compare("0"))
                                {
                                RemoveFile("ID",result.str(),refdb);
                                }
                                else
                                {
                                DBRemove("ID",result.str(),refdb);
                                }
                        }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void creaImageIO::TimestampDatabaseHandler::RemoveNode ( const std::string &  searchAtt,
const tree::Node node,
const std::string &  refdb 
)

Removes the given node.

Definition at line 321 of file creaImageIOTimestampDatabaseHandler.cpp.

References DBRemove(), creaImageIO::tree::Node::GetAttribute(), creaImageIO::tree::Node::GetChildrenList(), creaImageIO::tree::Node::GetLevel(), creaImageIO::tree::Node::GetNumberOfChildren(), and RemoveFile().

  {
          int n=node->GetNumberOfChildren();
          if(n>0)
          {
                  std::vector<tree::Node*> children=node->GetChildrenList();
                  std::vector<tree::Node*>::iterator it;
                  for(it=children.begin();it!=children.end();++it)
                  {
                          RemoveNode(searchAtt,(*it),refdb);
                  }
          }
          else if(node->GetLevel()==3)
          {
                  RemoveFile(searchAtt,node->GetAttribute("FullFileName"),refdb);
          }
          else
          {
                  DBRemove("TopLevelNodeId",node->GetAttribute("ID"),refdb);
          }


  }

Here is the call graph for this function:

void creaImageIO::TimestampDatabaseHandler::SetAttribute ( const std::string &  attName,
const std::string &  attValue,
const std::string &  searchParam,
const std::string &  searchValue 
)

Sets the attribute to the value passed as parameter where the searchParameter is searchValue.

Definition at line 298 of file creaImageIOTimestampDatabaseHandler.cpp.

References CleanPath(), and UPDATETIMESTAMPDB.

Referenced by AddDirectory(), and CheckTimestamp().

  {
        std::string av=attValue.c_str();
        std::string sv=searchValue.c_str();
        CleanPath(av);
        CleanPath(sv);

        std::string sql = "UPDATE FILES SET ";
    sql += attName;
    sql += " = '";
    sql += av;
    sql += "' WHERE ";
        sql += searchParam;
        sql += " = '";
    sql += sv;
        sql += "'";
    UPDATETIMESTAMPDB(sql);
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

The physical location associated to the DicomDatabase (directory, db file...).

Definition at line 100 of file creaImageIOTimestampDatabaseHandler.h.


The documentation for this class was generated from the following files: