From 257c8ea9f9ecb6eec120c653e9aeb17e1c12c84f Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Mon, 14 Jan 2019 20:52:50 +0100 Subject: [PATCH] Ref T472, aircraft category value object and list --- src/blackmisc/aviation/aircraftcategory.cpp | 160 ++++++++++++++++++ src/blackmisc/aviation/aircraftcategory.h | 139 +++++++++++++++ .../aviation/aircraftcategorylist.cpp | 53 ++++++ src/blackmisc/aviation/aircraftcategorylist.h | 62 +++++++ src/blackmisc/aviation/aviation.h | 2 + .../aviation/registermetadataaviation.cpp | 2 + src/blackmisc/db/datastoreobjectlist.cpp | 3 +- src/blackmisc/db/datastoreobjectlist.h | 1 + src/blackmisc/propertyindex.h | 2 +- src/blackmisc/timestampobjectlist.cpp | 2 + src/blackmisc/timestampobjectlist.h | 3 + 11 files changed, 427 insertions(+), 2 deletions(-) create mode 100644 src/blackmisc/aviation/aircraftcategory.cpp create mode 100644 src/blackmisc/aviation/aircraftcategory.h create mode 100644 src/blackmisc/aviation/aircraftcategorylist.cpp create mode 100644 src/blackmisc/aviation/aircraftcategorylist.h diff --git a/src/blackmisc/aviation/aircraftcategory.cpp b/src/blackmisc/aviation/aircraftcategory.cpp new file mode 100644 index 000000000..05d7920af --- /dev/null +++ b/src/blackmisc/aviation/aircraftcategory.cpp @@ -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 +#include +#include +#include +#include +#include +#include +#include + +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(); + 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(); return; } + if (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) { IDatastoreObjectWithIntegerKey::setPropertyByIndex(index, variant); return; } + const ColumnIndex i = index.frontCasted(); + switch (i) + { + case IndexName: this->setName(variant.value()); break; + case IndexDescription: this->setDescription(variant.value()); break; + case IndexAssignable: this->setAssignable(variant.value()); 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(); + 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 diff --git a/src/blackmisc/aviation/aircraftcategory.h b/src/blackmisc/aviation/aircraftcategory.h new file mode 100644 index 000000000..f4120e2d4 --- /dev/null +++ b/src/blackmisc/aviation/aircraftcategory.h @@ -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 +#include +#include +#include + +namespace BlackMisc +{ + namespace Aviation + { + //! Value object for aircraft categories + class BLACKMISC_EXPORT CAircraftCategory : + public CValueObject, + 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 diff --git a/src/blackmisc/aviation/aircraftcategorylist.cpp b/src/blackmisc/aviation/aircraftcategorylist.cpp new file mode 100644 index 000000000..71499300a --- /dev/null +++ b/src/blackmisc/aviation/aircraftcategorylist.cpp @@ -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 +#include +#include + +namespace BlackMisc +{ + namespace Aviation + { + CAircraftCategoryList::CAircraftCategoryList() + { } + + CAircraftCategoryList::CAircraftCategoryList(const CSequence &other) : + CSequence(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 diff --git a/src/blackmisc/aviation/aircraftcategorylist.h b/src/blackmisc/aviation/aircraftcategorylist.h new file mode 100644 index 000000000..7d1b32c28 --- /dev/null +++ b/src/blackmisc/aviation/aircraftcategorylist.h @@ -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 +#include +#include +#include + +namespace BlackMisc +{ + namespace Aviation + { + //! Value object encapsulating a list of ICAO codes. + class BLACKMISC_EXPORT CAircraftCategoryList : + public CSequence, + public Db::IDatastoreObjectList, + public Mixin::MetaType + { + public: + BLACKMISC_DECLARE_USING_MIXIN_METATYPE(CAircraftCategoryList) + + //! Default constructor. + CAircraftCategoryList(); + + //! Construct from a base class object. + CAircraftCategoryList(const CSequence &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) +Q_DECLARE_METATYPE(BlackMisc::CSequence) + +#endif //guard diff --git a/src/blackmisc/aviation/aviation.h b/src/blackmisc/aviation/aviation.h index 6e971ce1b..1ef9ead64 100644 --- a/src/blackmisc/aviation/aviation.h +++ b/src/blackmisc/aviation/aviation.h @@ -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" diff --git a/src/blackmisc/aviation/registermetadataaviation.cpp b/src/blackmisc/aviation/registermetadataaviation.cpp index ece9aeba2..ca597602f 100644 --- a/src/blackmisc/aviation/registermetadataaviation.cpp +++ b/src/blackmisc/aviation/registermetadataaviation.cpp @@ -21,6 +21,8 @@ namespace BlackMisc CAircraftEngineList::registerMetadata(); CAircraftIcaoCode::registerMetadata(); CAircraftIcaoCodeList::registerMetadata(); + CAircraftCategory::registerMetadata(); + CAircraftCategoryList::registerMetadata(); CAircraftLights::registerMetadata(); CAircraftParts::registerMetadata(); CAircraftPartsList::registerMetadata(); diff --git a/src/blackmisc/db/datastoreobjectlist.cpp b/src/blackmisc/db/datastoreobjectlist.cpp index f60ae8fb2..a29cfd9c1 100644 --- a/src/blackmisc/db/datastoreobjectlist.cpp +++ b/src/blackmisc/db/datastoreobjectlist.cpp @@ -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; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList; + template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE IDatastoreObjectList; diff --git a/src/blackmisc/db/datastoreobjectlist.h b/src/blackmisc/db/datastoreobjectlist.h index 8651cddb4..d41a470e4 100644 --- a/src/blackmisc/db/datastoreobjectlist.h +++ b/src/blackmisc/db/datastoreobjectlist.h @@ -108,6 +108,7 @@ namespace BlackMisc extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList; extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList; + extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList; extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList; extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList; extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE IDatastoreObjectList; diff --git a/src/blackmisc/propertyindex.h b/src/blackmisc/propertyindex.h index 5b5668308..8ed6dff07 100644 --- a/src/blackmisc/propertyindex.h +++ b/src/blackmisc/propertyindex.h @@ -104,7 +104,7 @@ namespace BlackMisc GlobalIndexCComSystem = 3000, GlobalIndexCModulator = 3100, GlobalIndexCTransponder = 3200, - GlobalIndexCAircraftIcaoData = 3500, + GlobalIndexCAircraftCategory = 3500, GlobalIndexCAircraftIcaoCode = 3600, GlobalIndexCAirlineIcaoCode = 3700, GlobalIndexCAirportIcaoCode = 3800, diff --git a/src/blackmisc/timestampobjectlist.cpp b/src/blackmisc/timestampobjectlist.cpp index c2e2709fd..7e48fc3bc 100644 --- a/src/blackmisc/timestampobjectlist.cpp +++ b/src/blackmisc/timestampobjectlist.cpp @@ -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; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; + template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; template class BLACKMISC_EXPORT_DEFINE_TEMPLATE ITimestampObjectList; diff --git a/src/blackmisc/timestampobjectlist.h b/src/blackmisc/timestampobjectlist.h index 39ca3f55a..54b5e89ed 100644 --- a/src/blackmisc/timestampobjectlist.h +++ b/src/blackmisc/timestampobjectlist.h @@ -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; extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList; + extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList; extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList; extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList; extern template class BLACKMISC_EXPORT_DECLARE_TEMPLATE ITimestampObjectList;