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