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