File indexing completed on 2024-12-15 03:45:00
0001 /* 0002 SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: MIT 0005 */ 0006 0007 #include "productimportjob.h" 0008 0009 #include <rest/restapi.h> 0010 #include <rest/restclient.h> 0011 #include <core/sample.h> 0012 #include <core/survey.h> 0013 0014 #include <QDir> 0015 #include <QNetworkReply> 0016 0017 using namespace KUserFeedback::Console; 0018 0019 ProductImportJob::ProductImportJob(const QString& source, RESTClient* restClient, QObject* parent) 0020 : Job(parent) 0021 , m_source(source) 0022 , m_restClient(restClient) 0023 { 0024 Q_ASSERT(m_restClient->isConnected()); 0025 doImportSchema(); 0026 } 0027 0028 ProductImportJob::~ProductImportJob() = default; 0029 0030 void ProductImportJob::doImportSchema() 0031 { 0032 QDir source(m_source); 0033 Q_ASSERT(source.exists()); 0034 0035 QFile f(source.absoluteFilePath(source.dirName() + QLatin1String(".schema"))); 0036 if (!f.open(QFile::ReadOnly)) { 0037 emitError(tr("Unable to open file: %1").arg(f.errorString())); 0038 return; 0039 } 0040 0041 const auto products = Product::fromJson(f.readAll()); 0042 if (products.size() != 1) { 0043 emitError(tr("Invalid product schema file.")); 0044 return; 0045 } 0046 m_product = products.at(0); 0047 0048 auto reply = RESTApi::createProduct(m_restClient, m_product); 0049 connect(reply, &QNetworkReply::finished, this, [this, reply]() { 0050 reply->deleteLater(); 0051 if (reply->error() != QNetworkReply::NoError) { 0052 deleteLater(); 0053 return; 0054 } 0055 doImportSurveys(); 0056 }); 0057 } 0058 0059 void ProductImportJob::doImportSurveys() 0060 { 0061 QDir source(m_source); 0062 QFile f(source.absoluteFilePath(source.dirName() + QLatin1String(".surveys"))); 0063 if (!f.open(QFile::ReadOnly)) { 0064 doImportData(); 0065 return; 0066 } 0067 0068 const auto surveys = Survey::fromJson(f.readAll()); 0069 if (surveys.isEmpty()) { 0070 doImportData(); 0071 return; 0072 } 0073 0074 for (const auto &s : surveys) { 0075 ++m_jobCount; 0076 auto reply = RESTApi::createSurvey(m_restClient, m_product, s); 0077 connect(reply, &QNetworkReply::finished, this, [this, reply]() { 0078 reply->deleteLater(); 0079 --m_jobCount; 0080 if (reply->error() != QNetworkReply::NoError) { 0081 deleteLater(); 0082 return; 0083 } 0084 if (m_jobCount == 0) 0085 doImportData(); 0086 }); 0087 } 0088 } 0089 0090 void ProductImportJob::doImportData() 0091 { 0092 QDir source(m_source); 0093 QFile f(source.absoluteFilePath(source.dirName() + QLatin1String(".data"))); 0094 if (!f.open(QFile::ReadOnly)) { 0095 emitFinished(); 0096 return; 0097 } 0098 0099 const auto samples = Sample::fromJson(f.readAll(), m_product); 0100 if (samples.isEmpty()) { 0101 emitFinished(); 0102 return; 0103 } 0104 0105 auto reply = RESTApi::addSamples(m_restClient, m_product, samples); 0106 connect(reply, &QNetworkReply::finished, this, [this, reply]() { 0107 reply->deleteLater(); 0108 if (reply->error() == QNetworkReply::NoError) 0109 emitFinished(); 0110 }); 0111 } 0112 0113 #include "moc_productimportjob.cpp"