first working version of the search
authortimkoma <timkoma@localhost>
Thu, 21 Jan 2010 09:49:41 +0000 (09:49 +0000)
committertimkoma <timkoma@localhost>
Thu, 21 Jan 2010 09:49:41 +0000 (09:49 +0000)
src/gui/mainwindow.cpp
src/gui/mainwindow.ui
src/mvc/event.cpp
src/mvc/event.h
src/mvc/eventmodel.cpp
src/mvc/eventmodel.h
src/orm/ormrecord.h
src/sql/sqlengine.cpp
src/sql/sqlengine.h

index 00ae49b..defed3c 100644 (file)
@@ -70,7 +70,7 @@ MainWindow::MainWindow(int aEventId, QWidget *aParent)
     trackTreeView->setModel(new EventModel());
     trackTreeView->setItemDelegate(new Delegate(trackTreeView));
 
-    // DAY EVENTS View
+    // SEARCH EVENTS View
        searchTreeView->setHeaderHidden(true);
        searchTreeView->setRootIsDecorated(false);
        searchTreeView->setIndentation(0);
@@ -111,6 +111,7 @@ MainWindow::MainWindow(int aEventId, QWidget *aParent)
         dayNavigator->setDates(aStartDate, aEndDate);
         trackDayNavigator->setDates(aStartDate, aEndDate);
         favouriteDayNavigator->setDates(aStartDate, aEndDate);
+        searchDayNavigator->setDates(aStartDate, aEndDate);
     }
 
     connect(tabWidget, SIGNAL(currentChanged(int)), this, SLOT(updateTab(int)));
@@ -274,9 +275,11 @@ void MainWindow::searchClicked()
     if( searchAbstract->isChecked() )
         columns.append( "abstract" );
 
+    searchTreeView->reset();
     if( mSqlEngine->searchEvent( confId, columns, searchEdit->text() ) > 0 ){
-        searchTreeView->setVisible(true);
-        searchDayNavigator->setVisible(true);
+        static_cast<EventModel*>(searchTreeView->model())->loadSearchResultEvents(Conference::getById(confId).start(),confId);
+        searchDayNavigator->show();
+        searchTreeView->show();
     }
 }
 
index 57d3afa..abde2b0 100644 (file)
         </item>
        </layout>
       </widget>
-      <widget class="QWidget" name="searchTab" >
-       <attribute name="title" >
+      <widget class="QWidget" name="searchTab">
+       <attribute name="title">
         <string>Search</string>
        </attribute>
-       <layout class="QVBoxLayout" name="verticalLayout_3" >
-        <item>
-         <layout class="QFormLayout" name="formLayout" >
-          <property name="fieldGrowthPolicy" >
-           <enum>QFormLayout::AllNonFixedFieldsGrow</enum>
-          </property>
-          <item row="0" column="0" >
-           <widget class="QCheckBox" name="searchTitle" >
-            <property name="text" >
-             <string>Title</string>
-            </property>
-           </widget>
-          </item>
-          <item row="0" column="1" >
-           <layout class="QHBoxLayout" name="horizontalLayout" >
-            <item>
-             <widget class="QLineEdit" name="searchEdit" >
-              <property name="enabled" >
-               <bool>true</bool>
-              </property>
-              <property name="toolTip" >
-               <string>type a keyword to search</string>
-              </property>
-             </widget>
-            </item>
-            <item>
-             <widget class="QPushButton" name="searchButton" >
-              <property name="text" >
-               <string>Search</string>
-              </property>
-              <property name="checkable" >
-               <bool>false</bool>
-              </property>
-              <property name="autoDefault" >
-               <bool>false</bool>
-              </property>
-              <property name="default" >
-               <bool>true</bool>
-              </property>
-              <property name="flat" >
-               <bool>false</bool>
-              </property>
-             </widget>
-            </item>
-           </layout>
-          </item>
-          <item row="1" column="0" >
-           <widget class="QCheckBox" name="searchAbstract" >
-            <property name="text" >
-             <string>Abstract</string>
-            </property>
-           </widget>
-          </item>
-         </layout>
-        </item>
-        <item>
-         <widget class="DayNavigatorWidget" native="1" name="searchDayNavigator" >
-          <property name="maximumSize" >
-           <size>
-            <width>16777215</width>
-            <height>16777215</height>
-           </size>
-          </property>
-         </widget>
-        </item>
-        <item>
-         <widget class="TreeView" name="searchTreeView" >
-          <property name="maximumSize" >
-           <size>
-            <width>16777215</width>
-            <height>16777215</height>
-           </size>
-          </property>
-         </widget>
-        </item>
-       </layout>
+       <layout class="QVBoxLayout" name="verticalLayout_3">
+         <item>
+          <layout class="QFormLayout" name="formLayout">
+           <property name="fieldGrowthPolicy">
+            <enum>QFormLayout::AllNonFixedFieldsGrow</enum>
+           </property>
+           <item row="0" column="0">
+            <widget class="QCheckBox" name="searchTitle">
+             <property name="text">
+              <string>Title</string>
+             </property>
+            </widget>
+           </item>
+           <item row="0" column="1">
+            <layout class="QHBoxLayout" name="horizontalLayout">
+             <item>
+              <widget class="QLineEdit" name="searchEdit">
+               <property name="enabled">
+                <bool>true</bool>
+               </property>
+               <property name="toolTip">
+                <string>type a keyword to search</string>
+               </property>
+              </widget>
+             </item>
+             <item>
+              <widget class="QPushButton" name="searchButton">
+               <property name="text">
+                <string>Search</string>
+               </property>
+               <property name="checkable">
+                <bool>false</bool>
+               </property>
+               <property name="autoDefault">
+                <bool>false</bool>
+               </property>
+               <property name="default">
+                <bool>true</bool>
+               </property>
+               <property name="flat">
+                <bool>false</bool>
+               </property>
+              </widget>
+             </item>
+            </layout>
+           </item>
+           <item row="1" column="0">
+            <widget class="QCheckBox" name="searchAbstract">
+             <property name="text">
+              <string>Abstract</string>
+             </property>
+            </widget>
+           </item>
+          </layout>
+         </item>
+         <item>
+          <widget class="DayNavigatorWidget" name="searchDayNavigator" native="true">
+           <property name="maximumSize">
+            <size>
+             <width>16777215</width>
+             <height>16777215</height>
+            </size>
+           </property>
+          </widget>
+         </item>
+         <item>
+          <widget class="TreeView" name="searchTreeView">
+           <property name="maximumSize">
+            <size>
+             <width>16777215</width>
+             <height>16777215</height>
+            </size>
+           </property>
+          </widget>
+         </item>
+        </layout>
       </widget>
       <widget class="QWidget" name="mapTab" >
        <attribute name="title" >
