1 | #include "event.h" |
---|
2 | #include "room.h" |
---|
3 | |
---|
4 | QString const Event::sTableName = QString("event"); |
---|
5 | |
---|
6 | QSqlRecord const Event::sColumns = Event::toRecord(QList<QSqlField>() |
---|
7 | << QSqlField("id", QVariant::Int) |
---|
8 | << QSqlField("xid_conference", QVariant::Int) |
---|
9 | << QSqlField("start", QVariant::DateTime) |
---|
10 | << QSqlField("duration", QVariant::Int) |
---|
11 | << QSqlField("xid_track", QVariant::Int) |
---|
12 | << QSqlField("type", QVariant::String) |
---|
13 | << QSqlField("language", QVariant::String) |
---|
14 | << QSqlField("favourite", QVariant::Bool) |
---|
15 | << QSqlField("alarm", QVariant::Bool) |
---|
16 | << QSqlField("tag", QVariant::String) |
---|
17 | << QSqlField("title", QVariant::String) |
---|
18 | << QSqlField("subtitle", QVariant::String) |
---|
19 | << QSqlField("abstract", QVariant::String) |
---|
20 | << QSqlField("description", QVariant::String)); |
---|
21 | |
---|
22 | |
---|
23 | Event Event::getById(int id, int conferenceId) |
---|
24 | { |
---|
25 | QSqlQuery query; |
---|
26 | query.prepare(selectQuery() + "WHERE id = :id AND xid_conference = :conf"); |
---|
27 | query.bindValue(":id", id); |
---|
28 | query.bindValue(":conf", conferenceId); |
---|
29 | return loadOne(query); |
---|
30 | } |
---|
31 | |
---|
32 | QList<Event> Event::getByDate(const QDate& date, int conferenceId, QString orderBy) |
---|
33 | { |
---|
34 | QSqlQuery query; |
---|
35 | query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy)); |
---|
36 | query.bindValue(":conf", conferenceId); |
---|
37 | query.bindValue(":start", convertToDb(date, QVariant::DateTime)); |
---|
38 | query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); |
---|
39 | |
---|
40 | return load(query); |
---|
41 | } |
---|
42 | |
---|
43 | QList<Event> Event::getByDateAndRoom(const QDate& date, int conferenceId) |
---|
44 | { |
---|
45 | QSqlQuery query; |
---|
46 | QString aliasEvent("E"); |
---|
47 | QString aliasEventRoom("R"); |
---|
48 | 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( |
---|
49 | columnsForSelect(aliasEvent), Event::sTableName, aliasEvent, "EVENT_ROOM", aliasEventRoom)); |
---|
50 | query.bindValue(":conf", conferenceId); |
---|
51 | query.bindValue(":start", convertToDb(date, QVariant::DateTime)); |
---|
52 | query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); |
---|
53 | |
---|
54 | return load(query); |
---|
55 | } |
---|
56 | |
---|
57 | QList<Event> Event::nowEvents(int conferenceId, QString orderBy) |
---|
58 | { |
---|
59 | //uint curTime_t = QDateTime(QDate::currentDate(),QTime::currentTime(),Qt::UTC).toTime_t(); |
---|
60 | uint curTime_t = 1265457610; // for testing |
---|
61 | |
---|
62 | QSqlQuery query; |
---|
63 | query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start <= :now1 AND ( start + duration ) > :now2 ORDER BY %1").arg(orderBy)); |
---|
64 | query.bindValue(":conf", conferenceId); |
---|
65 | query.bindValue(":now1", convertToDb(curTime_t, QVariant::DateTime)); |
---|
66 | query.bindValue(":now2", convertToDb(curTime_t, QVariant::DateTime)); |
---|
67 | |
---|
68 | return load(query); |
---|
69 | } |
---|
70 | |
---|
71 | QList<Event> Event::conflictEvents(int aEventId, int conferenceId) |
---|
72 | { |
---|
73 | QSqlQuery query; |
---|
74 | query.prepare( selectQuery() + QString("WHERE id IN ( SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf ) ORDER BY %1").arg("start")); |
---|
75 | query.bindValue(":id", aEventId); |
---|
76 | query.bindValue(":conf", conferenceId); |
---|
77 | |
---|
78 | return load(query); |
---|
79 | } |
---|
80 | |
---|
81 | QList<Event> Event::getFavByDate(const QDate& date, int conferenceId) |
---|
82 | { |
---|
83 | QSqlQuery query; |
---|
84 | query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite = 1 ORDER BY start")); |
---|
85 | query.bindValue(":conf", conferenceId); |
---|
86 | query.bindValue(":start", convertToDb(date, QVariant::DateTime)); |
---|
87 | query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); |
---|
88 | |
---|
89 | return load(query); |
---|
90 | } |
---|
91 | |
---|
92 | QString Event::room() const |
---|
93 | { |
---|
94 | QSqlQuery query; |
---|
95 | // TODO: conference ID isn't used here |
---|
96 | query.prepare("SELECT name FROM room WHERE id = (SELECT xid_room FROM event_room WHERE xid_event = :id)"); |
---|
97 | query.bindValue(":id", id()); |
---|
98 | query.exec(); |
---|
99 | // TODO: handle qeury error |
---|
100 | //qDebug() << query.lastError(); |
---|
101 | if(query.next()) |
---|
102 | return query.record().value("name").toString(); |
---|
103 | else |
---|
104 | return QString("not-available"); |
---|
105 | } |
---|
106 | |
---|
107 | int Event::roomId() const |
---|
108 | { |
---|
109 | QSqlQuery query; |
---|
110 | query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id"); |
---|
111 | query.bindValue(":id", id()); |
---|
112 | if (!query.isActive()) |
---|
113 | if (!query.exec()) |
---|
114 | throw OrmSqlException(query.lastError().text()); |
---|
115 | if (!query.next()) |
---|
116 | throw OrmNoObjectException(); |
---|
117 | return query.record().value("xid_room").toInt(); |
---|
118 | } |
---|
119 | |
---|
120 | QStringList Event::persons() const |
---|
121 | { |
---|
122 | QSqlQuery query; |
---|
123 | // TODO: conference ID isn't used here |
---|
124 | query.prepare("SELECT person.name FROM person INNER JOIN event_person ON person.id = event_person.xid_person AND event_person.xid_event = :id"); |
---|
125 | query.bindValue(":id", id()); |
---|
126 | query.exec(); |
---|
127 | // TODO: handle qeury error |
---|
128 | //qDebug() << query.lastError(); |
---|
129 | |
---|
130 | QStringList persons; |
---|
131 | while(query.next()) |
---|
132 | persons.append(query.record().value("name").toString()); |
---|
133 | |
---|
134 | return persons; |
---|
135 | } |
---|
136 | |
---|
137 | QMap<QString,QString> Event::links() const |
---|
138 | { |
---|
139 | QSqlQuery query; |
---|
140 | query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf"); |
---|
141 | query.bindValue(":id", id()); |
---|
142 | query.bindValue(":conf", conferenceId()); |
---|
143 | query.exec(); |
---|
144 | // TODO: handle qeury error |
---|
145 | //qDebug() << query.lastError(); |
---|
146 | |
---|
147 | QMap<QString,QString> links; |
---|
148 | while(query.next()) |
---|
149 | links.insert(query.record().value("name").toString(), query.record().value("url").toString()); |
---|
150 | |
---|
151 | return links; |
---|
152 | } |
---|
153 | |
---|
154 | QList<int> Event::conflicts() const |
---|
155 | { |
---|
156 | QSqlQuery query; |
---|
157 | // TODO: conference ID isn't used here |
---|
158 | query.prepare("SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf"); |
---|
159 | query.bindValue(":id", id()); |
---|
160 | query.bindValue(":conf", conferenceId()); |
---|
161 | query.exec(); |
---|
162 | // TODO: handle qeury error |
---|
163 | //qDebug() << query.lastError(); |
---|
164 | |
---|
165 | QList<int> conflicts; |
---|
166 | while(query.next()) |
---|
167 | conflicts.append(query.record().value("conflict_event").toInt()); |
---|
168 | |
---|
169 | return conflicts; |
---|
170 | } |
---|
171 | |
---|
172 | bool Event::hasTimeConflict() const |
---|
173 | { |
---|
174 | return conflicts().count() > 0 ? true : false; |
---|
175 | } |
---|
176 | |
---|
177 | void Event::updateConflicts() |
---|
178 | { |
---|
179 | qDebug() << "updating conflicts"; |
---|
180 | QSqlQuery query; |
---|
181 | query.prepare("SELECT id FROM event WHERE xid_conference = :conf AND ( \ |
---|
182 | ( start <= :start1 AND ( start + duration ) >= :start2 ) \ |
---|
183 | OR ( start >= :start3 AND ( start + duration ) <= :end1 ) \ |
---|
184 | OR ( start <= :end2 AND ( start + duration ) >= :end3 ) ) AND favourite = 1 ORDER BY start"); |
---|
185 | query.bindValue(":conf", conferenceId()); |
---|
186 | query.bindValue(":start1", convertToDb(start(), QVariant::DateTime)); |
---|
187 | query.bindValue(":start2", convertToDb(start(), QVariant::DateTime)); |
---|
188 | query.bindValue(":start3", convertToDb(start(), QVariant::DateTime)); |
---|
189 | query.bindValue(":end1", convertToDb(start().toTime_t()+duration(), QVariant::DateTime)); |
---|
190 | query.bindValue(":end2", convertToDb(start().toTime_t()+duration(), QVariant::DateTime)); |
---|
191 | query.bindValue(":end3", convertToDb(start().toTime_t()+duration(), QVariant::DateTime)); |
---|
192 | query.exec(); |
---|
193 | |
---|
194 | QList<int> conflicts; |
---|
195 | while(query.next()) |
---|
196 | { |
---|
197 | int idx = query.record().value("id").toInt(); |
---|
198 | if(idx != id()) |
---|
199 | conflicts.append(idx); |
---|
200 | } |
---|
201 | |
---|
202 | if(isFavourite()) // event became favourite |
---|
203 | { |
---|
204 | for(int i=0; i<conflicts.count(); i++) |
---|
205 | { |
---|
206 | QSqlQuery query; |
---|
207 | query.prepare("INSERT INTO event_conflict (xid_conference, xid_event, conflict_event) VALUES ( ? , ? , ? )"); |
---|
208 | query.bindValue(0, conferenceId()); |
---|
209 | query.bindValue(1, id()); |
---|
210 | query.bindValue(2, conflicts[i]); |
---|
211 | query.exec(); |
---|
212 | |
---|
213 | QSqlQuery query2; |
---|
214 | query2.prepare("INSERT INTO event_conflict (xid_conference, xid_event, conflict_event) VALUES ( ? , ? , ? )"); |
---|
215 | query2.bindValue(0, conferenceId()); |
---|
216 | query2.bindValue(1, conflicts[i]); |
---|
217 | query2.bindValue(2, id()); |
---|
218 | query2.exec(); |
---|
219 | } |
---|
220 | } |
---|
221 | else // event removed from favourities |
---|
222 | { |
---|
223 | qDebug() << "removing"; |
---|
224 | |
---|
225 | QSqlQuery queryRemove; |
---|
226 | queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf"); |
---|
227 | queryRemove.bindValue(":id",id()); |
---|
228 | queryRemove.bindValue(":conf",conferenceId()); |
---|
229 | queryRemove.exec(); |
---|
230 | |
---|
231 | for(int i=0; i<conflicts.count(); i++) |
---|
232 | { |
---|
233 | qDebug() << "removing: " << id() << " -> " << conflicts[i]; |
---|
234 | |
---|
235 | QSqlQuery queryRemove; |
---|
236 | queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id1 AND xid_conference = :conf AND conflict_event = :id2"); |
---|
237 | queryRemove.bindValue(":id1",conflicts[i]); |
---|
238 | queryRemove.bindValue(":conf",conferenceId()); |
---|
239 | queryRemove.bindValue(":id2",id()); |
---|
240 | queryRemove.exec(); |
---|
241 | } |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | void Event::setRoom(const QString &room) |
---|
246 | { |
---|
247 | Q_UNUSED(room); |
---|
248 | |
---|
249 | qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET"); |
---|
250 | // TODO: implement |
---|
251 | } |
---|
252 | |
---|
253 | void Event::setPersons(const QStringList &persons) |
---|
254 | { |
---|
255 | Q_UNUSED(persons); |
---|
256 | |
---|
257 | qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET"); |
---|
258 | // TODO: implement |
---|
259 | } |
---|
260 | |
---|
261 | void Event::setLinks(const QMap<QString,QString> &aLinks) |
---|
262 | { |
---|
263 | Q_UNUSED(aLinks); |
---|
264 | |
---|
265 | qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET"); |
---|
266 | // TODO: implement |
---|
267 | } |
---|
268 | |
---|
269 | QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) |
---|
270 | { |
---|
271 | |
---|
272 | QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect()); |
---|
273 | strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy); |
---|
274 | qDebug() << strQuery; |
---|
275 | QList<Event> list; |
---|
276 | QSqlQuery query; |
---|
277 | try{ |
---|
278 | if( !query.prepare( strQuery ) ){ |
---|
279 | qDebug() << "QSqlQuery.prepare error"; |
---|
280 | throw OrmSqlException( query.lastError().text() ); |
---|
281 | } |
---|
282 | |
---|
283 | query.bindValue(":conf", conferenceId); |
---|
284 | query.bindValue(":start", convertToDb(date, QVariant::DateTime)); |
---|
285 | query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); |
---|
286 | |
---|
287 | list = load(query); |
---|
288 | } |
---|
289 | catch(OrmException &e) |
---|
290 | { |
---|
291 | qDebug() << "getSearchResultByDate error: " << e.text(); |
---|
292 | } |
---|
293 | catch(...){ |
---|
294 | qDebug() << "getSearchResultByDate failed ..."; |
---|
295 | } |
---|
296 | return list; |
---|
297 | } |
---|
298 | |
---|