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