index 022a8ac..8bf984d 100644 (file)
@@ -113,3 +113,20 @@ void Event::setPersons(const QStringList &persons)
     // TODO: implement
 }
 
+QList<Event> Event::getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy)
+{
+
+    QString strQuery = QString("SELECT %1 FROM EVENT INNER JOIN VIRTUAL_EVENT USING (xid_conference, id) "
+        "INNER JOIN SEARCH_EVENT USING (xid_conference, id) ").arg( columnsForSelectJoin2T() );
+    strQuery += QString(
+        "WHERE %1.xid_conference = :conf AND %1.start >= :start AND %1.start < :end ORDER BY %1.%2").arg(sTable1Name, orderBy);
+    qDebug() << strQuery;
+    QSqlQuery query;
+    query.prepare( strQuery );
+    query.bindValue(":conf", conferenceId);
+    query.bindValue(":start", convertToDb(date, QVariant::DateTime));
+    query.bindValue(":end", convertToDb(date.addDays(1), QVariant::DateTime));
+
+    return load(query);
+}
+
index 2b392dc..93ca4e3 100644 (file)
@@ -27,6 +27,7 @@ public:
     static Event getById(int id, int conferenceId);
     static QList<Event> getByDate(const QDate & date, int conferenceId, QString orderBy);
     static QList<Event> getFavByDate(const QDate & date, int conferenceId); // get Favourities by Date
+    static QList<Event> getSearchResultByDate(const QDate& date, int conferenceId, QString orderBy);
 public:
     // Table 1
     int id() const { return value("id").toInt(); }
index c877f02..abd688f 100644 (file)
@@ -182,6 +182,18 @@ void EventModel::loadFavEvents(const QDate &aDate, int aConferenceId)
     createTimeGroups();
 }
 
+void EventModel::loadSearchResultEvents(const QDate &aDate, int aConferenceId)
+{
+    clearModel();
+    // check for existence of the conference in the DB
+    if(Conference::getAll().count())
+    {
+        qDebug() << "Loading search result Data: [" << Conference::getById(aConferenceId).title() << "] " << aDate;
+        mEvents = Event::getSearchResultByDate(QDate(aDate.year(), aDate.month(), aDate.day()), aConferenceId, "start");
+    }
+    createTimeGroups();
+}
+
 void EventModel::loadEventsByTrack(const QDate &aDate, int aConferenceId)
 {
     clearModel();
index 0fdbe9b..c36917b 100644 (file)
@@ -19,6 +19,7 @@ public:
     void loadEvents(const QDate &aDate, int aConferenceId); // loads Events from the DB
     void loadFavEvents(const QDate &aDate, int aConferenceId); // loads Favourite events from the DB
     void loadEventsByTrack(const QDate &aDate, int aConferenceId); // loads Events grouped by Track from the DB
+    void loadSearchResultEvents(const QDate &aDate, int aConferenceId);
     // a method to force 'EventModel' emit signal 'dataChanged()'
     // a 'view', eg. 'TreeView' listens for this signal and redraws changed items(indexes)
     void emitDataChangedSignal(const QModelIndex &aTopLeft, const QModelIndex &aBottomRight);
index ae848ea..a4923d7 100644 (file)
@@ -142,6 +142,10 @@ QList<T> OrmRecord<T>::load(QSqlQuery query)
             qDebug() << "Error: " << query.lastError().driverText() << "; Type: " << query.lastError().type();
             throw new OrmSqlException(query.lastError().text());
         }
