refs #561, Compressed JSON format for model

* based on RI DB keys
* utility functions
This commit is contained in:
Klaus Basan
2015-12-20 20:37:56 +01:00
parent e8afb78e90
commit 4e0e2cddf8
8 changed files with 98 additions and 5 deletions

View File

@@ -35,6 +35,13 @@ namespace BlackMisc
this->m_dbKey = k; this->m_dbKey = k;
} }
QJsonValue IDatastoreObjectWithIntegerKey::getDbKeyAsJsonValue() const
{
if (this->hasValidDbKey()) { return QJsonValue(this->m_dbKey); }
static const QJsonValue null;
return null;
}
void IDatastoreObjectWithIntegerKey::setKeyAndTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix) void IDatastoreObjectWithIntegerKey::setKeyAndTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix)
{ {
int dbKey = json.value(prefix + "id").toInt(-1); int dbKey = json.value(prefix + "id").toInt(-1);
@@ -103,6 +110,13 @@ namespace BlackMisc
return (i >= static_cast<int>(IndexDbIntegerKey)) && (i <= static_cast<int>(IndexDbIntegerKey)); return (i >= static_cast<int>(IndexDbIntegerKey)) && (i <= static_cast<int>(IndexDbIntegerKey));
} }
QJsonValue IDatastoreObjectWithStringKey::getDbKeyAsJsonValue() const
{
if (this->hasValidDbKey()) { return QJsonValue(this->m_dbKey); }
static const QJsonValue null;
return null;
}
void IDatastoreObjectWithStringKey::setKeyAndTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix) void IDatastoreObjectWithStringKey::setKeyAndTimestampFromDatabaseJson(const QJsonObject &json, const QString &prefix)
{ {
QString dbKey = json.value(prefix + "id").toString(); QString dbKey = json.value(prefix + "id").toString();

View File

@@ -37,6 +37,9 @@ namespace BlackMisc
//! DB key as string //! DB key as string
QString getDbKeyAsString() const; QString getDbKeyAsString() const;
//! Key as JSON value, or null
QJsonValue getDbKeyAsJsonValue() const;
//! Db ley in parentheses, e.g. "(3)" //! Db ley in parentheses, e.g. "(3)"
QString getDbKeyAsStringInParentheses() const; QString getDbKeyAsStringInParentheses() const;
@@ -95,6 +98,9 @@ namespace BlackMisc
//! Get DB key. //! Get DB key.
const QString &getDbKey() const { return m_dbKey; } const QString &getDbKey() const { return m_dbKey; }
//! Key as JSON value, or null
QJsonValue getDbKeyAsJsonValue() const;
//! Set the DB key //! Set the DB key
void setDbKey(const QString &key) { m_dbKey = key.trimmed().toUpper(); } void setDbKey(const QString &key) { m_dbKey = key.trimmed().toUpper(); }

View File

@@ -20,6 +20,13 @@ namespace BlackMisc
return BlackMisc::stringToBool(dbBool); return BlackMisc::stringToBool(dbBool);
} }
const QString &CDatastoreUtility::boolToDbYN(bool v)
{
static const QString y("Y");
static const QString n("N");
return v ? y : n;
}
int CDatastoreUtility::extractIntegerKey(const QString &stringWithKey) int CDatastoreUtility::extractIntegerKey(const QString &stringWithKey)
{ {
QString ks(stringWithKey.trimmed()); QString ks(stringWithKey.trimmed());

View File

@@ -23,23 +23,26 @@ namespace BlackMisc
/*! /*!
* Class with datastore related utilities * Class with datastore related utilities
*/ */
class CDatastoreUtility class BLACKMISC_EXPORT CDatastoreUtility
{ {
public: public:
//! No constructor //! No constructor
CDatastoreUtility() = delete; CDatastoreUtility() = delete;
//! DB Bool value to bool //! DB Bool value to bool
BLACKMISC_EXPORT static bool dbBoolStringToBool(const QString &dbBool); static bool dbBoolStringToBool(const QString &dbBool);
//! Bool to DB yes/no
static const QString &boolToDbYN(bool v);
//! Extract key from string like "MyAircraft (33)" //! Extract key from string like "MyAircraft (33)"
BLACKMISC_EXPORT static int extractIntegerKey(const QString &stringWithKey); static int extractIntegerKey(const QString &stringWithKey);
//! Parse a timestamp object //! Parse a timestamp object
BLACKMISC_EXPORT static QDateTime parseTimestamp(const QString &timestamp); static QDateTime parseTimestamp(const QString &timestamp);
//! Get id from a DB response //! Get id from a DB response
BLACKMISC_EXPORT static bool parseSwiftWriteResponse(const QString &jsonResponse, BlackMisc::CStatusMessageList &messages, BlackMisc::CVariant &key); static bool parseSwiftWriteResponse(const QString &jsonResponse, BlackMisc::CStatusMessageList &messages, BlackMisc::CVariant &key);
}; };
} // namespace } // namespace

View File

@@ -55,6 +55,41 @@ namespace BlackMisc
return s; return s;
} }
QJsonObject CAircraftModel::toDatabaseJson() const
{
QJsonObject obj;
// filename not in DB
obj.insert("id", this->getDbKeyAsJsonValue());
obj.insert("simkey", QJsonValue(this->m_modelString));
obj.insert("description", QJsonValue(this->m_description));
obj.insert("mode", QJsonValue(getModelModeAsString().left(1).toUpper()));
// sims
const CSimulatorInfo sim(getSimulatorInfo());
QString flag = CDatastoreUtility::boolToDbYN(sim.fsx());
obj.insert("simfsx", QJsonValue(flag));
flag = CDatastoreUtility::boolToDbYN(sim.p3d());
obj.insert("simp3d", QJsonValue(flag));
flag = CDatastoreUtility::boolToDbYN(sim.fs9());
obj.insert("simfs9", QJsonValue(flag));
flag = CDatastoreUtility::boolToDbYN(sim.xplane());
obj.insert("simxplane", QJsonValue(flag));
// foreign keys
obj.insert("iddistributor", this->getDistributor().getDbKeyAsJsonValue());
obj.insert("idaircrafticao", this->getAircraftIcaoCode().getDbKeyAsJsonValue());
obj.insert("idlivery", this->getLivery().getDbKeyAsJsonValue());
obj.insert("idairlineicao", this->getLivery().getAirlineIcaoCode().getDbKeyAsJsonValue()); // not really needed if livery is complete
return obj;
}
QString CAircraftModel::toDatabaseJsonString(QJsonDocument::JsonFormat format) const
{
return QJsonDocument(toDatabaseJson()).toJson(format);
}
CVariant CAircraftModel::propertyByIndex(const BlackMisc::CPropertyIndex &index) const CVariant CAircraftModel::propertyByIndex(const BlackMisc::CPropertyIndex &index) const
{ {
if (index.isMyself()) { return CVariant::from(*this); } if (index.isMyself()) { return CVariant::from(*this); }

View File

@@ -239,6 +239,12 @@ namespace BlackMisc
//! \copydoc CValueObject::convertToQString //! \copydoc CValueObject::convertToQString
QString convertToQString(bool i18n = false) const; QString convertToQString(bool i18n = false) const;
//! To database JSON
QJsonObject toDatabaseJson() const;
//! To database JSON
QString toDatabaseJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
//! Model type //! Model type
static QString modelTypeToString(ModelType type); static QString modelTypeToString(ModelType type);

View File

@@ -198,5 +198,21 @@ namespace BlackMisc
} }
return msgs; return msgs;
} }
QJsonArray CAircraftModelList::toDatabaseJson() const
{
QJsonArray array;
for (const CAircraftModel &model : *this)
{
QJsonValue v(model.toDatabaseJson());
array.append(v);
}
return array;
}
QString CAircraftModelList::toDatabaseJsonString(QJsonDocument::JsonFormat format) const
{
return QJsonDocument(toDatabaseJson()).toJson(format);
}
} // namespace } // namespace
} // namespace } // namespace

View File

@@ -89,6 +89,12 @@ namespace BlackMisc
//! Validate for publishing //! Validate for publishing
CStatusMessageList validateForPublishing() const; CStatusMessageList validateForPublishing() const;
//! To database JSON
QJsonArray toDatabaseJson() const;
//! To database JSON
QString toDatabaseJsonString(QJsonDocument::JsonFormat format = QJsonDocument::Compact) const;
}; };
} //namespace } //namespace