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