diff options
Diffstat (limited to 'common/store.cpp')
-rw-r--r-- | common/store.cpp | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/common/store.cpp b/common/store.cpp index 92915c4..1162a18 100644 --- a/common/store.cpp +++ b/common/store.cpp | |||
@@ -286,6 +286,51 @@ KAsync::Job<QList<typename DomainType::Ptr>> Store::fetch(const Sink::Query &que | |||
286 | }); | 286 | }); |
287 | } | 287 | } |
288 | 288 | ||
289 | template <class DomainType> | ||
290 | DomainType Store::readOne(const Sink::Query &query) | ||
291 | { | ||
292 | const auto list = read<DomainType>(query); | ||
293 | if (!list.isEmpty()) { | ||
294 | return list.first(); | ||
295 | } | ||
296 | return DomainType(); | ||
297 | } | ||
298 | |||
299 | template <class DomainType> | ||
300 | QList<DomainType> Store::read(const Sink::Query &q) | ||
301 | { | ||
302 | auto query = q; | ||
303 | query.synchronousQuery = true; | ||
304 | query.liveQuery = false; | ||
305 | QList<DomainType> list; | ||
306 | auto resources = getResources(query.resources, query.accounts, ApplicationDomain::getTypeName<DomainType>()); | ||
307 | auto aggregatingEmitter = AggregatingResultEmitter<typename DomainType::Ptr>::Ptr::create(); | ||
308 | aggregatingEmitter->onAdded([&list](const typename DomainType::Ptr &value){ | ||
309 | Trace() << "Found value: " << value->identifier(); | ||
310 | list << *value; | ||
311 | }); | ||
312 | for (const auto resourceInstanceIdentifier : resources.keys()) { | ||
313 | const auto resourceType = resources.value(resourceInstanceIdentifier); | ||
314 | Trace() << "Looking for " << resourceType << resourceInstanceIdentifier; | ||
315 | auto facade = FacadeFactory::instance().getFacade<DomainType>(resourceType, resourceInstanceIdentifier); | ||
316 | if (facade) { | ||
317 | Trace() << "Trying to fetch from resource " << resourceInstanceIdentifier; | ||
318 | auto result = facade->load(query); | ||
319 | if (result.second) { | ||
320 | aggregatingEmitter->addEmitter(result.second); | ||
321 | } else { | ||
322 | Warning() << "Null emitter for resource " << resourceInstanceIdentifier; | ||
323 | } | ||
324 | result.first.exec(); | ||
325 | aggregatingEmitter->fetch(typename DomainType::Ptr()); | ||
326 | } else { | ||
327 | Trace() << "Couldn't find a facade for " << resourceInstanceIdentifier; | ||
328 | // Ignore the error and carry on | ||
329 | } | ||
330 | } | ||
331 | return list; | ||
332 | } | ||
333 | |||
289 | #define REGISTER_TYPE(T) \ | 334 | #define REGISTER_TYPE(T) \ |
290 | template KAsync::Job<void> Store::remove<T>(const T &domainObject); \ | 335 | template KAsync::Job<void> Store::remove<T>(const T &domainObject); \ |
291 | template KAsync::Job<void> Store::create<T>(const T &domainObject); \ | 336 | template KAsync::Job<void> Store::create<T>(const T &domainObject); \ |
@@ -293,7 +338,9 @@ KAsync::Job<QList<typename DomainType::Ptr>> Store::fetch(const Sink::Query &que | |||
293 | template QSharedPointer<QAbstractItemModel> Store::loadModel<T>(Query query); \ | 338 | template QSharedPointer<QAbstractItemModel> Store::loadModel<T>(Query query); \ |
294 | template KAsync::Job<T> Store::fetchOne<T>(const Query &); \ | 339 | template KAsync::Job<T> Store::fetchOne<T>(const Query &); \ |
295 | template KAsync::Job<QList<T::Ptr>> Store::fetchAll<T>(const Query &); \ | 340 | template KAsync::Job<QList<T::Ptr>> Store::fetchAll<T>(const Query &); \ |
296 | template KAsync::Job<QList<T::Ptr>> Store::fetch<T>(const Query &, int); | 341 | template KAsync::Job<QList<T::Ptr>> Store::fetch<T>(const Query &, int); \ |
342 | template T Store::readOne<T>(const Query &); \ | ||
343 | template QList<T> Store::read<T>(const Query &); | ||
297 | 344 | ||
298 | REGISTER_TYPE(ApplicationDomain::Event); | 345 | REGISTER_TYPE(ApplicationDomain::Event); |
299 | REGISTER_TYPE(ApplicationDomain::Mail); | 346 | REGISTER_TYPE(ApplicationDomain::Mail); |