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