- /*
- //=====================================================================
- bool SQLiteTreeHandler::DBInsert(Node* alien_node,
- UpdateSummary& summary)
- {
- // std::cout << "SQLiteTreeHandler::Insert('"<<alien_node->GetLabel()
- // <<"')"<<std::endl;
-
- // if (!ChildrenLoaded()) DBLoadChildren(this,Node::Database);
-
- // Find parent
- Node* parent;
- std::string parent_id;
- parent = DBGetOrCreateParent(alien_node,parent_id,summary);
-
- // Insert
- DBRecursiveGetOrCreateNode(alien_node,parent,parent_id,summary);
- return true;
- }
- //=====================================================================
-
-
- //=====================================================================
- Node* SQLiteTreeHandler::DBGetOrCreateParent(Node* alien_node,
- std::string& parent_id,
- UpdateSummary& summary)
- {
- // std::cout << "DBGetOrCreateParent '" << alien_node->GetLabel()<<"'"
- // << std::endl;
- // Load the patients if not already done
- DBLoadChildren(this,Node::Patient);
-
- parent_id = "";
- Node* parent = this;
-
- // The chain of ancestors
- std::deque<Node*> chain;
- Node* cur = alien_node->GetParent();
- for (int type=Node::Patient;
- type<alien_node->GetType();++type)
- {
- chain.push_front(cur);
- cur = cur->GetParent();
- }
-
- // create the nodes if do not exist
- std::deque<Node*>::iterator i;
- for (i=chain.begin();i!=chain.end();++i)
- {
- // std::cout << " cur = '"<<(*i)->GetLabel()<<"'"<<std::endl;
- // std::string cur_id = DBGetNodeId(*i,parent_id);
- // if (cur_id.size()==0)
- // {
- // Node does not exist : create it
- std::string cur_id;
- parent = DBGetOrCreateNode(*i,
- parent,
- parent_id,
- cur_id,
- summary
- );
- DBLoadChildren(parent,parent->GetType()+1);
-
- parent_id = cur_id;
- }
- return parent;
- }
- //=====================================================================
-
-
-
- //=====================================================================
- void SQLiteTreeHandler::DBRecursiveGetOrCreateNode(Node* alien_node,
- Node* parent,
- const std::string& parent_id,
- UpdateSummary& summary)
- {
- // std::cout << "SQLiteTreeHandler::RecursiveGetOrCreateNode('"
- // <<alien_node->GetLabel()
- // <<"','"<<parent<<"','"<<parent_id<<"')"<<std::endl;
- if (parent != 0)
- {
- // std::cout << " -- Parent = '"<<parent->GetLabel()<<"'"<<std::endl;
- }
- std::string new_id;
- Node* new_node = DBGetOrCreateNode(alien_node,
- parent,
- parent_id,
- new_id,
- summary);
- Node::ChildrenListType::iterator i;
- for (i = alien_node->GetChildrenList().begin();
- i != alien_node->GetChildrenList().end();
- i++)
- {
- DBRecursiveGetOrCreateNode((*i),new_node,new_id,summary);
- }
- }
- //=====================================================================
-
-
- //=====================================================================
- Node* SQLiteTreeHandler::DBGetOrCreateNode(Node* alien_node,
- Node* internal_parent,
- std::string parent_id,
- std::string& node_id,
- UpdateSummary& summary)
- {
- // std::cout << "DBGetOrCreateNode('"<<alien_node->GetLabel()<<"','"
- // << internal_parent << "','"<< parent_id<<"')"<<std::endl;
- if (internal_parent != 0)
- {
- // std::cout << " -- Parent = '"<<internal_parent->GetLabel()<<"'"<<std::endl;
- }
- // Node Exists ? return it
- // First try among children of internal parent
- Node* node = GetChildrenLike(internal_parent,alien_node);
- if (node>0)
- {
- node_id = node->UnsafeGetFieldValue("ID");
- return node;
- }
- // Second : try in DB
-
- // Does not exist : Create new one
- node = new Node(alien_node->GetType(),this,internal_parent);
- node->SetChildrenLoaded(true);
- // Copy fields values from alien
- Node::FieldValueMapType::iterator i,j;
- for (i = node->GetFieldValueMap().begin();
- i != node->GetFieldValueMap().end();
- i++)
- {
- j = alien_node->GetFieldValueMap().find(i->first);
- if (j != alien_node->GetFieldValueMap().end() )
- {
- i->second = j->second;
- }
- }
-
- msw[2].Resume();
- if (node->GetType()!=Node::Patient)
- node->SetFieldValue("PARENT_ID",parent_id);
-
- // Insert in DB
- std::string val;
- BuildSQLFieldsValues(node,val);
- std::string insert("INSERT INTO ");
- insert += std::string(SQLiteTreeHandlerStructure::Table(node->GetType()))
- + " " + val + ";";
- // std::cout << "** SQL = '"<<insert<<"'"<<std::endl;
- UPDATEDB(insert);
- // std::cout << "** SQL OK"<<std::endl;
-
- // Store DB id of newly created node;
- long lastrow = mDB->lastRowId();
- std::stringstream ri;
- ri << mDB->lastRowId();
- node_id = ri.str();
- // std::cout << "LastRowId='"<<mDB->lastRowId()<<"' vs '"<<created_id<<"'"<<std::endl;
-
- node->SetFieldValue("ID",node_id);
- // 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++;
-
- return node;