Auto status change to "Under Review"
@@ -1,75 +1,77 | |||
|
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 | class DBEventProduct; | |
|
18 | 19 | |
|
19 | 20 | Q_DECLARE_LOGGING_CATEGORY(LOG_CatalogueController) |
|
20 | 21 | |
|
21 | 22 | class DataSourceItem; |
|
22 | 23 | class Variable; |
|
23 | 24 | |
|
24 | 25 | /** |
|
25 | 26 | * @brief The CatalogueController class aims to handle catalogues and event using the CatalogueAPI |
|
26 | 27 | * library. |
|
27 | 28 | */ |
|
28 | 29 | class SCIQLOP_CORE_EXPORT CatalogueController : public QObject { |
|
29 | 30 | Q_OBJECT |
|
30 | 31 | public: |
|
31 | 32 | explicit CatalogueController(QObject *parent = 0); |
|
32 | 33 | virtual ~CatalogueController(); |
|
33 | 34 | |
|
34 | 35 | // DB |
|
35 | 36 | QStringList getRepositories() const; |
|
36 | 37 | void addDB(const QString &dbPath); |
|
37 | 38 | void saveDB(const QString &destinationPath, const QString &repository); |
|
38 | 39 | |
|
39 | 40 | // Event |
|
40 | 41 | /// retrieveEvents with empty repository retrieve them from the default repository |
|
41 | 42 | std::list<std::shared_ptr<DBEvent> > retrieveEvents(const QString &repository) const; |
|
42 | 43 | std::list<std::shared_ptr<DBEvent> > retrieveAllEvents() const; |
|
43 | 44 | std::list<std::shared_ptr<DBEvent> > |
|
44 | 45 | retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const; |
|
45 | 46 | void addEvent(std::shared_ptr<DBEvent> event); |
|
46 | 47 | void updateEvent(std::shared_ptr<DBEvent> event); |
|
48 | void updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct); | |
|
47 | 49 | void removeEvent(std::shared_ptr<DBEvent> event); |
|
48 | 50 | // void trashEvent(std::shared_ptr<DBEvent> event); |
|
49 | 51 | // void restore(QUuid eventId); |
|
50 | 52 | void saveEvent(std::shared_ptr<DBEvent> event); |
|
51 | 53 | |
|
52 | 54 | // Catalogue |
|
53 | 55 | // bool createCatalogue(const QString &name, QVector<QUuid> eventList); |
|
54 | 56 | /// retrieveEvents with empty repository retrieve them from the default repository |
|
55 | 57 | std::list<std::shared_ptr<DBCatalogue> > retrieveCatalogues(const QString &repository |
|
56 | 58 | = QString()) const; |
|
57 | 59 | void updateCatalogue(std::shared_ptr<DBCatalogue> catalogue); |
|
58 | 60 | void removeCatalogue(std::shared_ptr<DBCatalogue> catalogue); |
|
59 | 61 | void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue); |
|
60 | 62 | |
|
61 | 63 | void saveAll(); |
|
62 | 64 | |
|
63 | 65 | public slots: |
|
64 | 66 | /// Manage init/end of the controller |
|
65 | 67 | void initialize(); |
|
66 | 68 | void finalize(); |
|
67 | 69 | |
|
68 | 70 | private: |
|
69 | 71 | void waitForFinish(); |
|
70 | 72 | |
|
71 | 73 | class CatalogueControllerPrivate; |
|
72 | 74 | spimpl::unique_impl_ptr<CatalogueControllerPrivate> impl; |
|
73 | 75 | }; |
|
74 | 76 | |
|
75 | 77 | #endif // SCIQLOP_CATALOGUECONTROLLER_H |
@@ -1,329 +1,334 | |||
|
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 <QMutex> |
|
16 | 16 | #include <QThread> |
|
17 | 17 | |
|
18 | 18 | #include <QDir> |
|
19 | 19 | #include <QStandardPaths> |
|
20 | 20 | |
|
21 | 21 | Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController") |
|
22 | 22 | |
|
23 | 23 | namespace { |
|
24 | 24 | |
|
25 | 25 | static QString REPOSITORY_WORK_SUFFIX = QString{"_work"}; |
|
26 | 26 | static QString REPOSITORY_TRASH_SUFFIX = QString{"_trash"}; |
|
27 | 27 | } |
|
28 | 28 | |
|
29 | 29 | class CatalogueController::CatalogueControllerPrivate { |
|
30 | 30 | |
|
31 | 31 | public: |
|
32 | 32 | explicit CatalogueControllerPrivate(CatalogueController *parent) : m_Q{parent} {} |
|
33 | 33 | |
|
34 | 34 | QMutex m_WorkingMutex; |
|
35 | 35 | CatalogueDao m_CatalogueDao; |
|
36 | 36 | |
|
37 | 37 | QStringList m_RepositoryList; |
|
38 | 38 | CatalogueController *m_Q; |
|
39 | 39 | |
|
40 | 40 | void copyDBtoDB(const QString &dbFrom, const QString &dbTo); |
|
41 | 41 | QString toWorkRepository(QString repository); |
|
42 | 42 | QString toSyncRepository(QString repository); |
|
43 | 43 | void savAllDB(); |
|
44 | 44 | |
|
45 | 45 | void saveEvent(std::shared_ptr<DBEvent> event, bool persist = true); |
|
46 | 46 | void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool persist = true); |
|
47 | 47 | }; |
|
48 | 48 | |
|
49 | 49 | CatalogueController::CatalogueController(QObject *parent) |
|
50 | 50 | : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>(this)} |
|
51 | 51 | { |
|
52 | 52 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction") |
|
53 | 53 | << QThread::currentThread(); |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | CatalogueController::~CatalogueController() |
|
57 | 57 | { |
|
58 | 58 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction") |
|
59 | 59 | << QThread::currentThread(); |
|
60 | 60 | this->waitForFinish(); |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | QStringList CatalogueController::getRepositories() const |
|
64 | 64 | { |
|
65 | 65 | return impl->m_RepositoryList; |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | void CatalogueController::addDB(const QString &dbPath) |
|
69 | 69 | { |
|
70 | 70 | QDir dbDir(dbPath); |
|
71 | 71 | if (dbDir.exists()) { |
|
72 | 72 | auto dirName = dbDir.dirName(); |
|
73 | 73 | |
|
74 | 74 | if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName) |
|
75 | 75 | != impl->m_RepositoryList.cend()) { |
|
76 | 76 | qCCritical(LOG_CatalogueController()) |
|
77 | 77 | << tr("Impossible to addDB that is already loaded"); |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) { |
|
81 | 81 | qCCritical(LOG_CatalogueController()) |
|
82 | 82 | << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath); |
|
83 | 83 | } |
|
84 | 84 | else { |
|
85 | 85 | impl->m_RepositoryList << dirName; |
|
86 | 86 | impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName)); |
|
87 | 87 | } |
|
88 | 88 | } |
|
89 | 89 | else { |
|
90 | 90 | qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ") |
|
91 | 91 | << dbPath; |
|
92 | 92 | } |
|
93 | 93 | } |
|
94 | 94 | |
|
95 | 95 | void CatalogueController::saveDB(const QString &destinationPath, const QString &repository) |
|
96 | 96 | { |
|
97 | 97 | if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) { |
|
98 | 98 | qCCritical(LOG_CatalogueController()) |
|
99 | 99 | << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath); |
|
100 | 100 | } |
|
101 | 101 | } |
|
102 | 102 | |
|
103 | 103 | std::list<std::shared_ptr<DBEvent> > |
|
104 | 104 | CatalogueController::retrieveEvents(const QString &repository) const |
|
105 | 105 | { |
|
106 | 106 | QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository; |
|
107 | 107 | |
|
108 | 108 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
109 | 109 | auto events = impl->m_CatalogueDao.getEvents(impl->toWorkRepository(dbDireName)); |
|
110 | 110 | for (auto event : events) { |
|
111 | 111 | eventsShared.push_back(std::make_shared<DBEvent>(event)); |
|
112 | 112 | } |
|
113 | 113 | return eventsShared; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const |
|
117 | 117 | { |
|
118 | 118 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
119 | 119 | for (auto repository : impl->m_RepositoryList) { |
|
120 | 120 | eventsShared.splice(eventsShared.end(), retrieveEvents(repository)); |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | return eventsShared; |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | std::list<std::shared_ptr<DBEvent> > |
|
127 | 127 | CatalogueController::retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const |
|
128 | 128 | { |
|
129 | 129 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
130 | 130 | auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue); |
|
131 | 131 | for (auto event : events) { |
|
132 | 132 | eventsShared.push_back(std::make_shared<DBEvent>(event)); |
|
133 | 133 | } |
|
134 | 134 | return eventsShared; |
|
135 | 135 | } |
|
136 | 136 | |
|
137 | 137 | void CatalogueController::updateEvent(std::shared_ptr<DBEvent> event) |
|
138 | 138 | { |
|
139 | 139 | event->setRepository(impl->toWorkRepository(event->getRepository())); |
|
140 | 140 | |
|
141 | 141 | impl->m_CatalogueDao.updateEvent(*event); |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | void CatalogueController::updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct) | |
|
145 | { | |
|
146 | impl->m_CatalogueDao.updateEventProduct(*eventProduct); | |
|
147 | } | |
|
148 | ||
|
144 | 149 | void CatalogueController::removeEvent(std::shared_ptr<DBEvent> event) |
|
145 | 150 | { |
|
146 | 151 | // Remove it from both repository and repository_work |
|
147 | 152 | event->setRepository(impl->toWorkRepository(event->getRepository())); |
|
148 | 153 | impl->m_CatalogueDao.removeEvent(*event); |
|
149 | 154 | event->setRepository(impl->toSyncRepository(event->getRepository())); |
|
150 | 155 | impl->m_CatalogueDao.removeEvent(*event); |
|
151 | 156 | } |
|
152 | 157 | |
|
153 | 158 | void CatalogueController::addEvent(std::shared_ptr<DBEvent> event) |
|
154 | 159 | { |
|
155 | 160 | event->setRepository(impl->toWorkRepository(event->getRepository())); |
|
156 | 161 | |
|
157 | 162 | auto eventTemp = *event; |
|
158 | 163 | impl->m_CatalogueDao.addEvent(eventTemp); |
|
159 | 164 | |
|
160 | 165 | // Call update is necessary at the creation of add Event if it has some tags or some event |
|
161 | 166 | // products |
|
162 | 167 | if (!event->getEventProducts().empty() || !event->getTags().empty()) { |
|
163 | 168 | |
|
164 | 169 | auto eventProductsTemp = eventTemp.getEventProducts(); |
|
165 | 170 | auto eventProductTempUpdated = std::list<DBEventProduct>{}; |
|
166 | 171 | for (auto eventProductTemp : eventProductsTemp) { |
|
167 | 172 | eventProductTemp.setEvent(eventTemp); |
|
168 | 173 | eventProductTempUpdated.push_back(eventProductTemp); |
|
169 | 174 | } |
|
170 | 175 | eventTemp.setEventProducts(eventProductTempUpdated); |
|
171 | 176 | |
|
172 | 177 | impl->m_CatalogueDao.updateEvent(eventTemp); |
|
173 | 178 | } |
|
174 | 179 | } |
|
175 | 180 | |
|
176 | 181 | void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event) |
|
177 | 182 | { |
|
178 | 183 | impl->saveEvent(event, true); |
|
179 | 184 | } |
|
180 | 185 | |
|
181 | 186 | std::list<std::shared_ptr<DBCatalogue> > |
|
182 | 187 | CatalogueController::retrieveCatalogues(const QString &repository) const |
|
183 | 188 | { |
|
184 | 189 | QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository; |
|
185 | 190 | |
|
186 | 191 | auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{}; |
|
187 | 192 | auto catalogues = impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName)); |
|
188 | 193 | for (auto catalogue : catalogues) { |
|
189 | 194 | cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue)); |
|
190 | 195 | } |
|
191 | 196 | return cataloguesShared; |
|
192 | 197 | } |
|
193 | 198 | |
|
194 | 199 | void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue> catalogue) |
|
195 | 200 | { |
|
196 | 201 | catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository())); |
|
197 | 202 | |
|
198 | 203 | impl->m_CatalogueDao.updateCatalogue(*catalogue); |
|
199 | 204 | } |
|
200 | 205 | |
|
201 | 206 | void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue> catalogue) |
|
202 | 207 | { |
|
203 | 208 | // Remove it from both repository and repository_work |
|
204 | 209 | catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository())); |
|
205 | 210 | impl->m_CatalogueDao.removeCatalogue(*catalogue); |
|
206 | 211 | catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository())); |
|
207 | 212 | impl->m_CatalogueDao.removeCatalogue(*catalogue); |
|
208 | 213 | } |
|
209 | 214 | |
|
210 | 215 | void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue> catalogue) |
|
211 | 216 | { |
|
212 | 217 | impl->saveCatalogue(catalogue, true); |
|
213 | 218 | } |
|
214 | 219 | |
|
215 | 220 | void CatalogueController::saveAll() |
|
216 | 221 | { |
|
217 | 222 | for (auto repository : impl->m_RepositoryList) { |
|
218 | 223 | // Save Event |
|
219 | 224 | auto events = this->retrieveEvents(repository); |
|
220 | 225 | for (auto event : events) { |
|
221 | 226 | impl->saveEvent(event, false); |
|
222 | 227 | } |
|
223 | 228 | |
|
224 | 229 | // Save Catalogue |
|
225 | 230 | auto catalogues = this->retrieveCatalogues(repository); |
|
226 | 231 | for (auto catalogue : catalogues) { |
|
227 | 232 | impl->saveCatalogue(catalogue, false); |
|
228 | 233 | } |
|
229 | 234 | } |
|
230 | 235 | |
|
231 | 236 | impl->savAllDB(); |
|
232 | 237 | } |
|
233 | 238 | |
|
234 | 239 | void CatalogueController::initialize() |
|
235 | 240 | { |
|
236 | 241 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init") |
|
237 | 242 | << QThread::currentThread(); |
|
238 | 243 | impl->m_WorkingMutex.lock(); |
|
239 | 244 | impl->m_CatalogueDao.initialize(); |
|
240 | 245 | auto defaultRepositoryLocation |
|
241 | 246 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); |
|
242 | 247 | |
|
243 | 248 | QDir defaultRepositoryLocationDir; |
|
244 | 249 | if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) { |
|
245 | 250 | defaultRepositoryLocationDir.cd(defaultRepositoryLocation); |
|
246 | 251 | auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT); |
|
247 | 252 | qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ") |
|
248 | 253 | << defaultRepository; |
|
249 | 254 | this->addDB(defaultRepository); |
|
250 | 255 | } |
|
251 | 256 | else { |
|
252 | 257 | qCWarning(LOG_CatalogueController()) |
|
253 | 258 | << tr("Cannot load the persistent default repository from ") |
|
254 | 259 | << defaultRepositoryLocation; |
|
255 | 260 | } |
|
256 | 261 | |
|
257 | 262 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END"); |
|
258 | 263 | } |
|
259 | 264 | |
|
260 | 265 | void CatalogueController::finalize() |
|
261 | 266 | { |
|
262 | 267 | impl->m_WorkingMutex.unlock(); |
|
263 | 268 | } |
|
264 | 269 | |
|
265 | 270 | void CatalogueController::waitForFinish() |
|
266 | 271 | { |
|
267 | 272 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
268 | 273 | } |
|
269 | 274 | |
|
270 | 275 | void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(const QString &dbFrom, |
|
271 | 276 | const QString &dbTo) |
|
272 | 277 | { |
|
273 | 278 | // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{}; |
|
274 | 279 | auto catalogues = m_CatalogueDao.getCatalogues(dbFrom); |
|
275 | 280 | auto events = m_CatalogueDao.getEvents(dbFrom); |
|
276 | 281 | for (auto catalogue : catalogues) { |
|
277 | 282 | m_CatalogueDao.copyCatalogue(catalogue, dbTo, true); |
|
278 | 283 | } |
|
279 | 284 | |
|
280 | 285 | for (auto event : events) { |
|
281 | 286 | m_CatalogueDao.copyEvent(event, dbTo, true); |
|
282 | 287 | } |
|
283 | 288 | } |
|
284 | 289 | |
|
285 | 290 | QString CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString repository) |
|
286 | 291 | { |
|
287 | 292 | auto syncRepository = toSyncRepository(repository); |
|
288 | 293 | |
|
289 | 294 | return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX); |
|
290 | 295 | } |
|
291 | 296 | |
|
292 | 297 | QString CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString repository) |
|
293 | 298 | { |
|
294 | 299 | auto syncRepository = repository; |
|
295 | 300 | if (repository.endsWith(REPOSITORY_WORK_SUFFIX)) { |
|
296 | 301 | syncRepository.remove(REPOSITORY_WORK_SUFFIX); |
|
297 | 302 | } |
|
298 | 303 | else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX)) { |
|
299 | 304 | syncRepository.remove(REPOSITORY_TRASH_SUFFIX); |
|
300 | 305 | } |
|
301 | 306 | return syncRepository; |
|
302 | 307 | } |
|
303 | 308 | |
|
304 | 309 | void CatalogueController::CatalogueControllerPrivate::savAllDB() |
|
305 | 310 | { |
|
306 | 311 | for (auto repository : m_RepositoryList) { |
|
307 | 312 | auto defaultRepositoryLocation |
|
308 | 313 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); |
|
309 | 314 | m_CatalogueDao.saveDB(defaultRepositoryLocation, repository); |
|
310 | 315 | } |
|
311 | 316 | } |
|
312 | 317 | |
|
313 | 318 | void CatalogueController::CatalogueControllerPrivate::saveEvent(std::shared_ptr<DBEvent> event, |
|
314 | 319 | bool persist) |
|
315 | 320 | { |
|
316 | 321 | m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()), true); |
|
317 | 322 | if (persist) { |
|
318 | 323 | savAllDB(); |
|
319 | 324 | } |
|
320 | 325 | } |
|
321 | 326 | |
|
322 | 327 | void CatalogueController::CatalogueControllerPrivate::saveCatalogue( |
|
323 | 328 | std::shared_ptr<DBCatalogue> catalogue, bool persist) |
|
324 | 329 | { |
|
325 | 330 | m_CatalogueDao.copyCatalogue(*catalogue, toSyncRepository(catalogue->getRepository()), true); |
|
326 | 331 | if (persist) { |
|
327 | 332 | savAllDB(); |
|
328 | 333 | } |
|
329 | 334 | } |
General Comments 3
Status change > Approved
You need to be logged in to leave comments.
Login now