+ }
+ }
+ //=====================================================================
+
+ //======================================================================
+ void SQLiteTreeHandler::DBInsert(tree::Node* n)
+ {
+ GimmickMessage(2,"Inserting in DB '"<<n->GetLabel()
+ <<"'"<<std::endl);
+ std::string val;
+ SQLAppendAttributesValues(n,val);
+ std::string insert("INSERT INTO ");
+ insert += GetTree().GetLevelDescriptor(n->GetLevel()).GetName();
+ insert += " " + val + ";";
+
+ UPDATEDB(insert);
+
+ // Store DB id of newly created node;
+ long lastrow = mDB->lastRowId();
+ std::stringstream ri;
+ ri << mDB->lastRowId();
+ n->SetAttribute("ID",ri.str());
+ }
+ //======================================================================
+
+ //======================================================================
+ /// Graft the branch defined by the attributes to the parent
+ void SQLiteTreeHandler::DBGraftToParent( tree::Node* parent,
+ const AttributeMapType& attr)
+ {
+ // std::cout <<"Grafting to parent '"<<parent->GetLabel()
+ // <<"'"<<std::endl;
+
+ for (int level = parent->GetLevel()+1;
+ level < GetTree().GetNumberOfLevels();
+ level++)
+ {
+ // Create Node
+ tree::Node* child = new tree::Node(parent,attr);
+ child->SetChildrenLoaded(true);
+ if (level>1)
+ {
+ int nc = GetNumberOfChildren(parent)+1;
+
+ // std::cout<<"Number of children "<<parent->GetNumberOfChildren()<<std::endl;
+ std::stringstream out;
+ out << nc;
+ SetAttribute(parent,"NumberOfChildren",out.str());
+ }
+
+ // Set PARENT_ID if necessary
+ if ( parent->GetLevel()>0 )
+ child->SetAttribute("PARENT_ID",parent->GetAttribute("ID"));
+
+ // Insert in DB
+ DBInsert(child);
+ /*
+ std::string val;
+ SQLAppendAttributesValues(child,val);
+ std::string insert("INSERT INTO ");
+ insert += GetTree().GetLevelDescriptor(child->GetLevel()).GetName();
+ insert += " " + val + ";";
+ UPDATEDB(insert);
+
+ // Store DB id of newly created node;
+ long lastrow = mDB->lastRowId();
+ std::stringstream ri;
+ ri << mDB->lastRowId();
+ child->SetAttribute("ID",ri.str());
+ */
+ // Down one level
+ parent = child;
+
+ /*
+ // Insert in TypeId map
+ TypeId ti;
+ ti.type = node->GetType();
+ ti.id = node_id;
+ mTypeIdToNodeMap[ti] = node;
+ // std::cout << "== Insert TypeId ("<<ti.type<<","<<ti.id<<")"<<std::endl;
+ //
+ msw[2].Pause();
+
+ if (node->GetType()==Node::Patient) summary.added_patients++;
+ if (node->GetType()==Node::Study) summary.added_studies++;
+ if (node->GetType()==Node::Series) summary.added_series++;
+ if (node->GetType()==Node::Image) summary.added_images++;
+ */
+ }
+ }
+ //======================================================================
+
+
+ //=====================================================================
+ /// Sets an attribute of a Node
+ bool SQLiteTreeHandler::DBSetAttribute(tree::Node* n,
+ const std::string& key,
+ const std::string& value)
+ {
+ GimmickMessage(3,"Setting Attribute of '"<<n->GetLabel()<<
+ "' "<<key<<"='"<<value<<"'"<<std::endl);
+
+ n->SetAttribute(key,value);
+ std::string sql = "UPDATE ";
+ sql += GetTree().GetLevelDescriptor(n->GetLevel()).GetName();
+ sql += " SET ";
+ sql += key;
+ sql += " = '";
+ sql += value;
+ sql += "' WHERE ID = '";
+ sql += n->GetAttribute("ID");
+ sql +="'";
+ // sql += " LIMIT 1";
+ UPDATEDB(sql);
+ return true;
+ }
+
+ //=====================================================================
+ /// Sets an attribute of a Node
+ void SQLiteTreeHandler::DBSetAttribute(const std::string& levelDescriptor,
+ const std::string& key,
+ const std::string& value,
+ const std::string& searchParam,
+ const std::string& searchVal)
+ {
+
+ std::string sql = "UPDATE ";
+ sql += levelDescriptor;
+ sql += " SET ";
+ sql += key;
+ sql += " = '";
+ sql += value;
+ sql += "' WHERE ";
+ sql += searchParam;
+ sql += " = '";
+ sql += searchVal;
+ sql += "'";
+ std::cout<<sql<<std::endl;
+ UPDATEDB(sql);
+ }
+ //=====================================================================
+ void SQLiteTreeHandler::DBRecursiveRemoveNode(Node* node)
+ {
+
+ std::string query = "DELETE FROM ";
+
+ query += GetTree().GetLevelDescriptor(node->GetLevel()).GetName();
+ query += " WHERE ID='"+ node->GetAttribute("ID") + "';";
+
+ UPDATEDB(query);
+ GimmickDebugMessage(2,
+ " Deleting '"
+ <<node->GetLabel()<<"' with ID '"
+ <<node->GetAttribute("ID")
+ <<"' in level "<< GetTree().GetLevelDescriptor(node->GetLevel()).GetName()
+ <<std::endl);
+
+
+ if(node->GetNumberOfChildren()!=0)
+ {
+ Node::ChildrenListType::iterator i;
+ for (i = node->GetChildrenList().begin();
+ i != node->GetChildrenList().end();
+ i++)
+ {
+ DBRecursiveRemoveNode((*i));
+ }
+ }
+ else if(node->GetLevel()<GetTree().GetNumberOfLevels()-1)
+ {
+ DBRecursiveRemoveNode(node->GetLevel()+1,node->GetAttribute("ID"));
+ }