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