source: src/mvc/event.cpp @ 21d7cc0

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

implemented 'links' in Event/EventDialog?

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