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