dbflight.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*
  2. *openPilot Log - A FOSS Pilot Logbook Application
  3. *Copyright (C) 2020 Felix Turowsky
  4. *
  5. *This program is free software: you can redistribute it and/or modify
  6. *it under the terms of the GNU General Public License as published by
  7. *the Free Software Foundation, either version 3 of the License, or
  8. *(at your option) any later version.
  9. *
  10. *This program is distributed in the hope that it will be useful,
  11. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. *GNU General Public License for more details.
  14. *
  15. *You should have received a copy of the GNU General Public License
  16. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. #include "dbflight.h"
  19. #include "dbpilots.h"
  20. #include "dbaircraft.h"
  21. /*!
  22. * \brief dbFlight::verifyInput Checks the validity of the data in a
  23. * flight object against the database and sets its verified flag
  24. * accordingly. Information about partial validity can be obtained
  25. * by querying the flight objects invalidItems member.
  26. */
  27. flight dbFlight::verifyInput(flight object)
  28. {
  29. if (object.doft.isValid()){
  30. object.invalidItems.removeOne("doft");
  31. qDebug() << "doft verified.";
  32. }else{
  33. qWarning() << "Invalid entry: doft";
  34. }
  35. if(dbAirport::checkICAOValid(object.dept)){
  36. object.invalidItems.removeOne("dept");
  37. qDebug() << "dept verified.";
  38. }else{
  39. qWarning() << "Invalid entry: dept";
  40. }
  41. if(dbAirport::checkICAOValid(object.dest)){
  42. object.invalidItems.removeOne("dest");
  43. qDebug() << "dest verified.";
  44. }else{
  45. qWarning() << "Invalid entry: dest";
  46. }
  47. if(object.tofb.isValid()){
  48. object.invalidItems.removeOne("tofb");
  49. qDebug() << "tofb verified.";
  50. }else{
  51. qWarning() << "Invalid entry: tofb";
  52. }
  53. if(object.tonb.isValid()){
  54. object.invalidItems.removeOne("tonb");
  55. qDebug() << "tonb verified.";
  56. }else{
  57. qWarning() << "Invalid entry: tonb";
  58. }
  59. if(dbPilots::verifyPilotExists(object.pic.split(QLatin1Char(',')))){
  60. object.invalidItems.removeOne("pic");
  61. qDebug() << "pic verified.";
  62. }else{
  63. qWarning() << "Invalid entry: pic";
  64. }
  65. //acft
  66. QTime tblkcheck = calc::blocktime(object.tofb, object.tonb);
  67. if(object.tblk.isValid() && !object.tblk.isNull() && tblkcheck == object.tblk){
  68. object.invalidItems.removeOne("tblk");
  69. qDebug() << "tblk verified.";
  70. }else{
  71. qWarning() << "Invalid entry: tblk";
  72. }
  73. if(!dbAircraft::retreiveTailId(object.acft).isEmpty()){
  74. object.invalidItems.removeOne("acft");
  75. qDebug() << "acft verified.";
  76. }else{
  77. qWarning() << "Invalid entry: acft";
  78. }
  79. if(object.invalidItems.isEmpty()){
  80. object.isValid = true;
  81. qDebug() << "All checks passed. Object is now verified.";
  82. }else{
  83. qDebug() << "Not all checks have been passed.";
  84. qDebug() << "Invalid items: " << object.invalidItems;
  85. }
  86. return object;
  87. /* To Do: Checks on internal logic, i.e. times <= tblk,
  88. * restrict what strings are allowed in remarks etc.
  89. object.pic = details[7];
  90. object.acft = details[8];
  91. object.tblk = QTime::fromString(details[9],"hh:mm");*/
  92. /*object.tSPSE = QTime::fromString(details[10],"hh:mm");
  93. object.tSPME = QTime::fromString(details[11],"hh:mm");
  94. object.tMP = QTime::fromString(details[12],"hh:mm");
  95. object.tNIGHT = QTime::fromString(details[13],"hh:mm");
  96. object.tIFR = QTime::fromString(details[14],"hh:mm");
  97. object.tPIC = QTime::fromString(details[15],"hh:mm");
  98. object.tPICUS = QTime::fromString(details[16],"hh:mm");
  99. object.tSIC = QTime::fromString(details[17],"hh:mm");
  100. object.tDUAL = QTime::fromString(details[18],"hh:mm");
  101. object.tFI = QTime::fromString(details[19],"hh:mm");
  102. object.tSIM = QTime::fromString(details[20],"hh:mm");
  103. object.pilotFlying = details[21].toInt();
  104. object.toDay = details[22].toInt();
  105. object.toNight = details[23].toInt();
  106. object.ldgDay = details[24].toInt();
  107. object.ldgNight = details[25].toInt();
  108. object.autoland = details[26].toInt();
  109. object.secondPilot = details[27];
  110. object.thirdPilot = details[28];
  111. object.approachType = details[29];
  112. object.flightNumber = details[30];
  113. object.remarks = details[31];*/
  114. }
  115. /*!
  116. * \brief dbFlight::retreiveFlight Runs a select statement on the database
  117. * for a given flight_id and returns a flight object containing the details
  118. * for the given ID. If no matching ID is found, an empty flight object is
  119. * returned
  120. * \param flight_id Primary Key of flight database
  121. */
  122. flight dbFlight::retreiveFlight(QString flight_id)
  123. {
  124. QSqlQuery query;
  125. query.prepare("SELECT * FROM flights WHERE id = ?");
  126. query.addBindValue(flight_id);
  127. query.exec();
  128. if(query.first());
  129. else
  130. {
  131. qWarning() << __func__ << "No Flight with this ID found";
  132. return flight(); //return empty
  133. }
  134. flight object;
  135. object.id = query.value(0).toInt();
  136. object.doft = QDate::fromString(query.value(1).toString(),Qt::ISODate);
  137. object.dept = query.value(2).toString();
  138. object.dest = query.value(3).toString();
  139. object.tofb = QTime::fromString(
  140. calc::minutes_to_string(
  141. query.value(4).toString()),"hh:mm");
  142. object.tonb = QTime::fromString(
  143. calc::minutes_to_string(
  144. query.value(5).toString()),"hh:mm");
  145. object.pic = dbPilots::retreivePilotNameFromID(
  146. query.value(6).toString());
  147. object.acft = dbAircraft::retreiveRegistration(
  148. query.value(7).toString());
  149. object.tblk = QTime::fromString(
  150. calc::minutes_to_string(
  151. query.value(8).toString()),"hh:mm");
  152. object.tSPSE = QTime::fromString(
  153. calc::minutes_to_string(
  154. query.value(9).toString()),"hh:mm");
  155. object.tSPME = QTime::fromString(
  156. calc::minutes_to_string(
  157. query.value(10).toString()),"hh:mm");
  158. object.tMP = QTime::fromString(
  159. calc::minutes_to_string(
  160. query.value(11).toString()),"hh:mm");
  161. object.tNIGHT = QTime::fromString(
  162. calc::minutes_to_string(
  163. query.value(12).toString()),"hh:mm");
  164. object.tIFR = QTime::fromString(
  165. calc::minutes_to_string(
  166. query.value(13).toString()),"hh:mm");
  167. object.tPIC = QTime::fromString(
  168. calc::minutes_to_string(
  169. query.value(14).toString()),"hh:mm");
  170. object.tPICUS = QTime::fromString(
  171. calc::minutes_to_string(
  172. query.value(15).toString()),"hh:mm");
  173. object.tSIC = QTime::fromString(
  174. calc::minutes_to_string(
  175. query.value(16).toString()),"hh:mm");
  176. object.tDUAL = QTime::fromString(
  177. calc::minutes_to_string(
  178. query.value(17).toString()),"hh:mm");
  179. object.tFI = QTime::fromString(
  180. calc::minutes_to_string(
  181. query.value(18).toString()),"hh:mm");
  182. object.tSIM = QTime::fromString(
  183. calc::minutes_to_string(
  184. query.value(19).toString()),"hh:mm");
  185. object.pilotFlying = query.value(20).toInt();
  186. object.toDay = query.value(21).toInt();
  187. object.toNight = query.value(22).toInt();
  188. object.ldgDay = query.value(23).toInt();
  189. object.ldgNight = query.value(24).toInt();
  190. object.autoland = query.value(25).toInt();
  191. object.secondPilot = query.value(26).toInt();
  192. object.thirdPilot = query.value(27).toInt();
  193. object.approachType = query.value(28).toString();
  194. object.flightNumber = query.value(29).toString();
  195. object.remarks = query.value(30).toString();
  196. //Database entries are assumed to be valid
  197. object.isValid = true;
  198. object.invalidItems.clear();
  199. return object;
  200. }
  201. bool dbFlight::commitFlight(flight object)
  202. {
  203. //To Do
  204. qDebug() << object;
  205. return false;
  206. }
  207. /*!
  208. * \brief SelectFlightById Retreives a single flight from the database.
  209. * \param flight_id Primary Key of flights database
  210. * \return Flight details of selected flight.
  211. */
  212. QVector<QString> dbFlight::selectFlightById(QString flight_id)
  213. {
  214. QSqlQuery query;
  215. query.prepare("SELECT * FROM flights WHERE id = ?");
  216. query.addBindValue(flight_id);
  217. query.exec();
  218. if(query.first());
  219. else
  220. {
  221. qDebug() << "db::SelectFlightById - No Flight with this ID found";
  222. QVector<QString> flight; //return empty
  223. return flight;
  224. }
  225. QVector<QString> flight;
  226. flight.append(query.value(0).toString());
  227. flight.append(query.value(1).toString());
  228. flight.append(query.value(2).toString());
  229. flight.append(query.value(3).toString());
  230. flight.append(query.value(4).toString());
  231. flight.append(query.value(5).toString());
  232. flight.append(query.value(6).toString());
  233. flight.append(query.value(7).toString());
  234. flight.append(query.value(8).toString());
  235. qDebug() << "db::SelectFlightById - retreived flight: " << flight;
  236. return flight;
  237. }
  238. /*!
  239. * \brief deleteFlightById Deletes a Flight from the database.
  240. * Entries in the basic flights table as well as in the extras table are deleted.
  241. * \param flight_id The primary key of the entry in the database
  242. * \return True if no errors, otherwise false
  243. */
  244. bool dbFlight::deleteFlightById(QString flight_id)
  245. {
  246. QSqlQuery query;
  247. query.prepare("DELETE FROM flights WHERE id = ?");
  248. query.addBindValue(flight_id);
  249. query.exec();
  250. QString error = query.lastError().text();
  251. QSqlQuery query2;
  252. query2.prepare("DELETE FROM extras WHERE extras_id = ?");
  253. query2.addBindValue(flight_id);
  254. query2.exec();
  255. QString error2 = query2.lastError().text();
  256. qDebug() << "db::deleteFlightById: Removing flight with ID#: " << flight_id;
  257. if(error.length() > 0 || error2.length() > 0)
  258. {
  259. qWarning() << "db::deleteFlightsById: Errors have occured: " << error << " " << error2;
  260. return false;
  261. }else
  262. {
  263. return true;
  264. }
  265. }
  266. /*!
  267. * \brief CreateFlightVectorFromInput Converts input from NewFlight Window into database format
  268. * \param doft Date of flight
  269. * \param dept Place of Departure
  270. * \param tofb Time Off Blocks (UTC)
  271. * \param dest Place of Destination
  272. * \param tonb Time On Blocks (UTC)
  273. * \param tblk Total Block Time
  274. * \param pic Pilot in command
  275. * \param acft Aircraft
  276. * \return Vector of values ready for committing
  277. */
  278. QVector<QString> dbFlight::createFlightVectorFromInput(QString doft, QString dept, QTime tofb, QString dest,
  279. QTime tonb, QTime tblk, QString pic, QString acft)
  280. {
  281. QVector<QString> flight;
  282. flight.insert(0, ""); // ID, created as primary key during commit
  283. flight.insert(1, doft);
  284. flight.insert(2, dept);
  285. flight.insert(3, QString::number(calc::time_to_minutes(tofb)));
  286. flight.insert(4, dest);
  287. flight.insert(5, QString::number(calc::time_to_minutes(tonb)));
  288. flight.insert(6, QString::number(calc::time_to_minutes(tblk)));
  289. flight.insert(7, pic); // lookup and matching tbd
  290. flight.insert(8, acft);// lookup and matching tbd
  291. //qDebug() << flight;
  292. return flight;
  293. }
  294. /*!
  295. * \brief CommitToScratchpad Commits the inputs of the NewFlight window to a scratchpad
  296. * to make them available for restoring entries when the input fields are being reloaded.
  297. * \param flight The input data, which was not accepted for commiting to the flights table.
  298. */
  299. void dbFlight::commitToScratchpad(QVector<QString> flight)// to store input mask
  300. {
  301. //qDebug() << "Saving invalid flight to scratchpad";
  302. QSqlQuery query;
  303. query.prepare("INSERT INTO scratchpad (doft, dept, tofb, dest, tonb, tblk, pic, acft) "
  304. "VALUES (:doft, :dept, :tofb, :dest, :tonb, :tblk, :pic, :acft)");
  305. //flight[0] is primary key, not required for commit
  306. query.bindValue(":doft", flight[1]); //string
  307. query.bindValue(":dept", flight[2]);
  308. query.bindValue(":tofb", flight[3].toInt()); //int
  309. query.bindValue(":dest", flight[4]);
  310. query.bindValue(":tonb", flight[5].toInt());
  311. query.bindValue(":tblk", flight[6].toInt());
  312. query.bindValue(":pic", flight[7].toInt());
  313. query.bindValue(":acft", flight[8].toInt());
  314. query.exec();
  315. qDebug() << query.lastError().text();
  316. }
  317. /*!
  318. * \brief RetreiveScratchpad Selects data from scratchpad
  319. * \return Vector of data contained in scratchpad
  320. */
  321. QVector<QString> dbFlight::retreiveScratchpad()
  322. {
  323. //qDebug() << "Retreiving invalid flight from scratchpad";
  324. QSqlQuery query;
  325. query.prepare("SELECT * FROM scratchpad");
  326. query.exec();
  327. if(query.first());
  328. else
  329. {
  330. //qDebug() << ("scratchpad empty");
  331. QVector<QString> flight; //return empty
  332. return flight;
  333. }
  334. query.previous();
  335. QVector<QString> flight;
  336. while (query.next()) {
  337. flight.append(query.value(0).toString());
  338. flight.append(query.value(1).toString());
  339. flight.append(query.value(2).toString());
  340. flight.append(calc::minutes_to_string((query.value(3).toString())));
  341. flight.append(query.value(4).toString());
  342. flight.append(calc::minutes_to_string((query.value(5).toString())));
  343. flight.append(calc::minutes_to_string((query.value(6).toString())));
  344. flight.append(query.value(7).toString());
  345. flight.append(query.value(8).toString());
  346. }
  347. clearScratchpad();
  348. return flight;
  349. }
  350. /*!
  351. * \brief CheckScratchpad Verifies if the scratchpad contains data
  352. * \return true if scratchpad contains data
  353. */
  354. bool dbFlight::checkScratchpad() // see if scratchpad is empty
  355. {
  356. //qDebug() << "Checking if scratchpad contains data";
  357. QSqlQuery query;
  358. query.prepare("SELECT * FROM scratchpad");
  359. query.exec();
  360. if(query.first())
  361. {
  362. //qDebug() << "Scratchpad contains data";
  363. return 1;
  364. }
  365. else
  366. {
  367. //qDebug() << ("Scratchpad contains NO data");
  368. return 0;
  369. }
  370. }
  371. /*!
  372. * \brief ClearScratchpad Deletes data contained in the scratchpad
  373. */
  374. void dbFlight::clearScratchpad()
  375. {
  376. qDebug() << "Deleting scratchpad";
  377. QSqlQuery query;
  378. query.prepare("DELETE FROM scratchpad;");
  379. query.exec();
  380. }