source: src/mvc/event.cpp @ 412cef6

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

some 'delegate' drawing optimizations

  • removed EVENT_CONFLICT table - used one SQL SELECT instead
  • Property mode set to 100644
File size: 7.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::conflictEvents(int aEventId, int conferenceId)
72{
73    QSqlQuery query;
74    Event event = Event::getById(aEventId,conferenceId);
75    query.prepare(selectQuery() + "WHERE xid_conference = :conf AND ( \
76           ( start <= :start1 AND ( start + duration ) >= :start2 ) \
77        OR ( start >= :start3 AND ( start + duration ) <= :end1 ) \
78        OR ( start <= :end2  AND ( start + duration ) >= :end3 ) ) AND favourite = 1 AND NOT id = :id ORDER BY start");
79    query.bindValue(":conf", event.conferenceId());
80    query.bindValue(":start1", convertToDb(event.start(), QVariant::DateTime));
81    query.bindValue(":start2", convertToDb(event.start(), QVariant::DateTime));
82    query.bindValue(":start3", convertToDb(event.start(), QVariant::DateTime));
83    query.bindValue(":end1", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime));
84    query.bindValue(":end2", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime));
85    query.bindValue(":end3", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime));
86    query.bindValue(":id", event.id());
87
88    return load(query);
89}
90
91QList<Event> Event::getFavByDate(const QDate& date, int conferenceId)
92{
93    QSqlQuery query;
94    query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start >= :start AND start < :end AND favourite = 1 ORDER BY start"));
95    query.bindValue(":conf", conferenceId);
96    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
97    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
98
99    return load(query);
100}
101
102QString Event::room() const
103{
104    QSqlQuery query;
105    // TODO: conference ID isn't used here
106    query.prepare("SELECT name FROM room WHERE id = (SELECT xid_room FROM event_room WHERE xid_event = :id)");
107    query.bindValue(":id", id());
108    query.exec();
109    // TODO: handle qeury error
110    //qDebug() << query.lastError();
111    if(query.next())
112        return query.record().value("name").toString();
113    else
114        return QString("not-available");
115}
116
117int Event::roomId() const
118{
119    QSqlQuery query;
120    query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id");
121    query.bindValue(":id", id());
122    if (!query.isActive())
123        if (!query.exec())
124            throw OrmSqlException(query.lastError().text());
125    if (!query.next())
126        throw OrmNoObjectException();
127    return query.record().value("xid_room").toInt();
128}
129
130QStringList Event::persons() const
131{
132    QSqlQuery query;
133    // TODO: conference ID isn't used here
134    query.prepare("SELECT person.name FROM person INNER JOIN event_person ON person.id = event_person.xid_person AND event_person.xid_event = :id");
135    query.bindValue(":id", id());
136    query.exec();
137    // TODO: handle qeury error
138    //qDebug() << query.lastError();
139
140    QStringList persons;
141    while(query.next())
142        persons.append(query.record().value("name").toString());
143
144    return persons;
145}
146
147QMap<QString,QString> Event::links() const
148{
149    QSqlQuery query;
150    query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf");
151    query.bindValue(":id", id());
152    query.bindValue(":conf", conferenceId());
153    query.exec();
154    // TODO: handle qeury error
155    //qDebug() << query.lastError();
156
157    QMap<QString,QString> links;
158    while(query.next())
159        links.insert(query.record().value("name").toString(), query.record().value("url").toString());
160
161    return links;
162}
163
164bool Event::hasTimeConflict() const
165{
166    if(!isFavourite()) // if it's not favourite, it can't have time-conflict
167        return false;
168
169    return conflictEvents(id(),conferenceId()).count() > 0 ? true : false;
170}
171
172void Event::setRoom(const QString &room)
173{
174    Q_UNUSED(room);
175
176    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
177    // TODO: implement
178}
179
180void Event::setPersons(const QStringList &persons)
181{
182    Q_UNUSED(persons);
183
184    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
185    // TODO: implement
186}
187
188void Event::setLinks(const QMap<QString,QString> &aLinks)
189{
190    Q_UNUSED(aLinks);
191
192    qWarning("WARINING: setLinks() is NOT IMPLEMENTED YET");
193    // TODO: implement
194}
195
196QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy)
197{
198
199    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
200    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
201    qDebug() << strQuery;
202    QList<Event> list;
203    QSqlQuery query;
204    try{
205        if( !query.prepare( strQuery ) ){
206            qDebug() << "QSqlQuery.prepare error";
207            throw OrmSqlException( query.lastError().text() );
208        }
209
210        query.bindValue(":conf", conferenceId);
211        query.bindValue(":start", convertToDb(date, QVariant::DateTime));
212        query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
213
214        list = load(query);
215    }
216    catch(OrmException &e)
217    {
218        qDebug() << "getSearchResultByDate error: " << e.text();
219    }
220    catch(...){
221        qDebug() << "getSearchResultByDate failed ...";
222    }
223    return list;
224}
225
Note: See TracBrowser for help on using the repository browser.