source: src/sql/schedulexmlparser.cpp

Last change on this file was 763b877, checked in by Philipp Spitzer <philipp@…>, 5 months ago

Fix bug: display_time_shift was lost when conference was reloaded.

  • Property mode set to 100644
File size: 11.3 KB
RevLine 
[ca90cb1]1/*
2 * Copyright (C) 2010 Ixonos Plc.
[ffb6be7]3 * Copyright (C) 2011-2021 Philipp Spitzer, gregor herrmann, Stefan Stahl
[ca90cb1]4 *
[6df32f2]5 * This file is part of ConfClerk.
[ca90cb1]6 *
[6df32f2]7 * ConfClerk is free software: you can redistribute it and/or modify it
[ca90cb1]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 *
[6df32f2]12 * ConfClerk is distributed in the hope that it will be useful, but
[ca90cb1]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
[6df32f2]18 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
[ca90cb1]19 */
[30e2bdf]20
21#include <QDomDocument>
22#include <QHash>
[7ba0378]23#include <QTime>
[30e2bdf]24
25#include "schedulexmlparser.h"
26#include "sqlengine.h"
[3329d39]27#include "../gui/errormessage.h"
[30e2bdf]28
29#include <QDebug>
[f519c91]30#include <stdexcept>
[30e2bdf]31
[61346c9]32ScheduleXmlParser::ScheduleXmlParser(SqlEngine* sqlEngine, QObject *aParent): QObject(aParent),sqlEngine(sqlEngine) {
[30e2bdf]33}
34
[61346c9]35
[9782bbb]36class ParseException: public std::runtime_error {
37public:
38    ParseException(const QString& message): std::runtime_error(message.toStdString()) {}
39};
40
41
[01d1452]42void 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
[961971e]51void ScheduleXmlParser::parseDataImpl(const QByteArray &aData, const QString& url, int conferenceId) {
[30e2bdf]52    QDomDocument document;
[3329d39]53    QString xml_error;
[d466553]54    int xml_error_line;
55    int xml_error_column;
[9782bbb]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));
[3329d39]58    }
[30e2bdf]59
60    QDomElement scheduleElement = document.firstChildElement("schedule");
61
[a2f0d1e]62    TransactionRaii transaction(*sqlEngine); // begins the transaction
[1bad318]63
[b431d47]64    QString conference_title;
[30e2bdf]65    if (!scheduleElement.isNull())
66    {
67        QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
[7ba0378]68        QTime conference_day_change;
[8d47489]69        QHash<QString,QString> conference;
[30e2bdf]70        if (!conferenceElement.isNull())
71        {
[09a5663]72            emit(parsingScheduleBegin());
[908b4ce]73            conference["id"] = QString::number(conferenceId); // conference ID is assigned automatically if 0
[30e2bdf]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
[d03ca41]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;
[30e2bdf]83            conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
[8d47489]84            conference["utc_offset"] = QString();
85            conference["display_time_shift"] = QString();
[d06ae27]86            conference["url"] = url;
[763b877]87            sqlEngine->addConferenceToDB(conference, conferenceId, conferenceId != 0);
[2dffed3]88            conferenceId = conference["id"].toInt();
[b431d47]89            conference_title = conference["title"];
[7ba0378]90            conference_day_change = QTime(0, 0).addSecs(conference["day_change"].toInt());
[30e2bdf]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");
[2dffed3]125                        room["conference_id"] = QString::number(conferenceId,10);
[61346c9]126                        sqlEngine->addRoomToDB(room);
[30e2bdf]127
128                        // process event's nodes
[72cd3af]129                        QHash<QString,QString> event;
[01d1452]130                        event["id"] = eventElement.attribute("id");
[2dffed3]131                        event["conference_id"] = QString::number(conferenceId, 10);
[7ba0378]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
[8d47489]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();
[7ba0378]146                        } else {
[8d47489]147                            event_date = QDate::fromString(dayElement.attribute("date"), Qt::ISODate); // date eg. 2009-02-07
[7ba0378]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
[72cd3af]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
[30e2bdf]156                        event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes"
[72cd3af]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
[01d1452]161                        checkEvent(event);
[61346c9]162                        sqlEngine->addEventToDB(event);
[72cd3af]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");
[2dffed3]171                            person["conference_id"] = QString::number(conferenceId, 10);
[61346c9]172                            sqlEngine->addPersonToDB(person);
[72cd3af]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");
[2dffed3]182                            link["conference_id"] = QString::number(conferenceId, 10);
[61346c9]183                            sqlEngine->addLinkToDB(link);
[72cd3af]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;
[30e2bdf]187                        progressStatus(status);
188                    } // parsing event elements
189                }
190            } // parsing room elements
191        } // parsing day elements
[8d47489]192
193        // Re-write conference as utc_offset was not known previously
194        if (!conference.value("utc_offset").isEmpty()) {
[763b877]195            sqlEngine->addConferenceToDB(conference, conferenceId, conferenceId != 0);
[8d47489]196        }
197
[30e2bdf]198    } // schedule element
[9782bbb]199    if (conference_title.isNull()) throw ParseException("Could not parse schedule");
200
201    transaction.commit();
202    emit parsingScheduleEnd(conferenceId);
[30e2bdf]203}
204
[961971e]205
206void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, int conferenceId) {
[9782bbb]207    try {
208        parseDataImpl(aData, url, conferenceId);
209    } catch (ParseException& e) {
210        error_message(e.what());
211    }
[961971e]212}
213
Note: See TracBrowser for help on using the repository browser.