4256290ce4b614a81a93e50e067fbe9c4242d03d
[toast/confclerk.git] / src / sql / schedulexmlparser.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011-2012 Philipp Spitzer, gregor herrmann, Stefan Stahl
4  *
5  * This file is part of ConfClerk.
6  *
7  * ConfClerk is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * ConfClerk is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <QDomDocument>
22 #include <QHash>
23
24 #include "schedulexmlparser.h"
25 #include "sqlengine.h"
26 #include "../gui/errormessage.h"
27
28 #include <QDebug>
29
30 ScheduleXmlParser::ScheduleXmlParser(SqlEngine* sqlEngine, QObject *aParent): QObject(aParent),sqlEngine(sqlEngine) {
31 }
32
33
34 void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, int conferenceId)
35 {
36     QDomDocument document;
37     QString xml_error;
38     int xml_error_line;
39     int xml_error_column;
40     if (!document.setContent (aData, false, &xml_error, &xml_error_line, &xml_error_column)) {
41         error_message("Could not parse schedule: " + xml_error + " at line " + QString("%1").arg(xml_error_line) + " column " + QString("%1").arg(xml_error_column));
42         return;
43     }
44
45     QDomElement scheduleElement = document.firstChildElement("schedule");
46
47     sqlEngine->beginTransaction();
48
49     QString conference_title;
50     if (!scheduleElement.isNull())
51     {
52         QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
53         if (!conferenceElement.isNull())
54         {
55             emit(parsingScheduleBegin());
56             QHash<QString,QString> conference;
57             conference["id"] = QVariant(conferenceId).toString(); // conference ID is assigned automatically if 0
58             conference["title"] = conferenceElement.firstChildElement("title").text();
59             conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
60             conference["venue"] = conferenceElement.firstChildElement("venue").text();
61             conference["city"] = conferenceElement.firstChildElement("city").text();
62             conference["start"] = conferenceElement.firstChildElement("start").text(); // date
63             conference["end"] = conferenceElement.firstChildElement("end").text(); // date
64             conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time
65             conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
66             conference["url"] = url;
67             sqlEngine->addConferenceToDB(conference, conferenceId);
68             conferenceId = conference["id"].toInt();
69             conference_title = conference["title"];
70         }
71
72         // we need to get count of all events in order to emit 'progressStatus' signal
73         int totalEventsCount = scheduleElement.elementsByTagName("event").count();
74
75         // parsing day elements
76         int currentEvent = 0; // hold global idx of processed event
77         QDomNodeList dayList = scheduleElement.elementsByTagName("day");
78         for (int i=0; i<dayList.count(); i++)
79         {
80             QDomElement dayElement = dayList.at(i).toElement();
81             //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT);
82             //int dayIndex = dayElement.attribute("index").toInt();
83
84             // parsing room elements
85             QDomNodeList roomList = dayElement.elementsByTagName("room");
86             for (int i=0; i<roomList.count(); i++)
87             {
88                 QDomElement roomElement = roomList.at(i).toElement();
89                 // roomElement has to be 'Element' and it has to have 'name' attribute
90                 // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute
91                 if(roomElement.hasAttribute("name"))
92                 {
93                     // parsing event elements
94                     QDomNodeList eventList = roomElement.elementsByTagName("event");
95                     for (int i=0; i<eventList.count(); i++)
96                     {
97                         currentEvent++;
98                         QDomElement eventElement = eventList.at(i).toElement();
99
100                         // now we have all info to create ROOM/EVENT_ROOM record(s)
101                         QHash<QString,QString> room;
102                         room["name"] = roomElement.attribute("name");
103                         room["event_id"] = eventElement.attribute("id");
104                         room["conference_id"] = QString::number(conferenceId,10);
105                         sqlEngine->addRoomToDB(room);
106
107                         // process event's nodes
108                         QHash<QString,QString> event;
109                         event["id"] = eventElement.attribute("id");;
110                         event["conference_id"] = QString::number(conferenceId, 10);
111                         event["start"] = eventElement.firstChildElement("start").text(); // time eg. 10:00
112                         event["date"] = dayElement.attribute("date"); // date eg. 2009-02-07
113                         event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
114                         event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson"
115                         event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome"
116                         event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome"
117                         event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string
118                         event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes"
119                         event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium"
120                         event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English"
121                         event["abstract"] = eventElement.firstChildElement("abstract").text(); // string
122                         event["description"] = eventElement.firstChildElement("description").text(); // string
123                         sqlEngine->addEventToDB(event);
124                         // process persons' nodes
125                         QDomElement personsElement = eventElement.firstChildElement("persons");
126                         QDomNodeList personList = personsElement.elementsByTagName("person");
127                         for(int i = 0;i < personList.count();i++){
128                             QHash<QString,QString> person;
129                             person["id"] = personList.at(i).toElement().attribute("id");
130                             person["name"] = personList.at(i).toElement().text();
131                             person["event_id"] = eventElement.attribute("id");
132                             person["conference_id"] = QString::number(conferenceId, 10);
133                             sqlEngine->addPersonToDB(person);
134                         }
135                         // process links' nodes
136                         QDomElement linksElement = eventElement.firstChildElement("links");
137                         QDomNodeList linkList = linksElement.elementsByTagName("link");
138                         for(int i = 0;i < linkList.count();i++){
139                             QHash<QString,QString> link;
140                             link["name"] = linkList.at(i).toElement().text();
141                             link["url"] = linkList.at(i).toElement().attribute("href");
142                             link["event_id"] = eventElement.attribute("id");
143                             link["conference_id"] = QString::number(conferenceId, 10);
144                             sqlEngine->addLinkToDB(link);
145                         }
146                         // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
147                         int status = currentEvent * 100 / totalEventsCount;
148                         progressStatus(status);
149                     } // parsing event elements
150                 }
151             } // parsing room elements
152         } // parsing day elements
153     } // schedule element
154     sqlEngine->commitTransaction();
155     if (!conference_title.isNull()) {
156         emit parsingScheduleEnd(conferenceId);
157     } else {
158         error_message("Could not parse schedule");
159     }
160 }
161