source: src/mvc/event.cpp @ 5d9409d

qt5
Last change on this file since 5d9409d was 5d9409d, checked in by timkoma <timkoma@…>, 12 years ago

UTC/LocalTime fix for import conference XML, DB queries for multiple conferences fixes

  • Property mode set to 100644
File size: 8.9 KB
Line 
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
22QString const Event::sTableName = QString("event");
23
24QSqlRecord 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
40Event::Event() :
41    mRoomId( 0 )
42{
43}
44
45Event Event::getById(int id, int conferenceId)
46{
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
54QList<Event> Event::getByDate(const QDate& date, int conferenceId, QString orderBy)
55{
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
62    return load(query);
63}
64
65QList<Event> Event::getByDateAndRoom(const QDate& date, int conferenceId)
66{
67    QSqlQuery query;
68    QString aliasEvent("E");
69    QString aliasEventRoom("R");
70    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(
71                    columnsForSelect(aliasEvent), Event::sTableName, aliasEvent, "EVENT_ROOM", aliasEventRoom));
72    query.bindValue(":conf", conferenceId);
73    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
74    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
75
76    return load(query);
77}
78
79QList<Event> Event::nowEvents(int conferenceId, QString orderBy)
80{
81    uint curTime_t =  QDateTime(QDate::currentDate(),QTime::currentTime(),Qt::UTC).toTime_t();
82    //uint curTime_t = 1265457610; // for testing
83
84    QSqlQuery query;
85    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start <= :now1 AND ( start + duration ) > :now2 ORDER BY %1").arg(orderBy));
86    query.bindValue(":conf", conferenceId);
87    query.bindValue(":now1", convertToDb(curTime_t, QVariant::DateTime));
88    query.bindValue(":now2", convertToDb(curTime_t, QVariant::DateTime));
89
90    return load(query);
91}
92
93QList<Event> Event::conflictEvents(int aEventId, int conferenceId)
94{
95    QSqlQuery query;
96    Event event = Event::getById(aEventId,conferenceId);
97    query.prepare(selectQuery() + "WHERE xid_conference = :conf AND ( \
98           ( start >= :s1 AND ( start + duration ) < :e1 ) \
99        OR ( ( start + duration ) > :s2 AND start < :e2 ) ) \
100        AND favourite = 1 AND NOT id = :id ORDER BY start");
101    query.bindValue(":conf", event.conferenceId());
102    query.bindValue(":s1", convertToDb(event.start(), QVariant::DateTime));
103    query.bindValue(":e1", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime));
104    query.bindValue(":s2", convertToDb(event.start(), QVariant::DateTime));
105    query.bindValue(":e2", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime));
106    query.bindValue(":id", event.id());
107
108    return load(query);
109}
110
111QList<Event> Event::getFavByDate(const QDate& date, int conferenceId)
112{
113    QSqlQuery query;
114    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite = 1 ORDER BY start"));
115    query.bindValue(":conf", conferenceId);
116    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
117    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
118
119    return load(query);
120}
121
122QString Event::room()
123{
124    if ( mRoomName.isEmpty() )
125    {
126        QSqlQuery query;
127        // TODO: conference ID isn't used here
128        query.prepare("SELECT name FROM room WHERE id = :roomId");
129        query.bindValue(":roomId", roomId());
130        query.exec();
131        // TODO: handle qeury error
132        //qDebug() << query.lastError();
133        if(query.next())
134            mRoomName = query.record().value("name").toString();
135        else
136            mRoomName = QString("not-available");
137    }
138    return mRoomName;
139}
140
141int Event::roomId()
142{
143    if ( mRoomId == 0 )
144    {
145        QSqlQuery query;
146        query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id AND xid_conference = :conf");
147        query.bindValue(":id", id());
148        query.bindValue(":conf", conferenceId());
149        if (!query.isActive())
150            if (!query.exec())
151                throw OrmSqlException(query.lastError().text());
152        if (!query.next())
153        {
154            qDebug() << "No room found for event id: " << id();
155            throw OrmNoObjectException();
156        }
157        mRoomId = query.record().value("xid_room").toInt();
158    }
159    return mRoomId;
160}
161
162QStringList Event::persons()
163{
164    if( mPersonsList.isEmpty() )
165    {
166        QSqlQuery query;
167        // TODO: conference ID isn't used here
168        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 = :conf");
169        query.bindValue(":id", id());
170        query.bindValue(":conf", conferenceId());
171        query.exec();
172        // TODO: handle qeury error
173        //qDebug() << query.lastError();
174
175        while(query.next())
176            mPersonsList.append(query.record().value("name").toString());
177    }
178
179    return mPersonsList;
180}
181
182QMap<QString,QString> Event::links()
183{
184    if ( mLinksList.isEmpty() )
185    {
186        QSqlQuery query;
187        query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf");
188        query.bindValue(":id", id());
189        query.bindValue(":conf", conferenceId());
190        query.exec();
191        // TODO: handle qeury error
192        //qDebug() << query.lastError();
193
194        while(query.next())
195            mLinksList.insert(query.record().value("name").toString(), query.record().value("url").toString());
196    }
197    return mLinksList;
198}
199
200bool Event::hasTimeConflict() const
201{
202    if(!isFavourite()) // if it's not favourite, it can't have time-conflict
203        return false;
204
205    return conflictEvents(id(),conferenceId()).count() > 0 ? true : false;
206}
207
208void Event::setRoom(const QString &room)
209{
210    Q_UNUSED(room);
211
212    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
213    // TODO: implement
214}
215
216void Event::setPersons(const QStringList &persons)
217{
218    Q_UNUSED(persons);
219
220    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
221    // TODO: implement
222}
223
224void Event::setLinks(const QMap<QString,QString> &aLinks)
225{
226    Q_UNUSED(aLinks);
227
228    qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET");
229    // TODO: implement
230}
231
232QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy)
233{
234    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
235    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
236    //qDebug() << strQuery;
237    QList<Event> list;
238    QSqlQuery query;
239    try{
240        if( !query.prepare( strQuery ) ){
241            qDebug() << "QSqlQuery.prepare error";
242            throw OrmSqlException( query.lastError().text() );
243        }
244
245        query.bindValue(":conf", conferenceId);
246        query.bindValue(":start", convertToDb(date, QVariant::DateTime));
247        query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
248
249        list = load(query);
250    }
251    catch(OrmException &e)
252    {
253        qDebug() << "getSearchResultByDate error: " << e.text();
254    }
255    catch(...){
256        qDebug() << "getSearchResultByDate failed ...";
257    }
258    return list;
259}
260
Note: See TracBrowser for help on using the repository browser.