Import schedules with dates attached to events correctly.
[toast/confclerk.git] / src / sql / schedulexmlparser.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011-2017 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 #include <QTime>
24
25 #include "schedulexmlparser.h"
26 #include "sqlengine.h"
27 #include "../gui/errormessage.h"
28
29 #include <QDebug>
30
31 ScheduleXmlParser::ScheduleXmlParser(SqlEngine* sqlEngine, QObject *aParent): QObject(aParent),sqlEngine(sqlEngine) {
32 }
33
34
35 void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, int conferenceId)
36 {
37     QDomDocument document;
38     QString xml_error;
39     int xml_error_line;
40     int xml_error_column;
41     if (!document.setContent (aData, false, &xml_error, &xml_error_line, &xml_error_column)) {
42         error_message("Could not parse schedule: " + xml_error + " at line " + QString("%1").arg(xml_error_line) + " column " + QString("%1").arg(xml_error_column));
43         return;
44     }
45
46     QDomElement scheduleElement = document.firstChildElement("schedule");
47
48     sqlEngine->beginTransaction();
49
50     QString conference_title;
51     if (!scheduleElement.isNull())
52     {
53         QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
54         QTime conference_day_change;
55         if (!conferenceElement.isNull())
56         {
57             emit(parsingScheduleBegin());
58             QHash<QString,QString> conference;
59             conference["id"] = QString::number(conferenceId); // conference ID is assigned automatically if 0
60             conference["title"] = conferenceElement.firstChildElement("title").text();
61             conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
62             conference["venue"] = conferenceElement.firstChildElement("venue").text();
63             conference["city"] = conferenceElement.firstChildElement("city").text();
64             conference["start"] = conferenceElement.firstChildElement("start").text(); // date
65             conference["end"] = conferenceElement.firstChildElement("end").text(); // date
66             conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time
67             conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
68             conference["url"] = url;
69             sqlEngine->addConferenceToDB(conference, conferenceId);
70             conferenceId = conference["id"].toInt();
71             conference_title = conference["title"];
72             conference_day_change = QTime(0, 0).addSecs(conference["day_change"].toInt());
73         }
74
75         // we need to get count of all events in order to emit 'progressStatus' signal
76         int totalEventsCount = scheduleElement.elementsByTagName("event").count();
77
78         // parsing day elements
79         int currentEvent = 0; // hold global idx of processed event
80         QDomNodeList dayList = scheduleElement.elementsByTagName("day");
81         for (int i=0; i<dayList.count(); i++)
82         {
83             QDomElement dayElement = dayList.at(i).toElement();
84             //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT);
85             //int dayIndex = dayElement.attribute("index").toInt();
86
87             // parsing room elements
88             QDomNodeList roomList = dayElement.elementsByTagName("room");
89             for (int i=0; i<roomList.count(); i++)
90             {
91                 QDomElement roomElement = roomList.at(i).toElement();
92                 // roomElement has to be 'Element' and it has to have 'name' attribute
93                 // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute
94                 if(roomElement.hasAttribute("name"))
95                 {
96                     // parsing event elements
97                     QDomNodeList eventList = roomElement.elementsByTagName("event");
98                     for (int i=0; i<eventList.count(); i++)
99                     {
100                         currentEvent++;
101                         QDomElement eventElement = eventList.at(i).toElement();
102
103                         // now we have all info to create ROOM/EVENT_ROOM record(s)
104                         QHash<QString,QString> room;
105                         room["name"] = roomElement.attribute("name");
106                         room["event_id"] = eventElement.attribute("id");
107                         room["conference_id"] = QString::number(conferenceId,10);
108                         sqlEngine->addRoomToDB(room);
109
110                         // process event's nodes
111                         QHash<QString,QString> event;
112                         event["id"] = eventElement.attribute("id");;
113                         event["conference_id"] = QString::number(conferenceId, 10);
114                         QTime event_start = QTime::fromString(eventElement.firstChildElement("start").text(), sqlEngine->TIME_FORMAT);
115                         event["start"] = event_start.toString(sqlEngine->TIME_FORMAT); // time eg. 10:00
116                         QDate event_date;
117                         QDomElement eventDateElement = eventElement.firstChildElement("date");
118                         if (!eventDateElement.isNull()) {
119                             QString date_str = eventDateElement.text(); // date eg. 2009-02-07T10:00:00+00:00
120                             event_date = QDate::fromString(date_str.left(sqlEngine->DATE_FORMAT.size()), sqlEngine->DATE_FORMAT);
121                         } else {
122                             event_date = QDate::fromString(dayElement.attribute("date"),sqlEngine->DATE_FORMAT); // date eg. 2009-02-07
123                             if (event_start < conference_day_change) event_date = event_date.addDays(1);
124                         }
125                         event["date"] = event_date.toString(sqlEngine->DATE_FORMAT); // date eg. 2009-02-07
126                         event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
127                         event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson"
128                         event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome"
129                         event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome"
130                         event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string
131                         event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes"
132                         event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium"
133                         event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English"
134                         event["abstract"] = eventElement.firstChildElement("abstract").text(); // string
135                         event["description"] = eventElement.firstChildElement("description").text(); // string
136                         sqlEngine->addEventToDB(event);
137                         // process persons' nodes
138                         QDomElement personsElement = eventElement.firstChildElement("persons");
139                         QDomNodeList personList = personsElement.elementsByTagName("person");
140                         for(int i = 0;i < personList.count();i++){
141                             QHash<QString,QString> person;
142                             person["id"] = personList.at(i).toElement().attribute("id");
143                             person["name"] = personList.at(i).toElement().text();
144                             person["event_id"] = eventElement.attribute("id");
145                             person["conference_id"] = QString::number(conferenceId, 10);
146                             sqlEngine->addPersonToDB(person);
147                         }
148                         // process links' nodes
149                         QDomElement linksElement = eventElement.firstChildElement("links");
150                         QDomNodeList linkList = linksElement.elementsByTagName("link");
151                         for(int i = 0;i < linkList.count();i++){
152                             QHash<QString,QString> link;
153                             link["name"] = linkList.at(i).toElement().text();
154                             link["url"] = linkList.at(i).toElement().attribute("href");
155                             link["event_id"] = eventElement.attribute("id");
156                             link["conference_id"] = QString::number(conferenceId, 10);
157                             sqlEngine->addLinkToDB(link);
158                         }
159                         // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
160                         int status = currentEvent * 100 / totalEventsCount;
161                         progressStatus(status);
162                     } // parsing event elements
163                 }
164             } // parsing room elements
165         } // parsing day elements
166     } // schedule element
167     sqlEngine->commitTransaction();
168     if (!conference_title.isNull()) {
169         emit parsingScheduleEnd(conferenceId);
170     } else {
171         error_message("Could not parse schedule");
172     }
173 }
174