X-Git-Url: https://git.toastfreeware.priv.at/toast/confclerk.git/blobdiff_plain/d49254dfd400b8c543966a2979580db43a1fc867..066b41f0f3e6bbc2cefb11500b07a1ee627d5660:/src/mvc/event.cpp?ds=sidebyside diff --git a/src/mvc/event.cpp b/src/mvc/event.cpp index ba7bbc4..86968e7 100644 --- a/src/mvc/event.cpp +++ b/src/mvc/event.cpp @@ -1,3 +1,22 @@ +/* + * Copyright (C) 2010 Ixonos Plc. + * Copyright (C) 2011 Philipp Spitzer, gregor herrmann + * + * This file is part of ConfClerk. + * + * ConfClerk is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 2 of the License, or (at your option) + * any later version. + * + * ConfClerk is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * ConfClerk. If not, see . + */ #include "event.h" #include "room.h" @@ -19,6 +38,10 @@ QSqlRecord const Event::sColumns = Event::toRecord(QList() << QSqlField("abstract", QVariant::String) << QSqlField("description", QVariant::String)); +Event::Event() : + room_(NULL) +{ +} Event Event::getById(int id, int conferenceId) { @@ -54,29 +77,24 @@ QList Event::getByDateAndRoom(const QDate& date, int conferenceId) return load(query); } -QList Event::nowEvents(int conferenceId, QString orderBy) -{ - //uint curTime_t = QDateTime(QDate::currentDate(),QTime::currentTime(),Qt::UTC).toTime_t(); - uint curTime_t = 1265457610; // for testing +QList Event::conflictEvents(int aEventId, int conferenceId) { QSqlQuery query; - query.prepare(selectQuery() + QString("WHERE xid_conference = :conf AND start <= :now1 AND ( start + duration ) > :now2 ORDER BY %1").arg(orderBy)); - query.bindValue(":conf", conferenceId); - query.bindValue(":now1", convertToDb(curTime_t, QVariant::DateTime)); - query.bindValue(":now2", convertToDb(curTime_t, QVariant::DateTime)); + Event event = Event::getById(aEventId,conferenceId); + query.prepare(selectQuery() + "WHERE xid_conference = :conf AND ( \ + ( start >= :s1 AND ( start + duration ) < :e1 ) \ + OR ( ( start + duration ) > :s2 AND start < :e2 ) ) \ + AND favourite = 1 AND NOT id = :id ORDER BY start"); + query.bindValue(":conf", event.conferenceId()); + query.bindValue(":s1", convertToDb(event.start(), QVariant::DateTime)); + query.bindValue(":e1", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime)); + query.bindValue(":s2", convertToDb(event.start(), QVariant::DateTime)); + query.bindValue(":e2", convertToDb(event.start().toTime_t()+event.duration(), QVariant::DateTime)); + query.bindValue(":id", event.id()); return load(query); } -QList Event::conflictEvents(int aEventId, int conferenceId) -{ - QSqlQuery query; - query.prepare( selectQuery() + QString("WHERE id IN ( SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf ) ORDER BY %1").arg("start")); - query.bindValue(":id", aEventId); - query.bindValue(":conf", conferenceId); - - return load(query); -} QList Event::getFavByDate(const QDate& date, int conferenceId) { @@ -89,157 +107,80 @@ QList Event::getFavByDate(const QDate& date, int conferenceId) return load(query); } -QString Event::room() const +Room* Event::room() { - QSqlQuery query; - // TODO: conference ID isn't used here - query.prepare("SELECT name FROM room WHERE id = (SELECT xid_room FROM event_room WHERE xid_event = :id)"); - query.bindValue(":id", id()); - query.exec(); - // TODO: handle qeury error - //qDebug() << query.lastError(); - if(query.next()) - return query.record().value("name").toString(); - else - return QString("not-available"); -} - -int Event::roomId() const -{ - QSqlQuery query; - query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id"); - query.bindValue(":id", id()); - if (!query.isActive()) - if (!query.exec()) - throw OrmSqlException(query.lastError().text()); - if (!query.next()) - throw OrmNoObjectException(); - return query.record().value("xid_room").toInt(); + if (room_ == NULL) + { + QSqlQuery query; + query.prepare("SELECT xid_room FROM event_room WHERE xid_event = :id AND xid_conference = :conf"); + query.bindValue(":id", id()); + query.bindValue(":conf", conferenceId()); + if (!query.isActive()) + if (!query.exec()) + throw OrmSqlException(query.lastError().text()); + if (!query.next()) + { + qDebug() << "No room found for event id: " << id(); + throw OrmNoObjectException(); + } + int id = query.record().value("xid_room").toInt(); + room_ = new Room(Room::retrieve(id)); + } + return room_; } -QStringList Event::persons() const +QString Event::roomName() { - QSqlQuery query; - // TODO: conference ID isn't used here - query.prepare("SELECT person.name FROM person INNER JOIN event_person ON person.id = event_person.xid_person AND event_person.xid_event = :id"); - query.bindValue(":id", id()); - query.exec(); - // TODO: handle qeury error - //qDebug() << query.lastError(); - - QStringList persons; - while(query.next()) - persons.append(query.record().value("name").toString()); - - return persons; + return room()->name(); } -QMap Event::links() const +int Event::roomId() { - QSqlQuery query; - query.prepare("SELECT name,url FROM link WHERE xid_event = :id AND xid_conference = :conf"); - query.bindValue(":id", id()); - query.bindValue(":conf", conferenceId()); - query.exec(); - // TODO: handle qeury error - //qDebug() << query.lastError(); - - QMap links; - while(query.next()) - links.insert(query.record().value("name").toString(), query.record().value("url").toString()); - - return links; + return room()->id(); } -QList Event::conflicts() const +QStringList Event::persons() { - QSqlQuery query; - // TODO: conference ID isn't used here - query.prepare("SELECT conflict_event FROM event_conflict WHERE xid_event = :id AND xid_conference = :conf"); - query.bindValue(":id", id()); - query.bindValue(":conf", conferenceId()); - query.exec(); - // TODO: handle qeury error - //qDebug() << query.lastError(); - - QList conflicts; - while(query.next()) - conflicts.append(query.record().value("conflict_event").toInt()); - - return conflicts; -} + if( mPersonsList.isEmpty() ) + { + QSqlQuery query; + query.prepare("SELECT person.name FROM person INNER JOIN event_person ON person.id = event_person.xid_person AND event_person.xid_event = :id AND event_person.xid_conference = :conf1 AND person.xid_conference = :conf2"); + query.bindValue(":id", id()); + query.bindValue(":conf1", conferenceId()); + query.bindValue(":conf2", conferenceId()); + if (!query.exec()) qDebug() << query.lastError(); + + while(query.next()) + mPersonsList.append(query.record().value("name").toString()); + } -bool Event::hasTimeConflict() const -{ - return conflicts().count() > 0 ? true : false; + return mPersonsList; } -void Event::updateConflicts() +QMap Event::links() { - qDebug() << "updating conflicts"; - QSqlQuery query; - query.prepare("SELECT id FROM event WHERE xid_conference = :conf AND ( \ - ( start <= :start1 AND ( start + duration ) >= :start2 ) \ - OR ( start >= :start3 AND ( start + duration ) <= :end1 ) \ - OR ( start <= :end2 AND ( start + duration ) >= :end3 ) ) AND favourite = 1 ORDER BY start"); - query.bindValue(":conf", conferenceId()); - query.bindValue(":start1", convertToDb(start(), QVariant::DateTime)); - query.bindValue(":start2", convertToDb(start(), QVariant::DateTime)); - query.bindValue(":start3", convertToDb(start(), QVariant::DateTime)); - query.bindValue(":end1", convertToDb(start().toTime_t()+duration(), QVariant::DateTime)); - query.bindValue(":end2", convertToDb(start().toTime_t()+duration(), QVariant::DateTime)); - query.bindValue(":end3", convertToDb(start().toTime_t()+duration(), QVariant::DateTime)); - query.exec(); - - QList conflicts; - while(query.next()) - { - int idx = query.record().value("id").toInt(); - if(idx != id()) - conflicts.append(idx); - } - - if(isFavourite()) // event became favourite + if ( mLinksList.isEmpty() ) { - for(int i=0; i " << conflicts[i]; - - QSqlQuery queryRemove; - queryRemove.prepare("DELETE FROM event_conflict WHERE xid_event = :id1 AND xid_conference = :conf AND conflict_event = :id2"); - queryRemove.bindValue(":id1",conflicts[i]); - queryRemove.bindValue(":conf",conferenceId()); - queryRemove.bindValue(":id2",id()); - queryRemove.exec(); - } - } + return conflictEvents(id(),conferenceId()).count() > 0 ? true : false; } void Event::setRoom(const QString &room) @@ -266,15 +207,23 @@ void Event::setLinks(const QMap &aLinks) // TODO: implement } -QList Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) -{ +QList Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy) { + QList list; + + // Check whether the temporary table SEARCH_EVENT exists (http://www.sqlite.org/faq.html#q7) + QSqlQuery query("SELECT count(*) FROM sqlite_temp_master WHERE type='table' and name='SEARCH_EVENT'"); + if (!query.exec()) { + qDebug() << "SQL Error: " << query.lastError().text(); + return list; + } + query.first(); + QVariant v = query.value(0); + if (v.toInt() != 1) return list; QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg(columnsForSelect()); strQuery += QString("WHERE xid_conference = :conf AND start >= :start AND start < :end ORDER BY %1").arg(orderBy); - qDebug() << strQuery; - QList list; - QSqlQuery query; - try{ + query = QSqlQuery(); + try { if( !query.prepare( strQuery ) ){ qDebug() << "QSqlQuery.prepare error"; throw OrmSqlException( query.lastError().text() );