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