check for existence of conference before inserting it into DB
authorpavelpa <pavelpa@localhost>
Thu, 21 Jan 2010 19:48:46 +0000 (19:48 +0000)
committerpavelpa <pavelpa@localhost>
Thu, 21 Jan 2010 19:48:46 +0000 (19:48 +0000)
src/gui/mainwindow.ui
src/sql/schedulexmlparser.cpp
src/sql/sql.pro
src/sql/sqlengine.cpp

index 548d021..69b9cd0 100644 (file)
@@ -23,7 +23,7 @@
     <item row="0" column="0" >
      <widget class="QTabWidget" name="tabWidget" >
       <property name="currentIndex" >
-       <number>5</number>
+       <number>0</number>
       </property>
       <widget class="QWidget" name="dayViewTab" >
        <attribute name="title" >
index db74630..88c4686 100644 (file)
@@ -23,14 +23,12 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, SqlEngine *aDBEngine)
 
     if (!scheduleElement.isNull())
     {
-        // TODO: assign conferenceID based on eg. title
-        int conferenceID = 1; // HARD-WIRED for now to '1' - only one Conference
-
+        int confId = 0;
         QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
         if (!conferenceElement.isNull())
         {
             QHash<QString,QString> conference;
-            conference["id"] = QString::number(conferenceID,10);
+            conference["id"] = QString::number(0); // conference ID is assigned automatically, or obtained from the DB
             conference["title"] = conferenceElement.firstChildElement("title").text();
             conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
             conference["venue"] = conferenceElement.firstChildElement("venue").text();
@@ -41,6 +39,7 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, SqlEngine *aDBEngine)
             conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time
             conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
             aDBEngine->addConferenceToDB(conference);
+            confId = conference["id"].toInt();
         }
 
         // we need to get count of all events in order to emit 'progressStatus' signal
@@ -75,14 +74,14 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, SqlEngine *aDBEngine)
                         QHash<QString,QString> room;
                         room["name"] = roomElement.attribute("name");
                         room["event_id"] = eventElement.attribute("id");
-                        room["conference_id"] = QString::number(conferenceID,10);
+                        room["conference_id"] = QString::number(confId,10);
                         room["picture"] = "NOT DEFINED YET"; // TODO: implement some mapping to assign correct picture to specified room_name
                         aDBEngine->addRoomToDB(room);
 
                         // process event's nodes
                         QHash<QString,QString> event;
                         event["id"] = eventElement.attribute("id");;
-                        event["conference_id"] = QString::number(conferenceID, 10);
+                        event["conference_id"] = QString::number(confId, 10);
                         event["start"] = eventElement.firstChildElement("start").text(); // time eg. 10:00
                         event["date"] = dayElement.attribute("date"); // date eg. 2009-02-07
                         event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
@@ -105,7 +104,7 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, SqlEngine *aDBEngine)
                             person["id"] = personList.at(i).toElement().attribute("id");
                             person["name"] = personList.at(i).toElement().text();
                             person["event_id"] = eventElement.attribute("id");
-                            person["conference_id"] = QString::number(conferenceID, 10);
+                            person["conference_id"] = QString::number(confId, 10);
                             //qDebug() << "adding Person: " << person["name"];
                             aDBEngine->addPersonToDB(person);
                         }
@@ -117,7 +116,7 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, SqlEngine *aDBEngine)
                             link["name"] = linkList.at(i).toElement().text();
                             link["url"] = linkList.at(i).toElement().attribute("href");
                             link["event_id"] = eventElement.attribute("id");
-                            link["conference_id"] = QString::number(conferenceID, 10);
+                            link["conference_id"] = QString::number(confId, 10);
                             aDBEngine->addLinkToDB(link);
                         }
                         // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
index e293aa3..90b4ab5 100644 (file)
@@ -7,7 +7,7 @@ QT += sql xml
 
 # module dependencies
 LIBS += -L$$DESTDIR -lmvc -lorm
-INCLUDEPATH += ../mvc ../orm
+INCLUDEPATH += ../mvc ../orm ../app
 DEPENDPATH += .
 
 HEADERS += sqlengine.h \
index c369256..aa5b2d7 100644 (file)
@@ -6,8 +6,10 @@
 #include <QDateTime>
 
 #include <QDir>
+#include <appsettings.h>
 #include "sqlengine.h"
 #include <track.h>
+#include <conference.h>
 
 #include <QDebug>
 
@@ -63,21 +65,43 @@ void SqlEngine::addConferenceToDB(QHash<QString,QString> &aConference)
 
     if (db.isValid() && db.isOpen())
     {
-        QString values = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9', '%10'") \
-                         .arg(aConference["id"]) \
-                         .arg(aConference["title"]) \
-                         .arg(aConference["subtitle"]) \
-                         .arg(aConference["venue"]) \
-                         .arg(aConference["city"]) \
-                         .arg(QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t()) \
-                         .arg(QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t()) \
-                         .arg(aConference["days"]) \
-                         .arg(-QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0))) \
-                         .arg(-QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
-
-        QString query = QString("INSERT INTO CONFERENCE (id,title,subtitle,venue,city,start,end,days,day_change,timeslot_duration) VALUES (%1)").arg(values);
-        QSqlQuery result (query, db);
-        //LOG_AUTOTEST(query);
+        int confId = 0;
+        QList<Conference> confsList = Conference::getAll();
+        if(confsList.count())
+        {
+            QListIterator<Conference> i(confsList);
+            while (i.hasNext())
+            {
+                Conference conf = i.next();
+                if( aConference["title"] == conf.title() )
+                {
+                    confId = conf.id();
+                    aConference["id"] = QString::number(confId);
+                    break;
+                }
+            }
+        }
+
+        if(!confId) // conference 'aConference' isn't in the table => insert
+        {
+            QString values = QString("'%1', '%2', '%3', '%4', '%5', '%6', '%7', '%8', '%9'") \
+                             .arg(aConference["title"]) \
+                             .arg(aConference["subtitle"]) \
+                             .arg(aConference["venue"]) \
+                             .arg(aConference["city"]) \
+                             .arg(QDateTime(QDate::fromString(aConference["start"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t()) \
+                             .arg(QDateTime(QDate::fromString(aConference["end"],DATE_FORMAT),QTime(0,0),Qt::UTC).toTime_t()) \
+                             .arg(aConference["days"]) \
+                             .arg(-QTime::fromString(aConference["day_change"],TIME_FORMAT).secsTo(QTime(0,0))) \
+                             .arg(-QTime::fromString(aConference["timeslot_duration"],TIME_FORMAT).secsTo(QTime(0,0)));
+
+            QString query = QString("INSERT INTO CONFERENCE (title,subtitle,venue,city,start,end,days,day_change,timeslot_duration) VALUES (%1)").arg(values);
+            QSqlQuery result (query, db);
+            aConference["id"] = result.lastInsertId().toString(); // 'id' is assigned automatically
+
+            if(!AppSettings::confId()) // default conf Id isn't set yet => set it up
+                AppSettings::setConfId(confId);
+        }
     }
 }