Ref T472, aircraft category value object and list

This commit is contained in:
Klaus Basan
2019-01-14 20:52:50 +01:00
committed by Mat Sutcliffe
parent e8e516cc3d
commit 257c8ea9f9
11 changed files with 427 additions and 2 deletions

View File

@@ -0,0 +1,160 @@
/* Copyright (C) 2019
* 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 "blackmisc/aviation/aircraftcategory.h"
#include "blackmisc/db/datastoreutility.h"
#include "blackmisc/comparefunctions.h"
#include "blackmisc/logcategorylist.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/statusmessage.h"
#include "blackmisc/variant.h"
#include <QChar>
#include <QJsonValue>
#include <QMultiMap>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QStringBuilder>
#include <Qt>
#include <QtGlobal>
using namespace BlackMisc;
using namespace BlackMisc::Db;
using namespace BlackMisc::Simulation;
namespace BlackMisc
{
namespace Aviation
{
CAircraftCategory::CAircraftCategory(const QString &name, const QString &description, const QString &path, bool assignable) :
m_name(name), m_description(description), m_path(path), m_assignable(assignable)
{ }
QString CAircraftCategory::getNameDbKey() const
{
return (this->isLoadedFromDb()) ?
this->getName() % u' ' % this->getDbKeyAsStringInParentheses() :
this->getName();
}
QString CAircraftCategory::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
return QStringLiteral("%1 %2").arg(this->getNameDbKey(), this->getDescription());
}
CStatusMessageList CAircraftCategory::validate() const
{
static const CLogCategoryList cats({ CLogCategory("swift.blackmisc.aircraftcategory"), CLogCategory::validation()});
CStatusMessageList msg;
return msg;
}
bool CAircraftCategory::isNull() const
{
return m_name.isEmpty() && m_description.isEmpty();
}
const CAircraftCategory &CAircraftCategory::null()
{
static const CAircraftCategory null;
return null;
}
bool CAircraftCategory::hasName() const
{
return !m_name.isEmpty();
}
bool CAircraftCategory::matchesName(const QString &name, Qt::CaseSensitivity cs) const
{
return stringCompare(name, this->getName(), cs);
}
void CAircraftCategory::setLevel(int l1, int l2, int l3)
{
m_level[0] = l1;
m_level[1] = l2;
m_level[2] = l3;
}
QString CAircraftCategory::getLevelString() const
{
return QStringLiteral("%1.%2.%3").arg(m_level[0]).arg(m_level[1]).arg(m_level[2]);
}
bool CAircraftCategory::matchesPath(const QString &path, Qt::CaseSensitivity cs)
{
return stringCompare(path, this->getPath(), cs);
}
CVariant CAircraftCategory::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{
if (index.isMyself()) { return CVariant::from(*this); }
if (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) { return IDatastoreObjectWithIntegerKey::propertyByIndex(index); }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexName: return CVariant::fromValue(m_name);
case IndexDescription: return CVariant::fromValue(m_description);
case IndexAssignable: return CVariant::fromValue(m_assignable);
case IndexPath: return CVariant::fromValue(m_path);
case IndexLevelString: return CVariant::fromValue(this->getLevelString());
default: return CValueObject::propertyByIndex(index);
}
}
void CAircraftCategory::setPropertyByIndex(const CPropertyIndex &index, const CVariant &variant)
{
if (index.isMyself()) { (*this) = variant.to<CAircraftCategory>(); return; }
if (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) { IDatastoreObjectWithIntegerKey::setPropertyByIndex(index, variant); return; }
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexName: this->setName(variant.value<QString>()); break;
case IndexDescription: this->setDescription(variant.value<QString>()); break;
case IndexAssignable: this->setAssignable(variant.value<bool>()); break;
default: CValueObject::setPropertyByIndex(index, variant); break;
}
}
int CAircraftCategory::comparePropertyByIndex(const CPropertyIndex &index, const CAircraftCategory &compareValue) const
{
if (index.isMyself()) { return m_path.compare(compareValue.getPath(), Qt::CaseInsensitive); }
if (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) { return IDatastoreObjectWithIntegerKey::comparePropertyByIndex(index, compareValue);}
const ColumnIndex i = index.frontCasted<ColumnIndex>();
switch (i)
{
case IndexName: return m_name.compare(compareValue.getName(), Qt::CaseInsensitive);
case IndexPath: return m_description.compare(compareValue.getPath(), Qt::CaseInsensitive);
case IndexDescription: return m_path.compare(compareValue.getDescription(), Qt::CaseInsensitive);
case IndexAssignable: return Compare::compare(this->isAssignable(), compareValue.isAssignable());
default: return CValueObject::comparePropertyByIndex(index, *this);
}
Q_ASSERT_X(false, Q_FUNC_INFO, "No comparison");
return 0;
}
CAircraftCategory CAircraftCategory::fromDatabaseJson(const QJsonObject &json, const QString &prefix)
{
const QString name(json.value(prefix % u"name").toString());
const QString description(json.value(prefix % u"description").toString());
const QString path(json.value(prefix % u"path").toString());
const bool assignable = CDatastoreUtility::dbBoolStringToBool(json.value(prefix % u"assignable").toString());
const int l1 = json.value(prefix % u"l1").toInt();
const int l2 = json.value(prefix % u"l2").toInt();
const int l3 = json.value(prefix % u"l3").toInt();
CAircraftCategory cat(name, description, path, assignable);
cat.setLevel(l1, l2, l3);
cat.setKeyVersionTimestampFromDatabaseJson(json, prefix);
return cat;
}
} // namespace
} // namespace

View File

@@ -0,0 +1,139 @@
/* Copyright (C) 2019
* 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 BLACKMISC_AVIATION_AIRCRAFTCATEGORY_H
#define BLACKMISC_AVIATION_AIRCRAFTCATEGORY_H
#include "blackmisc/db/datastore.h"
#include "blackmisc/blackmiscexport.h"
#include "blackmisc/metaclass.h"
#include "blackmisc/propertyindex.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/valueobject.h"
#include "blackmisc/variant.h"
#include <QJsonObject>
#include <QMetaType>
#include <QString>
#include <QStringList>
namespace BlackMisc
{
namespace Aviation
{
//! Value object for aircraft categories
class BLACKMISC_EXPORT CAircraftCategory :
public CValueObject<CAircraftCategory>,
public Db::IDatastoreObjectWithIntegerKey
{
public:
//! Properties by index
enum ColumnIndex
{
IndexName = BlackMisc::CPropertyIndex::GlobalIndexCAircraftCategory,
IndexDescription,
IndexLevelString,
IndexPath,
IndexAssignable
};
//! Default constructor.
CAircraftCategory() {}
//! Constructor.
CAircraftCategory(const QString &name, const QString &description, const QString &path, bool assignable);
//! Get name
const QString &getName() const { return m_name; }
//! Set name
void setName(const QString &name) { m_name = name.trimmed(); }
//! Designator and DB key
QString getNameDbKey() const;
//! Aircraft designator?
bool hasName() const;
//! Matching name?
bool matchesName(const QString &name, Qt::CaseSensitivity cs) const;
//! Description
const QString &getDescription() const { return m_description; }
//! Set description
void setDescription(const QString &description) { m_description = description.trimmed(); }
//! Path
const QString &getPath() const { return m_path; }
//! Level
void setLevel(int l1, int l2, int l3);
//! Level string
QString getLevelString() const;
//! Matching path?
bool matchesPath(const QString &path, Qt::CaseSensitivity cs);
//! Assignable?
bool isAssignable() const { return m_assignable; }
//! Mark/set assignable
void setAssignable(bool assignable) { m_assignable = assignable; }
//! \copydoc BlackMisc::Mixin::Index::propertyByIndex
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
//! \copydoc BlackMisc::Mixin::Index::setPropertyByIndex
void setPropertyByIndex(const BlackMisc::CPropertyIndex &index, const CVariant &variant);
//! \copydoc BlackMisc::Mixin::Index::comparePropertyByIndex
int comparePropertyByIndex(const CPropertyIndex &index, const CAircraftCategory &compareValue) const;
//! \copydoc BlackMisc::Mixin::String::toQString
QString convertToQString(bool i18n = false) const;
//! Validate data
BlackMisc::CStatusMessageList validate() const;
//! Null ICAO?
bool isNull() const;
//! NULL object
static const CAircraftCategory &null();
//! From our database JSON format
static CAircraftCategory fromDatabaseJson(const QJsonObject &json, const QString &prefix = QString());
private:
QString m_name; //!< name
QString m_description; //!< description
QString m_path; //!< path
bool m_assignable = true; //!< can assign to category?
int m_level[3] = { 0, 0, 0};
BLACK_METACLASS(
CAircraftCategory,
BLACK_METAMEMBER(dbKey),
BLACK_METAMEMBER(timestampMSecsSinceEpoch),
BLACK_METAMEMBER(name),
// BLACK_METAMEMBER(level),
BLACK_METAMEMBER(description),
BLACK_METAMEMBER(path),
BLACK_METAMEMBER(assignable)
);
};
} // namespace
} // namespace
Q_DECLARE_METATYPE(BlackMisc::Aviation::CAircraftCategory)
#endif // guard

View File

@@ -0,0 +1,53 @@
/* Copyright (C) 2019
* 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 "blackmisc/aviation/aircraftcategorylist.h"
#include "blackmisc/range.h"
#include <QJsonObject>
#include <QJsonValue>
#include <Qt>
namespace BlackMisc
{
namespace Aviation
{
CAircraftCategoryList::CAircraftCategoryList()
{ }
CAircraftCategoryList::CAircraftCategoryList(const CSequence<CAircraftCategory> &other) :
CSequence<CAircraftCategory>(other)
{ }
CAircraftCategoryList CAircraftCategoryList::findByName(const QString &name, Qt::CaseSensitivity cs) const
{
return this->findBy([&](const CAircraftCategory & category)
{
return category.matchesName(name, cs);
});
}
void CAircraftCategoryList::sortByRank()
{
this->sortBy(&CAircraftCategory::getPath);
}
CAircraftCategoryList CAircraftCategoryList::fromDatabaseJson(const QJsonArray &array)
{
CAircraftCategoryList codes;
for (const QJsonValue &value : array)
{
const CAircraftCategory category(CAircraftCategory::fromDatabaseJson(value.toObject()));
codes.push_back(category);
}
return codes;
}
} // namespace
} // namespace

View File

@@ -0,0 +1,62 @@
/* Copyright (C) 2019
* 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 BLACKMISC_AVIATION_AIRCRAFTCATEGORYLIST_H
#define BLACKMISC_AVIATION_AIRCRAFTCATEGORYLIST_H
#include "aircraftcategory.h"
#include "blackmisc/collection.h"
#include "blackmisc/db/datastoreobjectlist.h"
#include "blackmisc/sequence.h"
#include "blackmisc/variant.h"
#include "blackmisc/blackmiscexport.h"
#include <QJsonArray>
#include <QMetaType>
#include <QStringList>
#include <tuple>
namespace BlackMisc
{
namespace Aviation
{
//! Value object encapsulating a list of ICAO codes.
class BLACKMISC_EXPORT CAircraftCategoryList :
public CSequence<CAircraftCategory>,
public Db::IDatastoreObjectList<CAircraftCategory, CAircraftCategoryList, int>,
public Mixin::MetaType<CAircraftCategoryList>
{
public:
BLACKMISC_DECLARE_USING_MIXIN_METATYPE(CAircraftCategoryList)
//! Default constructor.
CAircraftCategoryList();
//! Construct from a base class object.
CAircraftCategoryList(const CSequence<CAircraftCategory> &other);
//! Find by name
CAircraftCategoryList findByName(const QString &name, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
//! Sort by path
void sortByRank();
//! From our database JSON format
static CAircraftCategoryList fromDatabaseJson(const QJsonArray &array);
};
} //namespace
} // namespace
Q_DECLARE_METATYPE(BlackMisc::Aviation::CAircraftCategoryList)
Q_DECLARE_METATYPE(BlackMisc::CCollection<BlackMisc::Aviation::CAircraftCategory>)
Q_DECLARE_METATYPE(BlackMisc::CSequence<BlackMisc::Aviation::CAircraftCategory>)
#endif //guard

View File

@@ -28,6 +28,8 @@
#include "blackmisc/aviation/atcstationlist.h"
#include "blackmisc/aviation/aircrafticaocode.h"
#include "blackmisc/aviation/aircrafticaocodelist.h"
#include "blackmisc/aviation/aircraftcategory.h"
#include "blackmisc/aviation/aircraftcategorylist.h"
#include "blackmisc/aviation/aircraftsituation.h"
#include "blackmisc/aviation/aircraftsituationchange.h"
#include "blackmisc/aviation/aircraftsituationlist.h"

View File

@@ -21,6 +21,8 @@ namespace BlackMisc
CAircraftEngineList::registerMetadata();
CAircraftIcaoCode::registerMetadata();
CAircraftIcaoCodeList::registerMetadata();
CAircraftCategory::registerMetadata();
CAircraftCategoryList::registerMetadata();
CAircraftLights::registerMetadata();
CAircraftParts::registerMetadata();
CAircraftPartsList::registerMetadata();

View File

@@ -9,10 +9,10 @@
#include "blackmisc/simulation/aircraftmodellist.h"
#include "blackmisc/simulation/distributorlist.h"
#include "blackmisc/aviation/airport.h"
#include "blackmisc/aviation/airportlist.h"
#include "blackmisc/aviation/liverylist.h"
#include "blackmisc/aviation/aircrafticaocodelist.h"
#include "blackmisc/aviation/aircraftcategorylist.h"
#include "blackmisc/aviation/airlineicaocodelist.h"
#include "blackmisc/db/datastoreobjectlist.h"
#include "blackmisc/db/dbinfolist.h"
@@ -346,6 +346,7 @@ namespace BlackMisc
//! \cond PRIVATE
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList, int>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList, int>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList<BlackMisc::Aviation::CAircraftCategory, BlackMisc::Aviation::CAircraftCategoryList, int>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList, int>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList<BlackMisc::Db::CDbInfo, BlackMisc::Db::CDbInfoList, int>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList<BlackMisc::Db::CArtifact, BlackMisc::Db::CArtifactList, int>;

View File

@@ -108,6 +108,7 @@ namespace BlackMisc
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList, int>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList, int>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList<BlackMisc::Aviation::CAircraftCategory, BlackMisc::Aviation::CAircraftCategoryList, int>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList, int>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList<BlackMisc::Aviation::CAirport, BlackMisc::Aviation::CAirportList, int>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList<BlackMisc::Db::CDbInfo, BlackMisc::Db::CDbInfoList, int>;

View File

@@ -104,7 +104,7 @@ namespace BlackMisc
GlobalIndexCComSystem = 3000,
GlobalIndexCModulator = 3100,
GlobalIndexCTransponder = 3200,
GlobalIndexCAircraftIcaoData = 3500,
GlobalIndexCAircraftCategory = 3500,
GlobalIndexCAircraftIcaoCode = 3600,
GlobalIndexCAirlineIcaoCode = 3700,
GlobalIndexCAirportIcaoCode = 3800,

View File

@@ -14,6 +14,7 @@
#include "blackmisc/aviation/aircraftsituationchangelist.h"
#include "blackmisc/aviation/liverylist.h"
#include "blackmisc/aviation/aircrafticaocodelist.h"
#include "blackmisc/aviation/aircraftcategorylist.h"
#include "blackmisc/aviation/airlineicaocodelist.h"
#include "blackmisc/aviation/airportlist.h"
#include "blackmisc/db/dbinfolist.h"
@@ -712,6 +713,7 @@ namespace BlackMisc
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::CIdentifier, BlackMisc::CIdentifierList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::CStatusMessage, BlackMisc::CStatusMessageList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftCategory, BlackMisc::Aviation::CAircraftCategoryList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAirport, BlackMisc::Aviation::CAirportList>;
template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;

View File

@@ -282,6 +282,8 @@ namespace BlackMisc
class CLiveryList;
class CAircraftIcaoCode;
class CAircraftIcaoCodeList;
class CAircraftCategory;
class CAircraftCategoryList;
class CAirlineIcaoCode;
class CAirlineIcaoCodeList;
}
@@ -323,6 +325,7 @@ namespace BlackMisc
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CLivery, BlackMisc::Aviation::CLiveryList>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftIcaoCode, BlackMisc::Aviation::CAircraftIcaoCodeList>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAircraftCategory, BlackMisc::Aviation::CAircraftCategoryList>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAirlineIcaoCode, BlackMisc::Aviation::CAirlineIcaoCodeList>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList<BlackMisc::Aviation::CAirport, BlackMisc::Aviation::CAirportList>;
extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList<BlackMisc::Db::CDbInfo, BlackMisc::Db::CDbInfoList>;