1 | #include "event.h" |
---|
2 | #include "room.h" |
---|
3 | |
---|
4 | QString const Event::sTableName = QString("event"); |
---|
5 | |
---|
6 | QSqlRecord 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 | |
---|
23 | Event 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 | |
---|
32 | QList<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 | |
---|
43 | QList<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 | |
---|
57 | QList<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 | |
---|
71 | QList<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 | |
---|
82 | QString 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 | |
---|
97 | int 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 | |
---|
110 | QStringList 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 | |
---|
127 | void Event::setRoom(const QString &room) |
---|
128 | { |
---|
129 | Q_UNUSED(room); |
---|
130 | |
---|
131 | qWarning("WARINING: setRoom() is NOT IMPLEMENTED YET"); |
---|
132 | // TODO: implement |
---|
133 | } |
---|
134 | |
---|
135 | void Event::setPersons(const QStringList &persons) |
---|
136 | { |
---|
137 | Q_UNUSED(persons); |
---|
138 | |
---|
139 | qWarning("WARINING: setPersons() is NOT IMPLEMENTED YET"); |
---|
140 | // TODO: implement |
---|
141 | } |
---|
142 | |
---|
143 | QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) |
---|
144 | { |
---|
145 | |
---|
146 | QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect()); |
---|
147 | strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy); |
---|
148 | qDebug() << strQuery; |
---|
149 | QList<Event> list; |
---|
150 | QSqlQuery query; |
---|
151 | try{ |
---|
152 | if( !query.prepare( strQuery ) ){ |
---|
153 | qDebug() << "QSqlQuery.prepare error"; |
---|
154 | throw OrmSqlException( query.lastError().text() ); |
---|
155 | } |
---|
156 | |
---|
157 | query.bindValue(":conf", conferenceId); |
---|
158 | query.bindValue(":start", convertToDb(date, QVariant::DateTime)); |
---|
159 | query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime)); |
---|
160 | |
---|
161 | list = load(query); |
---|
162 | } |
---|
163 | catch(OrmException &e) |
---|
164 | { |
---|
165 | qDebug() << "getSearchResultByDate error: " << e.text(); |
---|
166 | } |
---|
167 | catch(...){ |
---|
168 | qDebug() << "getSearchResultByDate failed ..."; |
---|
169 | } |
---|
170 | return list; |
---|
171 | } |
---|
172 | |
---|