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