mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-07 02:35:33 +08:00
refs #452, added distributor entity and lists
This commit is contained in:
committed by
Mathew Sutcliffe
parent
6a677c06d6
commit
46f743c382
@@ -16,12 +16,24 @@ namespace BlackMisc
|
||||
{
|
||||
CDistributor::CDistributor() { }
|
||||
|
||||
CDistributor::CDistributor(const QString &id)
|
||||
{
|
||||
this->setDbKey(id);
|
||||
}
|
||||
|
||||
CDistributor::CDistributor(const QString &id, const QString &description, const QString &alias1, const QString &alias2) :
|
||||
m_description(description), m_alias1(alias1.trimmed().toUpper()), m_alias2(alias2.trimmed().toUpper())
|
||||
{
|
||||
this->setDbKey(id);
|
||||
}
|
||||
|
||||
bool CDistributor::matchesIdOrAlias(const QString &idOrAlias) const
|
||||
{
|
||||
QString s(idOrAlias.trimmed().toUpper());
|
||||
if (s.isEmpty()) { return false; }
|
||||
return (getId() == s || getAlias1() == s || getAlias2() == s);
|
||||
}
|
||||
|
||||
CVariant CDistributor::propertyByIndex(const CPropertyIndex &index) const
|
||||
{
|
||||
if (index.isMyself()) { return CVariant::from(*this); }
|
||||
@@ -73,20 +85,36 @@ namespace BlackMisc
|
||||
return !this->m_description.isEmpty() && !this->m_dbKey.isEmpty();
|
||||
}
|
||||
|
||||
CDistributor CDistributor::fromDatabaseJson(const QJsonObject &json)
|
||||
CStatusMessageList CDistributor::validate() const
|
||||
{
|
||||
QJsonArray inner = json["cell"].toArray();
|
||||
Q_ASSERT_X(!inner.isEmpty(), Q_FUNC_INFO, "Missing JSON");
|
||||
if (inner.isEmpty()) { return CDistributor(); }
|
||||
static const CLogCategoryList cats( { CLogCategory(this->getClassName()), CLogCategory::validation()});
|
||||
CStatusMessageList msgs;
|
||||
if (!hasValidDbKey()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "Distributor: missing id")); }
|
||||
if (!hasDescription()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, "Distributor: missing description")); }
|
||||
return msgs;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
QString dbKey(inner.at(i++).toString());
|
||||
QString description(inner.at(i++).toString());
|
||||
QString alias1(inner.at(i++).toString());
|
||||
QString alias2(inner.at(i++).toString());
|
||||
Q_ASSERT_X(!dbKey.isEmpty(), Q_FUNC_INFO, "Missing key");
|
||||
void CDistributor::updateMissingParts(const CDistributor &otherDistributor)
|
||||
{
|
||||
if (!this->hasAlias1()) { this->setAlias1(otherDistributor.getAlias1()); }
|
||||
if (!this->hasAlias2()) { this->setAlias1(otherDistributor.getAlias2()); }
|
||||
if (!this->hasDescription()) { this->setDescription(otherDistributor.getDescription()); }
|
||||
}
|
||||
|
||||
CDistributor CDistributor::fromDatabaseJson(const QJsonObject &json, const QString &prefix)
|
||||
{
|
||||
if (!existsKey(json, prefix))
|
||||
{
|
||||
// when using relationship, this can be null
|
||||
return CDistributor();
|
||||
}
|
||||
|
||||
QString description(json.value(prefix + "description").toString());
|
||||
QString alias1(json.value(prefix + "alias1").toString());
|
||||
QString alias2(json.value(prefix + "alias2").toString());
|
||||
Q_ASSERT_X(!description.isEmpty(), Q_FUNC_INFO, "Missing description");
|
||||
CDistributor distributor(dbKey, description, alias1, alias2);
|
||||
CDistributor distributor("", description, alias1, alias2);
|
||||
distributor.setKeyAndTimestampFromDatabaseJson(json, prefix);
|
||||
return distributor;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "blackmisc/blackmiscexport.h"
|
||||
#include "blackmisc/valueobject.h"
|
||||
#include "blackmisc/datastore.h"
|
||||
#include "blackmisc/statusmessagelist.h"
|
||||
#include <QJsonObject>
|
||||
|
||||
namespace BlackMisc
|
||||
@@ -38,6 +39,9 @@ namespace BlackMisc
|
||||
//! Default constructor.
|
||||
CDistributor();
|
||||
|
||||
//! Constructor
|
||||
CDistributor(const QString &id);
|
||||
|
||||
//! Constructor
|
||||
CDistributor(const QString &id, const QString &description, const QString &alias1, const QString &alias2);
|
||||
|
||||
@@ -47,6 +51,12 @@ namespace BlackMisc
|
||||
//! Get description
|
||||
const QString &getDescription() const { return this->m_description;}
|
||||
|
||||
//! Set description
|
||||
void setDescription(const QString &description) { this->m_description = description.trimmed(); }
|
||||
|
||||
//! Has description
|
||||
bool hasDescription() const { return !this->m_description.isEmpty(); }
|
||||
|
||||
//! Get alias1
|
||||
const QString &getAlias1() const { return this->m_alias1;}
|
||||
|
||||
@@ -65,6 +75,9 @@ namespace BlackMisc
|
||||
//! Alias 2?
|
||||
bool hasAlias2() const { return !this->m_alias2.isEmpty(); }
|
||||
|
||||
//! Matches id or alias
|
||||
bool matchesIdOrAlias(const QString &idOrAlias) const;
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex
|
||||
CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const;
|
||||
|
||||
@@ -77,8 +90,14 @@ namespace BlackMisc
|
||||
//! Complete data?
|
||||
bool hasCompleteData() const;
|
||||
|
||||
//! Validate data
|
||||
BlackMisc::CStatusMessageList validate() const;
|
||||
|
||||
//! Update missing parts
|
||||
void updateMissingParts(const CDistributor &otherDistributor);
|
||||
|
||||
//! Object from JSON
|
||||
static CDistributor fromDatabaseJson(const QJsonObject &json);
|
||||
static CDistributor fromDatabaseJson(const QJsonObject &json, const QString &prefix = QString());
|
||||
|
||||
private:
|
||||
BLACK_ENABLE_TUPLE_CONVERSION(CDistributor)
|
||||
@@ -93,6 +112,7 @@ namespace BlackMisc
|
||||
Q_DECLARE_METATYPE(BlackMisc::Simulation::CDistributor)
|
||||
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::Simulation::CDistributor, (
|
||||
attr(o.m_dbKey, flags <CaseInsensitiveComparison> ()),
|
||||
attr(o.m_timestampMSecsSinceEpoch),
|
||||
attr(o.m_description),
|
||||
attr(o.m_alias1, flags <CaseInsensitiveComparison> ()),
|
||||
attr(o.m_alias2, flags <CaseInsensitiveComparison> ())
|
||||
|
||||
@@ -20,5 +20,15 @@ namespace BlackMisc
|
||||
CSequence<CDistributor>(other)
|
||||
{ }
|
||||
|
||||
CDistributor CDistributorList::findByIdOrAlias(const QString &idOrAlias)
|
||||
{
|
||||
if (idOrAlias.isEmpty()) { return CDistributor(); }
|
||||
for (const CDistributor &distributor : (*this))
|
||||
{
|
||||
if (distributor.matchesIdOrAlias(idOrAlias)) { return distributor; }
|
||||
}
|
||||
return CDistributor();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
@@ -39,6 +39,10 @@ namespace BlackMisc
|
||||
|
||||
//! Construct from a base class object.
|
||||
CDistributorList(const CSequence<CDistributor> &other);
|
||||
|
||||
//! Find by id or alias
|
||||
CDistributor findByIdOrAlias(const QString &name);
|
||||
|
||||
};
|
||||
} //namespace
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user