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