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