source: src/mvc/event.cpp @ 6df32f2

qt5
Last change on this file since 6df32f2 was 6df32f2, checked in by gregor herrmann <gregoa@…>, 10 years ago

Update GPL blurb in source files.

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[ca90cb1]1/*
2 * Copyright (C) 2010 Ixonos Plc.
3 *
[6df32f2]4 * This file is part of ConfClerk.
[ca90cb1]5 *
[6df32f2]6 * ConfClerk is free software: you can redistribute it and/or modify it
[ca90cb1]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 *
[6df32f2]11 * ConfClerk is distributed in the hope that it will be useful, but
[ca90cb1]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
[6df32f2]17 * ConfClerk.  If not, see <http://www.gnu.org/licenses/>.
[ca90cb1]18 */
[e5bc908]19#include "event.h"
[7620de0]20#include "room.h"
[e5bc908]21
[7d7659d]22QString const Event::sTableName = QString("event");
[c790268]23
[20a6010]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)
[4693fa6]29    << QSqlField("xid_track", QVariant::Int)
[20a6010]30    << QSqlField("type", QVariant::String)
[680a4da]31    << QSqlField("language", QVariant::String)
[c790268]32    << QSqlField("favourite", QVariant::Bool)
[b6cd05c]33    << QSqlField("alarm", QVariant::Bool)
[c790268]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));
[5a73d27]39
[a1755df]40Event::Event() :
[0d995ed]41    room_(NULL)
[a1755df]42{
43}
[20a6010]44
45Event Event::getById(int id, int conferenceId)
46{
[d0d0a66]47    QSqlQuery query;
[7d7659d]48    query.prepare(selectQuery() + "WHERE id = :id AND xid_conference = :conf");
[d0d0a66]49    query.bindValue(":id", id);
50    query.bindValue(":conf", conferenceId);
51    return loadOne(query);
52}
53
[64122f1]54QList<Event> Event::getByDate(const QDate& date, int conferenceId, QString orderBy)
[d0d0a66]55{
56    QSqlQuery query;
[7d7659d]57    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy));
[d0d0a66]58    query.bindValue(":conf", conferenceId);
59    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
60    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
[20a6010]61
[d0d0a66]62    return load(query);
[20a6010]63}
[680a4da]64
[7620de0]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
[b8a3ad1]79QList<Event> Event::nowEvents(int conferenceId, QString orderBy)
80{
[ac787b7]81    uint curTime_t =  QDateTime(QDate::currentDate(),QTime::currentTime(),Qt::UTC).toTime_t();
82    //uint curTime_t = 1265457610; // for testing
[b8a3ad1]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
[d49254d]93QList<Event> Event::conflictEvents(int aEventId, int conferenceId)
94{
95    QSqlQuery query;
[412cef6]96    Event event = Event::getById(aEventId,conferenceId);
97    query.prepare(selectQuery() + "WHERE xid_conference = :conf AND ( \
[eb21333]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");
[412cef6]101    query.bindValue(":conf", event.conferenceId());
[eb21333]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));
[412cef6]106    query.bindValue(":id", event.id());
[d49254d]107
108    return load(query);
109}
110
[6f39595]111QList<Event> Event::getFavByDate(const QDate& date, int conferenceId)
112{
113    QSqlQuery query;
[7d7659d]114    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite = 1 ORDER BY start"));
[6f39595]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
[0d995ed]122Room* Event::room()
[f9db452]123{
[0d995ed]124    if (room_ == NULL)
[a1755df]125    {
126        QSqlQuery query;
[5d9409d]127        query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id AND xid_conference = :conf");
[a1755df]128        query.bindValue(":id", id());
[5d9409d]129        query.bindValue(":conf", conferenceId());
[a1755df]130        if (!query.isActive())
131            if (!query.exec())
132                throw OrmSqlException(query.lastError().text());
133        if (!query.next())
134        {
135            qDebug() << "No room found for event id: " << id();
136            throw OrmNoObjectException();
137        }
[0d995ed]138        int id = query.record().value("xid_room").toInt();
139        room_ = new Room(Room::retrieve(id));
[a1755df]140    }
[0d995ed]141    return room_;
142}
143
144QString Event::roomName()
145{
146    return room()->name();
147}
148
149int Event::roomId()
150{
151    return room()->id();
[7620de0]152}
153
[78e3575]154QStringList Event::persons()
[395d6d3]155{
[a1755df]156    if( mPersonsList.isEmpty() )
[78e3575]157    {
158        QSqlQuery query;
159        // TODO: conference ID isn't used here
[5d9409d]160        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");
[78e3575]161        query.bindValue(":id", id());
[5d9409d]162        query.bindValue(":conf", conferenceId());
[78e3575]163        query.exec();
164        // TODO: handle qeury error
165        //qDebug() << query.lastError();
166
167        while(query.next())
[a1755df]168            mPersonsList.append(query.record().value("name").toString());
[78e3575]169    }
170
[a1755df]171    return mPersonsList;
[395d6d3]172}
173
[a1755df]174QMap<QString,QString> Event::links()
[6123b48]175{
[a1755df]176    if ( mLinksList.isEmpty() )
177    {
178        QSqlQuery query;
179        query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf");
180        query.bindValue(":id", id());
181        query.bindValue(":conf", conferenceId());
182        query.exec();
183        // TODO: handle qeury error
184        //qDebug() << query.lastError();
185
186        while(query.next())
187            mLinksList.insert(query.record().value("name").toString(), query.record().value("url").toString());
188    }
189    return mLinksList;
[6123b48]190}
191
[9f367eb]192bool Event::hasTimeConflict() const
193{
[412cef6]194    if(!isFavourite()) // if it's not favourite, it can't have time-conflict
195        return false;
[9f367eb]196
[412cef6]197    return conflictEvents(id(),conferenceId()).count() > 0 ? true : false;
[9f367eb]198}
199
[f9db452]200void Event::setRoom(const QString &room)
201{
[395d6d3]202    Q_UNUSED(room);
203
[f9db452]204    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
205    // TODO: implement
206}
[27102d5]207
[395d6d3]208void Event::setPersons(const QStringList &persons)
209{
210    Q_UNUSED(persons);
211
212    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
213    // TODO: implement
214}
215
[6123b48]216void Event::setLinks(const QMap<QString,QString> &aLinks)
217{
218    Q_UNUSED(aLinks);
219
220    qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET");
221    // TODO: implement
222}
223
[e662750]224QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy)
225{
[7d7659d]226    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
227    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
[bcf67d6]228    //qDebug() << strQuery;
[0d4ecc2]229    QList<Event> list;
[e662750]230    QSqlQuery query;
[0d4ecc2]231    try{
232        if( !query.prepare( strQuery ) ){
233            qDebug() << "QSqlQuery.prepare error";
234            throw OrmSqlException( query.lastError().text() );
235        }
236
237        query.bindValue(":conf", conferenceId);
238        query.bindValue(":start", convertToDb(date, QVariant::DateTime));
239        query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
240
241        list = load(query);
242    }
243    catch(OrmException &e)
244    {
245        qDebug() << "getSearchResultByDate error: " << e.text();
246    }
247    catch(...){
248        qDebug() << "getSearchResultByDate failed ...";
249    }
250    return list;
[e662750]251}
252
Note: See TracBrowser for help on using the repository browser.