bump version after release
[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 #include <stdexcept>
31
32 ScheduleXmlParser::ScheduleXmlParser(SqlEngine* sqlEngine, QObject *aParent): QObject(aParent),sqlEngine(sqlEngine) {
33 }
34
35
36 class ParseException: public std::runtime_error {
37 public:
38     ParseException(const QString& message): std::runtime_error(message.toStdString()) {}
39 };
40
41
42 void checkEvent(QHash<QString,QString>& event) {
43     QString event_id = event["id"];
44     if (event_id.trimmed().isEmpty()) throw ParseException(QObject::tr("The ID of event '%1' is missing.").arg(event["title"]));
45     bool ok;
46     event_id.toInt(&ok);
47     if (!ok) throw ParseException(QObject::tr("The ID '%2' of event '%1' is not numeric.").arg(event["title"]).arg(event_id));
48 }
49
50
51 void ScheduleXmlParser::parseDataImpl(const QByteArray &aData, const QString& url, int conferenceId) {
52     QDomDocument document;
53     QString xml_error;
54     int xml_error_line;
55     int xml_error_column;
56     if (!document.setContent(aData, false, &xml_error, &xml_error_line, &xml_error_column)) {
57         throw ParseException("Could not parse schedule: " + xml_error + " at line " + QString("%1").arg(xml_error_line) + " column " + QString("%1").arg(xml_error_column));
58     }
59
60     QDomElement scheduleElement = document.firstChildElement("schedule");
61
62     TransactionRaii transaction(*sqlEngine); // begins the transaction
63
64     QString conference_title;
65     if (!scheduleElement.isNull())
66     {
67         QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
68         QTime conference_day_change;
69         if (!conferenceElement.isNull())
70         {
71             emit(parsingScheduleBegin());
72             QHash<QString,QString> conference;
73             conference["id"] = QString::number(conferenceId); // conference ID is assigned automatically if 0
74             conference["title"] = conferenceElement.firstChildElement("title").text();
75             conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
76             conference["venue"] = conferenceElement.firstChildElement("venue").text();
77             conference["city"] = conferenceElement.firstChildElement("city").text();
78             conference["start"] = conferenceElement.firstChildElement("start").text(); // date
79             conference["end"] = conferenceElement.firstChildElement("end").text(); // date
80             QString conferenceDayChangeStr = conferenceElement.firstChildElement("day_change").text(); // time, e.g. "04:00:00"
81             if (conferenceDayChangeStr.isEmpty()) conferenceDayChangeStr = "04:00:00";
82             conference["day_change"] = conferenceDayChangeStr;
83             conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
84             conference["url"] = url;
85             sqlEngine->addConferenceToDB(conference, conferenceId);
86             conferenceId = conference["id"].toInt();
87             conference_title = conference["title"];
88             conference_day_change = QTime(0, 0).addSecs(conference["day_change"].toInt());
89         }
90
91         // we need to get count of all events in order to emit 'progressStatus' signal
92         int totalEventsCount = scheduleElement.elementsByTagName("event").count();
93
94         // parsing day elements
95         int currentEvent = 0; // hold global idx of processed event
96         QDomNodeList dayList = scheduleElement.elementsByTagName("day");
97         for (int i=0; i<dayList.count(); i++)
98         {
99             QDomElement dayElement = dayList.at(i).toElement();
100             //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT);
101             //int dayIndex = dayElement.attribute("index").toInt();
102
103             // parsing room elements
104             QDomNodeList roomList = dayElement.elementsByTagName("room");
105             for (int i=0; i<roomList.count(); i++)
106             {
107                 QDomElement roomElement = roomList.at(i).toElement();
108                 // roomElement has to be 'Element' and it has to have 'name' attribute
109                 // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute
110                 if(roomElement.hasAttribute("name"))
111                 {
112                     // parsing event elements
113                     QDomNodeList eventList = roomElement.elementsByTagName("event");
114                     for (int i=0; i<eventList.count(); i++)
115                     {
116                         currentEvent++;
117                         QDomElement eventElement = eventList.at(i).toElement();
118
119                         // now we have all info to create ROOM/EVENT_ROOM record(s)
120                         QHash<QString,QString> room;
121                         room["name"] = roomElement.attribute("name");
122                         room["event_id"] = eventElement.attribute("id");
123                         room["conference_id"] = QString::number(conferenceId,10);
124                         sqlEngine->addRoomToDB(room);
125
126                         // process event's nodes
127                         QHash<QString,QString> event;
128                         event["id"] = eventElement.attribute("id");
129                         event["conference_id"] = QString::number(conferenceId, 10);
130                         QTime event_start = QTime::fromString(eventElement.firstChildElement("start").text(), sqlEngine->TIME_FORMAT);
131                         event["start"] = event_start.toString(sqlEngine->TIME_FORMAT); // time eg. 10:00
132                         QDate event_date;
133                         QDomElement eventDateElement = eventElement.firstChildElement("date");
134                         if (!eventDateElement.isNull()) {
135                             QString date_str = eventDateElement.text(); // date eg. 2009-02-07T10:00:00+00:00
136                             event_date = QDate::fromString(date_str.left(sqlEngine->DATE_FORMAT.size()), sqlEngine->DATE_FORMAT);
137                         } else {
138                             event_date = QDate::fromString(dayElement.attribute("date"),sqlEngine->DATE_FORMAT); // date eg. 2009-02-07
139                             if (event_start < conference_day_change) event_date = event_date.addDays(1);
140                         }
141                         event["date"] = event_date.toString(sqlEngine->DATE_FORMAT); // date eg. 2009-02-07
142                         event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
143                         event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson"
144                         event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome"
145                         event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome"
146                         event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string
147                         event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes"
148                         event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium"
149                         event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English"
150                         event["abstract"] = eventElement.firstChildElement("abstract").text(); // string
151                         event["description"] = eventElement.firstChildElement("description").text(); // string
152                         checkEvent(event);
153                         sqlEngine->addEventToDB(event);
154                         // process persons' nodes
155                         QDomElement personsElement = eventElement.firstChildElement("persons");
156                         QDomNodeList personList = personsElement.elementsByTagName("person");
157                         for(int i = 0;i < personList.count();i++){
158                             QHash<QString,QString> person;
159                             person["id"] = personList.at(i).toElement().attribute("id");
160                             person["name"] = personList.at(i).toElement().text();
161                             person["event_id"] = eventElement.attribute("id");
162                             person["conference_id"] = QString::number(conferenceId, 10);
163                             sqlEngine->addPersonToDB(person);
164                         }
165                         // process links' nodes
166                         QDomElement linksElement = eventElement.firstChildElement("links");
167                         QDomNodeList linkList = linksElement.elementsByTagName("link");
168                         for(int i = 0;i < linkList.count();i++){
169                             QHash<QString,QString> link;
170                             link["name"] = linkList.at(i).toElement().text();
171                             link["url"] = linkList.at(i).toElement().attribute("href");
172                             link["event_id"] = eventElement.attribute("id");
173                             link["conference_id"] = QString::number(conferenceId, 10);
174                             sqlEngine->addLinkToDB(link);
175                         }
176                         // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
177                         int status = currentEvent * 100 / totalEventsCount;
178                         progressStatus(status);
179                     } // parsing event elements
180                 }
181             } // parsing room elements
182         } // parsing day elements
183     } // schedule element
184     if (conference_title.isNull()) throw ParseException("Could not parse schedule");
185
186     transaction.commit();
187     emit parsingScheduleEnd(conferenceId);
188 }
189
190
191 void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, int conferenceId) {
192     try {
193         parseDataImpl(aData, url, conferenceId);
194     } catch (ParseException& e) {
195         error_message(e.what());
196     }
197 }
198