source: src/mvc/event.cpp @ 83481c6

qt5
Last change on this file since 83481c6 was 83481c6, checked in by gregor herrmann <gregoa@…>, 9 years ago

bump copyright years

  • Property mode set to 100644
File size: 8.8 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 "event.h"
21#include "room.h"
22
23QString const Event::sTableName = QString("event");
24
25QSqlRecord 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
41Event::Event() :
42    room_(NULL)
43{
44}
45
46Event 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
55QList<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
64QList<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
79QList<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
97QList<Event> Event::getImminentAlarmEvents(int maxSecToAlarm, int conferenceId) {
98    QSqlQuery query;
99    query.prepare(selectQuery() + "WHERE xid_conference = :conf AND (start < :start AND alarm = 1) ORDER BY start, duration");
100    query.bindValue(":conf", conferenceId);
101    query.bindValue(":start", convertToDb(QDateTime::currentDateTime().addSecs(maxSecToAlarm), QVariant::DateTime));
102    return load(query);
103}
104
105
106QList<Event> Event::getFavByDate(const QDate& date, int conferenceId)
107{
108    QSqlQuery query;
109    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite = 1 ORDER BY start, duration"));
110    query.bindValue(":conf", conferenceId);
111    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
112    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
113
114    return load(query);
115}
116
117Room* Event::room()
118{
119    if (room_ == NULL)
120    {
121        QSqlQuery query;
122        query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id AND xid_conference = :conf");
123        query.bindValue(":id", id());
124        query.bindValue(":conf", conferenceId());
125        if (!query.isActive())
126            if (!query.exec())
127                throw OrmSqlException(query.lastError().text());
128        if (!query.next())
129        {
130            qDebug() << "No room found for event id: " << id();
131            throw OrmNoObjectException();
132        }
133        int id = query.record().value("xid_room").toInt();
134        room_ = new Room(Room::retrieve(id));
135    }
136    return room_;
137}
138
139QString Event::roomName()
140{
141    return room()->name();
142}
143
144int Event::roomId()
145{
146    return room()->id();
147}
148
149QStringList Event::persons()
150{
151    if( mPersonsList.isEmpty() )
152    {
153        QSqlQuery query;
154        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");
155        query.bindValue(":id", id());
156        query.bindValue(":conf1", conferenceId());
157        query.bindValue(":conf2", conferenceId());
158        if (!query.exec())  qDebug() << query.lastError();
159
160        while(query.next())
161            mPersonsList.append(query.record().value("name").toString());
162    }
163
164    return mPersonsList;
165}
166
167QMap<QString,QString> Event::links()
168{
169    if ( mLinksList.isEmpty() )
170    {
171        QSqlQuery query;
172        query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf");
173        query.bindValue(":id", id());
174        query.bindValue(":conf", conferenceId());
175        query.exec();
176        // TODO: handle query error
177        //qDebug() << query.lastError();
178
179        while(query.next())
180            mLinksList.insert(query.record().value("name").toString(), query.record().value("url").toString());
181    }
182    return mLinksList;
183}
184
185bool Event::hasTimeConflict() const
186{
187    if(!isFavourite()) // if it's not favourite, it can't have time-conflict
188        return false;
189
190    return conflictEvents(id(),conferenceId()).count() > 0 ? true : false;
191}
192
193void Event::setRoom(const QString &room)
194{
195    Q_UNUSED(room);
196
197    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
198    // TODO: implement
199}
200
201void Event::setPersons(const QStringList &persons)
202{
203    Q_UNUSED(persons);
204
205    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
206    // TODO: implement
207}
208
209void Event::setLinks(const QMap<QString,QString> &aLinks)
210{
211    Q_UNUSED(aLinks);
212
213    qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET");
214    // TODO: implement
215}
216
217QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) {
218    QList<Event> list;
219
220    // Check whether the temporary table SEARCH_EVENT exists (http://www.sqlite.org/faq.html#q7)
221    QSqlQuery query("SELECT count(*) FROM sqlite_temp_master WHERE type='table' and name='SEARCH_EVENT'");
222    if (!query.exec()) {
223        qDebug() << "SQL Error: " << query.lastError().text();
224        return list;
225    }
226    query.first();
227    QVariant v = query.value(0);
228    if (v.toInt() != 1) return list;
229
230    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
231    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
232    query = QSqlQuery();
233    try {
234        if( !query.prepare( strQuery ) ){
235            qDebug() << "QSqlQuery.prepare error";
236            throw OrmSqlException( query.lastError().text() );
237        }
238
239        query.bindValue(":conf", conferenceId);
240        query.bindValue(":start", convertToDb(date, QVariant::DateTime));
241        query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
242
243        list = load(query);
244    }
245    catch(OrmException &e)
246    {
247        qDebug() << "getSearchResultByDate error: " << e.text();
248    }
249    catch(...){
250        qDebug() << "getSearchResultByDate failed ...";
251    }
252    return list;
253}
254
Note: See TracBrowser for help on using the repository browser.