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