source: confclerk_git/src/sql/schedulexmlparser.cpp @ 01d1452

Last change on this file since 01d1452 was 01d1452, checked in by Philipp Spitzer <philipp@…>, 5 years ago

The ID of an event is checked now when importing the XML file.

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