#ifndef SERVICE_H #define SERVICE_H #include #include #include #include #include #include "core_global.h" #include "context.h" #include "iservice.h" #include "permissionevaluator.h" #include "iimporter.h" #include "iimportprogress.h" template class Service : public IService { public: explicit Service(QObject *parent = nullptr) :IService(parent) { } explicit Service(const QString &pluginId) { m_pluginId = pluginId; } QList > all(const QString &where = "") { QList > ret; if (!checkPermission(PERM_READ)) { return ret; } QScopedPointer entity(new T()); auto qEntity = dynamic_cast(entity.data()); QStringList relations; QString sWhere = where.isEmpty() ? "" : "WHERE " + where; QSqlError err; if (qEntity && QMetaObject::invokeMethod(qEntity, "eagerLoad", Qt::DirectConnection, Q_RETURN_ARG(QStringList, relations)) && !relations.isEmpty()) { err = qx::dao::fetch_by_query_with_relation(relations, sWhere, ret); } else { err = qx::dao::fetch_by_query(sWhere, ret); } if (!err.isValid()) { return ret; } else { qDebug() << err.text(); emit dbError(err.text()); } return ret; } void save(QSharedPointer entity, qx::QxSession* pSession = nullptr) { if (!checkPermission(PERM_ADD)) { return; } QScopedPointer ptrSession; if (pSession == nullptr) { ptrSession.reset(new qx::QxSession()); pSession = ptrSession.data(); } addDateAndUser(entity, true); *pSession += qx::dao::insert_with_all_relation(entity, pSession->database()); if (!pSession->isValid()) { qDebug() << pSession->firstError().text(); emit dbError(pSession->firstError().text()); emit dbErrorInsert(pSession->firstError().text()); return; } emit dataChanged(); } void update(QSharedPointer entity, qx::QxSession* pSession = nullptr) { if (!checkPermission(PERM_EDIT)) { return; } QScopedPointer ptrSession; if (pSession == nullptr) { ptrSession.reset(new qx::QxSession()); pSession = ptrSession.data(); } addDateAndUser(entity, false); *pSession += qx::dao::update_with_all_relation(entity, pSession->database()); if (!pSession->isValid()) { emit dbError(pSession->firstError().text()); emit dbErrorUpdate(pSession->firstError().text()); return; } emit dataChanged(); } QSharedPointer loadById(long id) { QSharedPointer entity; if (!checkPermission(PERM_READ)) { return entity; } entity = QSharedPointer(new T); entity->setId(id); auto err = qx::dao::fetch_by_id_with_all_relation(entity); if (err.isValid()) { emit dbError(err.text()); emit dbErrorRead(err.text()); } return entity; } void load(QSharedPointer& entity) { if (!checkPermission(PERM_READ)) { return; } auto err = qx::dao::fetch_by_id_with_all_relation(entity); if (err.isValid()) { emit dbError(err.text()); emit dbErrorRead(err.text()); } } QSharedPointer reload(int id) { return loadById(id); } void erase(QSharedPointer entity, qx::QxSession* pSession = nullptr) { if (!checkPermission(PERM_DELETE)) { return; } QScopedPointer ptrSession; if (pSession == nullptr) { ptrSession.reset(new qx::QxSession()); pSession = ptrSession.data(); } *pSession += qx::dao::delete_by_id(entity); if (!pSession->isValid()) { qDebug() << pSession->firstError().text(); emit dbError(pSession->firstError().text()); emit dbErrorDelete(pSession->firstError().text()); } } bool importData(IImporter *importer, IImportProgress *progress = nullptr) { int count = importer->recordCount(); if (importer->isError()) { return false; } for (int i = 0; i < count - 1; i++) { QSharedPointer qentity = importer->nextRecord(); if (importer->isError() || qentity.isNull()) { return false; } QSharedPointer entity = qentity.dynamicCast(); if (!entity.isNull()) { this->save(entity); } else { return false; } qApp->processEvents(); if (progress != nullptr && progress->terminate()) { return true; } if (progress != nullptr) { progress->updateProgress(i * 100 / count); } } return true; } protected: bool checkPermission(const QString &permission) { if (!m_pluginId.isEmpty()) { PermissionEvaluator ev; if (!ev.hasPermission(m_pluginId, permission)) { emit permissionDenied(permission); return false; } } return true; } void addDateAndUser(QSharedPointer entity, bool creating) { T *inner = entity.data(); auto *obj = dynamic_cast(inner); if (obj == nullptr) { return; } if (creating) { if (!Context::instance().currentUser().isNull()) { obj->setProperty("createdBy", Context::instance().currentUser()->login()); } obj->setProperty("created", QDateTime::currentDateTime()); } else { obj->setProperty("updatedBy", Context::instance().currentUser()->login()); obj->setProperty("updated", QDateTime::currentDateTime()); } } virtual QString defaultSort() { return ""; } }; #endif // SERVICE_H