dbflight.cpp 14 KB

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