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