On the way to fix #45.
[toast/confclerk.git] / src / sql / schedulexmlparser.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  * Copyright (C) 2011-2012 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
24 #include "schedulexmlparser.h"
25 #include "sqlengine.h"
26 #include "../gui/errormessage.h"
27
28 #include <QDebug>
29
30 ScheduleXmlParser::ScheduleXmlParser(QObject *aParent)
31     : QObject(aParent)
32 {
33 }
34
35 void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, int conferenceId)
36 {
37     QDomDocument document;
38     QString xml_error;
39     int xml_error_line;
40     int xml_error_column;
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));
43         return;
44     }
45
46     QDomElement scheduleElement = document.firstChildElement("schedule");
47
48     SqlEngine::beginTransaction();
49
50     QString conference_title;
51     if (!scheduleElement.isNull())
52     {
53         QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
54         if (!conferenceElement.isNull())
55         {
56             emit(parsingScheduleBegin());
57             QHash<QString,QString> conference;
58             conference["id"] = conferenceId; // conference ID is assigned automatically if 0
59             conference["title"] = conferenceElement.firstChildElement("title").text();
60             conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
61             conference["venue"] = conferenceElement.firstChildElement("venue").text();
62             conference["city"] = conferenceElement.firstChildElement("city").text();
63             conference["start"] = conferenceElement.firstChildElement("start").text(); // date
64             conference["end"] = conferenceElement.firstChildElement("end").text(); // date
65             conference["days"] = conferenceElement.firstChildElement("days").text(); // int
66             conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time
67             conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
68             conference["url"] = url;
69             SqlEngine::addConferenceToDB(conference, conferenceId);
70             conferenceId = conference["id"].toInt();
71             conference_title = conference["title"];
72         }
73
74         // we need to get count of all events in order to emit 'progressStatus' signal
75         int totalEventsCount = scheduleElement.elementsByTagName("event").count();
76
77         // parsing day elements
78         int currentEvent = 0; // hold global idx of processed event
79         QDomNodeList dayList = scheduleElement.elementsByTagName("day");
80         for (int i=0; i<dayList.count(); i++)
81         {
82             QDomElement dayElement = dayList.at(i).toElement();
83             //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT);
84             //int dayIndex = dayElement.attribute("index").toInt();
85
86             // parsing room elements
87             QDomNodeList roomList = dayElement.elementsByTagName("room");
88             for (int i=0; i<roomList.count(); i++)
89             {
90                 QDomElement roomElement = roomList.at(i).toElement();
91                 // roomElement has to be 'Element' and it has to have 'name' attribute
92                 // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute
93                 if(roomElement.hasAttribute("name"))
94                 {
95                     // parsing event elements
96                     QDomNodeList eventList = roomElement.elementsByTagName("event");
97                     for (int i=0; i<eventList.count(); i++)
98                     {
99                         currentEvent++;
100                         QDomElement eventElement = eventList.at(i).toElement();
101
102                         // now we have all info to create ROOM/EVENT_ROOM record(s)
103                         QHash<QString,QString> room;
104                         room["name"] = roomElement.attribute("name");
105                         room["event_id"] = eventElement.attribute("id");
106                         room["conference_id"] = QString::number(conferenceId,10);
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(conferenceId, 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                         QDomElement personsElement = eventElement.firstChildElement("persons");
128                         QDomNodeList personList = personsElement.elementsByTagName("person");
129                         for(int i = 0;i < personList.count();i++){
130                             QHash<QString,QString> person;
131                             person["id"] = personList.at(i).toElement().attribute("id");
132                             person["name"] = personList.at(i).toElement().text();
133                             person["event_id"] = eventElement.attribute("id");
134                             person["conference_id"] = QString::number(conferenceId, 10);
135                             SqlEngine::addPersonToDB(person);
136                         }
137                         // process links' nodes
138                         QDomElement linksElement = eventElement.firstChildElement("links");
139                         QDomNodeList linkList = linksElement.elementsByTagName("link");
140                         for(int i = 0;i < linkList.count();i++){
141                             QHash<QString,QString> link;
142                             link["name"] = linkList.at(i).toElement().text();
143                             link["url"] = linkList.at(i).toElement().attribute("href");
144                             link["event_id"] = eventElement.attribute("id");
145                             link["conference_id"] = QString::number(conferenceId, 10);
146                             SqlEngine::addLinkToDB(link);
147                         }
148                         // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
149                         int status = currentEvent * 100 / totalEventsCount;
150                         progressStatus(status);
151                     } // parsing event elements
152                 }
153             } // parsing room elements
154         } // parsing day elements
155     } // schedule element
156     SqlEngine::commitTransaction();
157     if (!conference_title.isNull()) {
158         emit parsingScheduleEnd(conference_title);
159     } else {
160         error_message("Could not parse schedule");
161     }
162 }
163