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