source: src/mvc/event.cpp @ 9f367eb

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

'conflicts' modifications

  • preparing for the dialog showing also list of events in the conflict
  • created 'EVENT_CONFLICT' for flaging events in conflict state
  • TODO: not finished
  • Property mode set to 100644
File size: 9.2 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
127QList<int> Event::conflicts() const
128{
129    QSqlQuery query;
130    // TODO: conference ID isn't used here
131    query.prepare("SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf");
132    query.bindValue(":id", id());
133    query.bindValue(":conf", conferenceId());
134    query.exec();
135    // TODO: handle qeury error
136    //qDebug() << query.lastError();
137
138    QList<int> conflicts;
139    while(query.next())
140        conflicts.append(query.record().value("conflict_event").toInt());
141
142    return conflicts;
143}
144
145bool Event::hasTimeConflict() const
146{
147    return conflicts().count() > 0 ? true : false;
148}
149
150void Event::updateConflicts()
151{
152    qDebug() << "updating conflicts";
153    QSqlQuery query;
154    query.prepare("SELECT id FROM event WHERE xid_conference = :conf AND ( \
155           ( start <= :start1 AND ( start + duration ) >= :start2 ) \
156        OR ( start >= :start3 AND ( start + duration ) <= :end1 ) \
157        OR ( start <= :end2  AND ( start + duration ) >= :end3 ) ) AND favourite = 1 ORDER BY start");
158    query.bindValue(":conf", conferenceId());
159    query.bindValue(":start1", convertToDb(start(), QVariant::DateTime));
160    query.bindValue(":start2", convertToDb(start(), QVariant::DateTime));
161    query.bindValue(":start3", convertToDb(start(), QVariant::DateTime));
162    query.bindValue(":end1", convertToDb(start().toTime_t()+duration(), QVariant::DateTime));
163    query.bindValue(":end2", convertToDb(start().toTime_t()+duration(), QVariant::DateTime));
164    query.bindValue(":end3", convertToDb(start().toTime_t()+duration(), QVariant::DateTime));
165    query.exec();
166
167    QList<int> conflicts;
168    while(query.next())
169    {
170        int idx = query.record().value("id").toInt();
171        if(idx != id())
172            conflicts.append(idx);
173    }
174
175    if(isFavourite()) // event became favourite
176    {
177        for(int i=0; i<conflicts.count(); i++)
178        {
179            QSqlQuery query;
180            query.prepare("INSERT INTO event_conflict (xid_conference, xid_event, conflict_event) VALUES ( ? , ? , ? )");
181            query.bindValue(0, conferenceId());
182            query.bindValue(1, id());
183            query.bindValue(2, conflicts[i]);
184            query.exec();
185
186            QSqlQuery query2;
187            query2.prepare("INSERT INTO event_conflict (xid_conference, xid_event, conflict_event) VALUES ( ? , ? , ? )");
188            query2.bindValue(0, conferenceId());
189            query2.bindValue(1, conflicts[i]);
190            query2.bindValue(2, id());
191            query2.exec();
192        }
193    }
194    else // event removed from favourities
195    {
196        qDebug() << "removing";
197
198        QSqlQuery queryRemove;
199        queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf");
200        queryRemove.bindValue(":id",id());
201        queryRemove.bindValue(":conf",conferenceId());
202        queryRemove.exec();
203
204        for(int i=0; i<conflicts.count(); i++)
205        {
206            qDebug() << "removing: " << id() << " -> " << conflicts[i];
207
208            QSqlQuery queryRemove;
209            queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id1 AND xid_conference = :conf AND conflict_event = :id2");
210            queryRemove.bindValue(":id1",conflicts[i]);
211            queryRemove.bindValue(":conf",conferenceId());
212            queryRemove.bindValue(":id2",id());
213            queryRemove.exec();
214        }
215    }
216}
217
218void Event::setRoom(const QString &room)
219{
220    Q_UNUSED(room);
221
222    qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET");
223    // TODO: implement
224}
225
226void Event::setPersons(const QStringList &persons)
227{
228    Q_UNUSED(persons);
229
230    qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET");
231    // TODO: implement
232}
233
234QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy)
235{
236
237    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect());
238    strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy);
239    qDebug() << strQuery;
240    QList<Event> list;
241    QSqlQuery query;
242    try{
243        if( !query.prepare( strQuery ) ){
244            qDebug() << "QSqlQuery.prepare error";
245            throw OrmSqlException( query.lastError().text() );
246        }
247
248        query.bindValue(":conf", conferenceId);
249        query.bindValue(":start", convertToDb(date, QVariant::DateTime));
250        query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
251
252        list = load(query);
253    }
254    catch(OrmException &e)
255    {
256        qDebug() << "getSearchResultByDate error: " << e.text();
257    }
258    catch(...){
259        qDebug() << "getSearchResultByDate failed ...";
260    }
261    return list;
262}
263
Note: See TracBrowser for help on using the repository browser.