source: src/mvc/event.cpp @ 2104023

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

search fixed

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