source: src/mvc/event.cpp @ d49254d

qt5
Last change on this file since d49254d was d49254d, checked in by pavelpa <pavelpa@…>, 12 years ago

'conflict' dialog now contains list of events in conflict with given eventId

  • Property mode set to 100644
File size: 10.2 KB
RevLine 
[e5bc908]1#include "event.h"
[7620de0]2#include "room.h"
[e5bc908]3
[7d7659d]4QString const Event::sTableName = QString("event");
[c790268]5
[20a6010]6QSqlRecord const Event::sColumns = Event::toRecord(QList<QSqlField>()
7    << QSqlField("id", QVariant::Int)
8    << QSqlField("xid_conference", QVariant::Int)
9    << QSqlField("start", QVariant::DateTime)
10    << QSqlField("duration", QVariant::Int)
[4693fa6]11    << QSqlField("xid_track", QVariant::Int)
[20a6010]12    << QSqlField("type", QVariant::String)
[680a4da]13    << QSqlField("language", QVariant::String)
[c790268]14    << QSqlField("favourite", QVariant::Bool)
[b6cd05c]15    << QSqlField("alarm", QVariant::Bool)
[c790268]16    << QSqlField("tag", QVariant::String)
17    << QSqlField("title", QVariant::String)
18    << QSqlField("subtitle", QVariant::String)
19    << QSqlField("abstract", QVariant::String)
20    << QSqlField("description", QVariant::String));
[5a73d27]21
[20a6010]22
23Event Event::getById(int id, int conferenceId)
24{
[d0d0a66]25    QSqlQuery query;
[7d7659d]26    query.prepare(selectQuery() + "WHERE id = :id AND xid_conference = :conf");
[d0d0a66]27    query.bindValue(":id", id);
28    query.bindValue(":conf", conferenceId);
29    return loadOne(query);
30}
31
[64122f1]32QList<Event> Event::getByDate(const QDate& date, int conferenceId, QString orderBy)
[d0d0a66]33{
34    QSqlQuery query;
[7d7659d]35    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy));
[d0d0a66]36    query.bindValue(":conf", conferenceId);
37    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
38    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
[20a6010]39
[d0d0a66]40    return load(query);
[20a6010]41}
[680a4da]42
[7620de0]43QList<Event> Event::getByDateAndRoom(const QDate& date, int conferenceId)
44{
45    QSqlQuery query;
46    QString aliasEvent("E");
47    QString aliasEventRoom("R");
48    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(
49                    columnsForSelect(aliasEvent), Event::sTableName, aliasEvent, "EVENT_ROOM", aliasEventRoom));
50    query.bindValue(":conf", conferenceId);
51    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
52    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
53
54    return load(query);
55}
56
[b8a3ad1]57QList<Event> Event::nowEvents(int conferenceId, QString orderBy)
58{
59    //uint curTime_t =  QDateTime(QDate::currentDate(),QTime::currentTime(),Qt::UTC).toTime_t();
60    uint curTime_t = 1265457610; // for testing
61
62    QSqlQuery query;
63    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start <= :now1 AND ( start + duration ) > :now2 ORDER BY %1").arg(orderBy));
64    query.bindValue(":conf", conferenceId);
65    query.bindValue(":now1", convertToDb(curTime_t, QVariant::DateTime));
66    query.bindValue(":now2", convertToDb(curTime_t, QVariant::DateTime));
67
68    return load(query);
69}
70
[d49254d]71QList<Event> Event::conflictEvents(int aEventId, int conferenceId)
72{
73    QSqlQuery query;
74    query.prepare( selectQuery() + QString("WHERE id IN ( SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf ) ORDER BY %1").arg("start"));
75    query.bindValue(":id", aEventId);
76    query.bindValue(":conf", conferenceId);
77
78    return load(query);
79}
80
[6f39595]81QList<Event> Event::getFavByDate(const QDate& date, int conferenceId)
82{
83    QSqlQuery query;
[7d7659d]84    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite = 1 ORDER BY start"));
[6f39595]85    query.bindValue(":conf", conferenceId);
86    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
87    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
88
89    return load(query);
90}
91
[f9db452]92QString Event::room() const
93{
94    QSqlQuery query;
[395d6d3]95    // TODO: conference ID isn't used here
[f9db452]96    query.prepare("SELECT name FROM room WHERE id = (SELECT xid_room FROM event_room WHERE xid_event = :id)");
97    query.bindValue(":id", id());
98    query.exec();
99    // TODO: handle qeury error
100    //qDebug() << query.lastError();
101    if(query.next())
[9afc0ee]102        return query.record().value("name").toString();
[f9db452]103    else
104        return QString("not-available");
105}
106
[7620de0]107int Event::roomId() const
108{
109    QSqlQuery query;
110    query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id");
111    query.bindValue(":id", id());
112    if (!query.isActive())
113        if (!query.exec())
114            throw OrmSqlException(query.lastError().text());
115    if (!query.next())
116        throw OrmNoObjectException();
117    return query.record().value("xid_room").toInt();
118}
119
[395d6d3]120QStringList Event::persons() const
121{
122    QSqlQuery query;
123    // TODO: conference ID isn't used here
124    query.prepare("SELECT person.name FROM person INNER JOIN event_person ON person.id = event_person.xid_person AND event_person.xid_event = :id");
125    query.bindValue(":id", id());
126    query.exec();
127    // TODO: handle qeury error
128    //qDebug() << query.lastError();
129
130    QStringList persons;
131    while(query.next())
132        persons.append(query.record().value("name").toString());
133
134    return persons;
135}
136
[6123b48]137QMap<QString,QString> Event::links() const
138{
139    QSqlQuery query;
140    query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf");
141    query.bindValue(":id", id());
142    query.bindValue(":conf", conferenceId());
143    query.exec();
144    // TODO: handle qeury error
145    //qDebug() << query.lastError();
146
147    QMap<QString,QString> links;
148    while(query.next())
149        links.insert(query.record().value("name").toString(), query.record().value("url").toString());
150
151    return links;
152}
153
[9f367eb]154QList<int> Event::conflicts() const
155{
156    QSqlQuery query;
157    // TODO: conference ID isn't used here
158    query.prepare("SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf");
159    query.bindValue(":id", id());
160    query.bindValue(":conf", conferenceId());
161    query.exec();
162    // TODO: handle qeury error
163    //qDebug() << query.lastError();
164
165    QList<int> conflicts;
166    while(query.next())
167        conflicts.append(query.record().value("conflict_event").toInt());
168
169    return conflicts;
170}
171
172bool Event::hasTimeConflict() const
173{
174    return conflicts().count() > 0 ? true : false;
175}
176
177void Event::updateConflicts()
178{
179    qDebug() << "updating conflicts";
180    QSqlQuery query;
181    query.prepare("SELECT id FROM event WHERE xid_conference = :conf AND ( \
182           ( start <= :start1 AND ( start + duration ) >= :start2 ) \
183        OR ( start >= :start3 AND ( start + duration ) <= :end1 ) \
184        OR ( start <= :end2  AND ( start + duration ) >= :end3 ) ) AND favourite = 1 ORDER BY start");
185    query.bindValue(":conf", conferenceId());
186    query.bindValue(":start1", convertToDb(start(), QVariant::DateTime));
187    query.bindValue(":start2", convertToDb(start(), QVariant::DateTime));
188    query.bindValue(":start3", convertToDb(start(), QVariant::DateTime));
189    query.bindValue(":end1", convertToDb(start().toTime_t()+duration(), QVariant::DateTime));
190    query.bindValue(":end2", convertToDb(start().toTime_t()+duration(), QVariant::DateTime));
191    query.bindValue(":end3", convertToDb(start().toTime_t()+duration(), QVariant::DateTime));
192    query.exec();
193
194    QList<int> conflicts;
195    while(query.next())
196    {
197        int idx = query.record().value("id").toInt();
198        if(idx != id())
199            conflicts.append(idx);
200    }
201
202    if(isFavourite()) // event became favourite
203    {
204        for(int i=0; i<conflicts.count(); i++)
205        {
206            QSqlQuery query;
207            query.prepare("INSERT INTO event_conflict (xid_conference, xid_event, conflict_event) VALUES ( ? , ? , ? )");
208            query.bindValue(0, conferenceId());
209            query.bindValue(1, id());
210            query.bindValue(2, conflicts[i]);
211            query.exec();
212
213            QSqlQuery query2;
214            query2.prepare("INSERT INTO event_conflict (xid_conference, xid_event, conflict_event) VALUES ( ? , ? , ? )");
215            query2.bindValue(0, conferenceId());
216            query2.bindValue(1, conflicts[i]);
217            query2.bindValue(2, id());
218            query2.exec();
219        }
220    }
221    else // event removed from favourities
222    {
223        qDebug() << "removing";
224
225        QSqlQuery queryRemove;
226        queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf");
227        queryRemove.bindValue(":id",id());
228        queryRemove.bindValue(":conf",conferenceId());
229        queryRemove.exec();
230
231        for(int i=0; i<conflicts.count(); i++)
232        {
233            qDebug() << "removing: " << id() << " -> " << conflicts[i];
234
235            QSqlQuery queryRemove;
236            queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id1 AND xid_conference = :conf AND conflict_event = :id2");
237            queryRemove.bindValue(":id1",conflicts[i]);
238            queryRemove.bindValue(":conf",conferenceId());
239            queryRemove.bindValue(":id2",id());
240            queryRemove.exec();
241        }
242    }
243}
244
[f9db452]245void Event::setRoom(const QString &room)
246{
[395d6d3]247    Q_UNUSED(room);
248
[f9db452]249    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
250    // TODO: implement
251}
[27102d5]252
[395d6d3]253void Event::setPersons(const QStringList &persons)
254{
255    Q_UNUSED(persons);
256
257    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
258    // TODO: implement
259}
260
[6123b48]261void Event::setLinks(const QMap<QString,QString> &aLinks)
262{
263    Q_UNUSED(aLinks);
264
265    qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET");
266    // TODO: implement
267}
268
[e662750]269QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy)
270{
271
[7d7659d]272    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
273    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
[e662750]274    qDebug() << strQuery;
[0d4ecc2]275    QList<Event> list;
[e662750]276    QSqlQuery query;
[0d4ecc2]277    try{
278        if( !query.prepare( strQuery ) ){
279            qDebug() << "QSqlQuery.prepare error";
280            throw OrmSqlException( query.lastError().text() );
281        }
282
283        query.bindValue(":conf", conferenceId);
284        query.bindValue(":start", convertToDb(date, QVariant::DateTime));
285        query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
286
287        list = load(query);
288    }
289    catch(OrmException &e)
290    {
291        qDebug() << "getSearchResultByDate error: " << e.text();
292    }
293    catch(...){
294        qDebug() << "getSearchResultByDate failed ...";
295    }
296    return list;
[e662750]297}
298
Note: See TracBrowser for help on using the repository browser.