##// END OF EJS Templates
Rebase fix CatalogueCatalogue
perrinel -
r1355:ce843f4ddfb0
parent child
Show More
@@ -1,500 +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
193 auto workPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::WORK);
194 // update event parameter
195 auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(
196 QString{"uniqId"}, event->getUniqId(), ComparaisonOperation::EQUALEQUAL);
197
198 auto workRepositoryPredicate = std::make_shared<ComparaisonPredicate>(
199 QString{"repository"}, impl->toWorkRepository(event->getRepository()),
200 ComparaisonOperation::EQUALEQUAL);
201
202 auto workPred = std::make_shared<CompoundPredicate>(CompoundOperation::AND);
203 workPred->AddRequestPredicate(uniqIdPredicate);
204 workPred->AddRequestPredicate(workRepositoryPredicate);
205
206
194
207 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
195 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
208 *event = workEvent;
196 *event = workEvent;
197
198
199 auto uniqueId = impl->eventUniqueKey(event);
200 impl->m_EventKeysWithChanges.insert(uniqueId);
209 }
201 }
210
202
211 void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event)
203 void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event)
212 {
204 {
213 impl->saveEvent(event, true);
205 impl->saveEvent(event, true);
214 impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
206 impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
215 }
207 }
216
208
217 void CatalogueController::discardEvent(std::shared_ptr<DBEvent> event, bool &removed)
209 void CatalogueController::discardEvent(std::shared_ptr<DBEvent> event, bool &removed)
218 {
210 {
219 auto syncPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::SYNC);
211 auto syncPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::SYNC);
220 auto workPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::WORK);
212 auto workPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::WORK);
221
213
222 auto syncEvent = impl->m_CatalogueDao.getEvent(syncPred);
214 auto syncEvent = impl->m_CatalogueDao.getEvent(syncPred);
223 if (!syncEvent.getUniqId().isNull()) {
215 if (!syncEvent.getUniqId().isNull()) {
224 removed = false;
216 removed = false;
225 impl->m_CatalogueDao.copyEvent(syncEvent, impl->toWorkRepository(event->getRepository()),
217 impl->m_CatalogueDao.copyEvent(syncEvent, impl->toWorkRepository(event->getRepository()),
226 true);
218 true);
227
219
228 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
220 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
229 *event = workEvent;
221 *event = workEvent;
230 impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
222 impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
231 }
223 }
232 else {
224 else {
233 removed = true;
225 removed = true;
234 // 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
235 event->setRepository(impl->toWorkRepository(event->getRepository()));
227 event->setRepository(impl->toWorkRepository(event->getRepository()));
236 impl->m_CatalogueDao.removeEvent(*event);
228 impl->m_CatalogueDao.removeEvent(*event);
237 }
229 }
238 }
230 }
239
231
240 bool CatalogueController::eventHasChanges(std::shared_ptr<DBEvent> event) const
232 bool CatalogueController::eventHasChanges(std::shared_ptr<DBEvent> event) const
241 {
233 {
242 return impl->m_EventKeysWithChanges.contains(impl->eventUniqueKey(event));
234 return impl->m_EventKeysWithChanges.contains(impl->eventUniqueKey(event));
243 }
235 }
244
236
245 std::list<std::shared_ptr<DBCatalogue> >
237 std::list<std::shared_ptr<DBCatalogue> >
246 CatalogueController::retrieveCatalogues(const QString &repository) const
238 CatalogueController::retrieveCatalogues(const QString &repository) const
247 {
239 {
248 QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository;
240 QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository;
249
241
250 auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
242 auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
251 auto catalogues = impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName));
243 auto catalogues = impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName));
252 for (auto catalogue : catalogues) {
244 for (auto catalogue : catalogues) {
253 cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue));
245 cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue));
254 }
246 }
255 return cataloguesShared;
247 return cataloguesShared;
256 }
248 }
257
249
258 void CatalogueController::addCatalogue(std::shared_ptr<DBCatalogue> catalogue)
250 void CatalogueController::addCatalogue(std::shared_ptr<DBCatalogue> catalogue)
259 {
251 {
260 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
252 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
261
253
262 auto catalogueTemp = *catalogue;
254 auto catalogueTemp = *catalogue;
263 impl->m_CatalogueDao.addCatalogue(catalogueTemp);
255 impl->m_CatalogueDao.addCatalogue(catalogueTemp);
264
256
265 auto workPred = impl->createFinder(catalogue->getUniqId(), catalogue->getRepository(), DBType::WORK);
257 auto workPred = impl->createFinder(catalogue->getUniqId(), catalogue->getRepository(), DBType::WORK);
266
258
267 auto workCatalogue = impl->m_CatalogueDao.getCatalogue(workPred);
259 auto workCatalogue = impl->m_CatalogueDao.getCatalogue(workPred);
268 *catalogue = workCatalogue;
260 *catalogue = workCatalogue;
269
261
270 // auto uniqueId = impl->eventUniqueKey(catalogue);
262 // auto uniqueId = impl->eventUniqueKey(catalogue);
271 // impl->m_EventKeysWithChanges.insert(uniqueId);
263 // impl->m_EventKeysWithChanges.insert(uniqueId);
272 }
264 }
273
265
274 void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue> catalogue)
266 void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue> catalogue)
275 {
267 {
276 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
268 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
277
269
278 // auto uniqueId = impl->eventUniqueKey(event);
270 // auto uniqueId = impl->eventUniqueKey(event);
279 // impl->m_EventKeysWithChanges.insert(uniqueId);
271 // impl->m_EventKeysWithChanges.insert(uniqueId);
280
272
281 impl->m_CatalogueDao.updateCatalogue(*catalogue);
273 impl->m_CatalogueDao.updateCatalogue(*catalogue);
282 }
274 }
283
275
284 void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue> catalogue)
276 void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue> catalogue)
285 {
277 {
286 // Remove it from both repository and repository_work
278 // Remove it from both repository and repository_work
287 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
279 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
288 impl->m_CatalogueDao.removeCatalogue(*catalogue);
280 impl->m_CatalogueDao.removeCatalogue(*catalogue);
289 catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository()));
281 catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository()));
290 impl->m_CatalogueDao.removeCatalogue(*catalogue);
282 impl->m_CatalogueDao.removeCatalogue(*catalogue);
291 impl->savAllDB();
283 impl->savAllDB();
292 }
284 }
293
285
294 void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue> catalogue)
286 void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue> catalogue)
295 {
287 {
296 impl->saveCatalogue(catalogue, true);
288 impl->saveCatalogue(catalogue, true);
297 // impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
289 // impl->m_EventKeysWithChanges.remove(impl->eventUniqueKey(event));
298 }
290 }
299
291
300 void CatalogueController::saveAll()
292 void CatalogueController::saveAll()
301 {
293 {
302 for (auto repository : impl->m_RepositoryList) {
294 for (auto repository : impl->m_RepositoryList) {
303 // Save Event
295 // Save Event
304 auto events = this->retrieveEvents(repository);
296 auto events = this->retrieveEvents(repository);
305 for (auto event : events) {
297 for (auto event : events) {
306 impl->saveEvent(event, false);
298 impl->saveEvent(event, false);
307 }
299 }
308
300
309 // Save Catalogue
301 // Save Catalogue
310 auto catalogues = this->retrieveCatalogues(repository);
302 auto catalogues = this->retrieveCatalogues(repository);
311 for (auto catalogue : catalogues) {
303 for (auto catalogue : catalogues) {
312 impl->saveCatalogue(catalogue, false);
304 impl->saveCatalogue(catalogue, false);
313 }
305 }
314 }
306 }
315
307
316 impl->savAllDB();
308 impl->savAllDB();
317 impl->m_EventKeysWithChanges.clear();
309 impl->m_EventKeysWithChanges.clear();
318 }
310 }
319
311
320 bool CatalogueController::hasChanges() const
312 bool CatalogueController::hasChanges() const
321 {
313 {
322 return !impl->m_EventKeysWithChanges.isEmpty(); // TODO: catalogues
314 return !impl->m_EventKeysWithChanges.isEmpty(); // TODO: catalogues
323 }
315 }
324
316
325 QByteArray
317 QByteArray
326 CatalogueController::mimeDataForEvents(const QVector<std::shared_ptr<DBEvent> > &events) const
318 CatalogueController::mimeDataForEvents(const QVector<std::shared_ptr<DBEvent> > &events) const
327 {
319 {
328 auto encodedData = QByteArray{};
320 auto encodedData = QByteArray{};
329
321
330 QMap<QString, QVariantList> idsPerRepository;
322 QMap<QString, QVariantList> idsPerRepository;
331 for (auto event : events) {
323 for (auto event : events) {
332 idsPerRepository[event->getRepository()] << event->getUniqId();
324 idsPerRepository[event->getRepository()] << event->getUniqId();
333 }
325 }
334
326
335 QDataStream stream{&encodedData, QIODevice::WriteOnly};
327 QDataStream stream{&encodedData, QIODevice::WriteOnly};
336 stream << idsPerRepository;
328 stream << idsPerRepository;
337
329
338 return encodedData;
330 return encodedData;
339 }
331 }
340
332
341 QVector<std::shared_ptr<DBEvent> >
333 QVector<std::shared_ptr<DBEvent> >
342 CatalogueController::eventsForMimeData(const QByteArray &mimeData) const
334 CatalogueController::eventsForMimeData(const QByteArray &mimeData) const
343 {
335 {
344 auto events = QVector<std::shared_ptr<DBEvent> >{};
336 auto events = QVector<std::shared_ptr<DBEvent> >{};
345 QDataStream stream{mimeData};
337 QDataStream stream{mimeData};
346
338
347 QMap<QString, QVariantList> idsPerRepository;
339 QMap<QString, QVariantList> idsPerRepository;
348 stream >> idsPerRepository;
340 stream >> idsPerRepository;
349
341
350 for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend(); ++it) {
342 for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend(); ++it) {
351 auto repository = it.key();
343 auto repository = it.key();
352 auto allRepositoryEvent = retrieveEvents(repository);
344 auto allRepositoryEvent = retrieveEvents(repository);
353 for (auto uuid : it.value()) {
345 for (auto uuid : it.value()) {
354 for (auto repositoryEvent : allRepositoryEvent) {
346 for (auto repositoryEvent : allRepositoryEvent) {
355 if (uuid.toUuid() == repositoryEvent->getUniqId()) {
347 if (uuid.toUuid() == repositoryEvent->getUniqId()) {
356 events << repositoryEvent;
348 events << repositoryEvent;
357 }
349 }
358 }
350 }
359 }
351 }
360 }
352 }
361
353
362 return events;
354 return events;
363 }
355 }
364
356
365 void CatalogueController::initialize()
357 void CatalogueController::initialize()
366 {
358 {
367 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init")
359 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init")
368 << QThread::currentThread();
360 << QThread::currentThread();
369
361
370 impl->m_CatalogueDao.initialize();
362 impl->m_CatalogueDao.initialize();
371 auto defaultRepositoryLocation
363 auto defaultRepositoryLocation
372 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
364 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
373
365
374 QDir defaultRepositoryLocationDir;
366 QDir defaultRepositoryLocationDir;
375 if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) {
367 if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) {
376 defaultRepositoryLocationDir.cd(defaultRepositoryLocation);
368 defaultRepositoryLocationDir.cd(defaultRepositoryLocation);
377 auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT);
369 auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT);
378
370
379 qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ")
371 qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ")
380 << defaultRepository;
372 << defaultRepository;
381
373
382 QDir dbDir(defaultRepository);
374 QDir dbDir(defaultRepository);
383 impl->m_RepositoryList << REPOSITORY_DEFAULT;
375 impl->m_RepositoryList << REPOSITORY_DEFAULT;
384 if (dbDir.exists()) {
376 if (dbDir.exists()) {
385 auto dirName = dbDir.dirName();
377 auto dirName = dbDir.dirName();
386
378
387 if (impl->m_CatalogueDao.addDB(defaultRepository, dirName)) {
379 if (impl->m_CatalogueDao.addDB(defaultRepository, dirName)) {
388 impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
380 impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
389 }
381 }
390 }
382 }
391 else {
383 else {
392 qCInfo(LOG_CatalogueController()) << tr("Initialisation of Default repository detected")
384 qCInfo(LOG_CatalogueController()) << tr("Initialisation of Default repository detected")
393 << defaultRepository;
385 << defaultRepository;
394 }
386 }
395 }
387 }
396 else {
388 else {
397 qCWarning(LOG_CatalogueController())
389 qCWarning(LOG_CatalogueController())
398 << tr("Cannot load the persistent default repository from ")
390 << tr("Cannot load the persistent default repository from ")
399 << defaultRepositoryLocation;
391 << defaultRepositoryLocation;
400 }
392 }
401
393
402 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END");
394 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END");
403 }
395 }
404
396
405 QString CatalogueController::CatalogueControllerPrivate::eventUniqueKey(
397 QString CatalogueController::CatalogueControllerPrivate::eventUniqueKey(
406 const std::shared_ptr<DBEvent> &event) const
398 const std::shared_ptr<DBEvent> &event) const
407 {
399 {
408 return event->getUniqId().toString().append(event->getRepository());
400 return event->getUniqId().toString().append(event->getRepository());
409 }
401 }
410
402
411 void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(const QString &dbFrom,
403 void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(const QString &dbFrom,
412 const QString &dbTo)
404 const QString &dbTo)
413 {
405 {
414 // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
406 // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
415 auto catalogues = m_CatalogueDao.getCatalogues(dbFrom);
407 auto catalogues = m_CatalogueDao.getCatalogues(dbFrom);
416 auto events = m_CatalogueDao.getEvents(dbFrom);
408 auto events = m_CatalogueDao.getEvents(dbFrom);
417 for (auto catalogue : catalogues) {
409 for (auto catalogue : catalogues) {
418 m_CatalogueDao.copyCatalogue(catalogue, dbTo, true);
410 m_CatalogueDao.copyCatalogue(catalogue, dbTo, true);
419 }
411 }
420
412
421 for (auto event : events) {
413 for (auto event : events) {
422 m_CatalogueDao.copyEvent(event, dbTo, true);
414 m_CatalogueDao.copyEvent(event, dbTo, true);
423 }
415 }
424 }
416 }
425
417
426 QString CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString repository)
418 QString CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString repository)
427 {
419 {
428 auto syncRepository = toSyncRepository(repository);
420 auto syncRepository = toSyncRepository(repository);
429
421
430 return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX);
422 return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX);
431 }
423 }
432
424
433 QString CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString repository)
425 QString CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString repository)
434 {
426 {
435 auto syncRepository = repository;
427 auto syncRepository = repository;
436 if (repository.endsWith(REPOSITORY_WORK_SUFFIX)) {
428 if (repository.endsWith(REPOSITORY_WORK_SUFFIX)) {
437 syncRepository.remove(REPOSITORY_WORK_SUFFIX);
429 syncRepository.remove(REPOSITORY_WORK_SUFFIX);
438 }
430 }
439 else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX)) {
431 else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX)) {
440 syncRepository.remove(REPOSITORY_TRASH_SUFFIX);
432 syncRepository.remove(REPOSITORY_TRASH_SUFFIX);
441 }
433 }
442 return syncRepository;
434 return syncRepository;
443 }
435 }
444
436
445 void CatalogueController::CatalogueControllerPrivate::savAllDB()
437 void CatalogueController::CatalogueControllerPrivate::savAllDB()
446 {
438 {
447 for (auto repository : m_RepositoryList) {
439 for (auto repository : m_RepositoryList) {
448 auto defaultRepositoryLocation
440 auto defaultRepositoryLocation
449 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
441 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
450 m_CatalogueDao.saveDB(defaultRepositoryLocation, repository);
442 m_CatalogueDao.saveDB(defaultRepositoryLocation, repository);
451 }
443 }
452 }
444 }
453
445
454 void CatalogueController::CatalogueControllerPrivate::saveEvent(std::shared_ptr<DBEvent> event,
446 void CatalogueController::CatalogueControllerPrivate::saveEvent(std::shared_ptr<DBEvent> event,
455 bool persist)
447 bool persist)
456 {
448 {
457 m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()), true);
449 m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()), true);
458 if (persist) {
450 if (persist) {
459 savAllDB();
451 savAllDB();
460 }
452 }
461 }
453 }
462
454
463 void CatalogueController::CatalogueControllerPrivate::saveCatalogue(
455 void CatalogueController::CatalogueControllerPrivate::saveCatalogue(
464 std::shared_ptr<DBCatalogue> catalogue, bool persist)
456 std::shared_ptr<DBCatalogue> catalogue, bool persist)
465 {
457 {
466 m_CatalogueDao.copyCatalogue(*catalogue, toSyncRepository(catalogue->getRepository()), true);
458 m_CatalogueDao.copyCatalogue(*catalogue, toSyncRepository(catalogue->getRepository()), true);
467 if (persist) {
459 if (persist) {
468 savAllDB();
460 savAllDB();
469 }
461 }
470 }
462 }
471
463
472 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)
473 {
465 {
474 // update catalogue parameter
466 // update catalogue parameter
475 auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(
467 auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(
476 QString{"uniqId"}, uniqId, ComparaisonOperation::EQUALEQUAL);
468 QString{"uniqId"}, uniqId, ComparaisonOperation::EQUALEQUAL);
477
469
478 auto repositoryType = repository;
470 auto repositoryType = repository;
479 switch (type) {
471 switch (type) {
480 case DBType::SYNC:
472 case DBType::SYNC:
481 repositoryType = toSyncRepository(repositoryType);
473 repositoryType = toSyncRepository(repositoryType);
482 break;
474 break;
483 case DBType::WORK:
475 case DBType::WORK:
484 repositoryType =toWorkRepository(repositoryType);
476 repositoryType =toWorkRepository(repositoryType);
485 break;
477 break;
486 case DBType::TRASH:
478 case DBType::TRASH:
487 default:
479 default:
488 break;
480 break;
489 }
481 }
490
482
491 auto repositoryPredicate = std::make_shared<ComparaisonPredicate>(
483 auto repositoryPredicate = std::make_shared<ComparaisonPredicate>(
492 QString{"repository"}, repositoryType,
484 QString{"repository"}, repositoryType,
493 ComparaisonOperation::EQUALEQUAL);
485 ComparaisonOperation::EQUALEQUAL);
494
486
495 auto finderPred = std::make_shared<CompoundPredicate>(CompoundOperation::AND);
487 auto finderPred = std::make_shared<CompoundPredicate>(CompoundOperation::AND);
496 finderPred->AddRequestPredicate(uniqIdPredicate);
488 finderPred->AddRequestPredicate(uniqIdPredicate);
497 finderPred->AddRequestPredicate(repositoryPredicate);
489 finderPred->AddRequestPredicate(repositoryPredicate);
498
490
499 return finderPred;
491 return finderPred;
500 }
492 }
General Comments 0
You need to be logged in to leave comments. Login now