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 | QHash<QString,QString> conference; |
---|
70 | if (!conferenceElement.isNull()) |
---|
71 | { |
---|
72 | emit(parsingScheduleBegin()); |
---|
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["utc_offset"] = QString(); |
---|
85 | conference["display_time_shift"] = QString(); |
---|
86 | conference["url"] = url; |
---|
87 | sqlEngine->addConferenceToDB(conference, conferenceId); |
---|
88 | conferenceId = conference["id"].toInt(); |
---|
89 | conference_title = conference["title"]; |
---|
90 | conference_day_change = QTime(0, 0).addSecs(conference["day_change"].toInt()); |
---|
91 | } |
---|
92 | |
---|
93 | // we need to get count of all events in order to emit 'progressStatus' signal |
---|
94 | int totalEventsCount = scheduleElement.elementsByTagName("event").count(); |
---|
95 | |
---|
96 | // parsing day elements |
---|
97 | int currentEvent = 0; // hold global idx of processed event |
---|
98 | QDomNodeList dayList = scheduleElement.elementsByTagName("day"); |
---|
99 | for (int i=0; i<dayList.count(); i++) |
---|
100 | { |
---|
101 | QDomElement dayElement = dayList.at(i).toElement(); |
---|
102 | //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT); |
---|
103 | //int dayIndex = dayElement.attribute("index").toInt(); |
---|
104 | |
---|
105 | // parsing room elements |
---|
106 | QDomNodeList roomList = dayElement.elementsByTagName("room"); |
---|
107 | for (int i=0; i<roomList.count(); i++) |
---|
108 | { |
---|
109 | QDomElement roomElement = roomList.at(i).toElement(); |
---|
110 | // roomElement has to be 'Element' and it has to have 'name' attribute |
---|
111 | // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute |
---|
112 | if(roomElement.hasAttribute("name")) |
---|
113 | { |
---|
114 | // parsing event elements |
---|
115 | QDomNodeList eventList = roomElement.elementsByTagName("event"); |
---|
116 | for (int i=0; i<eventList.count(); i++) |
---|
117 | { |
---|
118 | currentEvent++; |
---|
119 | QDomElement eventElement = eventList.at(i).toElement(); |
---|
120 | |
---|
121 | // now we have all info to create ROOM/EVENT_ROOM record(s) |
---|
122 | QHash<QString,QString> room; |
---|
123 | room["name"] = roomElement.attribute("name"); |
---|
124 | room["event_id"] = eventElement.attribute("id"); |
---|
125 | room["conference_id"] = QString::number(conferenceId,10); |
---|
126 | sqlEngine->addRoomToDB(room); |
---|
127 | |
---|
128 | // process event's nodes |
---|
129 | QHash<QString,QString> event; |
---|
130 | event["id"] = eventElement.attribute("id"); |
---|
131 | event["conference_id"] = QString::number(conferenceId, 10); |
---|
132 | QTime event_start = QTime::fromString(eventElement.firstChildElement("start").text(), sqlEngine->TIME_FORMAT); |
---|
133 | event["start"] = event_start.toString(sqlEngine->TIME_FORMAT); // time eg. 10:00 |
---|
134 | QDate event_date; |
---|
135 | QDomElement eventDateElement = eventElement.firstChildElement("date"); |
---|
136 | if (!eventDateElement.isNull()) { |
---|
137 | QString date_str = eventDateElement.text(); // date eg. 2009-02-07T10:00:00+00:00 |
---|
138 | QDateTime event_date_time = QDateTime::fromString(date_str, Qt::ISODate); |
---|
139 | if (conference.value("utc_offset").isEmpty()) { |
---|
140 | QDateTime utc_event_date_time = event_date_time; |
---|
141 | utc_event_date_time.setTimeSpec(Qt::UTC); |
---|
142 | int conference_utc_offset = event_date_time.secsTo(utc_event_date_time); |
---|
143 | conference["utc_offset"] = QString::number(conference_utc_offset); |
---|
144 | } |
---|
145 | event_date = event_date_time.date(); |
---|
146 | } else { |
---|
147 | event_date = QDate::fromString(dayElement.attribute("date"), Qt::ISODate); // date eg. 2009-02-07 |
---|
148 | if (event_start < conference_day_change) event_date = event_date.addDays(1); |
---|
149 | } |
---|
150 | event["date"] = event_date.toString(sqlEngine->DATE_FORMAT); // date eg. 2009-02-07 |
---|
151 | event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30 |
---|
152 | event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson" |
---|
153 | event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome" |
---|
154 | event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome" |
---|
155 | event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string |
---|
156 | event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes" |
---|
157 | event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium" |
---|
158 | event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English" |
---|
159 | event["abstract"] = eventElement.firstChildElement("abstract").text(); // string |
---|
160 | event["description"] = eventElement.firstChildElement("description").text(); // string |
---|
161 | checkEvent(event); |
---|
162 | sqlEngine->addEventToDB(event); |
---|
163 | // process persons' nodes |
---|
164 | QDomElement personsElement = eventElement.firstChildElement("persons"); |
---|
165 | QDomNodeList personList = personsElement.elementsByTagName("person"); |
---|
166 | for(int i = 0;i < personList.count();i++){ |
---|
167 | QHash<QString,QString> person; |
---|
168 | person["id"] = personList.at(i).toElement().attribute("id"); |
---|
169 | person["name"] = personList.at(i).toElement().text(); |
---|
170 | person["event_id"] = eventElement.attribute("id"); |
---|
171 | person["conference_id"] = QString::number(conferenceId, 10); |
---|
172 | sqlEngine->addPersonToDB(person); |
---|
173 | } |
---|
174 | // process links' nodes |
---|
175 | QDomElement linksElement = eventElement.firstChildElement("links"); |
---|
176 | QDomNodeList linkList = linksElement.elementsByTagName("link"); |
---|
177 | for(int i = 0;i < linkList.count();i++){ |
---|
178 | QHash<QString,QString> link; |
---|
179 | link["name"] = linkList.at(i).toElement().text(); |
---|
180 | link["url"] = linkList.at(i).toElement().attribute("href"); |
---|
181 | link["event_id"] = eventElement.attribute("id"); |
---|
182 | link["conference_id"] = QString::number(conferenceId, 10); |
---|
183 | sqlEngine->addLinkToDB(link); |
---|
184 | } |
---|
185 | // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %) |
---|
186 | int status = currentEvent * 100 / totalEventsCount; |
---|
187 | progressStatus(status); |
---|
188 | } // parsing event elements |
---|
189 | } |
---|
190 | } // parsing room elements |
---|
191 | } // parsing day elements |
---|
192 | |
---|
193 | // Re-write conference as utc_offset was not known previously |
---|
194 | if (!conference.value("utc_offset").isEmpty()) { |
---|
195 | sqlEngine->addConferenceToDB(conference, conferenceId); |
---|
196 | } |
---|
197 | |
---|
198 | } // schedule element |
---|
199 | if (conference_title.isNull()) throw ParseException("Could not parse schedule"); |
---|
200 | |
---|
201 | transaction.commit(); |
---|
202 | emit parsingScheduleEnd(conferenceId); |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, int conferenceId) { |
---|
207 | try { |
---|
208 | parseDataImpl(aData, url, conferenceId); |
---|
209 | } catch (ParseException& e) { |
---|
210 | error_message(e.what()); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|