#ifndef SERVICE_H #define SERVICE_H #include #include #include #include #include #include #include #include #include #include "core_global.h" #include "context.h" #include "iservice.h" #include "permissionevaluator.h" #include "iimporter.h" #include "iimportprogress.h" #include "transaction.h" template class Service : public IService { public: explicit Service(QObject *parent = NULL) :IService(parent) { } explicit Service(const QString &pluginId) { m_pluginId = pluginId; } QList > all(const QString &where = "") { QList > ret; if (!checkPermission(PERM_READ)) { return ret; } odb::database *db = Context::instance().db(); Q_ASSERT(db); Transaction tx; try { odb::result res; if (where.isEmpty()) { res = db->template query(); } else { res = db->template query(where.toStdString()); } for (typename odb::result::iterator it = res.begin(); it != res.end(); it++) { ret.append(it.load()); } tx.commit(); } catch (const odb::exception &ex) { emit dbError(ex.what()); } return ret; } void save(QSharedPointer entity) { if (!checkPermission(PERM_ADD)) { return; } odb::database *db = Context::instance().db(); Q_ASSERT(db); Transaction tx; try { db->persist(entity); tx.commit(); } catch (const odb::exception &ex) { emit dbError(ex.what()); emit dbErrorUpdate(ex.what()); return; } emit dataChanged(); } void update(QSharedPointer entity) { if (!checkPermission(PERM_EDIT)) { return; } odb::database *db = Context::instance().db(); Q_ASSERT(db); Transaction tx; try { db->update(entity); tx.commit(); } catch (const odb::exception &ex) { emit dbError(ex.what()); emit dbErrorInsert(ex.what()); return; } emit dataChanged(); } QSharedPointer loadById(int id) { QSharedPointer entity; if (!checkPermission(PERM_READ)) { return entity; } odb::database *db = Context::instance().db(); Q_ASSERT(db); Transaction tx; try { entity = db->template load(id); tx.commit(); } catch (const odb::exception &ex) { emit dbError(ex.what()); emit dbErrorRead(ex.what()); } return entity; } QSharedPointer reload(int id) { odb::database *db = Context::instance().db(); Q_ASSERT(db); Context::instance().session().cache_erase(*db, id); return loadById(id); } void erase(QSharedPointer entity) { if (!checkPermission(PERM_DELETE)) { return; } odb::database *db = Context::instance().db(); Q_ASSERT(db); Transaction tx; try { db->erase(entity); tx.commit(); } catch (const odb::exception &ex) { emit dbError(ex.what()); emit dbErrorDelete(ex.what()); } } bool importData(IImporter *importer, IImportProgress *progress = NULL) { 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 != NULL && progress->terminate()) { return true; } if (progress != NULL) { 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; } }; #endif // SERVICE_H