summaryrefslogtreecommitdiffstats
path: root/common/store.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'common/store.cpp')
-rw-r--r--common/store.cpp44
1 files changed, 42 insertions, 2 deletions
diff --git a/common/store.cpp b/common/store.cpp
index 4735113..1701a43 100644
--- a/common/store.cpp
+++ b/common/store.cpp
@@ -36,10 +36,34 @@
36#include "storage.h" 36#include "storage.h"
37#include "log.h" 37#include "log.h"
38 38
39#define ASSERT_ENUMS_MATCH(A, B) Q_STATIC_ASSERT_X(static_cast<int>(A) == static_cast<int>(B), "The enum values must match");
40
41//Ensure the copied enum matches
42typedef ModelResult<Sink::ApplicationDomain::Mail, Sink::ApplicationDomain::Mail::Ptr> MailModelResult;
43ASSERT_ENUMS_MATCH(Sink::Store::DomainObjectBaseRole, MailModelResult::DomainObjectBaseRole)
44ASSERT_ENUMS_MATCH(Sink::Store::ChildrenFetchedRole, MailModelResult::ChildrenFetchedRole)
45ASSERT_ENUMS_MATCH(Sink::Store::DomainObjectRole, MailModelResult::DomainObjectRole)
46ASSERT_ENUMS_MATCH(Sink::Store::StatusRole, MailModelResult::StatusRole)
47ASSERT_ENUMS_MATCH(Sink::Store::WarningRole, MailModelResult::WarningRole)
48ASSERT_ENUMS_MATCH(Sink::Store::ProgressRole, MailModelResult::ProgressRole)
49
39Q_DECLARE_METATYPE(QSharedPointer<Sink::ResultEmitter<Sink::ApplicationDomain::SinkResource::Ptr>>) 50Q_DECLARE_METATYPE(QSharedPointer<Sink::ResultEmitter<Sink::ApplicationDomain::SinkResource::Ptr>>)
40Q_DECLARE_METATYPE(QSharedPointer<Sink::ResourceAccessInterface>); 51Q_DECLARE_METATYPE(QSharedPointer<Sink::ResourceAccessInterface>);
41Q_DECLARE_METATYPE(std::shared_ptr<void>); 52Q_DECLARE_METATYPE(std::shared_ptr<void>);
42 53
54
55static bool sanityCheckQuery(const Sink::Query &query)
56{
57 for (const auto &id : query.ids()) {
58 if (id.isEmpty()) {
59 SinkError() << "Empty id in query.";
60 return false;
61 }
62 }
63 return true;
64}
65
66
43namespace Sink { 67namespace Sink {
44 68
45QString Store::storageLocation() 69QString Store::storageLocation()
@@ -138,6 +162,7 @@ static Log::Context getQueryContext(const Sink::Query &query, const QByteArray &
138template <class DomainType> 162template <class DomainType>
139QSharedPointer<QAbstractItemModel> Store::loadModel(const Query &query) 163QSharedPointer<QAbstractItemModel> Store::loadModel(const Query &query)
140{ 164{
165 Q_ASSERT(sanityCheckQuery(query));
141 auto ctx = getQueryContext(query, ApplicationDomain::getTypeName<DomainType>()); 166 auto ctx = getQueryContext(query, ApplicationDomain::getTypeName<DomainType>());
142 auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties, ctx); 167 auto model = QSharedPointer<ModelResult<DomainType, typename DomainType::Ptr>>::create(query, query.requestedProperties, ctx);
143 168
@@ -189,6 +214,10 @@ KAsync::Job<void> Store::create(const DomainType &domainObject)
189template <class DomainType> 214template <class DomainType>
190KAsync::Job<void> Store::modify(const DomainType &domainObject) 215KAsync::Job<void> Store::modify(const DomainType &domainObject)
191{ 216{
217 if (domainObject.changedProperties().isEmpty()) {
218 SinkLog() << "Nothing to modify: " << domainObject.identifier();
219 return KAsync::null();
220 }
192 SinkLog() << "Modify: " << domainObject; 221 SinkLog() << "Modify: " << domainObject;
193 // Potentially move to separate thread as well 222 // Potentially move to separate thread as well
194 auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier()); 223 auto facade = getFacade<DomainType>(domainObject.resourceInstanceIdentifier());
@@ -198,6 +227,10 @@ KAsync::Job<void> Store::modify(const DomainType &domainObject)
198template <class DomainType> 227template <class DomainType>
199KAsync::Job<void> Store::modify(const Query &query, const DomainType &domainObject) 228KAsync::Job<void> Store::modify(const Query &query, const DomainType &domainObject)
200{ 229{
230 if (domainObject.changedProperties().isEmpty()) {
231 SinkLog() << "Nothing to modify: " << domainObject.identifier();
232 return KAsync::null();
233 }
201 SinkLog() << "Modify: " << query << domainObject; 234 SinkLog() << "Modify: " << query << domainObject;
202 return fetchAll<DomainType>(query) 235 return fetchAll<DomainType>(query)
203 .each([=] (const typename DomainType::Ptr &entity) { 236 .each([=] (const typename DomainType::Ptr &entity) {
@@ -311,9 +344,14 @@ KAsync::Job<void> Store::synchronize(const Sink::Query &query)
311 344
312KAsync::Job<void> Store::synchronize(const Sink::SyncScope &scope) 345KAsync::Job<void> Store::synchronize(const Sink::SyncScope &scope)
313{ 346{
347 auto resourceFilter = scope.getResourceFilter();
348 //Filter resources by type by default
349 if (!resourceFilter.propertyFilter.contains(ApplicationDomain::SinkResource::Capabilities::name) && !scope.type().isEmpty()) {
350 resourceFilter.propertyFilter.insert(ApplicationDomain::SinkResource::Capabilities::name, Query::Comparator{scope.type(), Query::Comparator::Contains});
351 }
314 Sink::Query query; 352 Sink::Query query;
315 query.setFilter(scope.getResourceFilter()); 353 query.setFilter(resourceFilter);
316 SinkLog() << "Synchronizing: " << query; 354 SinkLog() << "Synchronizing all resource matching: " << query;
317 return fetchAll<ApplicationDomain::SinkResource>(query) 355 return fetchAll<ApplicationDomain::SinkResource>(query)
318 .template each([scope](const ApplicationDomain::SinkResource::Ptr &resource) -> KAsync::Job<void> { 356 .template each([scope](const ApplicationDomain::SinkResource::Ptr &resource) -> KAsync::Job<void> {
319 return synchronize(resource->identifier(), scope); 357 return synchronize(resource->identifier(), scope);
@@ -337,6 +375,7 @@ KAsync::Job<QList<typename DomainType::Ptr>> Store::fetchAll(const Sink::Query &
337template <class DomainType> 375template <class DomainType>
338KAsync::Job<QList<typename DomainType::Ptr>> Store::fetch(const Sink::Query &query, int minimumAmount) 376KAsync::Job<QList<typename DomainType::Ptr>> Store::fetch(const Sink::Query &query, int minimumAmount)
339{ 377{
378 Q_ASSERT(sanityCheckQuery(query));
340 auto model = loadModel<DomainType>(query); 379 auto model = loadModel<DomainType>(query);
341 auto list = QSharedPointer<QList<typename DomainType::Ptr>>::create(); 380 auto list = QSharedPointer<QList<typename DomainType::Ptr>>::create();
342 auto context = QSharedPointer<QObject>::create(); 381 auto context = QSharedPointer<QObject>::create();
@@ -388,6 +427,7 @@ DomainType Store::readOne(const Sink::Query &query)
388template <class DomainType> 427template <class DomainType>
389QList<DomainType> Store::read(const Sink::Query &query_) 428QList<DomainType> Store::read(const Sink::Query &query_)
390{ 429{
430 Q_ASSERT(sanityCheckQuery(query_));
391 auto query = query_; 431 auto query = query_;
392 query.setFlags(Query::SynchronousQuery); 432 query.setFlags(Query::SynchronousQuery);
393 433