+        else
+        {
+            qDebug() << "SQL OK";
+        }
     }
 
     QList<T> objects;
@@ -149,7 +153,7 @@ QList<T> OrmRecord<T>::load(QSqlQuery query)
     {
         objects << hydrate(query.record());
     }
-
+    qDebug() << "Fetch done";
     return objects;
 }
 
index d74792b..c9a0e21 100644 (file)
@@ -37,8 +37,10 @@ QString SqlEngine::login(const QString &aDatabaseType, const QString &aDatabaseN
         //  - create new DB from resource, which contains empty DB with tables
         result = createTables(database);
     }
-
-    database.open();
+    else
+    {
+        database.open();
+    }
 
     //LOG_INFO(QString("Opening '%1' database '%2'").arg(aDatabaseType).arg(aDatabaseName));
 
@@ -317,13 +319,15 @@ int SqlEngine::searchEvent(int aConferenceId, const QList<QString> &aColumns, co
     if ( !db.isValid() || !db.isOpen())
         return -1;
 
-    QString query = QString(
-        "DROP TABLE IF EXISTS SEARCH_EVENT;"
-        "CREATE TEMP TABLE SEARCH_EVENT ( xid_conference INTEGER  NOT NULL, id INTEGER NOT NULL );"
-        "INSERT INTO SEARCH_EVENT ( xid_conference, id) "
-            "SELECT xid_conference, id FROM EVENT AS e INNER JOIN VIRTUAL_EVENT AS ve USING (xid_conference, id) "
-            "WHERE xid_conference = %1 AND (").arg( aConferenceId );
 
+    // DROP
+    execQuery( db, "DROP TABLE IF EXISTS SEARCH_EVENT;");
+    // CREATE
+    execQuery( db, "CREATE TABLE SEARCH_EVENT ( xid_conference INTEGER  NOT NULL, id INTEGER NOT NULL );");
+    // INSERT
+    QString query = QString("INSERT INTO SEARCH_EVENT ( xid_conference, id) "
+                "SELECT xid_conference, id FROM EVENT AS e INNER JOIN VIRTUAL_EVENT AS ve USING (xid_conference, id) "
+                "WHERE xid_conference = %1 AND (").arg( aConferenceId );
     int i = 0;
     foreach (QString str, aColumns){
         query += QString("%1 LIKE '\%%2\%' OR ").arg( aColumns.at(i++), aKeyword );
@@ -331,17 +335,23 @@ int SqlEngine::searchEvent(int aConferenceId, const QList<QString> &aColumns, co
     query.chop( QString(" OR ").length() );
     query += QString(");");
 
-    qDebug() << "\nSQL: " << query;
+    execQuery( db, query );
 
-    db.exec();
+    //TODO: retun number of rows from SEARCH_EVENT
+    return 1;
+}
+
+bool SqlEngine::execQuery(QSqlDatabase &aDatabase, const QString &aQuery)
+{
+    qDebug() << "\nSQL: " << aQuery;
 
-    if( db.lastError().isValid() && db.lastError().number() != 0 ){
-        qDebug() << "SQL ERR: " << db.lastError().number() << ", " << db.lastError().text();
-        return 0;
+    QSqlQuery sqlQuery(aDatabase);
+    if( !sqlQuery.exec(aQuery) ){
+       qDebug() << "SQL ERR: " << sqlQuery.lastError().number() << ", " << sqlQuery.lastError().text();
+       return false;
     }
     else{
-        qDebug() << "SQL OK.\n";
-        return 1;
+       qDebug() << "SQL OK.\n";
+       return true;
     }
-
 }
index 110eb24..272865f 100644 (file)
@@ -24,6 +24,7 @@ class SqlEngine : public QObject
     private:
         QString login(const QString &aDatabaseType, const QString &aDatabaseName);
         bool createTables(QSqlDatabase &aDatabase);
+        bool execQuery(QSqlDatabase &aDatabase, const QString &aQuery);
 };
 
 #endif /* SQLENGINE_H */