summaryrefslogtreecommitdiffstats
path: root/common/resourcefacade.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-09-27 16:13:47 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-09-27 16:13:47 +0200
commit529db49c496f4f668cec3f7c59d2d0ec78c50c9a (patch)
treeb7622a54b4063f4dc735a8b4061525148377b6c9 /common/resourcefacade.cpp
parent577c2c344079c1a87d3d93be5f957e5f2d935bff (diff)
downloadsink-529db49c496f4f668cec3f7c59d2d0ec78c50c9a.tar.gz
sink-529db49c496f4f668cec3f7c59d2d0ec78c50c9a.zip
Don't hardcode the type property.
Diffstat (limited to 'common/resourcefacade.cpp')
-rw-r--r--common/resourcefacade.cpp45
1 files changed, 24 insertions, 21 deletions
diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp
index 391b1d1..aee5706 100644
--- a/common/resourcefacade.cpp
+++ b/common/resourcefacade.cpp
@@ -37,7 +37,7 @@ template <typename DomainType>
37static typename DomainType::Ptr readFromConfig(ConfigStore &configStore, const QByteArray &id, const QByteArray &type) 37static typename DomainType::Ptr readFromConfig(ConfigStore &configStore, const QByteArray &id, const QByteArray &type)
38{ 38{
39 auto object = DomainType::Ptr::create(id); 39 auto object = DomainType::Ptr::create(id);
40 object->setProperty("type", type); 40 object->setProperty(ApplicationDomain::SinkResource::ResourceType::name, type);
41 const auto configurationValues = configStore.get(id); 41 const auto configurationValues = configStore.get(id);
42 for (auto it = configurationValues.constBegin(); it != configurationValues.constEnd(); it++) { 42 for (auto it = configurationValues.constBegin(); it != configurationValues.constEnd(); it++) {
43 object->setProperty(it.key(), it.value()); 43 object->setProperty(it.key(), it.value());
@@ -48,7 +48,7 @@ static typename DomainType::Ptr readFromConfig(ConfigStore &configStore, const Q
48static bool matchesFilter(const QHash<QByteArray, Query::Comparator> &filter, const QMap<QByteArray, QVariant> &properties) 48static bool matchesFilter(const QHash<QByteArray, Query::Comparator> &filter, const QMap<QByteArray, QVariant> &properties)
49{ 49{
50 for (const auto &filterProperty : filter.keys()) { 50 for (const auto &filterProperty : filter.keys()) {
51 if (filterProperty == "type") { 51 if (filterProperty == ApplicationDomain::SinkResource::ResourceType::name) {
52 continue; 52 continue;
53 } 53 }
54 if (!filter.value(filterProperty).matches(properties.value(filterProperty))) { 54 if (!filter.value(filterProperty).matches(properties.value(filterProperty))) {
@@ -59,8 +59,8 @@ static bool matchesFilter(const QHash<QByteArray, Query::Comparator> &filter, co
59} 59}
60 60
61template<typename DomainType> 61template<typename DomainType>
62LocalStorageQueryRunner<DomainType>::LocalStorageQueryRunner(const Query &query, const QByteArray &identifier, ConfigNotifier &configNotifier) 62LocalStorageQueryRunner<DomainType>::LocalStorageQueryRunner(const Query &query, const QByteArray &identifier, const QByteArray &typeName, ConfigNotifier &configNotifier)
63 : mResultProvider(new ResultProvider<typename DomainType::Ptr>), mConfigStore(identifier), mGuard(new QObject) 63 : mResultProvider(new ResultProvider<typename DomainType::Ptr>), mConfigStore(identifier, typeName), mGuard(new QObject)
64{ 64{
65 QObject *guard = new QObject; 65 QObject *guard = new QObject;
66 mResultProvider->setFetcher([this, query, guard, &configNotifier](const QSharedPointer<DomainType> &) { 66 mResultProvider->setFetcher([this, query, guard, &configNotifier](const QSharedPointer<DomainType> &) {
@@ -68,7 +68,7 @@ LocalStorageQueryRunner<DomainType>::LocalStorageQueryRunner(const Query &query,
68 for (const auto &res : entries.keys()) { 68 for (const auto &res : entries.keys()) {
69 const auto type = entries.value(res); 69 const auto type = entries.value(res);
70 70
71 if (query.hasFilter("type") && query.getFilter("type").value.toByteArray() != type) { 71 if (query.hasFilter(ApplicationDomain::SinkResource::ResourceType::name) && query.getFilter(ApplicationDomain::SinkResource::ResourceType::name).value.toByteArray() != type) {
72 SinkTrace() << "Skipping due to type."; 72 SinkTrace() << "Skipping due to type.";
73 continue; 73 continue;
74 } 74 }
@@ -154,7 +154,7 @@ typename Sink::ResultEmitter<typename DomainType::Ptr>::Ptr LocalStorageQueryRun
154 154
155 155
156template <typename DomainType> 156template <typename DomainType>
157LocalStorageFacade<DomainType>::LocalStorageFacade(const QByteArray &identifier) : StoreFacade<DomainType>(), mIdentifier(identifier) 157LocalStorageFacade<DomainType>::LocalStorageFacade(const QByteArray &identifier, const QByteArray &typeName) : StoreFacade<DomainType>(), mIdentifier(identifier), mTypeName(typeName)
158{ 158{
159} 159}
160 160
@@ -167,15 +167,16 @@ template <typename DomainType>
167KAsync::Job<void> LocalStorageFacade<DomainType>::create(const DomainType &domainObject) 167KAsync::Job<void> LocalStorageFacade<DomainType>::create(const DomainType &domainObject)
168{ 168{
169 auto configStoreIdentifier = mIdentifier; 169 auto configStoreIdentifier = mIdentifier;
170 return KAsync::syncStart<void>([domainObject, configStoreIdentifier]() { 170 auto typeName = mTypeName;
171 const QByteArray type = domainObject.getProperty("type").toByteArray(); 171 return KAsync::syncStart<void>([domainObject, configStoreIdentifier, typeName]() {
172 const QByteArray type = domainObject.getProperty(typeName).toByteArray();
172 const QByteArray providedIdentifier = domainObject.identifier().isEmpty() ? domainObject.getProperty("identifier").toByteArray() : domainObject.identifier(); 173 const QByteArray providedIdentifier = domainObject.identifier().isEmpty() ? domainObject.getProperty("identifier").toByteArray() : domainObject.identifier();
173 const QByteArray identifier = providedIdentifier.isEmpty() ? ResourceConfig::newIdentifier(type) : providedIdentifier; 174 const QByteArray identifier = providedIdentifier.isEmpty() ? ResourceConfig::newIdentifier(type) : providedIdentifier;
174 auto configStore = ConfigStore(configStoreIdentifier); 175 auto configStore = ConfigStore(configStoreIdentifier, typeName);
175 configStore.add(identifier, type); 176 configStore.add(identifier, type);
176 auto changedProperties = domainObject.changedProperties(); 177 auto changedProperties = domainObject.changedProperties();
177 changedProperties.removeOne("identifier"); 178 changedProperties.removeOne("identifier");
178 changedProperties.removeOne("type"); 179 changedProperties.removeOne(typeName);
179 if (!changedProperties.isEmpty()) { 180 if (!changedProperties.isEmpty()) {
180 // We have some configuration values 181 // We have some configuration values
181 QMap<QByteArray, QVariant> configurationValues; 182 QMap<QByteArray, QVariant> configurationValues;
@@ -192,7 +193,8 @@ template <typename DomainType>
192KAsync::Job<void> LocalStorageFacade<DomainType>::modify(const DomainType &domainObject) 193KAsync::Job<void> LocalStorageFacade<DomainType>::modify(const DomainType &domainObject)
193{ 194{
194 auto configStoreIdentifier = mIdentifier; 195 auto configStoreIdentifier = mIdentifier;
195 return KAsync::syncStart<void>([domainObject, configStoreIdentifier]() { 196 auto typeName = mTypeName;
197 return KAsync::syncStart<void>([domainObject, configStoreIdentifier, typeName]() {
196 const QByteArray identifier = domainObject.identifier(); 198 const QByteArray identifier = domainObject.identifier();
197 if (identifier.isEmpty()) { 199 if (identifier.isEmpty()) {
198 SinkWarning() << "We need an \"identifier\" property to identify the entity to configure."; 200 SinkWarning() << "We need an \"identifier\" property to identify the entity to configure.";
@@ -200,8 +202,8 @@ KAsync::Job<void> LocalStorageFacade<DomainType>::modify(const DomainType &domai
200 } 202 }
201 auto changedProperties = domainObject.changedProperties(); 203 auto changedProperties = domainObject.changedProperties();
202 changedProperties.removeOne("identifier"); 204 changedProperties.removeOne("identifier");
203 changedProperties.removeOne("type"); 205 changedProperties.removeOne(typeName);
204 auto configStore = ConfigStore(configStoreIdentifier); 206 auto configStore = ConfigStore(configStoreIdentifier, typeName);
205 if (!changedProperties.isEmpty()) { 207 if (!changedProperties.isEmpty()) {
206 // We have some configuration values 208 // We have some configuration values
207 QMap<QByteArray, QVariant> configurationValues; 209 QMap<QByteArray, QVariant> configurationValues;
@@ -220,14 +222,15 @@ template <typename DomainType>
220KAsync::Job<void> LocalStorageFacade<DomainType>::remove(const DomainType &domainObject) 222KAsync::Job<void> LocalStorageFacade<DomainType>::remove(const DomainType &domainObject)
221{ 223{
222 auto configStoreIdentifier = mIdentifier; 224 auto configStoreIdentifier = mIdentifier;
223 return KAsync::syncStart<void>([domainObject, configStoreIdentifier]() { 225 auto typeName = mTypeName;
226 return KAsync::syncStart<void>([domainObject, configStoreIdentifier, typeName]() {
224 const QByteArray identifier = domainObject.identifier(); 227 const QByteArray identifier = domainObject.identifier();
225 if (identifier.isEmpty()) { 228 if (identifier.isEmpty()) {
226 SinkWarning() << "We need an \"identifier\" property to identify the entity to configure"; 229 SinkWarning() << "We need an \"identifier\" property to identify the entity to configure";
227 return; 230 return;
228 } 231 }
229 SinkTrace() << "Removing: " << identifier; 232 SinkTrace() << "Removing: " << identifier;
230 auto configStore = ConfigStore(configStoreIdentifier); 233 auto configStore = ConfigStore(configStoreIdentifier, typeName);
231 configStore.remove(identifier); 234 configStore.remove(identifier);
232 sConfigNotifier.remove(QSharedPointer<DomainType>::create(domainObject)); 235 sConfigNotifier.remove(QSharedPointer<DomainType>::create(domainObject));
233 }); 236 });
@@ -236,11 +239,11 @@ KAsync::Job<void> LocalStorageFacade<DomainType>::remove(const DomainType &domai
236template <typename DomainType> 239template <typename DomainType>
237QPair<KAsync::Job<void>, typename ResultEmitter<typename DomainType::Ptr>::Ptr> LocalStorageFacade<DomainType>::load(const Query &query) 240QPair<KAsync::Job<void>, typename ResultEmitter<typename DomainType::Ptr>::Ptr> LocalStorageFacade<DomainType>::load(const Query &query)
238{ 241{
239 auto runner = new LocalStorageQueryRunner<DomainType>(query, mIdentifier, sConfigNotifier); 242 auto runner = new LocalStorageQueryRunner<DomainType>(query, mIdentifier, mTypeName, sConfigNotifier);
240 return qMakePair(KAsync::null<void>(), runner->emitter()); 243 return qMakePair(KAsync::null<void>(), runner->emitter());
241} 244}
242 245
243ResourceFacade::ResourceFacade() : LocalStorageFacade<Sink::ApplicationDomain::SinkResource>("resources") 246ResourceFacade::ResourceFacade() : LocalStorageFacade<Sink::ApplicationDomain::SinkResource>("resources", Sink::ApplicationDomain::SinkResource::ResourceType::name)
244{ 247{
245} 248}
246 249
@@ -256,7 +259,7 @@ KAsync::Job<void> ResourceFacade::remove(const Sink::ApplicationDomain::SinkReso
256 259
257QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename ApplicationDomain::SinkResource::Ptr>::Ptr> ResourceFacade::load(const Sink::Query &query) 260QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename ApplicationDomain::SinkResource::Ptr>::Ptr> ResourceFacade::load(const Sink::Query &query)
258{ 261{
259 auto runner = new LocalStorageQueryRunner<ApplicationDomain::SinkResource>(query, mIdentifier, sConfigNotifier); 262 auto runner = new LocalStorageQueryRunner<ApplicationDomain::SinkResource>(query, mIdentifier, mTypeName, sConfigNotifier);
260 auto monitoredResources = QSharedPointer<QSet<QByteArray>>::create(); 263 auto monitoredResources = QSharedPointer<QSet<QByteArray>>::create();
261 runner->setStatusUpdater([runner, monitoredResources](ApplicationDomain::SinkResource &resource) { 264 runner->setStatusUpdater([runner, monitoredResources](ApplicationDomain::SinkResource &resource) {
262 auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource.identifier(), ResourceConfig::getResourceType(resource.identifier())); 265 auto resourceAccess = ResourceAccessFactory::instance().getAccess(resource.identifier(), ResourceConfig::getResourceType(resource.identifier()));
@@ -276,7 +279,7 @@ QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename ApplicationDomain
276} 279}
277 280
278 281
279AccountFacade::AccountFacade() : LocalStorageFacade<Sink::ApplicationDomain::SinkAccount>("accounts") 282AccountFacade::AccountFacade() : LocalStorageFacade<Sink::ApplicationDomain::SinkAccount>("accounts", "type")
280{ 283{
281} 284}
282 285
@@ -286,7 +289,7 @@ AccountFacade::~AccountFacade()
286 289
287QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename ApplicationDomain::SinkAccount::Ptr>::Ptr> AccountFacade::load(const Sink::Query &query) 290QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename ApplicationDomain::SinkAccount::Ptr>::Ptr> AccountFacade::load(const Sink::Query &query)
288{ 291{
289 auto runner = new LocalStorageQueryRunner<ApplicationDomain::SinkAccount>(query, mIdentifier, sConfigNotifier); 292 auto runner = new LocalStorageQueryRunner<ApplicationDomain::SinkAccount>(query, mIdentifier, mTypeName, sConfigNotifier);
290 auto monitoredResources = QSharedPointer<QSet<QByteArray>>::create(); 293 auto monitoredResources = QSharedPointer<QSet<QByteArray>>::create();
291 runner->setStatusUpdater([runner, monitoredResources](ApplicationDomain::SinkAccount &account) { 294 runner->setStatusUpdater([runner, monitoredResources](ApplicationDomain::SinkAccount &account) {
292 Query query; 295 Query query;
@@ -333,7 +336,7 @@ QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename ApplicationDomain
333 return qMakePair(KAsync::null<void>(), runner->emitter()); 336 return qMakePair(KAsync::null<void>(), runner->emitter());
334} 337}
335 338
336IdentityFacade::IdentityFacade() : LocalStorageFacade<Sink::ApplicationDomain::Identity>("identities") 339IdentityFacade::IdentityFacade() : LocalStorageFacade<Sink::ApplicationDomain::Identity>("identities", "type")
337{ 340{
338} 341}
339 342