summaryrefslogtreecommitdiffstats
path: root/common/resourcefacade.cpp
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-04-12 22:17:52 +0200
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-04-12 22:17:52 +0200
commit471825e65bb629d1d3370c314a48a6594c3f5bca (patch)
treee4e945b0b5fbcfefd4dc1d1c96232ec79a5e41d1 /common/resourcefacade.cpp
parent2cbbdaca7c6e96e40b17b6af4672af2b6735bf8e (diff)
downloadsink-471825e65bb629d1d3370c314a48a6594c3f5bca.tar.gz
sink-471825e65bb629d1d3370c314a48a6594c3f5bca.zip
Ported ResourceFacade to LocalStorageFacade
Diffstat (limited to 'common/resourcefacade.cpp')
-rw-r--r--common/resourcefacade.cpp133
1 files changed, 25 insertions, 108 deletions
diff --git a/common/resourcefacade.cpp b/common/resourcefacade.cpp
index 935c613..32b508a 100644
--- a/common/resourcefacade.cpp
+++ b/common/resourcefacade.cpp
@@ -113,6 +113,19 @@ KAsync::Job<void> LocalStorageFacade<DomainType>::remove(const DomainType &domai
113 }); 113 });
114} 114}
115 115
116static bool matchesFilter(const QHash<QByteArray, QVariant> &filter, const QMap<QByteArray, QVariant> &properties)
117{
118 for (const auto &filterProperty : filter.keys()) {
119 if (filterProperty == "type") {
120 continue;
121 }
122 if (filter.value(filterProperty).toByteArray() != properties.value(filterProperty).toByteArray()) {
123 return false;
124 }
125 }
126 return true;
127}
128
116template <typename DomainType> 129template <typename DomainType>
117QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename DomainType::Ptr>::Ptr> LocalStorageFacade<DomainType>::load(const Sink::Query &query) 130QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename DomainType::Ptr>::Ptr> LocalStorageFacade<DomainType>::load(const Sink::Query &query)
118{ 131{
@@ -125,9 +138,18 @@ QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename DomainType::Ptr>:
125 const auto entries = mConfigStore.getEntries(); 138 const auto entries = mConfigStore.getEntries();
126 for (const auto &res : entries.keys()) { 139 for (const auto &res : entries.keys()) {
127 const auto type = entries.value(res); 140 const auto type = entries.value(res);
141 if (query.propertyFilter.contains("type") && query.propertyFilter.value("type").toByteArray() != type) {
142 Trace() << "Skipping due to type.";
143 continue;
144 }
128 if (!query.ids.isEmpty() && !query.ids.contains(res)) { 145 if (!query.ids.isEmpty() && !query.ids.contains(res)) {
129 continue; 146 continue;
130 } 147 }
148 const auto configurationValues = mConfigStore.get(res);
149 if (!matchesFilter(query.propertyFilter, configurationValues)){
150 Trace() << "Skipping due to filter.";
151 continue;
152 }
131 resultProvider->add(readFromConfig(res, type)); 153 resultProvider->add(readFromConfig(res, type));
132 } 154 }
133 if (query.liveQuery) { 155 if (query.liveQuery) {
@@ -149,12 +171,7 @@ QPair<KAsync::Job<void>, typename Sink::ResultEmitter<typename DomainType::Ptr>:
149} 171}
150 172
151 173
152 174ResourceFacade::ResourceFacade(const QByteArray &) : LocalStorageFacade<Sink::ApplicationDomain::SinkResource>("resources")
153
154
155
156
157ResourceFacade::ResourceFacade(const QByteArray &) : Sink::StoreFacade<Sink::ApplicationDomain::SinkResource>()
158{ 175{
159} 176}
160 177
@@ -162,60 +179,10 @@ ResourceFacade::~ResourceFacade()
162{ 179{
163} 180}
164 181
165KAsync::Job<void> ResourceFacade::create(const Sink::ApplicationDomain::SinkResource &resource)
166{
167 return KAsync::start<void>([resource, this]() {
168 const QByteArray type = resource.getProperty("type").toByteArray();
169 const QByteArray providedIdentifier = resource.getProperty("identifier").toByteArray();
170 // It is currently a requirement that the resource starts with the type
171 const QByteArray identifier = providedIdentifier.isEmpty() ? ResourceConfig::newIdentifier(type) : providedIdentifier;
172 Trace() << "Creating resource " << type << identifier;
173 ResourceConfig::addResource(identifier, type);
174 auto changedProperties = resource.changedProperties();
175 changedProperties.removeOne("identifier");
176 changedProperties.removeOne("type");
177 if (!changedProperties.isEmpty()) {
178 // We have some configuration values
179 QMap<QByteArray, QVariant> configurationValues;
180 for (const auto &property : changedProperties) {
181 configurationValues.insert(property, resource.getProperty(property));
182 }
183 ResourceConfig::configureResource(identifier, configurationValues);
184 }
185 });
186}
187
188KAsync::Job<void> ResourceFacade::modify(const Sink::ApplicationDomain::SinkResource &resource)
189{
190 return KAsync::start<void>([resource, this]() {
191 const QByteArray identifier = resource.identifier();
192 if (identifier.isEmpty()) {
193 Warning() << "We need an \"identifier\" property to identify the resource to configure.";
194 return;
195 }
196 auto changedProperties = resource.changedProperties();
197 changedProperties.removeOne("identifier");
198 changedProperties.removeOne("type");
199 if (!changedProperties.isEmpty()) {
200 auto config = ResourceConfig::getConfiguration(identifier);
201 for (const auto &property : changedProperties) {
202 config.insert(property, resource.getProperty(property));
203 }
204 ResourceConfig::configureResource(identifier, config);
205 }
206 });
207}
208
209KAsync::Job<void> ResourceFacade::remove(const Sink::ApplicationDomain::SinkResource &resource) 182KAsync::Job<void> ResourceFacade::remove(const Sink::ApplicationDomain::SinkResource &resource)
210{ 183{
211 return KAsync::start<void>([resource, this]() { 184 const auto identifier = resource.identifier();
212 const QByteArray identifier = resource.identifier(); 185 return LocalStorageFacade<Sink::ApplicationDomain::SinkResource>::remove(resource).then<void>([identifier]() {
213 if (identifier.isEmpty()) {
214 Warning() << "We need an \"identifier\" property to identify the resource to configure";
215 return;
216 }
217 Trace() << "Removing resource " << identifier;
218 ResourceConfig::removeResource(identifier);
219 // TODO shutdown resource, or use the resource process with a --remove option to cleanup (so we can take advantage of the file locking) 186 // TODO shutdown resource, or use the resource process with a --remove option to cleanup (so we can take advantage of the file locking)
220 QDir dir(Sink::storageLocation()); 187 QDir dir(Sink::storageLocation());
221 for (const auto &folder : dir.entryList(QStringList() << identifier + "*")) { 188 for (const auto &folder : dir.entryList(QStringList() << identifier + "*")) {
@@ -224,56 +191,6 @@ KAsync::Job<void> ResourceFacade::remove(const Sink::ApplicationDomain::SinkReso
224 }); 191 });
225} 192}
226 193
227static bool matchesFilter(const QHash<QByteArray, QVariant> &filter, const QMap<QByteArray, QVariant> &properties)
228{
229 for (const auto &filterProperty : filter.keys()) {
230 if (filterProperty == "type") {
231 continue;
232 }
233 if (filter.value(filterProperty).toByteArray() != properties.value(filterProperty).toByteArray()) {
234 return false;
235 }
236 }
237 return true;
238}
239
240QPair<KAsync::Job<void>, typename Sink::ResultEmitter<Sink::ApplicationDomain::SinkResource::Ptr>::Ptr> ResourceFacade::load(const Sink::Query &query)
241{
242 auto resultProvider = new Sink::ResultProvider<typename Sink::ApplicationDomain::SinkResource::Ptr>();
243 auto emitter = resultProvider->emitter();
244 resultProvider->setFetcher([](const QSharedPointer<Sink::ApplicationDomain::SinkResource> &) {});
245 resultProvider->onDone([resultProvider]() { delete resultProvider; });
246 auto job = KAsync::start<void>([query, resultProvider]() {
247 const auto configuredResources = ResourceConfig::getResources();
248 for (const auto &res : configuredResources.keys()) {
249 const auto type = configuredResources.value(res);
250 if (query.propertyFilter.contains("type") && query.propertyFilter.value("type").toByteArray() != type) {
251 continue;
252 }
253 if (!query.ids.isEmpty() && !query.ids.contains(res)) {
254 continue;
255 }
256 const auto configurationValues = ResourceConfig::getConfiguration(res);
257 if (!matchesFilter(query.propertyFilter, configurationValues)){
258 continue;
259 }
260
261 auto resource = Sink::ApplicationDomain::SinkResource::Ptr::create(res);
262 resource->setProperty("type", type);
263 for (auto it = configurationValues.constBegin(); it != configurationValues.constEnd(); it++) {
264 resource->setProperty(it.key(), it.value());
265 }
266
267 resultProvider->add(resource);
268 }
269 // TODO initialResultSetComplete should be implicit
270 resultProvider->initialResultSetComplete(Sink::ApplicationDomain::SinkResource::Ptr());
271 resultProvider->complete();
272 });
273 return qMakePair(job, emitter);
274}
275
276
277 194
278AccountFacade::AccountFacade(const QByteArray &) : LocalStorageFacade<Sink::ApplicationDomain::SinkAccount>("accounts") 195AccountFacade::AccountFacade(const QByteArray &) : LocalStorageFacade<Sink::ApplicationDomain::SinkAccount>("accounts")
279{ 196{