mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-15 09:15:34 +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
|
||||||
49
src/blackcore/db/databaseutils.h
Normal file
49
src/blackcore/db/databaseutils.h
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//! \file
|
||||||
|
|
||||||
|
#ifndef BLACKCORE_DB_DATABASEUTILS_H
|
||||||
|
#define BLACKCORE_DB_DATABASEUTILS_H
|
||||||
|
|
||||||
|
#include "blackcore/blackcoreexport.h"
|
||||||
|
#include "blackmisc/simulation/aircraftmodel.h"
|
||||||
|
|
||||||
|
namespace BlackCore
|
||||||
|
{
|
||||||
|
namespace Db
|
||||||
|
{
|
||||||
|
//! Read information about data from Database
|
||||||
|
class BLACKCORE_EXPORT CDatabaseUtils
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//! No constructor
|
||||||
|
CDatabaseUtils() = delete;
|
||||||
|
|
||||||
|
//! Consolidate model data with DB data
|
||||||
|
static BlackMisc::Simulation::CAircraftModel consolidateModelWithDbData(const BlackMisc::Simulation::CAircraftModel &model, bool force);
|
||||||
|
|
||||||
|
//! Consolidate model data with DB data
|
||||||
|
static BlackMisc::Simulation::CAircraftModel consolidateModelWithDbData(const BlackMisc::Simulation::CAircraftModel &model, bool force, bool *modified);
|
||||||
|
|
||||||
|
//! Consolidate models with DB data
|
||||||
|
static int consolidateModelsWithDbData(BlackMisc::Simulation::CAircraftModelList &models, bool force);
|
||||||
|
|
||||||
|
//! Consolidate models with DB data (simpler/faster version of CAircraftModel::consolidateModelWithDbData)
|
||||||
|
static int consolidateModelsWithDbModelAndDistributor(BlackMisc::Simulation::CAircraftModelList &models, bool force);
|
||||||
|
|
||||||
|
//! Consolidate model data with DB distributor
|
||||||
|
static BlackMisc::Simulation::CAircraftModel consolidateModelWithDbDistributor(const BlackMisc::Simulation::CAircraftModel &model, bool force);
|
||||||
|
|
||||||
|
//! Consolidate model data with DB distributors
|
||||||
|
static int consolidateModelsWithDbDistributor(BlackMisc::Simulation::CAircraftModelList &models, bool force);
|
||||||
|
};
|
||||||
|
} // ns
|
||||||
|
} // ns
|
||||||
|
#endif // guard
|
||||||
@@ -14,25 +14,6 @@ namespace BlackMisc
|
|||||||
{
|
{
|
||||||
namespace Simulation
|
namespace Simulation
|
||||||
{
|
{
|
||||||
int CAircraftModelUtilities::mergeWithDbData(CAircraftModelList &modelToBeModified, const CAircraftModelList &dbModels, bool force)
|
|
||||||
{
|
|
||||||
if (dbModels.isEmpty() || modelToBeModified.isEmpty()) { return 0; }
|
|
||||||
int c = 0;
|
|
||||||
for (CAircraftModel &simModel : modelToBeModified)
|
|
||||||
{
|
|
||||||
if (!force && simModel.hasValidDbKey()) { continue; } // already done
|
|
||||||
CAircraftModel dbModel(dbModels.findFirstByModelStringOrDefault(simModel.getModelString()));
|
|
||||||
if (!dbModel.hasValidDbKey())
|
|
||||||
{
|
|
||||||
continue; // not found
|
|
||||||
}
|
|
||||||
dbModel.updateMissingParts(simModel, false);
|
|
||||||
simModel = dbModel;
|
|
||||||
c++;
|
|
||||||
}
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CAircraftModelUtilities::mergeWithVPilotData(CAircraftModelList &modelToBeModified, const CAircraftModelList &vPilotModels, bool force)
|
bool CAircraftModelUtilities::mergeWithVPilotData(CAircraftModelList &modelToBeModified, const CAircraftModelList &vPilotModels, bool force)
|
||||||
{
|
{
|
||||||
if (vPilotModels.isEmpty() || modelToBeModified.isEmpty()) { return false; }
|
if (vPilotModels.isEmpty() || modelToBeModified.isEmpty()) { return false; }
|
||||||
|
|||||||
@@ -26,9 +26,6 @@ namespace BlackMisc
|
|||||||
//! No constructor
|
//! No constructor
|
||||||
CAircraftModelUtilities() = delete;
|
CAircraftModelUtilities() = delete;
|
||||||
|
|
||||||
//! Merge with DB data if possible
|
|
||||||
static int mergeWithDbData(BlackMisc::Simulation::CAircraftModelList &modelToBeModified, const BlackMisc::Simulation::CAircraftModelList &dbModels, bool force = false);
|
|
||||||
|
|
||||||
//! Merge with vPilot data if possible
|
//! Merge with vPilot data if possible
|
||||||
static bool mergeWithVPilotData(BlackMisc::Simulation::CAircraftModelList &modelToBeModified, const BlackMisc::Simulation::CAircraftModelList &vPilotModels, bool force = false);
|
static bool mergeWithVPilotData(BlackMisc::Simulation::CAircraftModelList &modelToBeModified, const BlackMisc::Simulation::CAircraftModelList &vPilotModels, bool force = false);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user