use transactions to make import faster
[toast/confclerk.git] / src / sql / schedulexmlparser.cpp
1 /*
2  * Copyright (C) 2010 Ixonos Plc.
3  *
4  * This file is part of fosdem-schedule.
5  *
6  * fosdem-schedule is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation, either version 2 of the License, or (at your option)
9  * any later version.
10  *
11  * fosdem-schedule is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * fosdem-schedule.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <QDomDocument>
21 #include <QHash>
22
23 #include "schedulexmlparser.h"
24 #include "sqlengine.h"
25
26 #include <QDebug>
27
28 ScheduleXmlParser::ScheduleXmlParser(QObject *aParent)
29     : QObject(aParent)
30 {
31 }
32
33 int ScheduleXmlParser::parseData(const QByteArray &aData)
34 {
35     QDomDocument document;
36     document.setContent (aData, false);
37
38     QDomElement scheduleElement = document.firstChildElement("schedule");
39
40     SqlEngine::beginTransaction();
41
42     int confId = 0;
43     if (!scheduleElement.isNull())
44     {
45         QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
46         if (!conferenceElement.isNull())
47         {
48             QHash<QString,QString> conference;
49             conference["id"] = QString::number(0); // conference ID is assigned automatically, or obtained from the DB
50             conference["title"] = conferenceElement.firstChildElement("title").text();
51             conference["subtitle"] = conferenceElement.firstChildElement("subtitle").text();
52             conference["venue"] = conferenceElement.firstChildElement("venue").text();
53             conference["city"] = conferenceElement.firstChildElement("city").text();
54             conference["start"] = conferenceElement.firstChildElement("start").text(); // date
55             conference["end"] = conferenceElement.firstChildElement("end").text(); // date
56             conference["days"] = conferenceElement.firstChildElement("days").text(); // int
57             conference["day_change"] = conferenceElement.firstChildElement("day_change").text(); // time
58             conference["timeslot_duration"] = conferenceElement.firstChildElement("timeslot_duration").text(); // time
59             SqlEngine::addConferenceToDB(conference);
60             confId = conference["id"].toInt();
61             emit(parsingSchedule(conference["title"]));
62         }
63
64         // we need to get count of all events in order to emit 'progressStatus' signal
65         int totalEventsCount = scheduleElement.elementsByTagName("event").count();
66
67         // parsing day elements
68         int currentEvent = 0; // hold global idx of processed event
69         QDomNodeList dayList = scheduleElement.elementsByTagName("day");
70         for (int i=0; i<dayList.count(); i++)
71         {
72             QDomElement dayElement = dayList.at(i).toElement();
73             //QDate dayDate = QDate::fromString(dayElement.attribute("date"),DATE_FORMAT);
74             //int dayIndex = dayElement.attribute("index").toInt();
75
76             // parsing room elements
77             QDomNodeList roomList = dayElement.elementsByTagName("room");
78             for (int i=0; i<roomList.count(); i++)
79             {
80                 QDomElement roomElement = roomList.at(i).toElement();
81                 // roomElement has to be 'Element' and it has to have 'name' attribute
82                 // TODO: 'event' has also 'room' node, so it can be unstable if that node has also 'name' attribute
83                 if(roomElement.hasAttribute("name"))
84                 {
85                     // parsing event elements
86                     QDomNodeList eventList = roomElement.elementsByTagName("event");
87                     for (int i=0; i<eventList.count(); i++)
88                     {
89                         currentEvent++;
90                         QDomElement eventElement = eventList.at(i).toElement();
91
92                         // now we have all info to create ROOM/EVENT_ROOM record(s)
93                         QHash<QString,QString> room;
94                         room["name"] = roomElement.attribute("name");
95                         room["event_id"] = eventElement.attribute("id");
96                         room["conference_id"] = QString::number(confId,10);
97                         room["picture"] = "NOT DEFINED YET"; // TODO: implement some mapping to assign correct picture to specified room_name
98                         SqlEngine::addRoomToDB(room);
99
100                         // process event's nodes
101                         QHash<QString,QString> event;
102                         event["id"] = eventElement.attribute("id");;
103                         event["conference_id"] = QString::number(confId, 10);
104                         event["start"] = eventElement.firstChildElement("start").text(); // time eg. 10:00
105                         event["date"] = dayElement.attribute("date"); // date eg. 2009-02-07
106                         event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
107                         event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson"
108                         event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome"
109                         event["title"] = eventElement.firstChildElement("title").text(); // string eg. "Welcome"
110                         event["subtitle"] = eventElement.firstChildElement("subtitle").text(); // string
111                         event["track"] = eventElement.firstChildElement("track").text(); // string eg. "Keynotes"
112                         event["type"] = eventElement.firstChildElement("type").text(); // string eg. "Podium"
113                         event["language"] = eventElement.firstChildElement("language").text(); // language eg. "English"
114                         event["abstract"] = eventElement.firstChildElement("abstract").text(); // string
115                         event["description"] = eventElement.firstChildElement("description").text(); // string
116                         SqlEngine::addEventToDB(event);
117                         // process persons' nodes
118                         QList<QString> persons;
119                         QDomElement personsElement = eventElement.firstChildElement("persons");
120                         QDomNodeList personList = personsElement.elementsByTagName("person");
121                         for(int i = 0;i < personList.count();i++){
122                             QHash<QString,QString> person;
123                             person["id"] = personList.at(i).toElement().attribute("id");
124                             person["name"] = personList.at(i).toElement().text();
125                             person["event_id"] = eventElement.attribute("id");
126                             person["conference_id"] = QString::number(confId, 10);
127                             //qDebug() << "adding Person: " << person["name"];
128                             SqlEngine::addPersonToDB(person);
129                         }
130                         // process links' nodes
131                         QDomElement linksElement = eventElement.firstChildElement("links");
132                         QDomNodeList linkList = linksElement.elementsByTagName("link");
133                         for(int i = 0;i < linkList.count();i++){
134                             QHash<QString,QString> link;
135                             link["name"] = linkList.at(i).toElement().text();
136                             link["url"] = linkList.at(i).toElement().attribute("href");
137                             link["event_id"] = eventElement.attribute("id");
138                             link["conference_id"] = QString::number(confId, 10);
139                             SqlEngine::addLinkToDB(link);
140                         }
141                         // emit signal to inform the user about the current status (how many events are parsed so far - expressed in %)
142                         int status = currentEvent * 100 / totalEventsCount;
143                         progressStatus(status);
144                     } // parsing event elements
145                 }
146             } // parsing room elements
147         } // parsing day elements
148     } // schedule element
149     SqlEngine::commitTransaction();
150
151     return confId;
152 }
153