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 | #include "event.h" |
---|
20 | #include "room.h" |
---|
21 | |
---|
22 | QString const Event::sTableName = QString("event"); |
---|
23 | |
---|
24 | QSqlRecord const Event::sColumns = Event::toRecord(QList<QSqlField>() |
---|
25 | << QSqlField("id", QVariant::Int) |
---|
26 | << QSqlField("xid_conference", QVariant::Int) |
---|
27 | << QSqlField("start", QVariant::DateTime) |
---|
28 | << QSqlField("duration", QVariant::Int) |
---|
29 | << QSqlField("xid_track", QVariant::Int) |
---|
30 | << QSqlField("type", QVariant::String) |
---|
31 | << QSqlField("language", QVariant::String) |
---|
32 | << QSqlField("favourite", QVariant::Bool) |
---|
33 | << QSqlField("alarm", QVariant::Bool) |
---|
34 | << QSqlField("tag", QVariant::String) |
---|
35 | << QSqlField("title", QVariant::String) |
---|
36 | << QSqlField("subtitle", QVariant::String) |
---|
37 | << QSqlField("abstract", QVariant::String) |
---|
38 | << QSqlField("description", QVariant::String)); |
---|
39 | |
---|
40 | |
---|
41 | Event Event::getById(int id, int conferenceId) |
---|
42 | { |
---|
43 | QSqlQuery query; |
---|
44 | query.prepare(selectQuery() + "WHERE id = :id AND xid_conference = :conf"); |
---|
45 | query.bindValue(":id", id); |
---|
46 | query.bindValue(":conf", conferenceId); |
---|
47 | return loadOne(query); |
---|
48 | } |
---|
49 | |
---|
50 | QList<Event> Event::getByDate(const QDate& date, int conferenceId, QString orderBy) |
---|
51 | { |
---|
52 | QSqlQuery query; |
---|
53 | query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy)); |
---|
54 | query.bindValue(":conf", conferenceId); |
---|
55 | query.bindValue(":start", convertToDb(date, QVariant::DateTime)); |
---|
56 | query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); |
---|
57 | |
---|
58 | return load(query); |
---|
59 | } |
---|
60 | |
---|
61 | QList<Event> Event::getByDateAndRoom(const QDate& date, int conferenceId) |
---|
62 | { |
---|
63 | QSqlQuery query; |
---|
64 | QString aliasEvent("E"); |
---|
65 | QString aliasEventRoom("R"); |
---|
66 | query.prepare(QString("SELECT %1 FROM %2 %3, %4 %5 WHERE %3.xid_conference = :conf AND %3.start >= :start AND %3.start < :end AND %3.id = R.xid_event ORDER BY %5.xid_room, %3.start").arg( |
---|
67 | columnsForSelect(aliasEvent), Event::sTableName, aliasEvent, "EVENT_ROOM", aliasEventRoom)); |
---|
68 | query.bindValue(":conf", conferenceId); |
---|
69 | query.bindValue(":start", convertToDb(date, QVariant::DateTime)); |
---|
70 | query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); |
---|
71 | |
---|
72 | return load(query); |
---|
73 | } |
---|
74 | |
---|
75 | QList<Event> Event::nowEvents(int conferenceId, QString orderBy) |
---|
76 | { |
---|
77 | uint curTime_t = QDateTime(QDate::currentDate(),QTime::currentTime(),Qt::UTC).toTime_t(); |
---|
78 | //uint curTime_t = 1265457610; // for testing |
---|
79 | |
---|
80 | QSqlQuery query; |
---|
81 | query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start <= :now1 AND ( start + duration ) > :now2 ORDER BY %1").arg(orderBy)); |
---|
82 | query.bindValue(":conf", conferenceId); |
---|
83 | query.bindValue(":now1", convertToDb(curTime_t, QVariant::DateTime)); |
---|
84 | query.bindValue(":now2", convertToDb(curTime_t, QVariant::DateTime)); |
---|
85 | |
---|
86 | return load(query); |
---|
87 | } |
---|
88 | |
---|
89 | QList<Event> Event::conflictEvents(int aEventId, int conferenceId) |
---|
90 | { |
---|
91 | QSqlQuery query; |
---|
92 | Event event = Event::getById(aEventId,conferenceId); |
---|
93 | query.prepare(selectQuery() + "WHERE xid_conference = :conf AND ( \ |
---|
94 | ( start >= :s1 AND ( start + duration ) < :e1 ) \ |
---|
95 | OR ( ( start + duration ) > :s2 AND start < :e2 ) ) \ |
---|
96 | AND favourite = 1 AND NOT id = :id ORDER BY start"); |
---|
97 | query.bindValue(":conf", event.conferenceId()); |
---|
98 | query.bindValue(":s1", convertToDb(event.start(), QVariant::DateTime)); |
---|
99 | query.bindValue(":e1", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime)); |
---|
100 | query.bindValue(":s2", convertToDb(event.start(), QVariant::DateTime)); |
---|
101 | query.bindValue(":e2", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime)); |
---|
102 | query.bindValue(":id", event.id()); |
---|
103 | |
---|
104 | return load(query); |
---|
105 | } |
---|
106 | |
---|
107 | QList<Event> Event::getFavByDate(const QDate& date, int conferenceId) |
---|
108 | { |
---|
109 | QSqlQuery query; |
---|
110 | query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite = 1 ORDER BY start")); |
---|
111 | query.bindValue(":conf", conferenceId); |
---|
112 | query.bindValue(":start", convertToDb(date, QVariant::DateTime)); |
---|
113 | query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); |
---|
114 | |
---|
115 | return load(query); |
---|
116 | } |
---|
117 | |
---|
118 | QString Event::room() const |
---|
119 | { |
---|
120 | QSqlQuery query; |
---|
121 | // TODO: conference ID isn't used here |
---|
122 | query.prepare("SELECT name FROM room WHERE id = (SELECT xid_room FROM event_room WHERE xid_event = :id)"); |
---|
123 | query.bindValue(":id", id()); |
---|
124 | query.exec(); |
---|
125 | // TODO: handle qeury error |
---|
126 | //qDebug() << query.lastError(); |
---|
127 | if(query.next()) |
---|
128 | return query.record().value("name").toString(); |
---|
129 | else |
---|
130 | return QString("not-available"); |
---|
131 | } |
---|
132 | |
---|
133 | int Event::roomId() const |
---|
134 | { |
---|
135 | QSqlQuery query; |
---|
136 | query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id"); |
---|
137 | query.bindValue(":id", id()); |
---|
138 | if (!query.isActive()) |
---|
139 | if (!query.exec()) |
---|
140 | throw OrmSqlException(query.lastError().text()); |
---|
141 | if (!query.next()) |
---|
142 | throw OrmNoObjectException(); |
---|
143 | return query.record().value("xid_room").toInt(); |
---|
144 | } |
---|
145 | |
---|
146 | QStringList Event::persons() const |
---|
147 | { |
---|
148 | QSqlQuery query; |
---|
149 | // TODO: conference ID isn't used here |
---|
150 | query.prepare("SELECT person.name FROM person INNER JOIN event_person ON person.id = event_person.xid_person AND event_person.xid_event = :id"); |
---|
151 | query.bindValue(":id", id()); |
---|
152 | query.exec(); |
---|
153 | // TODO: handle qeury error |
---|
154 | //qDebug() << query.lastError(); |
---|
155 | |
---|
156 | QStringList persons; |
---|
157 | while(query.next()) |
---|
158 | persons.append(query.record().value("name").toString()); |
---|
159 | |
---|
160 | return persons; |
---|
161 | } |
---|
162 | |
---|
163 | QMap<QString,QString> Event::links() const |
---|
164 | { |
---|
165 | QSqlQuery query; |
---|
166 | query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf"); |
---|
167 | query.bindValue(":id", id()); |
---|
168 | query.bindValue(":conf", conferenceId()); |
---|
169 | query.exec(); |
---|
170 | // TODO: handle qeury error |
---|
171 | //qDebug() << query.lastError(); |
---|
172 | |
---|
173 | QMap<QString,QString> links; |
---|
174 | while(query.next()) |
---|
175 | links.insert(query.record().value("name").toString(), query.record().value("url").toString()); |
---|
176 | |
---|
177 | return links; |
---|
178 | } |
---|
179 | |
---|
180 | bool Event::hasTimeConflict() const |
---|
181 | { |
---|
182 | if(!isFavourite()) // if it's not favourite, it can't have time-conflict |
---|
183 | return false; |
---|
184 | |
---|
185 | return conflictEvents(id(),conferenceId()).count() > 0 ? true : false; |
---|
186 | } |
---|
187 | |
---|
188 | void Event::setRoom(const QString &room) |
---|
189 | { |
---|
190 | Q_UNUSED(room); |
---|
191 | |
---|
192 | qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET"); |
---|
193 | // TODO: implement |
---|
194 | } |
---|
195 | |
---|
196 | void Event::setPersons(const QStringList &persons) |
---|
197 | { |
---|
198 | Q_UNUSED(persons); |
---|
199 | |
---|
200 | qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET"); |
---|
201 | // TODO: implement |
---|
202 | } |
---|
203 | |
---|
204 | void Event::setLinks(const QMap<QString,QString> &aLinks) |
---|
205 | { |
---|
206 | Q_UNUSED(aLinks); |
---|
207 | |
---|
208 | qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET"); |
---|
209 | // TODO: implement |
---|
210 | } |
---|
211 | |
---|
212 | QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) |
---|
213 | { |
---|
214 | QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect()); |
---|
215 | strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy); |
---|
216 | //qDebug() << strQuery; |
---|
217 | QList<Event> list; |
---|
218 | QSqlQuery query; |
---|
219 | try{ |
---|
220 | if( !query.prepare( strQuery ) ){ |
---|
221 | qDebug() << "QSqlQuery.prepare error"; |
---|
222 | throw OrmSqlException( query.lastError().text() ); |
---|
223 | } |
---|
224 | |
---|
225 | query.bindValue(":conf", conferenceId); |
---|
226 | query.bindValue(":start", convertToDb(date, QVariant::DateTime)); |
---|
227 | query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); |
---|
228 | |
---|
229 | list = load(query); |
---|
230 | } |
---|
231 | catch(OrmException &e) |
---|
232 | { |
---|
233 | qDebug() << "getSearchResultByDate error: " << e.text(); |
---|
234 | } |
---|
235 | catch(...){ |
---|
236 | qDebug() << "getSearchResultByDate failed ..."; |
---|
237 | } |
---|
238 | return list; |
---|
239 | } |
---|
240 | |
---|