Initial commit
This commit is contained in:
commit
b7d90b9afe
16245 files changed
+3396666
No files matched your search
+136
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Load/Publish db-translations
|
||||
* $Id$
|
||||
* $HeadURL$
|
||||
* $Author$
|
||||
*/
|
||||
|
||||
#include "translation.h"
|
||||
#include "dds_handler.h"
|
||||
#include <QTimer>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QCryptographicHash>
|
||||
#include <LtsUtilsCore/dds_vector.h>
|
||||
|
||||
namespace ProjectDataManagement
|
||||
{
|
||||
static const int s_translationBlobId = 1;
|
||||
|
||||
Translation::Translation(const std::shared_ptr<DdsHandler> &dds, const std::shared_ptr<DatabaseInterface> &db)
|
||||
: Interface::Fts::InsertUpdateTranslationRequestHandler(dds->fts())
|
||||
, m_dds(dds)
|
||||
, m_db(db)
|
||||
, m_logger(getLogger("Translation"))
|
||||
{
|
||||
DEBUG_LOG(m_logger) << "Translation ctor";
|
||||
m_publishTimer.setInterval(1000);
|
||||
m_publishTimer.setSingleShot(true);
|
||||
m_publishTimer.setTimerType(Qt::VeryCoarseTimer);
|
||||
connect(&m_publishTimer, &QTimer::timeout, this, &Translation::publishData);
|
||||
|
||||
QTimer::singleShot(1000, this, &Translation::collectAndPublishData);
|
||||
}
|
||||
|
||||
Translation::~Translation()
|
||||
{
|
||||
m_dds->dts()->removeTranslationBlobByKey(s_translationBlobId);
|
||||
}
|
||||
|
||||
void Translation::collectAndPublishData()
|
||||
{
|
||||
DEBUG_LOG(m_logger) << "Collecting translation data";
|
||||
m_jsonData.clear();
|
||||
const auto cb = [this](const Database::PdmTranslation &data)
|
||||
{
|
||||
this->addToJson(data);
|
||||
};
|
||||
|
||||
if (!m_db->select(cb))
|
||||
{
|
||||
WARNING_LOG(m_logger) << "Error fetching translations from db";
|
||||
return;
|
||||
}
|
||||
|
||||
publishData();
|
||||
|
||||
DEBUG_LOG(m_logger) << "Collecting translation data - finished";
|
||||
}
|
||||
|
||||
void Translation::publishData()
|
||||
{
|
||||
QJsonArray arr;
|
||||
for (const QJsonObject &obj : qAsConst(m_jsonData))
|
||||
arr += obj;
|
||||
|
||||
const QJsonObject jObj{ {QStringLiteral("translations"), arr} };
|
||||
const QByteArray data = QJsonDocument(jObj).toBinaryData();
|
||||
const QByteArray compressed = qCompress(data, 9);
|
||||
const QByteArray md5Sum = QCryptographicHash::hash(compressed, QCryptographicHash::Md5).toHex();
|
||||
|
||||
INFO_LOG(m_logger) << "Created translation blob with " << arr.size() << " elements, size: " << compressed.size()
|
||||
<< " (uncompressed: " << data.size() << "), md5Sum: " << md5Sum;
|
||||
m_dds->dts()->publish(ProjectDataManagement::TranslationBlob(s_translationBlobId,
|
||||
LtsUtils::convert<std::vector<uint8_t>>(compressed),
|
||||
md5Sum.data()));
|
||||
}
|
||||
|
||||
static inline QJsonObject toJsonObject(const Database::PdmTranslation &data)
|
||||
{
|
||||
return QJsonObject({{QStringLiteral("isoCode"), data.isoCode},
|
||||
{QStringLiteral("context"), data.context},
|
||||
{QStringLiteral("key"), data.key},
|
||||
{QStringLiteral("disambiguation"), data.disambiguation},
|
||||
{QStringLiteral("translation"), data.translation}
|
||||
});
|
||||
}
|
||||
|
||||
void Translation::addToJson(const Database::PdmTranslation &data)
|
||||
{
|
||||
m_jsonData[data.id] = toJsonObject(data);
|
||||
}
|
||||
|
||||
void Translation::onInsertUpdateTranslationRequest(const rti::core::SampleIdentity &cookie,
|
||||
const InsertUpdateTranslationRequest &request)
|
||||
{
|
||||
Database::PdmTranslation te;
|
||||
te.isoCode = QString::fromStdString(request.isoCode());
|
||||
te.context = QString::fromStdString(request.ctx());
|
||||
te.key = QString::fromStdString(request.key());
|
||||
te.disambiguation = QString::fromStdString(request.disambiguation());
|
||||
te.translation = QString::fromStdString(request.translation());
|
||||
|
||||
// not fast but also not really needed ...
|
||||
te.id = -1;
|
||||
auto end = m_jsonData.end();
|
||||
for (auto it = m_jsonData.begin(); it != end; ++it)
|
||||
{
|
||||
const QJsonObject &obj = it.value();
|
||||
if (obj.value(QStringLiteral("isoCode")).toString() == te.isoCode &&
|
||||
obj.value(QStringLiteral("context")).toString() == te.context &&
|
||||
obj.value(QStringLiteral("key")).toString() == te.key &&
|
||||
obj.value(QStringLiteral("disambiguation")).toString() == te.disambiguation)
|
||||
{
|
||||
te.id = it.key();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const bool bRet = (te.id > 0) ? m_db->insert(te) : m_db->update(te);
|
||||
Core::FunctionReplyObject fro(te.id);
|
||||
if (bRet)
|
||||
{
|
||||
addToJson(te);
|
||||
m_publishTimer.start();
|
||||
}
|
||||
else
|
||||
{
|
||||
fro = Core::FunctionReplyObject(te.id, tr_lts("dds", "Can not insert/update translation element"));
|
||||
WARNING_LOG(m_logger) << fro();
|
||||
}
|
||||
m_dds->fts()->insertUpdateTranslationReply(cookie, fro);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user