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.

251 lines
6.3 KiB
C

#ifndef SERVICE_H
#define SERVICE_H
#include <QList>
#include <QSharedPointer>
#include <QString>
#include <QEventLoop>
#include <QApplication>
#include "core_global.h"
#include "context.h"
#include "iservice.h"
#include "permissionevaluator.h"
#include "iimporter.h"
#include "iimportprogress.h"
template<class T>
class Service : public IService
{
public:
explicit Service(QObject *parent = nullptr) :IService(parent) { }
explicit Service(const QString &pluginId) {
m_pluginId = pluginId;
}
QList<QSharedPointer<T> > all(const QString &where = "") {
QList<QSharedPointer<T> > ret;
if (!checkPermission(PERM_READ)) {
return ret;
}
QScopedPointer<T> entity(new T());
auto qEntity = dynamic_cast<QObject*>(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;
}
virtual void save(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) {
if (!checkPermission(PERM_ADD)) {
return;
}
QScopedPointer<qx::QxSession> 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();
}
virtual void update(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) {
if (!checkPermission(PERM_EDIT)) {
return;
}
QScopedPointer<qx::QxSession> 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<T> loadById(long id) {
QSharedPointer<T> entity;
if (!checkPermission(PERM_READ)) {
return entity;
}
entity = QSharedPointer<T>(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<T>& 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<T> reload(int id) {
return loadById(id);
}
void erase(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) {
if (!checkPermission(PERM_DELETE)) {
return;
}
QScopedPointer<qx::QxSession> 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<QObject> qentity = importer->nextRecord();
if (importer->isError() || qentity.isNull()) {
return false;
}
QSharedPointer<T> entity = qentity.dynamicCast<T>();
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<T> entity, bool creating) {
T *inner = entity.data();
auto *obj = dynamic_cast<QObject*>(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