Files
pilotclient/src/blackmisc/db/datastoreutility.cpp
Klaus Basan 23ccb35ed8 refs #751, improved model writing to DB
* a pending request can be aborted when taking too long
* success / directWrite flags in signals
2016-09-19 16:18:41 +02:00

161 lines
5.5 KiB
C++

/* Copyright (C) 2015
* 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/db/datastoreutility.h"
#include "blackmisc/logcategory.h"
#include "blackmisc/logcategorylist.h"
#include "blackmisc/simulation/aircraftmodellist.h"
#include "blackmisc/network/networkutils.h"
#include "blackmisc/statusmessage.h"
#include "blackmisc/statusmessagelist.h"
#include "blackmisc/stringutils.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QRegExp>
#include <QTimeZone>
using namespace BlackMisc;
using namespace BlackMisc::Simulation;
using namespace BlackMisc::Network;
namespace BlackMisc
{
namespace Db
{
bool CDatastoreUtility::dbBoolStringToBool(const QString &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)
{
QString ks(stringWithKey.trimmed());
if (ks.isEmpty()) { return -1; }
bool ok = false;
int key = ks.toInt(&ok);
if (ok) { return key; } // only a number
// key in string with ()
int i1 = ks.lastIndexOf('(');
if (i1 < 0) { return -1; }
int i2 = ks.lastIndexOf(')');
if (i2 <= i1 + 1) { return -1;}
QString n(ks.mid(i1 + 1, i2 - i1 - 1));
ok = false;
key = n.toInt(&ok);
return ok ? key : -1;
}
QDateTime CDatastoreUtility::parseTimestamp(const QString &timestamp)
{
if (!timestamp.isEmpty())
{
QString ts(timestamp.trimmed().remove(' ').remove('-').remove(':')); // normalize
QDateTime dt = QDateTime::fromString(ts, "yyyyMMddHHmmss");
dt.setTimeZone(QTimeZone::utc());
return dt;
}
else
{
return QDateTime();
}
}
bool CDatastoreUtility::parseSwiftPublishResponse(const QString &jsonResponse, CAircraftModelList &publishedModels, CAircraftModelList &skippedModels, CStatusMessageList &messages, bool &directWrite)
{
static const CLogCategoryList cats({ CLogCategory::swiftDbWebservice()});
directWrite = false;
if (jsonResponse.isEmpty())
{
messages.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "Empty JSON data"));
return false;
}
QJsonDocument jsonDoc(QJsonDocument::fromJson(jsonResponse.toUtf8()));
// array of messages only
if (jsonDoc.isArray())
{
CStatusMessageList msgs(CStatusMessageList::fromDatabaseJson(jsonDoc.array()));
messages.push_back(msgs);
return true;
}
// no object -> most likely some fucked up HTML string with the PHP error
if (!jsonDoc.isObject())
{
const QString phpError(CNetworkUtils::removeHtmlPartsFromPhpErrorMessage(jsonResponse));
messages.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, phpError));
return false;
}
// fully blown object
QJsonObject json(jsonDoc.object());
bool hasData = false;
if (json.contains("msgs"))
{
QJsonValue msgJson(json.take("msgs"));
CStatusMessageList msgs(CStatusMessageList::fromDatabaseJson(msgJson.toArray()));
if (!msgs.isEmpty())
{
messages.push_back(msgs);
hasData = true;
}
}
// direct write means models written, otherwise CRs
if (json.contains("directWrite"))
{
QJsonValue dw(json.take("directWrite"));
directWrite = dw.toBool(false);
}
if (json.contains("publishedModels"))
{
QJsonValue publishedJson(json.take("publishedModels"));
CAircraftModelList published = CAircraftModelList::fromDatabaseJson(publishedJson.toArray(), "");
if (!published.isEmpty())
{
publishedModels.push_back(published);
hasData = true;
}
}
if (json.contains("skippedModels"))
{
QJsonValue skippedJson(json.take("skippedModels"));
CAircraftModelList skipped = CAircraftModelList::fromDatabaseJson(skippedJson.toArray(), "");
if (!skipped.isEmpty())
{
skippedModels.push_back(skipped);
hasData = true;
}
}
if (!hasData)
{
messages.push_back(CStatusMessage(cats, CStatusMessage::SeverityError, "Received response, but no JSON data"));
}
return hasData;
}
} // ns
} // ns