##// END OF EJS Templates
Rebase fix CatalogueCatalogue
perrinel -
r1323:66e1a8e9fbb1
parent child
Show More
@@ -1,505 +1,492
1 1 #include <Catalogue/CatalogueController.h>
2 2
3 3 #include <Variable/Variable.h>
4 4
5 5 #include <CatalogueDao.h>
6 6
7 7 #include <ComparaisonPredicate.h>
8 8 #include <CompoundPredicate.h>
9 9 #include <DBCatalogue.h>
10 10 #include <DBEvent.h>
11 11 #include <DBEventProduct.h>
12 12 #include <DBTag.h>
13 13 #include <IRequestPredicate.h>
14 14
15 15 #include <QDataStream>
16 16 #include <QMutex>
17 17 #include <QThread>
18 18
19 19 #include <QDir>
20 20 #include <QStandardPaths>
21 21
22 22 Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController")
23 23
24 24 namespace {
25 25
26 26 static QString REPOSITORY_WORK_SUFFIX = QString{"_work"};
27 27 static QString REPOSITORY_TRASH_SUFFIX = QString{"_trash"};
28 28 }
29 29
30 30 /**
31 31 * Possible types of an repository
32 32 */
33 33 enum class DBType { SYNC, WORK, TRASH};
34 34 class CatalogueController::CatalogueControllerPrivate {
35 35
36 36 public:
37 37 explicit CatalogueControllerPrivate(CatalogueController *parent) : m_Q{parent} {}
38 38
39 39 CatalogueDao m_CatalogueDao;
40 40
41 41 QStringList m_RepositoryList;
42 42 CatalogueController *m_Q;
43 43
44 44 QSet<QString> m_EventKeysWithChanges;
45 45
46 46 QString eventUniqueKey(const std::shared_ptr<DBEvent> &event) const;
47 47
48 48 void copyDBtoDB(const QString &dbFrom, const QString &dbTo);
49 49 QString toWorkRepository(QString repository);
50 50 QString toSyncRepository(QString repository);
51 51 void savAllDB();
52 52
53 53 void saveEvent(std::shared_ptr<DBEvent> event, bool persist = true);
54 54 void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool persist = true);
55 55
56 56 std::shared_ptr<IRequestPredicate> createFinder(const QUuid& uniqId, const QString & repository, DBType type);
57 57 };
58 58
59 59 CatalogueController::CatalogueController(QObject *parent)
60 60 : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>(this)}
61 61 {
62 62 qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction")
63 63 << QThread::currentThread();
64 64 }
65 65
66 66 CatalogueController::~CatalogueController()
67 67 {
68 68 qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction")
69 69 << QThread::currentThread();
70 70 }
71 71
72 72 QStringList CatalogueController::getRepositories() const
73 73 {
74 74 return impl->m_RepositoryList;
75 75 }
76 76
77 77 void CatalogueController::addDB(const QString &dbPath)
78 78 {
79 79 QDir dbDir(dbPath);
80 80 if (dbDir.exists()) {
81 81 auto dirName = dbDir.dirName();
82 82
83 83 if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName)
84 84 != impl->m_RepositoryList.cend()) {
85 85 qCCritical(LOG_CatalogueController())
86 86 << tr("Impossible to addDB that is already loaded");
87 87 }
88 88
89 89 if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) {
90 90 qCCritical(LOG_CatalogueController())
91 91 << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath);
92 92 }
93 93 else {
94 94 impl->m_RepositoryList << dirName;
95 95 impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
96 96 }
97 97 }
98 98 else {
99 99 qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ")
100 100 << dbPath;
101 101 }
102 102 }
103 103
104 104 void CatalogueController::saveDB(const QString &destinationPath, const QString &repository)
105 105 {
106 106 if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) {
107 107 qCCritical(LOG_CatalogueController())
108 108 << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath);
109 109 }
110 110 }
111 111
112 112 std::list<std::shared_ptr<DBEvent> >
113 113 CatalogueController::retrieveEvents(const QString &repository) const
114 114 {
115 115 QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository;
116 116
117 117 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
118 118 auto events = impl->m_CatalogueDao.getEvents(impl->toWorkRepository(dbDireName));
119 119 for (auto event : events) {
120 120 eventsShared.push_back(std::make_shared<DBEvent>(event));
121 121 }
122 122 return eventsShared;
123 123 }
124 124
125 125 std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const
126 126 {
127 127 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
128 128 for (auto repository : impl->m_RepositoryList) {
129 129 eventsShared.splice(eventsShared.end(), retrieveEvents(repository));
130 130 }
131 131
132 132 return eventsShared;
133 133 }
134 134
135 135 std::list<std::shared_ptr<DBEvent> >
136 136 CatalogueController::retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const
137 137 {
138 138 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
139 139 auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue);
140 140 for (auto event : events) {
141 141 eventsShared.push_back(std::make_shared<DBEvent>(event));
142 142 }
143 143 return eventsShared;
144 144 }
145 145
146 146 void CatalogueController::updateEvent(std::shared_ptr<DBEvent> event)
147 147 {
148 148 event->setRepository(impl->toWorkRepository(event->getRepository()));
149 149
150 150 auto uniqueId = impl->eventUniqueKey(event);
151 151 impl->m_EventKeysWithChanges.insert(uniqueId);
152 152
153 153 impl->m_CatalogueDao.updateEvent(*event);
154 154 }
155 155
156 156 void CatalogueController::updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct)
157 157 {
158 158 impl->m_CatalogueDao.updateEventProduct(*eventProduct);
159 159 }
160 160
161 161 void CatalogueController::removeEvent(std::shared_ptr<DBEvent> event)
162 162 {
163 163 // Remove it from both repository and repository_work
164 164 event->setRepository(impl->toWorkRepository(event->getRepository()));
165 165 impl->m_CatalogueDao.removeEvent(*event);
166 166 event->setRepository(impl->toSyncRepository(event->getRepository()));
167 167 impl->m_CatalogueDao.removeEvent(*event);
168 168 impl->savAllDB();
169 169 }
170 170
171 171 void CatalogueController::addEvent(std::shared_ptr<DBEvent> event)
172 172 {
173 173 event->setRepository(impl->toWorkRepository(event->getRepository()));
174 174
175 175 auto eventTemp = *event;
176 176 impl->m_CatalogueDao.addEvent(eventTemp);
177 177
178 178 // Call update is necessary at the creation of add Event if it has some tags or some event
179 179 // products
180 180 if (!event->getEventProducts().empty() || !event->getTags().empty()) {
181 181
182 182 auto eventProductsTemp = eventTemp.getEventProducts();
183 183 auto eventProductTempUpdated = std::list<DBEventProduct>{};
184 184 for (auto eventProductTemp : eventProductsTemp) {
185 185 eventProductTemp.setEvent(eventTemp);
186 186 eventProductTempUpdated.push_back(eventProductTemp);
187 187 }
188 188 eventTemp.setEventProducts(eventProductTempUpdated);
189 189
190 190 impl->m_CatalogueDao.updateEvent(eventTemp);
191 191 }
192 192
193 <<<<<<< Upstream, based on feature/CatalogueDevelop2
194 auto uniqueId = impl->eventUniqueKey(event);
195 impl->m_EventKeysWithChanges.insert(uniqueId);
196 =======
197
198 // update event parameter
199 auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(
200 QString{"uniqId"}, event->getUniqId(), ComparaisonOperation::EQUALEQUAL);
201
202 auto workRepositoryPredicate = std::make_shared<ComparaisonPredicate>(
203 QString{"repository"}, impl->toWorkRepository(event->getRepository()),
204 ComparaisonOperation::EQUALEQUAL);
205
206 auto workPred = std::make_shared<CompoundPredicate>(CompoundOperation::AND);
207 workPred->AddRequestPredicate(uniqIdPredicate);
208 workPred->AddRequestPredicate(workRepositoryPredicate);
209
193 auto workPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::WORK);
210 194
211 195 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
212 196 *event = workEvent;
213 >>>>>>> 575bb1a Discard an added event remove it now.
197
198
199 auto uniqueId = impl->eventUniqueKey(event);
200 impl->m_EventKeysWithChanges.insert(uniqueId);
214 201 }
215 202
216 203 void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event)
217 204 {
218 205 impl->saveEvent(event, true);
219 206 impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
220 207 }
221 208
222 209 void CatalogueController::discardEvent(std::shared_ptr<DBEvent> event, bool &removed)
223 210 {
224 211 auto syncPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::SYNC);
225 212 auto workPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::WORK);
226 213
227 214 auto syncEvent = impl->m_CatalogueDao.getEvent(syncPred);
228 215 if (!syncEvent.getUniqId().isNull()) {
229 216 removed = false;
230 217 impl->m_CatalogueDao.copyEvent(syncEvent, impl->toWorkRepository(event->getRepository()),
231 218 true);
232 219
233 220 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
234 221 *event = workEvent;
235 222 impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
236 223 }
237 224 else {
238 225 removed = true;
239 226 // Since the element wasn't in sync repository. Discard it means remove it
240 227 event->setRepository(impl->toWorkRepository(event->getRepository()));
241 228 impl->m_CatalogueDao.removeEvent(*event);
242 229 }
243 230 }
244 231
245 232 bool CatalogueController::eventHasChanges(std::shared_ptr<DBEvent> event) const
246 233 {
247 234 return impl->m_EventKeysWithChanges.contains(impl->eventUniqueKey(event));
248 235 }
249 236
250 237 std::list<std::shared_ptr<DBCatalogue> >
251 238 CatalogueController::retrieveCatalogues(const QString &repository) const
252 239 {
253 240 QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository;
254 241
255 242 auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
256 243 auto catalogues = impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName));
257 244 for (auto catalogue : catalogues) {
258 245 cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue));
259 246 }
260 247 return cataloguesShared;
261 248 }
262 249
263 250 void CatalogueController::addCatalogue(std::shared_ptr<DBCatalogue> catalogue)
264 251 {
265 252 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
266 253
267 254 auto catalogueTemp = *catalogue;
268 255 impl->m_CatalogueDao.addCatalogue(catalogueTemp);
269 256
270 257 auto workPred = impl->createFinder(catalogue->getUniqId(), catalogue->getRepository(), DBType::WORK);
271 258
272 259 auto workCatalogue = impl->m_CatalogueDao.getCatalogue(workPred);
273 260 *catalogue = workCatalogue;
274 261
275 262 // auto uniqueId = impl->eventUniqueKey(catalogue);
276 263 // impl->m_EventKeysWithChanges.insert(uniqueId);
277 264 }
278 265
279 266 void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue> catalogue)
280 267 {
281 268 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
282 269
283 270 // auto uniqueId = impl->eventUniqueKey(event);
284 271 // impl->m_EventKeysWithChanges.insert(uniqueId);
285 272
286 273 impl->m_CatalogueDao.updateCatalogue(*catalogue);
287 274 }
288 275
289 276 void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue> catalogue)
290 277 {
291 278 // Remove it from both repository and repository_work
292 279 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
293 280 impl->m_CatalogueDao.removeCatalogue(*catalogue);
294 281 catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository()));
295 282 impl->m_CatalogueDao.removeCatalogue(*catalogue);
296 283 impl->savAllDB();
297 284 }
298 285
299 286 void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue> catalogue)
300 287 {
301 288 impl->saveCatalogue(catalogue, true);
302 289 // impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
303 290 }
304 291
305 292 void CatalogueController::saveAll()
306 293 {
307 294 for (auto repository : impl->m_RepositoryList) {
308 295 // Save Event
309 296 auto events = this->retrieveEvents(repository);
310 297 for (auto event : events) {
311 298 impl->saveEvent(event, false);
312 299 }
313 300
314 301 // Save Catalogue
315 302 auto catalogues = this->retrieveCatalogues(repository);
316 303 for (auto catalogue : catalogues) {
317 304 impl->saveCatalogue(catalogue, false);
318 305 }
319 306 }
320 307
321 308 impl->savAllDB();
322 309 impl->m_EventKeysWithChanges.clear();
323 310 }
324 311
325 312 bool CatalogueController::hasChanges() const
326 313 {
327 314 return !impl->m_EventKeysWithChanges.isEmpty(); // TODO: catalogues
328 315 }
329 316
330 317 QByteArray
331 318 CatalogueController::mimeDataForEvents(const QVector<std::shared_ptr<DBEvent> > &events) const
332 319 {
333 320 auto encodedData = QByteArray{};
334 321
335 322 QMap<QString, QVariantList> idsPerRepository;
336 323 for (auto event : events) {
337 324 idsPerRepository[event->getRepository()] << event->getUniqId();
338 325 }
339 326
340 327 QDataStream stream{&encodedData, QIODevice::WriteOnly};
341 328 stream << idsPerRepository;
342 329
343 330 return encodedData;
344 331 }
345 332
346 333 QVector<std::shared_ptr<DBEvent> >
347 334 CatalogueController::eventsForMimeData(const QByteArray &mimeData) const
348 335 {
349 336 auto events = QVector<std::shared_ptr<DBEvent> >{};
350 337 QDataStream stream{mimeData};
351 338
352 339 QMap<QString, QVariantList> idsPerRepository;
353 340 stream >> idsPerRepository;
354 341
355 342 for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend(); ++it) {
356 343 auto repository = it.key();
357 344 auto allRepositoryEvent = retrieveEvents(repository);
358 345 for (auto uuid : it.value()) {
359 346 for (auto repositoryEvent : allRepositoryEvent) {
360 347 if (uuid.toUuid() == repositoryEvent->getUniqId()) {
361 348 events << repositoryEvent;
362 349 }
363 350 }
364 351 }
365 352 }
366 353
367 354 return events;
368 355 }
369 356
370 357 void CatalogueController::initialize()
371 358 {
372 359 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init")
373 360 << QThread::currentThread();
374 361
375 362 impl->m_CatalogueDao.initialize();
376 363 auto defaultRepositoryLocation
377 364 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
378 365
379 366 QDir defaultRepositoryLocationDir;
380 367 if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) {
381 368 defaultRepositoryLocationDir.cd(defaultRepositoryLocation);
382 369 auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT);
383 370
384 371 qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ")
385 372 << defaultRepository;
386 373
387 374 QDir dbDir(defaultRepository);
388 375 impl->m_RepositoryList << REPOSITORY_DEFAULT;
389 376 if (dbDir.exists()) {
390 377 auto dirName = dbDir.dirName();
391 378
392 379 if (impl->m_CatalogueDao.addDB(defaultRepository, dirName)) {
393 380 impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
394 381 }
395 382 }
396 383 else {
397 384 qCInfo(LOG_CatalogueController()) << tr("Initialisation of Default repository detected")
398 385 << defaultRepository;
399 386 }
400 387 }
401 388 else {
402 389 qCWarning(LOG_CatalogueController())
403 390 << tr("Cannot load the persistent default repository from ")
404 391 << defaultRepositoryLocation;
405 392 }
406 393
407 394 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END");
408 395 }
409 396
410 397 QString CatalogueController::CatalogueControllerPrivate::eventUniqueKey(
411 398 const std::shared_ptr<DBEvent> &event) const
412 399 {
413 400 return event->getUniqId().toString().append(event->getRepository());
414 401 }
415 402
416 403 void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(const QString &dbFrom,
417 404 const QString &dbTo)
418 405 {
419 406 // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
420 407 auto catalogues = m_CatalogueDao.getCatalogues(dbFrom);
421 408 auto events = m_CatalogueDao.getEvents(dbFrom);
422 409 for (auto catalogue : catalogues) {
423 410 m_CatalogueDao.copyCatalogue(catalogue, dbTo, true);
424 411 }
425 412
426 413 for (auto event : events) {
427 414 m_CatalogueDao.copyEvent(event, dbTo, true);
428 415 }
429 416 }
430 417
431 418 QString CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString repository)
432 419 {
433 420 auto syncRepository = toSyncRepository(repository);
434 421
435 422 return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX);
436 423 }
437 424
438 425 QString CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString repository)
439 426 {
440 427 auto syncRepository = repository;
441 428 if (repository.endsWith(REPOSITORY_WORK_SUFFIX)) {
442 429 syncRepository.remove(REPOSITORY_WORK_SUFFIX);
443 430 }
444 431 else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX)) {
445 432 syncRepository.remove(REPOSITORY_TRASH_SUFFIX);
446 433 }
447 434 return syncRepository;
448 435 }
449 436
450 437 void CatalogueController::CatalogueControllerPrivate::savAllDB()
451 438 {
452 439 for (auto repository : m_RepositoryList) {
453 440 auto defaultRepositoryLocation
454 441 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
455 442 m_CatalogueDao.saveDB(defaultRepositoryLocation, repository);
456 443 }
457 444 }
458 445
459 446 void CatalogueController::CatalogueControllerPrivate::saveEvent(std::shared_ptr<DBEvent> event,
460 447 bool persist)
461 448 {
462 449 m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()), true);
463 450 if (persist) {
464 451 savAllDB();
465 452 }
466 453 }
467 454
468 455 void CatalogueController::CatalogueControllerPrivate::saveCatalogue(
469 456 std::shared_ptr<DBCatalogue> catalogue, bool persist)
470 457 {
471 458 m_CatalogueDao.copyCatalogue(*catalogue, toSyncRepository(catalogue->getRepository()), true);
472 459 if (persist) {
473 460 savAllDB();
474 461 }
475 462 }
476 463
477 464 std::shared_ptr<IRequestPredicate> CatalogueController::CatalogueControllerPrivate::createFinder(const QUuid &uniqId, const QString &repository, DBType type)
478 465 {
479 466 // update catalogue parameter
480 467 auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(
481 468 QString{"uniqId"}, uniqId, ComparaisonOperation::EQUALEQUAL);
482 469
483 470 auto repositoryType = repository;
484 471 switch (type) {
485 472 case DBType::SYNC:
486 473 repositoryType = toSyncRepository(repositoryType);
487 474 break;
488 475 case DBType::WORK:
489 476 repositoryType =toWorkRepository(repositoryType);
490 477 break;
491 478 case DBType::TRASH:
492 479 default:
493 480 break;
494 481 }
495 482
496 483 auto repositoryPredicate = std::make_shared<ComparaisonPredicate>(
497 484 QString{"repository"}, repositoryType,
498 485 ComparaisonOperation::EQUALEQUAL);
499 486
500 487 auto finderPred = std::make_shared<CompoundPredicate>(CompoundOperation::AND);
501 488 finderPred->AddRequestPredicate(uniqIdPredicate);
502 489 finderPred->AddRequestPredicate(repositoryPredicate);
503 490
504 491 return finderPred;
505 492 }
General Comments 0
You need to be logged in to leave comments. Login now