source: src/mvc/event.cpp @ 41c4ceb

qt5
Last change on this file since 41c4ceb was 41c4ceb, checked in by Philipp Spitzer <philipp@…>, 9 years ago

Now the dayChange time is taken into account. This fixes #43.

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