2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011-2017 Philipp Spitzer, gregor herrmann, Stefan Stahl
5 * This file is part of ConfClerk.
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)
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
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/>.
21 #include <QDomDocument>
25 #include "schedulexmlparser.h"
26 #include "sqlengine.h"
27 #include "../gui/errormessage.h"
31 ScheduleXmlParser::ScheduleXmlParser(SqlEngine* sqlEngine, QObject *aParent): QObject(aParent),sqlEngine(sqlEngine) {
35 class ParseException: public std::runtime_error {
37 ParseException(const QString& message): std::runtime_error(message.toStdString()) {}
41 void ScheduleXmlParser::parseDataImpl(const QByteArray &aData, const QString& url, int conferenceId) {
42 QDomDocument document;
46 if (!document.setContent(aData, false, &xml_error, &xml_error_line, &xml_error_column)) {
47 throw ParseException("Could not parse schedule: " + xml_error + " at line " + QString("%1").arg(xml_error_line) + " column " + QString("%1").arg(xml_error_column));
50 QDomElement scheduleElement = document.firstChildElement("schedule");
52 TransactionRaii transaction(*sqlEngine); // begins the transaction
54 QString conference_title;
55 if (!scheduleElement.isNull())
57 QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
58 QTime conference_day_change;
59 if (!conferenceElement.isNull())
61 emit(parsingScheduleBegin());
62 QHash<QString,QString> conference;
63 conference["id"] = QString::number(conferenceId); // conference ID is assigned automatically if 0
64 conference["title"] = conferenceElement.firstChildElement("title").text();
65 conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
66 conference["venue"] = conferenceElement.firstChildElement("venue").text();
67 conference["city"] = conferenceElement.firstChildElement("city").text();
68 conference["start"] = conferenceElement.firstChildElement("start").text(); // date
69 conference["end"] = conferenceElement.firstChildElement("end").text(); // date
70 QString conferenceDayChangeStr = conferenceElement.firstChildElement("day_change").text(); // time, e.g. "04:00:00"
71 if (conferenceDayChangeStr.isEmpty()) conferenceDayChangeStr = "04:00:00";
72 conference["day_change"] = conferenceDayChangeStr;
73 conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
74 conference["url"] = url;
75 sqlEngine->addConferenceToDB(conference, conferenceId);
76 conferenceId = conference["id"].toInt();
77 conference_title = conference["title"];
78 conference_day_change = QTime(0, 0).addSecs(conference["day_change"].toInt());
81 // we need to get count of all events in order to emit 'progressStatus' signal
82 int totalEventsCount = scheduleElement.elementsByTagName("event").count();
84 // parsing day elements
85 int currentEvent = 0; // hold global idx of processed event
86 QDomNodeList dayList = scheduleElement.elementsByTagName("day");
87 for (int i=0; i<dayList.count(); i++)
89 QDomElement dayElement = dayList.at(i).toElement();
90 //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT);
91 //int dayIndex = dayElement.attribute("index").toInt();
93 // parsing room elements
94 QDomNodeList roomList = dayElement.elementsByTagName("room");
95 for (int i=0; i<roomList.count(); i++)
97 QDomElement roomElement = roomList.at(i).toElement();
98 // roomElement has to be 'Element' and it has to have 'name' attribute
99 // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute
100 if(roomElement.hasAttribute("name"))
102 // parsing event elements
103 QDomNodeList eventList = roomElement.elementsByTagName("event");
104 for (int i=0; i<eventList.count(); i++)
107 QDomElement eventElement = eventList.at(i).toElement();
109 // now we have all info to create ROOM/EVENT_ROOM record(s)
110 QHash<QString,QString> room;
111 room["name"] = roomElement.attribute("name");
112 room["event_id"] = eventElement.attribute("id");
113 room["conference_id"] = QString::number(conferenceId,10);
114 sqlEngine->addRoomToDB(room);
116 // process event's nodes
117 QHash<QString,QString> event;
118 event["id"] = eventElement.attribute("id");;
119 event["conference_id"] = QString::number(conferenceId, 10);
120 QTime event_start = QTime::fromString(eventElement.firstChildElement("start").text(), sqlEngine->TIME_FORMAT);
121 event["start"] = event_start.toString(sqlEngine->TIME_FORMAT); // time eg. 10:00
123 QDomElement eventDateElement = eventElement.firstChildElement("date");
124 if (!eventDateElement.isNull()) {
125 QString date_str = eventDateElement.text(); // date eg. 2009-02-07T10:00:00+00:00
126 event_date = QDate::fromString(date_str.left(sqlEngine->DATE_FORMAT.size()), sqlEngine->DATE_FORMAT);
128 event_date = QDate::fromString(dayElement.attribute("date"),sqlEngine->DATE_FORMAT); // date eg. 2009-02-07
129 if (event_start < conference_day_change) event_date = event_date.addDays(1);
131 event["date"] = event_date.toString(sqlEngine->DATE_FORMAT); // date eg. 2009-02-07
132 event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
133 event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson"
134 event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome"
135 event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome"
136 event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string
137 event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes"
138 event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium"
139 event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English"
140 event["abstract"] = eventElement.firstChildElement("abstract").text(); // string
141 event["description"] = eventElement.firstChildElement("description").text(); // string
142 sqlEngine->addEventToDB(event);
143 // process persons' nodes
144 QDomElement personsElement = eventElement.firstChildElement("persons");
145 QDomNodeList personList = personsElement.elementsByTagName("person");
146 for(int i = 0;i < personList.count();i++){
147 QHash<QString,QString> person;
148 person["id"] = personList.at(i).toElement().attribute("id");
149 person["name"] = personList.at(i).toElement().text();
150 person["event_id"] = eventElement.attribute("id");
151 person["conference_id"] = QString::number(conferenceId, 10);
152 sqlEngine->addPersonToDB(person);
154 // process links' nodes
155 QDomElement linksElement = eventElement.firstChildElement("links");
156 QDomNodeList linkList = linksElement.elementsByTagName("link");
157 for(int i = 0;i < linkList.count();i++){
158 QHash<QString,QString> link;
159 link["name"] = linkList.at(i).toElement().text();
160 link["url"] = linkList.at(i).toElement().attribute("href");
161 link["event_id"] = eventElement.attribute("id");
162 link["conference_id"] = QString::number(conferenceId, 10);
163 sqlEngine->addLinkToDB(link);
165 // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
166 int status = currentEvent * 100 / totalEventsCount;
167 progressStatus(status);
168 } // parsing event elements
170 } // parsing room elements
171 } // parsing day elements
172 } // schedule element
173 if (conference_title.isNull()) throw ParseException("Could not parse schedule");
175 transaction.commit();
176 emit parsingScheduleEnd(conferenceId);
180 void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, int conferenceId) {
182 parseDataImpl(aData, url, conferenceId);
183 } catch (ParseException& e) {
184 error_message(e.what());