source: src/sql/schedulexmlparser.cpp @ 68b2df2

qt5
Last change on this file since 68b2df2 was 68b2df2, checked in by gregor herrmann <gregoa@…>, 10 years ago

Add copyright to source.

  • Property mode set to 100644
File size: 8.6 KB
Line 
1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 * Copyright (C) 2011 Philipp Spitzer, gregor herrmann
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
24#include "schedulexmlparser.h"
25#include "sqlengine.h"
26#include "../gui/errormessage.h"
27
28#include <QDebug>
29
30ScheduleXmlParser::ScheduleXmlParser(QObject *aParent)
31    : QObject(aParent)
32{
33}
34
35void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url)
36{
37    QDomDocument document;
38    QString xml_error;
39    if (!document.setContent (aData, false, &xml_error)) {
40        error_message("Could not parse schedule: " + xml_error);
41        return;
42    }
43
44    QDomElement scheduleElement = document.firstChildElement("schedule");
45
46    SqlEngine::beginTransaction();
47
48    int confId = 0;
49    QString conference_title;
50    if (!scheduleElement.isNull())
51    {
52        QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
53        if (!conferenceElement.isNull())
54        {
55            QHash<QString,QString> conference;
56            conference["id"] = QString::number(0); // conference ID is assigned automatically, or obtained from the DB
57            conference["title"] = conferenceElement.firstChildElement("title").text();
58            conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
59            conference["venue"] = conferenceElement.firstChildElement("venue").text();
60            conference["city"] = conferenceElement.firstChildElement("city").text();
61            conference["start"] = conferenceElement.firstChildElement("start").text(); // date
62            conference["end"] = conferenceElement.firstChildElement("end").text(); // date
63            conference["days"] = conferenceElement.firstChildElement("days").text(); // int
64            conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time
65            conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
66            conference["url"] = url;
67            SqlEngine::addConferenceToDB(conference);
68            confId = conference["id"].toInt();
69            conference_title = conference["title"];
70            emit(parsingScheduleBegin());
71        }
72
73        // we need to get count of all events in order to emit 'progressStatus' signal
74        int totalEventsCount = scheduleElement.elementsByTagName("event").count();
75
76        // parsing day elements
77        int currentEvent = 0; // hold global idx of processed event
78        QDomNodeList dayList = scheduleElement.elementsByTagName("day");
79        for (int i=0; i<dayList.count(); i++)
80        {
81            QDomElement dayElement = dayList.at(i).toElement();
82            //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT);
83            //int dayIndex = dayElement.attribute("index").toInt();
84
85            // parsing room elements
86            QDomNodeList roomList = dayElement.elementsByTagName("room");
87            for (int i=0; i<roomList.count(); i++)
88            {
89                QDomElement roomElement = roomList.at(i).toElement();
90                // roomElement has to be 'Element' and it has to have 'name' attribute
91                // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute
92                if(roomElement.hasAttribute("name"))
93                {
94                    // parsing event elements
95                    QDomNodeList eventList = roomElement.elementsByTagName("event");
96                    for (int i=0; i<eventList.count(); i++)
97                    {
98                        currentEvent++;
99                        QDomElement eventElement = eventList.at(i).toElement();
100
101                        // now we have all info to create ROOM/EVENT_ROOM record(s)
102                        QHash<QString,QString> room;
103                        room["name"] = roomElement.attribute("name");
104                        room["event_id"] = eventElement.attribute("id");
105                        room["conference_id"] = QString::number(confId,10);
106                        room["picture"] = ""; // TODO: implement some mapping to assign correct picture to specified room_name
107                        SqlEngine::addRoomToDB(room);
108
109                        // process event's nodes
110                        QHash<QString,QString> event;
111                        event["id"] = eventElement.attribute("id");;
112                        event["conference_id"] = QString::number(confId, 10);
113                        event["start"] = eventElement.firstChildElement("start").text(); // time eg. 10:00
114                        event["date"] = dayElement.attribute("date"); // date eg. 2009-02-07
115                        event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
116                        event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson"
117                        event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome"
118                        event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome"
119                        event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string
120                        event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes"
121                        event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium"
122                        event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English"
123                        event["abstract"] = eventElement.firstChildElement("abstract").text(); // string
124                        event["description"] = eventElement.firstChildElement("description").text(); // string
125                        SqlEngine::addEventToDB(event);
126                        // process persons' nodes
127                        QList<QString> persons;
128                        QDomElement personsElement = eventElement.firstChildElement("persons");
129                        QDomNodeList personList = personsElement.elementsByTagName("person");
130                        for(int i = 0;i < personList.count();i++){
131                            QHash<QString,QString> person;
132                            person["id"] = personList.at(i).toElement().attribute("id");
133                            person["name"] = personList.at(i).toElement().text();
134                            person["event_id"] = eventElement.attribute("id");
135                            person["conference_id"] = QString::number(confId, 10);
136                            //qDebug() << "adding Person: " << person["name"];
137                            SqlEngine::addPersonToDB(person);
138                        }
139                        // process links' nodes
140                        QDomElement linksElement = eventElement.firstChildElement("links");
141                        QDomNodeList linkList = linksElement.elementsByTagName("link");
142                        for(int i = 0;i < linkList.count();i++){
143                            QHash<QString,QString> link;
144                            link["name"] = linkList.at(i).toElement().text();
145                            link["url"] = linkList.at(i).toElement().attribute("href");
146                            link["event_id"] = eventElement.attribute("id");
147                            link["conference_id"] = QString::number(confId, 10);
148                            SqlEngine::addLinkToDB(link);
149                        }
150                        // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
151                        int status = currentEvent * 100 / totalEventsCount;
152                        progressStatus(status);
153                    } // parsing event elements
154                }
155            } // parsing room elements
156        } // parsing day elements
157    } // schedule element
158    SqlEngine::commitTransaction();
159    if (!conference_title.isNull()) {
160        emit parsingScheduleEnd(conference_title);
161    } else {
162        error_message("Could not parse schedule");
163    }
164}
165
Note: See TracBrowser for help on using the repository browser.