importcrewlounge.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "importcrewlounge.h"
  2. #include "src/database/adatabase.h"
  3. #include "src/opl.h"
  4. #include "src/classes/row.h"
  5. #include "src/testing/importCrewlounge/processpilots.h"
  6. #include "src/testing/importCrewlounge/processaircraft.h"
  7. #include "src/testing/importCrewlounge/processflights.h"
  8. #include "src/functions/areadcsv.h"
  9. namespace ImportCrewlounge
  10. {
  11. void exec(const QString &csv_file_path)
  12. {
  13. // Inhibit HomeWindow Updating
  14. QSignalBlocker blocker(aDB);
  15. // Prepare database and set up exclusive transaction for mass commit
  16. QSqlQuery q;
  17. q.prepare(QStringLiteral("BEGIN EXCLUSIVE TRANSACTION"));
  18. q.exec();
  19. // Read from CSV and remove first line (headers)
  20. auto raw_csv_data = aReadCsvAsRows(csv_file_path);
  21. raw_csv_data.removeFirst();
  22. // Process Pilots
  23. auto proc_pilots = ProcessPilots(raw_csv_data);
  24. proc_pilots.init();
  25. const auto p_maps = proc_pilots.getProcessedPilotMaps();
  26. for (const auto & pilot_data : p_maps) {
  27. OPL::PilotEntry pe(pilot_data.value(OPL::Db::PILOTS_ROWID).toInt(), pilot_data);
  28. aDB->commit(pe);
  29. }
  30. // Process Tails
  31. auto proc_tails = ProcessAircraft(raw_csv_data);
  32. proc_tails.init();
  33. const auto t_maps = proc_tails.getProcessedTailMaps();
  34. for (const auto& tail_data : t_maps) {
  35. OPL::TailEntry te(tail_data.value(OPL::Db::PILOTS_ROWID).toInt(), tail_data);
  36. aDB->commit(te);
  37. }
  38. auto proc_flights = ProcessFlights(raw_csv_data,
  39. proc_pilots.getProcessedPilotsIds(),
  40. proc_tails.getProcessedTailIds());
  41. proc_flights.init();
  42. const auto flights = proc_flights.getProcessedFlights();
  43. for (const auto &flight_data : flights) {
  44. OPL::FlightEntry fe(flight_data);
  45. aDB->commit(fe);
  46. }
  47. // Commit the exclusive transaction
  48. q.prepare(QStringLiteral("COMMIT"));
  49. q.exec();
  50. // destroy blocker
  51. blocker.unblock();
  52. emit aDB->dataBaseUpdated();
  53. }
  54. }// namespace ImportCrewLongue