File indexing completed on 2024-05-12 05:25:59

0001 /*
0002  * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
0019  */
0020 
0021 #pragma once
0022 
0023 #include <KAsync/Async>
0024 #include <QByteArray>
0025 #include <QSharedPointer>
0026 #include <QPair>
0027 #include "applicationdomaintype.h"
0028 #include "resultprovider.h"
0029 
0030 namespace Sink {
0031 class Query;
0032 namespace Log {
0033     struct Context;
0034 }
0035 
0036 /**
0037  * Interface for the store facade.
0038  *
0039  * All methods are synchronous.
0040  * Facades are stateful (they hold connections to resources and database).
0041  *
0042  * TODO: would it make sense to split the write, read and notification parts? (we could potentially save some connections)
0043  */
0044 template <class DomainType>
0045 class StoreFacade
0046 {
0047 public:
0048     virtual ~StoreFacade(){};
0049     QByteArray type() const
0050     {
0051         return ApplicationDomain::getTypeName<DomainType>();
0052     }
0053 
0054     /**
0055      * Create an entity in the store.
0056      *
0057      * The job returns succefully once the task has been successfully placed in the queue
0058      */
0059     virtual KAsync::Job<void> create(const DomainType &domainObject) = 0;
0060 
0061     /**
0062      * Modify an entity in the store.
0063      *
0064      * The job returns succefully once the task has been successfully placed in the queue
0065      */
0066     virtual KAsync::Job<void> modify(const DomainType &domainObject) = 0;
0067 
0068     /**
0069      * Move an entity to a new resource.
0070      *
0071      * The job returns succefully once the task has been successfully placed in the queue
0072      */
0073     virtual KAsync::Job<void> move(const DomainType &domainObject, const QByteArray &newResource) = 0;
0074 
0075     /**
0076      * Copy an entity to a new resource.
0077      *
0078      * The job returns succefully once the task has been successfully placed in the queue
0079      */
0080     virtual KAsync::Job<void> copy(const DomainType &domainObject, const QByteArray &newResource) = 0;
0081 
0082     /**
0083      * Remove an entity from the store.
0084      *
0085      * The job returns succefully once the task has been successfully placed in the queue
0086      */
0087     virtual KAsync::Job<void> remove(const DomainType &domainObject) = 0;
0088 
0089     /**
0090      * Load entities from the store.
0091      */
0092     virtual QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename DomainType::Ptr>::Ptr> load(const Query &query, const Log::Context &) = 0;
0093 };
0094 
0095 template <class DomainType>
0096 class NullFacade : public StoreFacade<DomainType>
0097 {
0098 public:
0099     virtual ~NullFacade(){};
0100     KAsync::Job<void> create(const DomainType &domainObject) override
0101     {
0102         return KAsync::error<void>(-1, "Failed to create a facade");
0103     }
0104 
0105     KAsync::Job<void> modify(const DomainType &domainObject) override
0106     {
0107         return KAsync::error<void>(-1, "Failed to create a facade");
0108     }
0109 
0110     KAsync::Job<void> move(const DomainType &domainObject, const QByteArray &newResource) override
0111     {
0112         return KAsync::error<void>(-1, "Failed to create a facade");
0113     }
0114 
0115     KAsync::Job<void> copy(const DomainType &domainObject, const QByteArray &newResource) override
0116     {
0117         return KAsync::error<void>(-1, "Failed to create a facade");
0118     }
0119 
0120     KAsync::Job<void> remove(const DomainType &domainObject) override
0121     {
0122         return KAsync::error<void>(-1, "Failed to create a facade");
0123     }
0124 
0125     QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename DomainType::Ptr>::Ptr> load(const Query &query, const Log::Context &) override
0126     {
0127         return qMakePair(KAsync::null<void>(), typename Sink::ResultEmitter<typename DomainType::Ptr>::Ptr());
0128     }
0129 };
0130 }