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