check for existence of conference before inserting it into DB
[toast/confclerk.git] / src / sql / schedulexmlparser.cpp
1
2 #include <QDomDocument>
3 #include <QHash>
4
5 #include "schedulexmlparser.h"
6 #include "sqlengine.h"
7
8 #include <QDebug>
9
10 ScheduleXmlParser::ScheduleXmlParser(QObject *aParent)
11     : QObject(aParent)
12 {
13 }
14
15 void ScheduleXmlParser::parseData(const QByteArray &aData, SqlEngine *aDBEngine)
16 {
17     Q_ASSERT(NULL != aDBEngine);
18
19     QDomDocument document;
20     document.setContent (aData, false);
21
22     QDomElement scheduleElement = document.firstChildElement("schedule");
23
24     if (!scheduleElement.isNull())
25     {
26         int confId = 0;
27         QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
28         if (!conferenceElement.isNull())
29         {
30             QHash<QString,QString> conference;
31             conference["id"] = QString::number(0); // conference ID is assigned automatically, or obtained from the DB
32             conference["title"] = conferenceElement.firstChildElement("title").text();
33             conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
34             conference["venue"] = conferenceElement.firstChildElement("venue").text();
35             conference["city"] = conferenceElement.firstChildElement("city").text();
36             conference["start"] = conferenceElement.firstChildElement("start").text(); // date
37             conference["end"] = conferenceElement.firstChildElement("end").text(); // date
38             conference["days"] = conferenceElement.firstChildElement("days").text(); // int
39             conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time
40             conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
41             aDBEngine->addConferenceToDB(conference);
42             confId = conference["id"].toInt();
43         }
44
45         // we need to get count of all events in order to emit 'progressStatus' signal
46         int totalEventsCount = scheduleElement.elementsByTagName("event").count();
47
48         // parsing day elements
49         int currentEvent = 0; // hold global idx of processed event
50         QDomNodeList dayList = scheduleElement.elementsByTagName("day");
51         for (int i=0; i<dayList.count(); i++)
52         {
53             QDomElement dayElement = dayList.at(i).toElement();
54             //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT);
55             //int dayIndex = dayElement.attribute("index").toInt();
56
57             // parsing room elements
58             QDomNodeList roomList = dayElement.elementsByTagName("room");
59             for (int i=0; i<roomList.count(); i++)
60             {
61                 QDomElement roomElement = roomList.at(i).toElement();
62                 // roomElement has to be 'Element' and it has to have 'name' attribute
63                 // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute
64                 if(roomElement.hasAttribute("name"))
65                 {
66                     // parsing event elements
67                     QDomNodeList eventList = roomElement.elementsByTagName("event");
68                     for (int i=0; i<eventList.count(); i++)
69                     {
70                         currentEvent++;
71                         QDomElement eventElement = eventList.at(i).toElement();
72
73                         // now we have all info to create ROOM/EVENT_ROOM record(s)
74                         QHash<QString,QString> room;
75                         room["name"] = roomElement.attribute("name");
76                         room["event_id"] = eventElement.attribute("id");
77                         room["conference_id"] = QString::number(confId,10);
78                         room["picture"] = "NOT DEFINED YET"; // TODO: implement some mapping to assign correct picture to specified room_name
79                         aDBEngine->addRoomToDB(room);
80
81                         // process event's nodes
82                         QHash<QString,QString> event;
83                         event["id"] = eventElement.attribute("id");;
84                         event["conference_id"] = QString::number(confId, 10);
85                         event["start"] = eventElement.firstChildElement("start").text(); // time eg. 10:00
86                         event["date"] = dayElement.attribute("date"); // date eg. 2009-02-07
87                         event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
88                         event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson"
89                         event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome"
90                         event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome"
91                         event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string
92                         event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes"
93                         event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium"
94                         event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English"
95                         event["abstract"] = eventElement.firstChildElement("abstract").text(); // string
96                         event["description"] = eventElement.firstChildElement("description").text(); // string
97                         aDBEngine->addEventToDB(event);
98                         // process persons' nodes
99                         QList<QString> persons;
100                         QDomElement personsElement = eventElement.firstChildElement("persons");
101                         QDomNodeList personList = personsElement.elementsByTagName("person");
102                         for(int i = 0;i < personList.count();i++){
103                             QHash<QString,QString> person;
104                             person["id"] = personList.at(i).toElement().attribute("id");
105                             person["name"] = personList.at(i).toElement().text();
106                             person["event_id"] = eventElement.attribute("id");
107                             person["conference_id"] = QString::number(confId, 10);
108                             //qDebug() << "adding Person: " << person["name"];
109                             aDBEngine->addPersonToDB(person);
110                         }
111                         // process links' nodes
112                         QDomElement linksElement = eventElement.firstChildElement("links");
113                         QDomNodeList linkList = linksElement.elementsByTagName("link");
114                         for(int i = 0;i < linkList.count();i++){
115                             QHash<QString,QString> link;
116                             link["name"] = linkList.at(i).toElement().text();
117                             link["url"] = linkList.at(i).toElement().attribute("href");
118                             link["event_id"] = eventElement.attribute("id");
119                             link["conference_id"] = QString::number(confId, 10);
120                             aDBEngine->addLinkToDB(link);
121                         }
122                         // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
123                         int status = currentEvent * 100 / totalEventsCount;
124                         progressStatus(status);
125                     } // parsing event elements
126                 }
127             } // parsing room elements
128         } // parsing day elements
129     } // schedule element
130 }
131