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

0001 /*
0002  *   Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 
0020 #include "facade.h"
0021 
0022 #include "commands.h"
0023 #include "log.h"
0024 #include "storage.h"
0025 #include "definitions.h"
0026 #include "domainadaptor.h"
0027 #include "queryrunner.h"
0028 #include "bufferutils.h"
0029 #include "resourceconfig.h"
0030 
0031 using namespace Sink;
0032 
0033 template <class DomainType>
0034 GenericFacade<DomainType>::GenericFacade(const ResourceContext &context)
0035     : Sink::StoreFacade<DomainType>(), mResourceContext(context), mResourceAccess(mResourceContext.resourceAccess())
0036 {
0037 }
0038 
0039 template <class DomainType>
0040 GenericFacade<DomainType>::~GenericFacade()
0041 {
0042 }
0043 
0044 template <class DomainType>
0045 QByteArray GenericFacade<DomainType>::bufferTypeForDomainType()
0046 {
0047     // We happen to have a one to one mapping
0048     return Sink::ApplicationDomain::getTypeName<DomainType>();
0049 }
0050 
0051 template <class DomainType>
0052 KAsync::Job<void> GenericFacade<DomainType>::create(const DomainType &domainObject)
0053 {
0054     flatbuffers::FlatBufferBuilder entityFbb;
0055     if (!mResourceContext.adaptorFactory<DomainType>().createBuffer(domainObject, entityFbb)) {
0056         SinkWarning() << "No domain type adaptor factory available";
0057         return KAsync::error<void>();
0058     }
0059     return mResourceAccess->sendCreateCommand(domainObject.identifier(), bufferTypeForDomainType(), BufferUtils::extractBuffer(entityFbb));
0060 }
0061 
0062 template <class DomainType>
0063 KAsync::Job<void> GenericFacade<DomainType>::modify(const DomainType &domainObject)
0064 {
0065     SinkTrace() << "Modifying entity: " << domainObject.identifier() << domainObject.changedProperties();
0066     flatbuffers::FlatBufferBuilder entityFbb;
0067     if (!mResourceContext.adaptorFactory<DomainType>().createBuffer(domainObject, entityFbb)) {
0068         SinkWarning() << "No domain type adaptor factory available";
0069         return KAsync::error<void>();
0070     }
0071     return mResourceAccess->sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), BufferUtils::extractBuffer(entityFbb), domainObject.changedProperties(), QByteArray(), false);
0072 }
0073 
0074 template <class DomainType>
0075 KAsync::Job<void> GenericFacade<DomainType>::move(const DomainType &domainObject, const QByteArray &newResource)
0076 {
0077     SinkTrace() << "Moving entity: " << domainObject.identifier() << domainObject.changedProperties() << newResource;
0078     flatbuffers::FlatBufferBuilder entityFbb;
0079     if (!mResourceContext.adaptorFactory<DomainType>().createBuffer(domainObject, entityFbb)) {
0080         SinkWarning() << "No domain type adaptor factory available";
0081         return KAsync::error<void>();
0082     }
0083     return mResourceAccess->sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), BufferUtils::extractBuffer(entityFbb), domainObject.changedProperties(), newResource, true);
0084 }
0085 
0086 template <class DomainType>
0087 KAsync::Job<void> GenericFacade<DomainType>::copy(const DomainType &domainObject, const QByteArray &newResource)
0088 {
0089     SinkTrace() << "Copying entity: " << domainObject.identifier() << domainObject.changedProperties() << newResource;
0090     flatbuffers::FlatBufferBuilder entityFbb;
0091     if (!mResourceContext.adaptorFactory<DomainType>().createBuffer(domainObject, entityFbb)) {
0092         SinkWarning() << "No domain type adaptor factory available";
0093         return KAsync::error<void>();
0094     }
0095     return mResourceAccess->sendModifyCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType(), QByteArrayList(), BufferUtils::extractBuffer(entityFbb), domainObject.changedProperties(), newResource, false);
0096 }
0097 
0098 template <class DomainType>
0099 KAsync::Job<void> GenericFacade<DomainType>::remove(const DomainType &domainObject)
0100 {
0101     return mResourceAccess->sendDeleteCommand(domainObject.identifier(), domainObject.revision(), bufferTypeForDomainType());
0102 }
0103 
0104 template <class DomainType>
0105 QPair<KAsync::Job<void>, typename ResultEmitter<typename DomainType::Ptr>::Ptr> GenericFacade<DomainType>::load(const Sink::Query &query, const Log::Context &ctx)
0106 {
0107     Q_ASSERT(DomainType::name == query.type() || query.type().isEmpty());
0108     // The runner lives for the lifetime of the query
0109     auto runner = new QueryRunner<DomainType>(query, mResourceContext, bufferTypeForDomainType(), ctx);
0110     runner->setResultTransformation(mResultTransformation);
0111     return qMakePair(KAsync::null<void>(), runner->emitter());
0112 }
0113 
0114 #define REGISTER_TYPE(T) \
0115     template class Sink::GenericFacade<T>; \
0116 
0117 SINK_REGISTER_TYPES()