From: pavelpa Date: Thu, 21 Jan 2010 19:48:46 +0000 (+0000) Subject: check for existence of conference before inserting it into DB X-Git-Tag: 0.5.0~221 X-Git-Url: https://git.toastfreeware.priv.at/toast/confclerk.git/commitdiff_plain/1735f5512cf3a65b37ef7dcdd7572457372f5095 check for existence of conference before inserting it into DB --- diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index 548d021..69b9cd0 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -23,7 +23,7 @@ - 5 + 0 diff --git a/src/sql/schedulexmlparser.cpp b/src/sql/schedulexmlparser.cpp index db74630..88c4686 100644 --- a/src/sql/schedulexmlparser.cpp +++ b/src/sql/schedulexmlparser.cpp @@ -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 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 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 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 %) diff --git a/src/sql/sql.pro b/src/sql/sql.pro index e293aa3..90b4ab5 100644 --- a/src/sql/sql.pro +++ b/src/sql/sql.pro @@ -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 \ diff --git a/src/sql/sqlengine.cpp b/src/sql/sqlengine.cpp index c369256..aa5b2d7 100644 --- a/src/sql/sqlengine.cpp +++ b/src/sql/sqlengine.cpp @@ -6,8 +6,10 @@ #include #include +#include #include "sqlengine.h" #include +#include #include @@ -63,21 +65,43 @@ void SqlEngine::addConferenceToDB(QHash &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 confsList = Conference::getAll(); + if(confsList.count()) + { + QListIterator 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); + } } }