Main Page | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

dbListManager.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Graeme Foster                                   *
00003  *   email    foster.graeme@gmail.com                                      *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include "dbListManager.h"
00022 
00023 dbListManager* dbListManager::_instance = 0;
00024 
00025 dbListManager * dbListManager::Instance()
00026 {
00027         if (_instance == 0)
00028         {
00029                 _instance = new dbListManager;
00030         }
00031         return _instance;
00032 }
00033 
00034 
00035 
00036 dbListManager::dbListManager()
00037 {
00038         dbConn = sql::Instance();
00039         pInstituteList  = new dbList();
00040         pProgrammeList  = new dbList();
00041         pModuleList     = new dbList();
00042         pFacultyList    = new dbList();
00043         pDepartmentList = new dbList();
00044         pStaffList      = new dbList();
00045         pRoomList       = new dbList();
00046         pMLAllocList    = new dbList();
00047         pTimeSlotList   = new dbList();
00048         newInstitute();
00049         newAlloc();
00050         newTimeSlot();
00051 }
00052 
00053 void dbListManager::registerModel(RootModel * model, int lists)
00054 {
00055         registeredModels * rm = new registeredModels(model);
00056         pRegisteredModels.append(rm);
00057         if (lists & regInstitute)
00058                 rm->registerList(pInstituteList);
00059         if (lists & regProgramme)
00060                 rm->registerList(pProgrammeList);
00061         if (lists & regModule)
00062                 rm->registerList(pModuleList);
00063         if (lists & regFaculty)
00064                 rm->registerList(pFacultyList);
00065         if (lists & regDepartment)
00066                 rm->registerList(pDepartmentList);
00067         if (lists & regStaff)
00068                 rm->registerList(pStaffList);
00069         if (lists & regRoom)
00070                 rm->registerList(pRoomList);
00071         if (lists & regMLAlloc)
00072                 rm->registerList(pMLAllocList);
00073         if (lists & regTimeSlot)
00074                 rm->registerList(pTimeSlotList);
00075 }
00076 
00077 void dbListManager::newInstitute ()
00078 {
00079         Institute * instObj;
00080         int dbid;
00081         QString name, abbrev;
00082 
00083         QString sqlLine = "SELECT id, name, abbrev FROM basic.institute ORDER BY name";
00084         QString * dbConnName = dbConn->useModuleConn();
00085         sql::Instance()->prepare(sqlLine, *dbConnName);
00086         sql::Instance()->query();
00087         
00088         while (sql::Instance()->next())
00089         {
00090                 dbid = dbConn->get(0).toInt();
00091                 name = dbConn->get(1).toString();
00092                 abbrev = dbConn->get(2).toString();
00093                 instObj = new Institute (dbid, name, abbrev);
00094                 pInstituteList->append(instObj);
00095                 // Now get the Programmes, Faculties and Rooms for this Institute
00096                 // Store the connection, so that the position is not lost
00097                 dbConn->setCursor();
00098                 newProgramme(instObj);
00099                 newFaculty(instObj);
00100                 newRoom(instObj);
00101                 // Restore the connection
00102                 dbConn->restoreCursor();
00103         }
00104 }
00105 
00106 void dbListManager::newProgramme (Institute * instObj)
00107 {
00108         Programme * progObj;
00109         int dbid;
00110         QString name, abbrev;
00111 
00112         QString sqlLine = "SELECT id, name, abbrev FROM basic.programme WHERE institute_id = " + QString::number(instObj->getID()) + " ORDER BY name";
00113         QString * dbConnName = dbConn->useModuleConn();
00114         sql::Instance()->prepare(sqlLine, *dbConnName);
00115         sql::Instance()->query();
00116         
00117         while (sql::Instance()->next())
00118         {
00119                 dbid = dbConn->get(0).toInt();
00120                 name = dbConn->get(1).toString();
00121                 abbrev = dbConn->get(2).toString();
00122                 progObj = new Programme (dbid, name, abbrev, instObj);
00123                 pProgrammeList->append(progObj);
00124                 // Now get the Modules for this Programme
00125                 // Store the connection, so that the position is not lost
00126                 dbConn->setCursor();
00127                 newModule(progObj);
00128                 // Restore the connection
00129                 dbConn->restoreCursor();
00130         }
00131 }
00132 
00133 void dbListManager::newModule (Programme * progObj)
00134 {
00135         Module * moduleObj;
00136         int dbid;
00137         QString name, nomenclature;
00138         int duration, period;
00139         short semester;
00140         
00141         QString sqlLine = "SELECT id, name, nomenclature, duration, weekly_periods, semester FROM basic.module WHERE programme_id = "
00142                                                  + QString::number(progObj->getID()) + " ORDER BY name";
00143         QString * dbConnName = dbConn->useModuleConn();
00144         sql::Instance()->prepare(sqlLine, *dbConnName);
00145         sql::Instance()->query();
00146         
00147         while (sql::Instance()->next())
00148         {
00149                 dbid = dbConn->get(0).toInt();
00150                 name = dbConn->get(1).toString();
00151                 nomenclature = dbConn->get(2).toString();
00152                 duration = dbConn->get(3).toInt();
00153                 period = dbConn->get(4).toInt();
00154                 semester = dbConn->get(5).toInt();
00155                 moduleObj = new Module (dbid, progObj, name, nomenclature, duration, period, semester);
00156                 pModuleList->append(moduleObj);
00157         }
00158 }
00159 
00160 void dbListManager::newFaculty    (Institute  * instObj)
00161 {
00162         Faculty * facultyObj;
00163         int dbid;
00164         QString name, abbrev;
00165 
00166         QString sqlLine = "SELECT id, name, abbrev FROM basic.faculty WHERE institute_id = " + QString::number(instObj->getID()) + " ORDER BY name";
00167         QString * dbConnName = dbConn->useModuleConn();
00168         sql::Instance()->prepare(sqlLine, *dbConnName);
00169         sql::Instance()->query();
00170         
00171         while (sql::Instance()->next())
00172         {
00173                 dbid = dbConn->get(0).toInt();
00174                 name = dbConn->get(1).toString();
00175                 abbrev = dbConn->get(2).toString();
00176                 facultyObj = new Faculty (dbid, name, abbrev, instObj);
00177                 pFacultyList->append(facultyObj);
00178                 // Now get the Departments for this Faculty
00179                 // Store the connection, so that the position is not lost
00180                 dbConn->setCursor();
00181                 newDepartment(facultyObj);
00182                 dbConn->restoreCursor();
00183                 // Restore the connection
00184         }
00185 }
00186 
00187 void dbListManager::newDepartment (Faculty    * facultyObj)
00188 {
00189         Department * deptObj;
00190         int dbid;
00191         QString name, abbrev;
00192         
00193         QString sqlLine = "SELECT id, name, abbrev FROM basic.department WHERE faculty_id = "
00194                                                  + QString::number(facultyObj->getID()) + " ORDER BY name";
00195         QString * dbConnName = dbConn->useModuleConn();
00196         sql::Instance()->prepare(sqlLine, *dbConnName);
00197         sql::Instance()->query();
00198         
00199         while (sql::Instance()->next())
00200         {
00201                 dbid = dbConn->get(0).toInt();
00202                 name = dbConn->get(1).toString();
00203                 abbrev = dbConn->get(2).toString();
00204                 deptObj = new Department (dbid, name, abbrev, facultyObj);
00205                 pDepartmentList->append(deptObj);
00206                 // Now get the Staff for this Department
00207                 // Store the connection, so that the position is not lost
00208                 dbConn->setCursor();
00209                 newStaff(deptObj);
00210                 // Restore the connection
00211                 dbConn->restoreCursor();
00212         }
00213 }
00214 
00215 void dbListManager::newStaff      (Department * deptObj)
00216 {
00217         Staff * staffObj;
00218         int dbid;
00219         QString fName, lName;
00220         
00221         QString sqlLine = "SELECT id, first_name, last_name FROM basic.staff WHERE dept_id = "
00222                                                  + QString::number(deptObj->getID()) + " ORDER BY first_name";
00223         QString * dbConnName = dbConn->useModuleConn();
00224         sql::Instance()->prepare(sqlLine, *dbConnName);
00225         sql::Instance()->query();
00226         
00227         while (sql::Instance()->next())
00228         {
00229                 dbid = dbConn->get(0).toInt();
00230                 fName = dbConn->get(1).toString();
00231                 lName = dbConn->get(2).toString();
00232                 staffObj = new Staff (dbid, fName, lName, deptObj);
00233                 pStaffList->append(staffObj);
00234         }
00235 }
00236 
00237 void dbListManager::newRoom       (Institute  * instObj)
00238 {
00239         Room * roomObj;
00240         int dbid;
00241         QString name, code;
00242         int capacity;
00243 
00244         QString sqlLine = "SELECT id, name, code, capacity FROM basic.room WHERE institute_id = " + QString::number(instObj->getID()) + " ORDER BY name";
00245         QString * dbConnName = dbConn->useModuleConn();
00246         sql::Instance()->prepare(sqlLine, *dbConnName);
00247         sql::Instance()->query();
00248         
00249         while (sql::Instance()->next())
00250         {
00251                 dbid = dbConn->get(0).toInt();
00252                 name = dbConn->get(1).toString();
00253                 code = dbConn->get(2).toString();
00254                 capacity = dbConn->get(3).toInt();
00255                 roomObj = new Room (dbid, name, code, capacity, instObj);
00256                 pRoomList->append(roomObj);
00257         }
00258 }
00259 
00260 void dbListManager::newAlloc()
00261 {
00262         int inst_id, prog_id, mod_id, staff_id, sem, hours, c_size, c_groups, l_hours, l_groups;
00263    Module * modObj;
00264    Staff  * staffObj;
00265    ModLecAlloc * MLAllocObj;
00266    
00267         QString sqlLine = "SELECT institute_id, programme_id, module_id, lecturer_id, hours, semester, class_size, groups, lab_hours, lab_groups FROM basic.allocated_modules ORDER BY programme_id, semester, nomenclature";
00268         QString * dbConnName = dbConn->useModuleConn();
00269         sql::Instance()->prepare(sqlLine, *dbConnName);
00270         sql::Instance()->query();
00271         
00272         while (sql::Instance()->next())
00273         {
00274                 inst_id  = dbConn->get(0).toInt();
00275                 prog_id  = dbConn->get(1).toInt();
00276                 mod_id   = dbConn->get(2).toInt();
00277                 staff_id = dbConn->get(3).toInt();
00278                 hours    = dbConn->get(4).toInt();
00279                 sem      = dbConn->get(5).toInt();
00280                 c_size   = dbConn->get(6).toInt();
00281                 c_groups = dbConn->get(7).toInt();
00282                 l_hours  = dbConn->get(8).toInt();
00283                 l_groups = dbConn->get(9).toInt();
00284                 modObj=getModuleFromID(mod_id);
00285                 staffObj=getStaffFromID(staff_id);
00286                 MLAllocObj = new ModLecAlloc(0, modObj, staffObj, hours, sem, c_size, c_groups, l_hours, l_groups);
00287                 pMLAllocList->append(MLAllocObj);
00288         }
00289 }
00290 
00291 void dbListManager::newTimeSlot()
00292 {
00293         int id, inst_id, theDay, duration;
00294    QTime startTime, finishTime;
00295    Institute * instObj;
00296    TimeSlot  * TSlotObj;
00297    
00298         QString sqlLine = "SELECT id, institute_id, day_of_week, time_start, time_finish FROM basic.timeslot ORDER BY institute_id, day_of_week, time_start";
00299         QString * dbConnName = dbConn->useModuleConn();
00300         sql::Instance()->prepare(sqlLine, *dbConnName);
00301         sql::Instance()->query();
00302         
00303         while (sql::Instance()->next())
00304         {
00305                 id         = dbConn->get(0).toInt();
00306                 inst_id    = dbConn->get(1).toInt();
00307                 theDay     = dbConn->get(2).toInt();
00308                 startTime  = dbConn->get(3).toTime();
00309                 finishTime = dbConn->get(4).toTime();
00310                 
00311                 instObj=getInstituteFromID(inst_id);
00312                 duration = startTime.secsTo(finishTime)/60;
00313                 TSlotObj = new TimeSlot(id, instObj,TimeSlot::days(theDay), duration, startTime);
00314                 pTimeSlotList->append(TSlotObj);
00315         }
00316 }
00317 
00318 void dbListManager::updateRMInsert(dbList * list, RootItem * item, QModelIndex & index, int posn)
00319 {
00320         RootModel * rm;
00321         for (int i = 0; i < pRegisteredModels.size(); i++)
00322         {
00323                 if (pRegisteredModels.at(i)->listIsMember(list))
00324                 {
00325                         rm = pRegisteredModels.at(i)->pModel;
00326                         rm->insertItem(item->clone(), index, posn);
00327                 }
00328         }
00329 }
00330 
00331 void dbListManager::updateRMUpdate(dbList * list, DataClass * dcObj, CompareRI compare)
00332 {
00333         RootModel * rm;
00334         for (int i = 0; i < pRegisteredModels.size(); i++)
00335         {
00336                 if (pRegisteredModels.at(i)->listIsMember(list))
00337                 {
00338                         rm = pRegisteredModels.at(i)->pModel;
00339                         rm->sort(compare,dcObj);
00340                 }
00341         }
00342 }
00343 
00344 void dbListManager::updateRMDelete(dbList * list, DataClass * dcObj)
00345 {
00346         RootModel * rm;
00347         for (int i = 0; i < pRegisteredModels.size(); i++)
00348         {
00349                 if (pRegisteredModels.at(i)->listIsMember(list))
00350                 {
00351                         rm = pRegisteredModels.at(i)->pModel;
00352                         rm->deleteItem(dcObj);
00353                 }
00354         }
00355 }
00356 
00357 void dbListManager::insert(RootItem * item, dbList * list, QModelIndex & index, CompareDC lessThan)
00358 {
00359         DataClass * dataObj = item->getItemData();
00360         int posn = 0;
00361         int i = 0;
00362         while (i < list->size())
00363         {
00364                 if (lessThan(list->at(i),dataObj))
00365                         break;
00366                 posn = i;
00367                 i++;
00368         }
00369         if (i == list->size())
00370                 posn = i;
00371         list->insert(posn,dataObj);
00372         updateRMInsert(list, item, index, posn);
00373         // Now save the item on the database
00374         item->dbSave();
00375 }
00376 
00377 //template<typename Compare>
00378 void dbListManager::update(RootItem * item, dbList * list, CompareRI compare)
00379 {
00380         DataClass * dataObj = item->getItemData();
00381         list->resort(DataClass::dcCompareCI);
00382         updateRMUpdate(list, dataObj, compare);
00383         // Now save the item on the database
00384         item->dbSave();
00385 }
00386 
00387 void dbListManager::remove(RootItem * item, dbList * list, bool cascade)
00388 {
00389         DataClass * dataObj = item->getItemData();
00390         int posn = 0;
00391         while (posn < list->size())
00392         {
00393                 if (list->at(posn) == dataObj)
00394                 {
00395                         list->remove(posn);
00396                         break;
00397                 }
00398                 posn++;
00399         }
00400         // Cascade delete on the dbLists
00401         if (cascade)
00402                 removeAllChilden(dataObj);
00403         // Delete from the Models (cascade delete is inherent in the data structure)
00404         updateRMDelete(list, dataObj);
00405         // Now remove the item from the database (cascade delete is on the database)
00406         item->dbDelete();
00407 }
00408 
00409 void dbListManager::removeAllChilden(DataClass * dcObj)
00410 {
00411         removeChild(dcObj,pInstituteList);
00412         removeChild(dcObj,pProgrammeList);
00413         removeChild(dcObj,pModuleList);
00414         removeChild(dcObj,pFacultyList);
00415         removeChild(dcObj,pDepartmentList);
00416         removeChild(dcObj,pStaffList);
00417         removeChild(dcObj,pRoomList);
00418         removeChild(dcObj,pMLAllocList);
00419         removeChild(dcObj,pTimeSlotList);
00420 }
00421 
00422 void dbListManager::removeChild(DataClass * dcObj, dbList * list)
00423 {
00424         int posn = 0;
00425         while (posn < list->size())
00426         {
00427                 if (list->at(posn)->getParent() == dcObj)
00428                 {
00429                         DataClass * newObj = list->at(posn);
00430                         list->remove(posn);
00431                         removeAllChilden(newObj);
00432                 }
00433                 else
00434                         posn++;
00435         }
00436 }
00437 
00438 
00439 void dbListManager::dump(bool details)
00440 {
00441         std::cout << "dbListManager\n";
00442         std::cout << "   pInstituteList [" << pInstituteList->size()<< "] - ";
00443         for (int i = 0; i < pInstituteList->size(); i++)
00444         {
00445                 std::cout << " (";
00446                 std::cout << getProgrammeList(static_cast<Institute*>(pInstituteList->at(i)))->size();
00447                 std::cout << ")";
00448         }
00449         std::cout << "\n";
00450         if (details)
00451                 for (int i = 0; i < pInstituteList->size(); i++)
00452                 {
00453                         std::cout << "      " << pInstituteList->at(i)->getDescription().toStdString();
00454                         std::cout << getProgrammeList(static_cast<Institute*>(pInstituteList->at(i)))->size();
00455                         std::cout << "\n";
00456                 }
00457         std::cout << "   pProgrammeList [" << pProgrammeList->size()<< "]\n";
00458         if (details)
00459                 for (int i = 0; i < pProgrammeList->size(); i++)
00460                 {
00461                         std::cout << "         " << pProgrammeList->at(i)->getDescription().toStdString() <<"\n";
00462                 }
00463         std::cout << "      pModuleList [" << pModuleList->size()<< "]\n";
00464         if (details)
00465                 for (int i = 0; i < pModuleList->size(); i++)
00466                 {
00467                         std::cout << "            " << pModuleList->at(i)->getDescription().toStdString() <<"\n";
00468                 }
00469         std::cout << "   pFacultyList [" << pFacultyList->size()<< "]\n";
00470         if (details)
00471                 for (int i = 0; i < pFacultyList->size(); i++)
00472                 {
00473                         std::cout << "         " << pFacultyList->at(i)->getDescription().toStdString() <<"\n";
00474                 }
00475         std::cout << "   pDepartmentList [" << pDepartmentList->size()<< "]\n";
00476         if (details)
00477                 for (int i = 0; i < pDepartmentList->size(); i++)
00478                 {
00479                         std::cout << "         " << pDepartmentList->at(i)->getDescription().toStdString() <<"\n";
00480                 }
00481         std::cout << "   pStaffList [" << pStaffList->size()<< "]\n";
00482         if (details)
00483                 for (int i = 0; i < pStaffList->size(); i++)
00484                 {
00485                         std::cout << "         " << pStaffList->at(i)->getDescription().toStdString() <<"\n";
00486                 }
00487         std::cout << "   pRoomList [" << pRoomList->size()<< "]\n";
00488         if (details)
00489                 for (int i = 0; i < pRoomList->size(); i++)
00490                 {
00491                         std::cout << "         " << pRoomList->at(i)->getDescription().toStdString() <<"\n";
00492                 }
00493         std::cout << "RegisteredModels [" << pRegisteredModels.size()<< "]\n";
00494         if (details)
00495                 for (int i = 0; i < pRegisteredModels.size(); i++)
00496                 {
00497                         pRegisteredModels.at(i)->dump();
00498                 }
00499 }
00500 
00501 void dbListManager::registeredModels::dump()
00502 {
00503         std::cout << "   " << pModel->metaObject()->className() << "\n";
00504         for (int i = 0; i<listOfLists.size(); i++)
00505                 listOfLists.at(i)->dump();
00506 }
00507 

Generated on Thu Apr 6 16:27:17 2006 for time-table by  doxygen 1.4.4