mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-23 23:45:35 +08:00
refs #720, moved data consolidation with DB data into own class in BlackCore
(in blackcore I can access the readers)
This commit is contained in:
committed by
Mathew Sutcliffe
parent
f9922353c4
commit
d75b105ee3
135
src/blackcore/db/databaseutils.cpp
Normal file
135
src/blackcore/db/databaseutils.cpp
Normal file
@@ -0,0 +1,135 @@
|
||||
/* Copyright (C) 2016
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
|
||||
* including this file, may be copied, modified, propagated, or distributed except according to the terms
|
||||
* contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "blackcore/application.h"
|
||||
#include "databaseutils.h"
|
||||
#include "blackcore/webdataservices.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Aviation;
|
||||
using namespace BlackMisc::Simulation;
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
namespace Db
|
||||
{
|
||||
CAircraftModel CDatabaseUtils::consolidateModelWithDbData(const CAircraftModel &model, bool force)
|
||||
{
|
||||
return CDatabaseUtils::consolidateModelWithDbData(model, force, nullptr);
|
||||
}
|
||||
|
||||
CAircraftModel CDatabaseUtils::consolidateModelWithDbData(const CAircraftModel &model, bool force, bool *modified)
|
||||
{
|
||||
if (modified) { *modified = false; }
|
||||
if (!model.hasModelString()) { return model; }
|
||||
if (!force && model.hasValidDbKey()) { return model; }
|
||||
CAircraftModel dbModel(sApp->getWebDataServices()->getModelForModelString(model.getModelString()));
|
||||
if (dbModel.hasValidDbKey())
|
||||
{
|
||||
if (modified) { *modified = true; }
|
||||
return dbModel;
|
||||
}
|
||||
|
||||
// we try to best update by DB data here
|
||||
// we have no(!) DB model, so we update each of it subobjects
|
||||
CAircraftModel consolidatedModel(model); // copy over
|
||||
if (!consolidatedModel.getLivery().hasValidDbKey())
|
||||
{
|
||||
const CLivery dbLivery(sApp->getWebDataServices()->smartLiverySelector(consolidatedModel.getLivery()));
|
||||
if (dbLivery.hasValidDbKey())
|
||||
{
|
||||
if (modified) { *modified = true; }
|
||||
consolidatedModel.setLivery(dbLivery);
|
||||
}
|
||||
}
|
||||
if (!consolidatedModel.getAircraftIcaoCode().hasValidDbKey() && consolidatedModel.hasAircraftDesignator())
|
||||
{
|
||||
// try to find DB aircraft ICAO here
|
||||
const CAircraftIcaoCode dbIcao(sApp->getWebDataServices()->smartAircraftIcaoSelector(consolidatedModel.getAircraftIcaoCode()));
|
||||
if (dbIcao.hasValidDbKey())
|
||||
{
|
||||
if (modified) { *modified = true; }
|
||||
consolidatedModel.setAircraftIcaoCode(dbIcao);
|
||||
}
|
||||
}
|
||||
|
||||
const CDistributor dbDistributor(sApp->getWebDataServices()->getDistributors().smartDistributorSelector(model.getDistributor(), model));
|
||||
if (dbDistributor.isLoadedFromDb())
|
||||
{
|
||||
if (modified) { *modified = true; }
|
||||
consolidatedModel.setDistributor(dbDistributor);
|
||||
}
|
||||
return consolidatedModel;
|
||||
}
|
||||
|
||||
int CDatabaseUtils::consolidateModelsWithDbData(CAircraftModelList &models, bool force)
|
||||
{
|
||||
QTime timer;
|
||||
timer.start();
|
||||
int c = 0;
|
||||
if (models.isEmpty()) { return c; }
|
||||
for (CAircraftModel &model : models)
|
||||
{
|
||||
if (!force && model.hasValidDbKey()) { continue; }
|
||||
bool modified = false;
|
||||
model = CDatabaseUtils::consolidateModelWithDbData(model, force, &modified);
|
||||
if (modified || model.hasValidDbKey()) { c++; }
|
||||
}
|
||||
CLogMessage().debug() << "Consolidated " << models.size() << " in " << timer.elapsed() << "ms";
|
||||
return c;
|
||||
}
|
||||
|
||||
int CDatabaseUtils::consolidateModelsWithDbModelAndDistributor(CAircraftModelList &models, bool force)
|
||||
{
|
||||
int c = 0;
|
||||
if (models.isEmpty()) { return c; }
|
||||
for (CAircraftModel &model : models)
|
||||
{
|
||||
if (!force && model.hasValidDbKey()) { continue; }
|
||||
const CAircraftModel dbModel(sApp->getWebDataServices()->getModelForModelString(model.getModelString()));
|
||||
if (dbModel.hasValidDbKey())
|
||||
{
|
||||
model = dbModel;
|
||||
c++;
|
||||
continue;
|
||||
}
|
||||
const CDistributor distributor = sApp->getWebDataServices()->smartDistributorSelector(model.getDistributor(), model);
|
||||
if (distributor.isLoadedFromDb())
|
||||
{
|
||||
model.setDistributor(distributor);
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
CAircraftModel CDatabaseUtils::consolidateModelWithDbDistributor(const CAircraftModel &model, bool force)
|
||||
{
|
||||
if (!force && model.getDistributor().isLoadedFromDb()) { return model; }
|
||||
const CDistributor distributor = sApp->getWebDataServices()->smartDistributorSelector(model.getDistributor(), model);
|
||||
if (!distributor.isLoadedFromDb()) { return model; }
|
||||
CAircraftModel newModel(model);
|
||||
newModel.setDistributor(distributor);
|
||||
return newModel;
|
||||
}
|
||||
|
||||
int CDatabaseUtils::consolidateModelsWithDbDistributor(CAircraftModelList &models, bool force)
|
||||
{
|
||||
int c = 0;
|
||||
if (models.isEmpty()) { return c; }
|
||||
for (CAircraftModel &model : models)
|
||||
{
|
||||
if (model.hasValidDbKey() || model.getDistributor().hasValidDbKey()) { continue; }
|
||||
model = CDatabaseUtils::consolidateModelWithDbDistributor(model, force);
|
||||
if (model.getDistributor().hasValidDbKey()) { c++; }
|
||||
}
|
||||
return c;
|
||||
}
|
||||
} // ns
|
||||
} // ns
|
||||
Reference in New Issue
Block a user