| 
					
				 | 
			
			
				@@ -1,14 +1,32 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+/* 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *openPilotLog - A FOSS Pilot Logbook Application 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *Copyright (C) 2020-2021 Felix Turowsky 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *This program is free software: you can redistribute it and/or modify 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *it under the terms of the GNU General Public License as published by 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *the Free Software Foundation, either version 3 of the License, or 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *(at your option) any later version. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *This program is distributed in the hope that it will be useful, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *but WITHOUT ANY WARRANTY; without even the implied warranty of 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *GNU General Public License for more details. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ * 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *You should have received a copy of the GNU General Public License 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ *along with this program.  If not, see <https://www.gnu.org/licenses/>. 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 #include "ajson.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+#include "src/database/adatabasesetup.h" 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+const QList<QPair<QLatin1String, ADatabaseTable>> AJson::tables { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    qMakePair(Opl::Db::TABLE_TAILS, ADatabaseTable::tails), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    qMakePair(Opl::Db::TABLE_PILOTS, ADatabaseTable::pilots), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    qMakePair(Opl::Db::TABLE_CURRENCIES, ADatabaseTable::currencies), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    qMakePair(Opl::Db::TABLE_FLIGHTS, ADatabaseTable::flights), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+}; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void AJson::exportDatabase() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    const QList<QPair<QLatin1String, ADatabaseTables>> tables { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        qMakePair(Opl::Db::TABLE_TAILS, ADatabaseTables::tails), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        qMakePair(Opl::Db::TABLE_PILOTS, ADatabaseTables::pilots), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        qMakePair(Opl::Db::TABLE_CURRENCIES, ADatabaseTables::currencies), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        qMakePair(Opl::Db::TABLE_FLIGHTS, ADatabaseTables::flights), 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    }; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     for (const auto &pair : tables){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         QJsonArray arr; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         const auto rows = aDB->getTable(pair.second); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -19,6 +37,24 @@ void AJson::exportDatabase() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+void AJson::importDatabase() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    TODO << "Another function should do some checking here if data exists etc..."; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // clear tables 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    QSqlQuery q; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    // make sure flights is cleared first due to foreign key contstraints 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    q.prepare(QLatin1String("DELETE FROM FLIGHTS")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    q.exec(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    for (const auto & pair : tables) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        q.prepare(QLatin1String("DELETE FROM ") + pair.first); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        q.exec(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        const auto doc = readJson(AStandardPaths::asChildOfDir(AStandardPaths::JSON, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                                                               pair.first + QLatin1String(".json"))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ADataBaseSetup::commitDataJson(doc.array(), pair.first); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 void AJson::writeJson(const QJsonDocument &doc, const QString &file_name) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     QFile pilots_out(AStandardPaths::asChildOfDir(AStandardPaths::JSON,file_name)); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -27,7 +63,13 @@ void AJson::writeJson(const QJsonDocument &doc, const QString &file_name) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     pilots_out.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-void AJson::readJson(const QString &file_name) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+QJsonDocument AJson::readJson(const QString &file_path) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    TODO << "implement..." << file_name; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    QFile file(file_path); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    file.open(QIODevice::ReadOnly | QIODevice::Text); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    QString raw = file.readAll(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    file.close(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    QJsonDocument doc = QJsonDocument::fromJson(raw.toUtf8()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    return doc; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |