You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
prodejna/core/imetadataplugin.cpp

224 lines
5.2 KiB
C++

#include "imetadataplugin.h"
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonDocument>
#include <QLocale>
#include <QDebug>
#include <QApplication>
#include <QFile>
#include "igridform.h"
IMetaDataPlugin::IMetaDataPlugin()
{
m_service = nullptr;
m_ui = nullptr;
}
IMetaDataPlugin::~IMetaDataPlugin()
{
}
QString IMetaDataPlugin::pluginName()
{
return m_name;
}
QString IMetaDataPlugin::pluginId()
{
return m_id;
}
QString IMetaDataPlugin::pluginDescription()
{
return m_description;
}
int IMetaDataPlugin::schemaVersion()
{
return m_schemaVersion;
}
QStringList IMetaDataPlugin::schemas()
{
return m_schemas;
}
QStringList IMetaDataPlugin::dependsOn()
{
return m_dependsOn;
}
ReportList IMetaDataPlugin::reports()
{
return m_reports;
}
void IMetaDataPlugin::init(const QJsonObject &metaData)
{
parseMetaData(metaData);
qApp->installTranslator(this->translator());
initServiceUi();
if (IGridForm *pluginUi = dynamic_cast<IGridForm*>(m_ui))
{
pluginUi->setPluginId(pluginId());
pluginUi->setTranslations(m_translations);
}
if (m_service != NULL)
{
m_service->setPluginId(pluginId());
}
}
void IMetaDataPlugin::parseMetaData(const QJsonObject &metaData)
{
qDebug() << metaData;
QJsonValue data = metaData["MetaData"];
if (!data.isObject()) {
return;
}
auto metaBase = loadBaseMetaData(metaData);
if (!metaBase) {
return;
}
m_name = metaBase->getName();
m_description = metaBase->getDescription();
m_id = metaBase->getId();
m_schemaVersion = metaBase->getSchemaVersion();
m_schemas = metaBase->getSchemas();
m_dependsOn = metaBase->getDependsOn();
QJsonValue trVal = data.toObject()["translations"];
QString locale = QLocale::system().name();
QJsonValue trLocalized;
foreach (QString key, trVal.toObject().keys()) {
if (locale.contains(key, Qt::CaseInsensitive)) {
trLocalized = trVal.toObject()[key];
break;
}
}
if (!trLocalized.isNull())
{
foreach (QString key, trLocalized.toObject().keys()) {
m_translations[key] = trLocalized.toObject()[key].toString();
}
}
QJsonValue repArray = data.toObject()["reports"];
addReportsFromJson(repArray);
addCustomReports();
}
QString IMetaDataPlugin::parseLocaleText(const QJsonObject &object)
{
QString locale = QLocale::system().name();
QString ret;
foreach (QString key, object.keys()) {
if (locale.contains(key, Qt::CaseInsensitive)) {
ret = object[key].toString();
break;
}
}
if (ret.isEmpty()) {
ret = object["default"].toString();
}
return ret;
}
void IMetaDataPlugin::addCustomReports()
{
QString jsonPath = qApp->applicationDirPath() + REPORT_ROOT + "/" + pluginId().toLower() + ".json";
QFile f (jsonPath);
if (f.open(QIODevice::ReadOnly | QIODevice::Text))
{
QJsonDocument d = QJsonDocument::fromJson(f.readAll());
QJsonObject obj = d.object();
addReportsFromJson(obj.value("reports"));
}
}
void IMetaDataPlugin::addReportsFromJson(const QJsonValue &repArray)
{
foreach (QJsonValue repVal, repArray.toArray()) {
QJsonObject repObj = repVal.toObject();
ReportPtr report = ReportPtr(new Report());
report->setName(repObj["name"].toString());
report->setDescription(repObj["description"].toString());
report->setListReport(repObj["listReport"].toBool());
report->setFile(repObj["file"].toString());
m_reports.append(report);
}
}
MetaDataPtr IMetaDataPlugin::loadBaseMetaData(const QJsonObject& metaData) {
qDebug() << metaData;
QJsonValue data = metaData["MetaData"];
if (!data.isObject()) {
return {};
}
QStringList schemas;
for (const QJsonValue& schVal : data.toObject()["sql"].toArray()) {
schemas.append(schVal.toString());
}
QStringList dependsOn;
for (const QJsonValue& depVal : data.toObject().value("dependencies").toArray()) {
dependsOn.append(depVal.toString());
}
return MetaDataPtr::create(
parseLocaleText(data.toObject()["name"].toObject()),
data.toObject()["id"].toString(),
parseLocaleText(data.toObject()["description"].toObject()),
data.toObject()["schemaVersion"].toInt(),
schemas,
dependsOn);
}
MetaData::MetaData(const QString &mName, const QString &mId, const QString &mDescription, int mSchemaVersion,
const QStringList &mSchemas, const QStringList &mDependsOn)
: m_name(mName), m_id(mId), m_description(mDescription), m_schemaVersion(mSchemaVersion),
m_schemas(mSchemas), m_dependsOn(mDependsOn) {}
const QString& MetaData::getName() const {
return m_name;
}
const QString& MetaData::getId() const {
return m_id;
}
const QString& MetaData::getDescription() const {
return m_description;
}
int MetaData::getSchemaVersion() const {
return m_schemaVersion;
}
const QStringList& MetaData::getSchemas() const {
return m_schemas;
}
const QStringList& MetaData::getDependsOn() const {
return m_dependsOn;
}