##// END OF EJS Templates
[Catalogues] Switch to new catalogues lib in progress, load/saves/query methods implemented for new...
jeandet -
r53:cd10c3acad49
parent child
Show More
@@ -0,0 +1,51
1 ---
2 BasedOnStyle: LLVM
3 AlignAfterOpenBracket: Align
4 AlignConsecutiveAssignments: 'true'
5 AlignConsecutiveDeclarations: 'false'
6 AlignEscapedNewlines: Right
7 AlignOperands: 'true'
8 AlignTrailingComments: 'true'
9 AllowAllParametersOfDeclarationOnNextLine: 'true'
10 AllowShortBlocksOnASingleLine: 'true'
11 AllowShortCaseLabelsOnASingleLine: 'true'
12 AllowShortFunctionsOnASingleLine: All
13 AllowShortIfStatementsOnASingleLine: 'true'
14 AllowShortLoopsOnASingleLine: 'false'
15 AlwaysBreakAfterReturnType: None
16 BreakBeforeBraces: Custom
17 BraceWrapping:
18 AfterClass: true
19 AfterControlStatement: true
20 AfterEnum: true
21 AfterFunction: true
22 AfterStruct: true
23 AfterNamespace: true
24 AfterUnion: true
25 AfterExternBlock: true
26 BeforeCatch: false
27 BeforeElse: true
28 SplitEmptyFunction: false
29 SplitEmptyRecord: false
30 SplitEmptyNamespace: false
31 Cpp11BracedListStyle: 'true'
32 FixNamespaceComments: 'true'
33 IncludeBlocks: Regroup
34 IndentCaseLabels: 'true'
35 IndentWidth: 2
36 KeepEmptyLinesAtTheStartOfBlocks: 'false'
37 Language: Cpp
38 NamespaceIndentation: All
39 PointerAlignment: Left
40 SortUsingDeclarations: 'false'
41 SpaceAfterTemplateKeyword: 'false'
42 SpaceBeforeAssignmentOperators: 'true'
43 SpaceBeforeParens: Never
44 SpaceInEmptyParentheses: 'false'
45 SpacesInAngles: 'false'
46 SpacesInContainerLiterals: 'false'
47 SpacesInParentheses: 'false'
48 SpacesInSquareBrackets: 'false'
49 UseTab: Never
50
51 ...
@@ -0,0 +1,1
1 *.txt.user
@@ -0,0 +1,1
1 Subproject commit 6547208a6ef0692c8fa0431e651c919ada111f4e
@@ -0,0 +1,22
1 #include <Catalogue/CatalogueController.h>
2 #include <QObject>
3 #include <QtTest>
4 #include <TestUtils/TestProviders.h>
5
6
7 class TestCatalogueController : public QObject
8 {
9 Q_OBJECT
10 public:
11 explicit TestCatalogueController(QObject* parent = nullptr) : QObject(parent) {}
12 signals:
13
14 private slots:
15 void initTestCase() {}
16 void cleanupTestCase() {}
17 };
18
19
20 QTEST_MAIN(TestCatalogueController)
21
22 #include "TestCatalogueController.moc"
@@ -4,3 +4,6
4 [submodule "external/libcatalogs"]
4 [submodule "external/libcatalogs"]
5 path = external/libcatalogs
5 path = external/libcatalogs
6 url = https://hephaistos.lpp.polytechnique.fr/rhodecode/HG_REPOSITORIES/LPP/SciQLOP_Repos/libcatalogs
6 url = https://hephaistos.lpp.polytechnique.fr/rhodecode/HG_REPOSITORIES/LPP/SciQLOP_Repos/libcatalogs
7 [submodule "external/catalogicpp"]
8 path = external/catalogicpp
9 url = https://github.com/jeandet/catalogicpp
@@ -43,6 +43,16 if (NOT catalogs_FOUND)
43 add_subdirectory(external/libcatalogs)
43 add_subdirectory(external/libcatalogs)
44 endif()
44 endif()
45
45
46 find_package(catalogicpp CONFIG QUIET)
47 if (NOT catalogicpp_FOUND)
48 if(NOT EXISTS ${CMAKE_CURRENT_LIST_DIR}/external/catalogicpp/CMakeLists.txt)
49 message("Init submodule catalogicpp")
50 execute_process(COMMAND git submodule init external/catalogicpp WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
51 execute_process(COMMAND git submodule update external/catalogicpp WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
52 endif()
53 add_subdirectory(external/catalogicpp)
54 endif()
55
46 macro(declare_test testname testexe sources libraries)
56 macro(declare_test testname testexe sources libraries)
47 add_executable(${testexe} ${sources})
57 add_executable(${testexe} ${sources})
48 target_link_libraries(${testexe} ${libraries})
58 target_link_libraries(${testexe} ${libraries})
@@ -168,6 +178,7 target_link_libraries(sciqlopcore PUBLIC
168 if(Catalog)
178 if(Catalog)
169 target_link_libraries(sciqlopcore PUBLIC
179 target_link_libraries(sciqlopcore PUBLIC
170 catalogs
180 catalogs
181 CatalogiCpp
171 )
182 )
172 endif()
183 endif()
173
184
@@ -3,14 +3,13
3
3
4 #include "CoreGlobal.h"
4 #include "CoreGlobal.h"
5
5
6 #include <Common/spimpl.h>
6 #include <Data/DateTimeRange.h>
7 #include <Data/DateTimeRange.h>
7
8 #include <QLoggingCategory>
8 #include <QLoggingCategory>
9 #include <QObject>
9 #include <QObject>
10 #include <QUuid>
10 #include <QUuid>
11
11 #include <Repository.hpp>
12 #include <Common/spimpl.h>
12 #include <map>
13
14 #include <memory>
13 #include <memory>
15
14
16 class DBCatalogue;
15 class DBCatalogue;
@@ -23,71 +22,102 class DataSourceItem;
23 class Variable;
22 class Variable;
24
23
25 /**
24 /**
26 * @brief The CatalogueController class aims to handle catalogues and event using the CatalogueAPI
25 * @brief The CatalogueController class aims to handle catalogues and event
27 * library.
26 * using the CatalogueAPI library.
28 */
27 */
29 class SCIQLOP_CORE_EXPORT CatalogueController : public QObject {
28 class SCIQLOP_CORE_EXPORT CatalogueController : public QObject
30 Q_OBJECT
29 {
30 using time_t = double;
31 using Repository_t = CatalogiCpp::Repository<time_t>;
32 using Event_t = Repository_t::Event_t;
33 using Event_ptr = Repository_t::Event_ptr;
34 using Catalogue_t = Repository_t::Catalogue_t;
35 using Catalogue_ptr = typename Repository_t::Catalogue_ptr;
36 using uuid_t = Repository_t::uuid_t;
37
38 std::map<QString, CatalogiCpp::Repository<time_t>> _lastSavedRepos;
39 std::map<QString, CatalogiCpp::Repository<time_t>> _currentRepos;
40
41 Q_OBJECT
31 public:
42 public:
32 explicit CatalogueController(QObject *parent = 0);
43 explicit CatalogueController(QObject* parent = nullptr);
33 virtual ~CatalogueController();
44 virtual ~CatalogueController();
34
45
35 // DB
46 QStringList getRepositories() const;
36 QStringList getRepositories() const;
47 void loadRepository(const QString& path, const QString& name);
37 void addDB(const QString &dbPath);
48 void saveRepository(const QString& path, const QString& name);
38 void saveDB(const QString &destinationPath, const QString &repository);
49
39
50 std::vector<Event_ptr> events();
40 // Event
51 std::vector<Event_ptr> events(const QString& repository);
41 /// retrieveEvents with empty repository retrieve them from the default repository
52
42 std::list<std::shared_ptr<DBEvent> > retrieveEvents(const QString &repository) const;
53 std::vector<Catalogue_ptr> catalogues();
43 std::list<std::shared_ptr<DBEvent> > retrieveAllEvents() const;
54 std::vector<Catalogue_ptr> catalogues(const QString& repository);
44
55
45 void addEvent(std::shared_ptr<DBEvent> event);
56 bool hasUnsavedChanges(Event_ptr event);
46 void updateEvent(std::shared_ptr<DBEvent> event);
57
47 void updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct);
58 std::optional<QString> repository(Event_ptr event);
48 void removeEvent(std::shared_ptr<DBEvent> event);
59 std::optional<QString> repository(Catalogue_ptr catalogue);
49 // void trashEvent(std::shared_ptr<DBEvent> event);
60
50 // void restore(std::shared_ptr<DBEvent> event);
61 void save(Event_ptr event);
51 void saveEvent(std::shared_ptr<DBEvent> event);
62 void save(Catalogue_ptr catalogue);
52 void discardEvent(std::shared_ptr<DBEvent> event, bool &removed);
63 void save(const QString& repository);
53 bool eventHasChanges(std::shared_ptr<DBEvent> event) const;
64
54
65 // // Event
55 // Catalogue
66 // /// retrieveEvents with empty repository retrieve them from the default
56 std::list<std::shared_ptr<DBEvent> >
67 // repository std::list<std::shared_ptr<DBEvent>> retrieveEvents(const
57 retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const;
68 // QString& repository) const; std::list<std::shared_ptr<DBEvent>>
58
69 // retrieveAllEvents() const;
59 /// retrieveEvents with empty repository retrieve them from the default repository
70
60 std::list<std::shared_ptr<DBCatalogue> > retrieveCatalogues(const QString &repository
71 // void addEvent(std::shared_ptr<DBEvent> event);
61 = QString()) const;
72 // void updateEvent(std::shared_ptr<DBEvent> event);
62 void addCatalogue(std::shared_ptr<DBCatalogue> catalogue);
73 // void updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct);
63 void updateCatalogue(std::shared_ptr<DBCatalogue> catalogue);
74 // void removeEvent(std::shared_ptr<DBEvent> event);
64 void removeCatalogue(std::shared_ptr<DBCatalogue> catalogue);
75 // // void trashEvent(std::shared_ptr<DBEvent> event);
65 void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue);
76 // // void restore(std::shared_ptr<DBEvent> event);
66 void discardCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool &removed);
77 // void saveEvent(std::shared_ptr<DBEvent> event);
67
78 // void discardEvent(std::shared_ptr<DBEvent> event, bool& removed);
68 void saveAll();
79 // bool eventHasChanges(std::shared_ptr<DBEvent> event) const;
69 bool hasChanges() const;
80
70
81 // // Catalogue
71 /// Returns the MIME data associated to a list of variables
82 // std::list<std::shared_ptr<DBEvent>> retrieveEventsFromCatalogue(
72 QByteArray mimeDataForEvents(const QVector<std::shared_ptr<DBEvent> > &events) const;
83 // std::shared_ptr<DBCatalogue> catalogue) const;
73
84
74 /// Returns the list of variables contained in a MIME data
85 // /// retrieveEvents with empty repository retrieve them from the default
75 QVector<std::shared_ptr<DBEvent> > eventsForMimeData(const QByteArray &mimeData) const;
86 // repository std::list<std::shared_ptr<DBCatalogue>> retrieveCatalogues(
76
87 // const QString& repository = QString()) const;
77 /// Returns the MIME data associated to a list of variables
88 // void addCatalogue(std::shared_ptr<DBCatalogue> catalogue);
78 QByteArray
89 // void updateCatalogue(std::shared_ptr<DBCatalogue> catalogue);
79 mimeDataForCatalogues(const QVector<std::shared_ptr<DBCatalogue> > &catalogues) const;
90 // void removeCatalogue(std::shared_ptr<DBCatalogue> catalogue);
80
91 // void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue);
81 /// Returns the list of variables contained in a MIME data
92 // void discardCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool&
82 QVector<std::shared_ptr<DBCatalogue> > cataloguesForMimeData(const QByteArray &mimeData) const;
93 // removed);
83
94
84 public slots:
95 // void saveAll();
85 /// Manage init/end of the controller
96 // bool hasChanges() const;
86 void initialize();
97
98 // /// Returns the MIME data associated to a list of variables
99 // QByteArray mimeDataForEvents(const QVector<std::shared_ptr<DBEvent>>&
100 // events) const;
101
102 // /// Returns the list of variables contained in a MIME data
103 // QVector<std::shared_ptr<DBEvent>> eventsForMimeData(const QByteArray&
104 // mimeData) const;
105
106 // /// Returns the MIME data associated to a list of variables
107 // QByteArray mimeDataForCatalogues(const
108 // QVector<std::shared_ptr<DBCatalogue>>& catalogues) const;
109
110 // /// Returns the list of variables contained in a MIME data
111 // QVector<std::shared_ptr<DBCatalogue>> cataloguesForMimeData(const
112 // QByteArray& mimeData) const;
113
114 // public slots:
115 // /// Manage init/end of the controller
116 // void initialize();
87
117
88 private:
118 private:
89 class CatalogueControllerPrivate;
119 // class CatalogueControllerPrivate;
90 spimpl::unique_impl_ptr<CatalogueControllerPrivate> impl;
120 // spimpl::unique_impl_ptr<CatalogueControllerPrivate> impl;
91 };
121 };
92
122
93 #endif // SCIQLOP_CATALOGUECONTROLLER_H
123 #endif // SCIQLOP_CATALOGUECONTROLLER_H
@@ -1,16 +1,28
1 #ifndef CONTAINERS_H
1 #ifndef CONTAINERS_H
2 #define CONTAINERS_H
2 #define CONTAINERS_H
3 #include <algorithm>
3 #include "cpp_utils.h"
4
4
5 namespace SciQLop::containers {
5 #include <algorithm>
6 #include <map>
7 #include <vector>
6
8
7 template <class T1,class T2>
9 namespace SciQLop::containers
8 bool contains(const T1 &container, const T2 &value)
9 {
10 {
10 return std::find(container.begin(), container.end(), value) != container.end();
11 template<class T1, class T2>
11 }
12 auto contains(const T1& container, const T2& value)
13 -> decltype(container.front(), std::end(container), true)
14 {
15 return std::find(std::cbegin(container), std::cend(container), value) !=
16 std::cend(container);
17 }
12
18
13 }
19 template<class T1, class T2>
20 auto contains(const T1& container, const T2& value)
21 -> decltype(container.find(value), std::cend(container), true)
22 {
23 return container.find(value) != std::cend(container);
24 }
14
25
26 } // namespace SciQLop::containers
15
27
16 #endif // CONTAINERS_H
28 #endif // CONTAINERS_H
This diff has been collapsed as it changes many lines, (1222 lines changed) Show them Hide them
@@ -1,574 +1,760
1 #include <Catalogue/CatalogueController.h>
1 #include <Catalogue/CatalogueController.h>
2
2 #include <CatalogueIO.hpp>
3 #include <Variable/Variable.h>
3 #include <Common/containers.h>
4
4 #include <Common/debug.h>
5 #include <CatalogueDao.h>
6
7 #include <ComparaisonPredicate.h>
8 #include <CompoundPredicate.h>
9 #include <DBCatalogue.h>
10 #include <DBEvent.h>
11 #include <DBEventProduct.h>
12 #include <DBTag.h>
13 #include <IRequestPredicate.h>
14
15 #include <QDataStream>
5 #include <QDataStream>
16 #include <QMutex>
17 #include <QThread>
18
19 #include <QDir>
6 #include <QDir>
7 #include <QMutex>
20 #include <QStandardPaths>
8 #include <QStandardPaths>
9 #include <QThread>
21
10
22 Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController")
11 using namespace SciQLop::containers;
23
24 namespace {
25
12
26 static QString REPOSITORY_WORK_SUFFIX = QString{"_work"};
13 // class CatalogueController::CatalogueControllerPrivate
27 static QString REPOSITORY_TRASH_SUFFIX = QString{"_trash"};
14 //{
28 }
29
15
30 /**
16 // public:
31 * Possible types of an repository
17 // explicit CatalogueControllerPrivate(CatalogueController* parent) : m_Q {
32 */
18 // parent } {}
33 enum class DBType { SYNC, WORK, TRASH };
34 class CatalogueController::CatalogueControllerPrivate {
35
19
36 public:
20 // CatalogueDao m_CatalogueDao;
37 explicit CatalogueControllerPrivate(CatalogueController *parent) : m_Q{parent} {}
38
21
39 CatalogueDao m_CatalogueDao;
22 // QStringList m_RepositoryList;
23 // CatalogueController* m_Q;
40
24
41 QStringList m_RepositoryList;
25 // QSet<QString> m_KeysWithChanges;
42 CatalogueController *m_Q;
43
26
44 QSet<QString> m_KeysWithChanges;
27 // QString eventUniqueKey(const std::shared_ptr<DBEvent>& event) const;
28 // QString catalogueUniqueKey(const std::shared_ptr<DBCatalogue>& catalogue)
29 // const;
45
30
46 QString eventUniqueKey(const std::shared_ptr<DBEvent> &event) const;
31 // void copyDBtoDB(const QString& dbFrom, const QString& dbTo);
47 QString catalogueUniqueKey(const std::shared_ptr<DBCatalogue> &catalogue) const;
32 // QString toWorkRepository(QString repository);
33 // QString toSyncRepository(QString repository);
34 // void savAllDB();
48
35
49 void copyDBtoDB(const QString &dbFrom, const QString &dbTo);
36 // void saveEvent(std::shared_ptr<DBEvent> event, bool persist = true);
50 QString toWorkRepository(QString repository);
37 // void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool persist =
51 QString toSyncRepository(QString repository);
38 // true);
52 void savAllDB();
53
39
54 void saveEvent(std::shared_ptr<DBEvent> event, bool persist = true);
40 // std::shared_ptr<IRequestPredicate> createFinder(
55 void saveCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool persist = true);
41 // const QUuid& uniqId, const QString& repository, DBType type);
42 //};
56
43
57 std::shared_ptr<IRequestPredicate> createFinder(const QUuid &uniqId, const QString &repository,
44 CatalogueController::CatalogueController(QObject* parent)
58 DBType type);
45 //: impl { spimpl::make_unique_impl<CatalogueControllerPrivate>(this) }
59 };
46 {}
60
47
61 CatalogueController::CatalogueController(QObject *parent)
48 CatalogueController::~CatalogueController() {}
62 : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>(this)}
63 {
64 qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction")
65 << QThread::currentThread();
66 }
67
68 CatalogueController::~CatalogueController()
69 {
70 qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction")
71 << QThread::currentThread();
72 }
73
49
74 QStringList CatalogueController::getRepositories() const
50 QStringList CatalogueController::getRepositories() const
75 {
51 {
76 return impl->m_RepositoryList;
52 QStringList repos;
77 }
53 std::transform(std::begin(_currentRepos), std::end(_currentRepos),
78
54 std::back_inserter(repos),
79 void CatalogueController::addDB(const QString &dbPath)
55 [](auto& pair) { return pair.first; });
80 {
56 return repos;
81 QDir dbDir(dbPath);
82 if (dbDir.exists()) {
83 auto dirName = dbDir.dirName();
84
85 if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName)
86 != impl->m_RepositoryList.cend()) {
87 qCCritical(LOG_CatalogueController())
88 << tr("Impossible to addDB that is already loaded");
89 }
90
91 if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) {
92 qCCritical(LOG_CatalogueController())
93 << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath);
94 }
95 else {
96 impl->m_RepositoryList << dirName;
97 impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
98 }
99 }
100 else {
101 qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ")
102 << dbPath;
103 }
104 }
105
106 void CatalogueController::saveDB(const QString &destinationPath, const QString &repository)
107 {
108 if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) {
109 qCCritical(LOG_CatalogueController())
110 << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath);
111 }
112 }
113
114 std::list<std::shared_ptr<DBEvent> >
115 CatalogueController::retrieveEvents(const QString &repository) const
116 {
117 QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository;
118
119 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
120 auto events = impl->m_CatalogueDao.getEvents(impl->toWorkRepository(dbDireName));
121 for (auto event : events) {
122 eventsShared.push_back(std::make_shared<DBEvent>(event));
123 }
124 return eventsShared;
125 }
126
127 std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const
128 {
129 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
130 for (auto repository : impl->m_RepositoryList) {
131 eventsShared.splice(eventsShared.end(), retrieveEvents(repository));
132 }
133
134 return eventsShared;
135 }
136
137 std::list<std::shared_ptr<DBEvent> >
138 CatalogueController::retrieveEventsFromCatalogue(std::shared_ptr<DBCatalogue> catalogue) const
139 {
140 auto eventsShared = std::list<std::shared_ptr<DBEvent> >{};
141 auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue);
142 for (auto event : events) {
143 eventsShared.push_back(std::make_shared<DBEvent>(event));
144 }
145 return eventsShared;
146 }
147
148 void CatalogueController::updateEvent(std::shared_ptr<DBEvent> event)
149 {
150 event->setRepository(impl->toWorkRepository(event->getRepository()));
151
152 auto uniqueId = impl->eventUniqueKey(event);
153 impl->m_KeysWithChanges.insert(uniqueId);
154
155 impl->m_CatalogueDao.updateEvent(*event);
156 }
157
158 void CatalogueController::updateEventProduct(std::shared_ptr<DBEventProduct> eventProduct)
159 {
160 impl->m_CatalogueDao.updateEventProduct(*eventProduct);
161 }
162
163 void CatalogueController::removeEvent(std::shared_ptr<DBEvent> event)
164 {
165 // Remove it from both repository and repository_work
166 event->setRepository(impl->toWorkRepository(event->getRepository()));
167 impl->m_CatalogueDao.removeEvent(*event);
168 event->setRepository(impl->toSyncRepository(event->getRepository()));
169 impl->m_CatalogueDao.removeEvent(*event);
170 impl->savAllDB();
171 }
172
173 void CatalogueController::addEvent(std::shared_ptr<DBEvent> event)
174 {
175 event->setRepository(impl->toWorkRepository(event->getRepository()));
176
177 auto eventTemp = *event;
178 impl->m_CatalogueDao.addEvent(eventTemp);
179
180 // Call update is necessary at the creation of add Event if it has some tags or some event
181 // products
182 if (!event->getEventProducts().empty() || !event->getTags().empty()) {
183
184 auto eventProductsTemp = eventTemp.getEventProducts();
185 auto eventProductTempUpdated = std::list<DBEventProduct>{};
186 for (auto eventProductTemp : eventProductsTemp) {
187 eventProductTemp.setEvent(eventTemp);
188 eventProductTempUpdated.push_back(eventProductTemp);
189 }
190 eventTemp.setEventProducts(eventProductTempUpdated);
191
192 impl->m_CatalogueDao.updateEvent(eventTemp);
193 }
194
195 auto workPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::WORK);
196
197 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
198 *event = workEvent;
199
200
201 auto uniqueId = impl->eventUniqueKey(event);
202 impl->m_KeysWithChanges.insert(uniqueId);
203 }
204
205 void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event)
206 {
207 impl->saveEvent(event, true);
208 impl->m_KeysWithChanges.remove(impl->eventUniqueKey(event));
209 }
210
211 void CatalogueController::discardEvent(std::shared_ptr<DBEvent> event, bool &removed)
212 {
213 auto syncPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::SYNC);
214 auto workPred = impl->createFinder(event->getUniqId(), event->getRepository(), DBType::WORK);
215
216 auto syncEvent = impl->m_CatalogueDao.getEvent(syncPred);
217 if (!syncEvent.getUniqId().isNull()) {
218 removed = false;
219 impl->m_CatalogueDao.copyEvent(syncEvent, impl->toWorkRepository(event->getRepository()),
220 true);
221
222 auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
223 *event = workEvent;
224 impl->m_KeysWithChanges.remove(impl->eventUniqueKey(event));
225 }
226 else {
227 removed = true;
228 // Since the element wasn't in sync repository. Discard it means remove it
229 event->setRepository(impl->toWorkRepository(event->getRepository()));
230 impl->m_CatalogueDao.removeEvent(*event);
231 }
232 }
233
234 bool CatalogueController::eventHasChanges(std::shared_ptr<DBEvent> event) const
235 {
236 return impl->m_KeysWithChanges.contains(impl->eventUniqueKey(event));
237 }
238
239 std::list<std::shared_ptr<DBCatalogue> >
240 CatalogueController::retrieveCatalogues(const QString &repository) const
241 {
242 QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT : repository;
243
244 auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
245 auto catalogues = impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName));
246 for (auto catalogue : catalogues) {
247 cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue));
248 }
249 return cataloguesShared;
250 }
251
252 void CatalogueController::addCatalogue(std::shared_ptr<DBCatalogue> catalogue)
253 {
254 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
255
256 auto catalogueTemp = *catalogue;
257 impl->m_CatalogueDao.addCatalogue(catalogueTemp);
258
259 auto workPred
260 = impl->createFinder(catalogue->getUniqId(), catalogue->getRepository(), DBType::WORK);
261
262 auto workCatalogue = impl->m_CatalogueDao.getCatalogue(workPred);
263 *catalogue = workCatalogue;
264
265 auto uniqueId = impl->catalogueUniqueKey(catalogue);
266 impl->m_KeysWithChanges.insert(uniqueId);
267 }
268
269 void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue> catalogue)
270 {
271 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
272
273 auto uniqueId = impl->catalogueUniqueKey(catalogue);
274 impl->m_KeysWithChanges.insert(uniqueId);
275
276 impl->m_CatalogueDao.updateCatalogue(*catalogue);
277 }
278
279 void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue> catalogue)
280 {
281 // Remove it from both repository and repository_work
282 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
283 impl->m_CatalogueDao.removeCatalogue(*catalogue);
284 catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository()));
285 impl->m_CatalogueDao.removeCatalogue(*catalogue);
286 impl->savAllDB();
287 }
288
289 void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue> catalogue)
290 {
291 impl->saveCatalogue(catalogue, true);
292 impl->m_KeysWithChanges.remove(impl->catalogueUniqueKey(catalogue));
293
294 // remove key of events of the catalogue
295 if (catalogue->getType() == CatalogueType::STATIC) {
296 auto events = this->retrieveEventsFromCatalogue(catalogue);
297 for (auto event : events) {
298 impl->m_KeysWithChanges.remove(impl->eventUniqueKey(event));
299 }
300 }
301 }
302
303 void CatalogueController::discardCatalogue(std::shared_ptr<DBCatalogue> catalogue, bool &removed)
304 {
305 auto syncPred
306 = impl->createFinder(catalogue->getUniqId(), catalogue->getRepository(), DBType::SYNC);
307 auto workPred
308 = impl->createFinder(catalogue->getUniqId(), catalogue->getRepository(), DBType::WORK);
309
310 auto syncCatalogue = impl->m_CatalogueDao.getCatalogue(syncPred);
311 if (!syncCatalogue.getUniqId().isNull()) {
312 removed = false;
313 impl->m_CatalogueDao.copyCatalogue(
314 syncCatalogue, impl->toWorkRepository(catalogue->getRepository()), true);
315
316 auto workCatalogue = impl->m_CatalogueDao.getCatalogue(workPred);
317 *catalogue = workCatalogue;
318 impl->m_KeysWithChanges.remove(impl->catalogueUniqueKey(catalogue));
319 }
320 else {
321 removed = true;
322 // Since the element wasn't in sync repository. Discard it means remove it
323 catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
324 impl->m_CatalogueDao.removeCatalogue(*catalogue);
325 }
326 }
327
328 void CatalogueController::saveAll()
329 {
330 for (auto repository : impl->m_RepositoryList) {
331 // Save Event
332 auto events = this->retrieveEvents(repository);
333 for (auto event : events) {
334 impl->saveEvent(event, false);
335 }
336
337 // Save Catalogue
338 auto catalogues = this->retrieveCatalogues(repository);
339 for (auto catalogue : catalogues) {
340 impl->saveCatalogue(catalogue, false);
341 }
342 }
343
344 impl->savAllDB();
345 impl->m_KeysWithChanges.clear();
346 }
347
348 bool CatalogueController::hasChanges() const
349 {
350 return !impl->m_KeysWithChanges.isEmpty();
351 }
352
353 QByteArray
354 CatalogueController::mimeDataForEvents(const QVector<std::shared_ptr<DBEvent> > &events) const
355 {
356 auto encodedData = QByteArray{};
357
358 QMap<QString, QVariantList> idsPerRepository;
359 for (auto event : events) {
360 idsPerRepository[event->getRepository()] << event->getUniqId();
361 }
362
363 QDataStream stream{&encodedData, QIODevice::WriteOnly};
364 stream << idsPerRepository;
365
366 return encodedData;
367 }
57 }
368
58
369 QVector<std::shared_ptr<DBEvent> >
59 void CatalogueController::loadRepository(const QString& path,
370 CatalogueController::eventsForMimeData(const QByteArray &mimeData) const
60 const QString& name)
371 {
61 {
372 auto events = QVector<std::shared_ptr<DBEvent> >{};
62 if(QFile::exists(path))
373 QDataStream stream{mimeData};
63 {
374
64 auto repo = CatalogiCpp::load_repository<time_t>(path.toStdString());
375 QMap<QString, QVariantList> idsPerRepository;
65 _lastSavedRepos[name] = repo;
376 stream >> idsPerRepository;
66 _currentRepos[name] = repo;
377
67 }
378 for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend(); ++it) {
379 auto repository = it.key();
380 auto allRepositoryEvent = retrieveEvents(repository);
381 for (auto uuid : it.value()) {
382 for (auto repositoryEvent : allRepositoryEvent) {
383 if (uuid.toUuid() == repositoryEvent->getUniqId()) {
384 events << repositoryEvent;
385 }
386 }
387 }
388 }
389
390 return events;
391 }
68 }
392
69
393 QByteArray CatalogueController::mimeDataForCatalogues(
70 void CatalogueController::saveRepository(const QString& path,
394 const QVector<std::shared_ptr<DBCatalogue> > &catalogues) const
71 const QString& name)
395 {
72 {
396 auto encodedData = QByteArray{};
73 if(contains(_currentRepos, name))
397
74 {
398 QMap<QString, QVariantList> idsPerRepository;
75 CatalogiCpp::save_repository(_currentRepos[name], path.toStdString());
399 for (auto catalogue : catalogues) {
76 _lastSavedRepos[name] = _currentRepos[name];
400 idsPerRepository[catalogue->getRepository()] << catalogue->getUniqId();
77 }
401 }
402
403 QDataStream stream{&encodedData, QIODevice::WriteOnly};
404 stream << idsPerRepository;
405
406 return encodedData;
407 }
78 }
408
79
409 QVector<std::shared_ptr<DBCatalogue> >
80 std::vector<CatalogueController::Event_ptr> CatalogueController::events()
410 CatalogueController::cataloguesForMimeData(const QByteArray &mimeData) const
411 {
81 {
412 auto catalogues = QVector<std::shared_ptr<DBCatalogue> >{};
82 std::vector<CatalogueController::Event_ptr> e_list;
413 QDataStream stream{mimeData};
83 for(auto& [_, repo] : _currentRepos)
414
84 {
415 QMap<QString, QVariantList> idsPerRepository;
85 for(auto& [_, event] : repo.events())
416 stream >> idsPerRepository;
86 e_list.push_back(event);
417
87 }
418 for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend(); ++it) {
88 return e_list;
419 auto repository = it.key();
420 auto allRepositoryCatalogues = retrieveCatalogues(repository);
421 for (auto uuid : it.value()) {
422 for (auto repositoryCatalogues : allRepositoryCatalogues) {
423 if (uuid.toUuid() == repositoryCatalogues->getUniqId()) {
424 catalogues << repositoryCatalogues;
425 }
426 }
427 }
428 }
429
430 return catalogues;
431 }
89 }
432
90
433 void CatalogueController::initialize()
91 std::vector<std::shared_ptr<CatalogueController::Event_t>>
92 CatalogueController::events(const QString& repository)
434 {
93 {
435 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init")
94 std::vector<std::shared_ptr<CatalogueController::Event_t>> e_list;
436 << QThread::currentThread();
95 if(contains(_currentRepos, repository))
437
96 {
438 impl->m_CatalogueDao.initialize();
97 auto repo = _currentRepos[repository];
439 auto defaultRepositoryLocation
98 for(auto& [_, event] : repo.events())
440 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
99 e_list.push_back(event);
441
100 }
442 QDir defaultRepositoryLocationDir;
101 return e_list;
443 if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) {
444 defaultRepositoryLocationDir.cd(defaultRepositoryLocation);
445 auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT);
446
447 qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ")
448 << defaultRepository;
449
450 QDir dbDir(defaultRepository);
451 impl->m_RepositoryList << REPOSITORY_DEFAULT;
452 if (dbDir.exists()) {
453 auto dirName = dbDir.dirName();
454
455 if (impl->m_CatalogueDao.addDB(defaultRepository, dirName)) {
456 impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
457 }
458 }
459 else {
460 qCInfo(LOG_CatalogueController()) << tr("Initialisation of Default repository detected")
461 << defaultRepository;
462 }
463 }
464 else {
465 qCWarning(LOG_CatalogueController())
466 << tr("Cannot load the persistent default repository from ")
467 << defaultRepositoryLocation;
468 }
469
470 qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END");
471 }
102 }
472
103
473 QString CatalogueController::CatalogueControllerPrivate::eventUniqueKey(
104 std::vector<CatalogueController::Catalogue_ptr>
474 const std::shared_ptr<DBEvent> &event) const
105 CatalogueController::catalogues()
475 {
106 {
476 return event->getUniqId().toString().append(event->getRepository());
107 std::vector<CatalogueController::Catalogue_ptr> c_list;
108 for(auto& [_, repo] : _currentRepos)
109 {
110 for(auto& [_, catalogue] : repo.catalogues())
111 c_list.push_back(catalogue);
112 }
113 return c_list;
477 }
114 }
478
115
479 QString CatalogueController::CatalogueControllerPrivate::catalogueUniqueKey(
116 std::vector<CatalogueController::Catalogue_ptr>
480 const std::shared_ptr<DBCatalogue> &catalogue) const
117 CatalogueController::catalogues(const QString& repository)
481 {
118 {
482 return catalogue->getUniqId().toString().append(catalogue->getRepository());
119 std::vector<CatalogueController::Catalogue_ptr> c_list;
120 if(contains(_currentRepos, repository))
121 {
122 auto repo = _currentRepos[repository];
123 for(auto& [_, catalogue] : repo.catalogues())
124 c_list.push_back(catalogue);
125 }
126 return c_list;
483 }
127 }
484
128
485 void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(const QString &dbFrom,
129 bool CatalogueController::hasUnsavedChanges(
486 const QString &dbTo)
130 CatalogueController::Event_ptr event)
487 {
131 {
488 // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
132 if(auto repo = repository(event))
489 auto catalogues = m_CatalogueDao.getCatalogues(dbFrom);
133 {
490 auto events = m_CatalogueDao.getEvents(dbFrom);
134 if(contains(_lastSavedRepos, *repo))
491 for (auto catalogue : catalogues) {
135 { return *event != *(_lastSavedRepos[*repo].event(event->uuid)); }
492 m_CatalogueDao.copyCatalogue(catalogue, dbTo, true);
136 }
493 }
137 return true;
494
495 for (auto event : events) {
496 m_CatalogueDao.copyEvent(event, dbTo, true);
497 }
498 }
138 }
499
139
500 QString CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString repository)
140 std::optional<QString>
141 CatalogueController::repository(CatalogueController::Event_ptr event)
501 {
142 {
502 auto syncRepository = toSyncRepository(repository);
143 for(auto& [repoName, repo] : _currentRepos)
503
144 {
504 return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX);
145 if(repo.event(event->uuid)) return repoName;
146 }
147 return std::nullopt;
505 }
148 }
506
149
507 QString CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString repository)
150 std::optional<QString>
151 CatalogueController::repository(CatalogueController::Catalogue_ptr catalogue)
508 {
152 {
509 auto syncRepository = repository;
153 for(auto& [repoName, repo] : _currentRepos)
510 if (repository.endsWith(REPOSITORY_WORK_SUFFIX)) {
154 {
511 syncRepository.remove(REPOSITORY_WORK_SUFFIX);
155 if(repo.catalogue(catalogue->uuid)) return repoName;
512 }
156 }
513 else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX)) {
157 return std::nullopt;
514 syncRepository.remove(REPOSITORY_TRASH_SUFFIX);
515 }
516 return syncRepository;
517 }
158 }
518
159
519 void CatalogueController::CatalogueControllerPrivate::savAllDB()
160 void CatalogueController::save(CatalogueController::Event_ptr event)
520 {
161 {
521 for (auto repository : m_RepositoryList) {
162 auto repo_name = repository(event);
522 auto defaultRepositoryLocation
163 if(repo_name && contains(_lastSavedRepos, *repo_name))
523 = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
164 {
524 m_CatalogueDao.saveDB(defaultRepositoryLocation, repository);
165 auto repo = _lastSavedRepos[*repo_name];
166 if(contains(repo.events(), event->uuid))
167 {
168 auto saved_evemt = _lastSavedRepos[*repo_name].event(event->uuid);
169 *saved_evemt = *event;
525 }
170 }
526 }
171 else
527
172 {
528 void CatalogueController::CatalogueControllerPrivate::saveEvent(std::shared_ptr<DBEvent> event,
173 // TODO/Question should we also take care of which catalogue has it?
529 bool persist)
174 // if an event is created and added to a catalogue, what should we do if
530 {
175 // the user clicks the event save button?
531 m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()), true);
176 // - Only save event value in all events list?
532 if (persist) {
177 // - Also save the fact that this event is added to a catalogue and save
533 savAllDB();
178 // the catalogue?
179 repo.add(event);
534 }
180 }
181 }
535 }
182 }
536
183
537 void CatalogueController::CatalogueControllerPrivate::saveCatalogue(
184 void CatalogueController::save(CatalogueController::Catalogue_ptr catalogue)
538 std::shared_ptr<DBCatalogue> catalogue, bool persist)
539 {
185 {
540 m_CatalogueDao.copyCatalogue(*catalogue, toSyncRepository(catalogue->getRepository()), true);
186 auto repo_name = repository(catalogue);
541 if (persist) {
187 if(repo_name && contains(_lastSavedRepos, *repo_name))
542 savAllDB();
188 {
189 auto repo = _lastSavedRepos[*repo_name];
190 if(contains(repo.catalogues(), catalogue->uuid))
191 {
192 auto saved_catalogue = repo.catalogue(catalogue->uuid);
193 *saved_catalogue = *catalogue;
543 }
194 }
544 }
195 else
545
196 {
546 std::shared_ptr<IRequestPredicate> CatalogueController::CatalogueControllerPrivate::createFinder(
197 repo.add(catalogue);
547 const QUuid &uniqId, const QString &repository, DBType type)
548 {
549 // update catalogue parameter
550 auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(QString{"uniqId"}, uniqId,
551 ComparaisonOperation::EQUALEQUAL);
552
553 auto repositoryType = repository;
554 switch (type) {
555 case DBType::SYNC:
556 repositoryType = toSyncRepository(repositoryType);
557 break;
558 case DBType::WORK:
559 repositoryType = toWorkRepository(repositoryType);
560 break;
561 case DBType::TRASH:
562 default:
563 break;
564 }
198 }
565
199 }
566 auto repositoryPredicate = std::make_shared<ComparaisonPredicate>(
200 }
567 QString{"repository"}, repositoryType, ComparaisonOperation::EQUALEQUAL);
201
568
202 void CatalogueController::save(const QString& repository)
569 auto finderPred = std::make_shared<CompoundPredicate>(CompoundOperation::AND);
203 {
570 finderPred->AddRequestPredicate(uniqIdPredicate);
204 if(contains(_currentRepos, repository))
571 finderPred->AddRequestPredicate(repositoryPredicate);
205 { _lastSavedRepos[repository] = _currentRepos[repository]; }
572
206 else
573 return finderPred;
207 {
574 }
208 SCIQLOP_ERROR(CatalogueController, "Trying to save an unknown repository");
209 }
210 }
211
212 // void CatalogueController::saveDB(const QString& destinationPath, const
213 // QString& repositoryName)
214 //{
215 // if (!impl->m_CatalogueDao.saveDB(destinationPath, repositoryName))
216 // {
217 // qCCritical(LOG_CatalogueController())
218 // << tr("Impossible to saveDB %1 from %2 ").arg(repositoryName,
219 // destinationPath);
220 // }
221 //}
222
223 // std::list<std::shared_ptr<DBEvent>> CatalogueController::retrieveEvents(
224 // const QString& repository) const
225 //{
226 // QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT :
227 // repository;
228
229 // auto eventsShared = std::list<std::shared_ptr<DBEvent>> {};
230 // auto events =
231 // impl->m_CatalogueDao.getEvents(impl->toWorkRepository(dbDireName)); for
232 // (auto event : events)
233 // {
234 // eventsShared.push_back(std::make_shared<DBEvent>(event));
235 // }
236 // return eventsShared;
237 //}
238
239 // std::list<std::shared_ptr<DBEvent>> CatalogueController::retrieveAllEvents()
240 // const
241 //{
242 // auto eventsShared = std::list<std::shared_ptr<DBEvent>> {};
243 // for (auto repository : impl->m_RepositoryList)
244 // {
245 // eventsShared.splice(eventsShared.end(), retrieveEvents(repository));
246 // }
247
248 // return eventsShared;
249 //}
250
251 // std::list<std::shared_ptr<DBEvent>>
252 // CatalogueController::retrieveEventsFromCatalogue(
253 // std::shared_ptr<DBCatalogue> catalogue) const
254 //{
255 // auto eventsShared = std::list<std::shared_ptr<DBEvent>> {};
256 // auto events = impl->m_CatalogueDao.getCatalogueEvents(*catalogue);
257 // for (auto event : events)
258 // {
259 // eventsShared.push_back(std::make_shared<DBEvent>(event));
260 // }
261 // return eventsShared;
262 //}
263
264 // void CatalogueController::updateEvent(std::shared_ptr<DBEvent> event)
265 //{
266 // event->setRepository(impl->toWorkRepository(event->getRepository()));
267
268 // auto uniqueId = impl->eventUniqueKey(event);
269 // impl->m_KeysWithChanges.insert(uniqueId);
270
271 // impl->m_CatalogueDao.updateEvent(*event);
272 //}
273
274 // void CatalogueController::updateEventProduct(std::shared_ptr<DBEventProduct>
275 // eventProduct)
276 //{
277 // impl->m_CatalogueDao.updateEventProduct(*eventProduct);
278 //}
279
280 // void CatalogueController::removeEvent(std::shared_ptr<DBEvent> event)
281 //{
282 // // Remove it from both repository and repository_work
283 // event->setRepository(impl->toWorkRepository(event->getRepository()));
284 // impl->m_CatalogueDao.removeEvent(*event);
285 // event->setRepository(impl->toSyncRepository(event->getRepository()));
286 // impl->m_CatalogueDao.removeEvent(*event);
287 // impl->savAllDB();
288 //}
289
290 // void CatalogueController::addEvent(std::shared_ptr<DBEvent> event)
291 //{
292 // event->setRepository(impl->toWorkRepository(event->getRepository()));
293
294 // auto eventTemp = *event;
295 // impl->m_CatalogueDao.addEvent(eventTemp);
296
297 // // Call update is necessary at the creation of add Event if it has some
298 // tags or some event
299 // // products
300 // if (!event->getEventProducts().empty() || !event->getTags().empty())
301 // {
302
303 // auto eventProductsTemp = eventTemp.getEventProducts();
304 // auto eventProductTempUpdated = std::list<DBEventProduct> {};
305 // for (auto eventProductTemp : eventProductsTemp)
306 // {
307 // eventProductTemp.setEvent(eventTemp);
308 // eventProductTempUpdated.push_back(eventProductTemp);
309 // }
310 // eventTemp.setEventProducts(eventProductTempUpdated);
311
312 // impl->m_CatalogueDao.updateEvent(eventTemp);
313 // }
314
315 // auto workPred = impl->createFinder(event->getUniqId(),
316 // event->getRepository(), DBType::WORK);
317
318 // auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
319 // *event = workEvent;
320
321 // auto uniqueId = impl->eventUniqueKey(event);
322 // impl->m_KeysWithChanges.insert(uniqueId);
323 //}
324
325 // void CatalogueController::saveEvent(std::shared_ptr<DBEvent> event)
326 //{
327 // impl->saveEvent(event, true);
328 // impl->m_KeysWithChanges.remove(impl->eventUniqueKey(event));
329 //}
330
331 // void CatalogueController::discardEvent(std::shared_ptr<DBEvent> event, bool&
332 // removed)
333 //{
334 // auto syncPred = impl->createFinder(event->getUniqId(),
335 // event->getRepository(), DBType::SYNC); auto workPred =
336 // impl->createFinder(event->getUniqId(), event->getRepository(),
337 // DBType::WORK);
338
339 // auto syncEvent = impl->m_CatalogueDao.getEvent(syncPred);
340 // if (!syncEvent.getUniqId().isNull())
341 // {
342 // removed = false;
343 // impl->m_CatalogueDao.copyEvent(
344 // syncEvent, impl->toWorkRepository(event->getRepository()), true);
345
346 // auto workEvent = impl->m_CatalogueDao.getEvent(workPred);
347 // *event = workEvent;
348 // impl->m_KeysWithChanges.remove(impl->eventUniqueKey(event));
349 // }
350 // else
351 // {
352 // removed = true;
353 // // Since the element wasn't in sync repository. Discard it means
354 // remove it
355 // event->setRepository(impl->toWorkRepository(event->getRepository()));
356 // impl->m_CatalogueDao.removeEvent(*event);
357 // }
358 //}
359
360 // bool CatalogueController::eventHasChanges(std::shared_ptr<DBEvent> event)
361 // const
362 //{
363 // return impl->m_KeysWithChanges.contains(impl->eventUniqueKey(event));
364 //}
365
366 // std::list<std::shared_ptr<DBCatalogue>>
367 // CatalogueController::retrieveCatalogues(
368 // const QString& repository) const
369 //{
370 // QString dbDireName = repository.isEmpty() ? REPOSITORY_DEFAULT :
371 // repository;
372
373 // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue>> {};
374 // auto catalogues =
375 // impl->m_CatalogueDao.getCatalogues(impl->toWorkRepository(dbDireName));
376 // for (auto catalogue : catalogues)
377 // {
378 // cataloguesShared.push_back(std::make_shared<DBCatalogue>(catalogue));
379 // }
380 // return cataloguesShared;
381 //}
382
383 // void CatalogueController::addCatalogue(std::shared_ptr<DBCatalogue>
384 // catalogue)
385 //{
386 // catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
387
388 // auto catalogueTemp = *catalogue;
389 // impl->m_CatalogueDao.addCatalogue(catalogueTemp);
390
391 // auto workPred
392 // = impl->createFinder(catalogue->getUniqId(),
393 // catalogue->getRepository(), DBType::WORK);
394
395 // auto workCatalogue = impl->m_CatalogueDao.getCatalogue(workPred);
396 // *catalogue = workCatalogue;
397
398 // auto uniqueId = impl->catalogueUniqueKey(catalogue);
399 // impl->m_KeysWithChanges.insert(uniqueId);
400 //}
401
402 // void CatalogueController::updateCatalogue(std::shared_ptr<DBCatalogue>
403 // catalogue)
404 //{
405 // catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
406
407 // auto uniqueId = impl->catalogueUniqueKey(catalogue);
408 // impl->m_KeysWithChanges.insert(uniqueId);
409
410 // impl->m_CatalogueDao.updateCatalogue(*catalogue);
411 //}
412
413 // void CatalogueController::removeCatalogue(std::shared_ptr<DBCatalogue>
414 // catalogue)
415 //{
416 // // Remove it from both repository and repository_work
417 // catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
418 // impl->m_CatalogueDao.removeCatalogue(*catalogue);
419 // catalogue->setRepository(impl->toSyncRepository(catalogue->getRepository()));
420 // impl->m_CatalogueDao.removeCatalogue(*catalogue);
421 // impl->savAllDB();
422 //}
423
424 // void CatalogueController::saveCatalogue(std::shared_ptr<DBCatalogue>
425 // catalogue)
426 //{
427 // impl->saveCatalogue(catalogue, true);
428 // impl->m_KeysWithChanges.remove(impl->catalogueUniqueKey(catalogue));
429
430 // // remove key of events of the catalogue
431 // if (catalogue->getType() == CatalogueType::STATIC)
432 // {
433 // auto events = this->retrieveEventsFromCatalogue(catalogue);
434 // for (auto event : events)
435 // {
436 // impl->m_KeysWithChanges.remove(impl->eventUniqueKey(event));
437 // }
438 // }
439 //}
440
441 // void CatalogueController::discardCatalogue(std::shared_ptr<DBCatalogue>
442 // catalogue, bool& removed)
443 //{
444 // auto syncPred
445 // = impl->createFinder(catalogue->getUniqId(),
446 // catalogue->getRepository(), DBType::SYNC);
447 // auto workPred
448 // = impl->createFinder(catalogue->getUniqId(),
449 // catalogue->getRepository(), DBType::WORK);
450
451 // auto syncCatalogue = impl->m_CatalogueDao.getCatalogue(syncPred);
452 // if (!syncCatalogue.getUniqId().isNull())
453 // {
454 // removed = false;
455 // impl->m_CatalogueDao.copyCatalogue(
456 // syncCatalogue, impl->toWorkRepository(catalogue->getRepository()),
457 // true);
458
459 // auto workCatalogue = impl->m_CatalogueDao.getCatalogue(workPred);
460 // *catalogue = workCatalogue;
461 // impl->m_KeysWithChanges.remove(impl->catalogueUniqueKey(catalogue));
462 // }
463 // else
464 // {
465 // removed = true;
466 // // Since the element wasn't in sync repository. Discard it means
467 // remove it
468 // catalogue->setRepository(impl->toWorkRepository(catalogue->getRepository()));
469 // impl->m_CatalogueDao.removeCatalogue(*catalogue);
470 // }
471 //}
472
473 // void CatalogueController::saveAll()
474 //{
475 // for (auto repository : impl->m_RepositoryList)
476 // {
477 // // Save Event
478 // auto events = this->retrieveEvents(repository);
479 // for (auto event : events)
480 // {
481 // impl->saveEvent(event, false);
482 // }
483
484 // // Save Catalogue
485 // auto catalogues = this->retrieveCatalogues(repository);
486 // for (auto catalogue : catalogues)
487 // {
488 // impl->saveCatalogue(catalogue, false);
489 // }
490 // }
491
492 // impl->savAllDB();
493 // impl->m_KeysWithChanges.clear();
494 //}
495
496 // bool CatalogueController::hasChanges() const
497 //{
498 // return !impl->m_KeysWithChanges.isEmpty();
499 //}
500
501 // QByteArray CatalogueController::mimeDataForEvents(
502 // const QVector<std::shared_ptr<DBEvent>>& events) const
503 //{
504 // auto encodedData = QByteArray {};
505
506 // QMap<QString, QVariantList> idsPerRepository;
507 // for (auto event : events)
508 // {
509 // idsPerRepository[event->getRepository()] << event->getUniqId();
510 // }
511
512 // QDataStream stream { &encodedData, QIODevice::WriteOnly };
513 // stream << idsPerRepository;
514
515 // return encodedData;
516 //}
517
518 // QVector<std::shared_ptr<DBEvent>> CatalogueController::eventsForMimeData(
519 // const QByteArray& mimeData) const
520 //{
521 // auto events = QVector<std::shared_ptr<DBEvent>> {};
522 // QDataStream stream { mimeData };
523
524 // QMap<QString, QVariantList> idsPerRepository;
525 // stream >> idsPerRepository;
526
527 // for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend();
528 // ++it)
529 // {
530 // auto repository = it.key();
531 // auto allRepositoryEvent = retrieveEvents(repository);
532 // for (auto uuid : it.value())
533 // {
534 // for (auto repositoryEvent : allRepositoryEvent)
535 // {
536 // if (uuid.toUuid() == repositoryEvent->getUniqId())
537 // {
538 // events << repositoryEvent;
539 // }
540 // }
541 // }
542 // }
543
544 // return events;
545 //}
546
547 // QByteArray CatalogueController::mimeDataForCatalogues(
548 // const QVector<std::shared_ptr<DBCatalogue>>& catalogues) const
549 //{
550 // auto encodedData = QByteArray {};
551
552 // QMap<QString, QVariantList> idsPerRepository;
553 // for (auto catalogue : catalogues)
554 // {
555 // idsPerRepository[catalogue->getRepository()] <<
556 // catalogue->getUniqId();
557 // }
558
559 // QDataStream stream { &encodedData, QIODevice::WriteOnly };
560 // stream << idsPerRepository;
561
562 // return encodedData;
563 //}
564
565 // QVector<std::shared_ptr<DBCatalogue>>
566 // CatalogueController::cataloguesForMimeData(
567 // const QByteArray& mimeData) const
568 //{
569 // auto catalogues = QVector<std::shared_ptr<DBCatalogue>> {};
570 // QDataStream stream { mimeData };
571
572 // QMap<QString, QVariantList> idsPerRepository;
573 // stream >> idsPerRepository;
574
575 // for (auto it = idsPerRepository.cbegin(); it != idsPerRepository.cend();
576 // ++it)
577 // {
578 // auto repository = it.key();
579 // auto allRepositoryCatalogues = retrieveCatalogues(repository);
580 // for (auto uuid : it.value())
581 // {
582 // for (auto repositoryCatalogues : allRepositoryCatalogues)
583 // {
584 // if (uuid.toUuid() == repositoryCatalogues->getUniqId())
585 // {
586 // catalogues << repositoryCatalogues;
587 // }
588 // }
589 // }
590 // }
591
592 // return catalogues;
593 //}
594
595 // void CatalogueController::initialize()
596 //{
597 // qCDebug(LOG_CatalogueController())
598 // << tr("CatalogueController init") << QThread::currentThread();
599
600 // impl->m_CatalogueDao.initialize();
601 // auto defaultRepositoryLocation
602 // = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
603
604 // QDir defaultRepositoryLocationDir;
605 // if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation))
606 // {
607 // defaultRepositoryLocationDir.cd(defaultRepositoryLocation);
608 // auto defaultRepository =
609 // defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT);
610
611 // qCInfo(LOG_CatalogueController())
612 // << tr("Persistant data loading from: ") << defaultRepository;
613
614 // QDir dbDir(defaultRepository);
615 // impl->m_RepositoryList << REPOSITORY_DEFAULT;
616 // if (dbDir.exists())
617 // {
618 // auto dirName = dbDir.dirName();
619
620 // if (impl->m_CatalogueDao.addDB(defaultRepository, dirName))
621 // {
622 // impl->copyDBtoDB(dirName, impl->toWorkRepository(dirName));
623 // }
624 // }
625 // else
626 // {
627 // qCInfo(LOG_CatalogueController())
628 // << tr("Initialisation of Default repository detected") <<
629 // defaultRepository;
630 // }
631 // }
632 // else
633 // {
634 // qCWarning(LOG_CatalogueController())
635 // << tr("Cannot load the persistent default repository from ")
636 // << defaultRepositoryLocation;
637 // }
638
639 // qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END");
640 //}
641
642 // QString CatalogueController::CatalogueControllerPrivate::eventUniqueKey(
643 // const std::shared_ptr<DBEvent>& event) const
644 //{
645 // return event->getUniqId().toString().append(event->getRepository());
646 //}
647
648 // QString CatalogueController::CatalogueControllerPrivate::catalogueUniqueKey(
649 // const std::shared_ptr<DBCatalogue>& catalogue) const
650 //{
651 // return
652 // catalogue->getUniqId().toString().append(catalogue->getRepository());
653 //}
654
655 // void CatalogueController::CatalogueControllerPrivate::copyDBtoDB(
656 // const QString& dbFrom, const QString& dbTo)
657 //{
658 // // auto cataloguesShared = std::list<std::shared_ptr<DBCatalogue> >{};
659 // auto catalogues = m_CatalogueDao.getCatalogues(dbFrom);
660 // auto events = m_CatalogueDao.getEvents(dbFrom);
661 // for (auto catalogue : catalogues)
662 // {
663 // m_CatalogueDao.copyCatalogue(catalogue, dbTo, true);
664 // }
665
666 // for (auto event : events)
667 // {
668 // m_CatalogueDao.copyEvent(event, dbTo, true);
669 // }
670 //}
671
672 // QString
673 // CatalogueController::CatalogueControllerPrivate::toWorkRepository(QString
674 // repository)
675 //{
676 // auto syncRepository = toSyncRepository(repository);
677
678 // return QString("%1%2").arg(syncRepository, REPOSITORY_WORK_SUFFIX);
679 //}
680
681 // QString
682 // CatalogueController::CatalogueControllerPrivate::toSyncRepository(QString
683 // repository)
684 //{
685 // auto syncRepository = repository;
686 // if (repository.endsWith(REPOSITORY_WORK_SUFFIX))
687 // {
688 // syncRepository.remove(REPOSITORY_WORK_SUFFIX);
689 // }
690 // else if (repository.endsWith(REPOSITORY_TRASH_SUFFIX))
691 // {
692 // syncRepository.remove(REPOSITORY_TRASH_SUFFIX);
693 // }
694 // return syncRepository;
695 //}
696
697 // void CatalogueController::CatalogueControllerPrivate::savAllDB()
698 //{
699 // for (auto repository : m_RepositoryList)
700 // {
701 // auto defaultRepositoryLocation
702 // =
703 // QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
704 // m_CatalogueDao.saveDB(defaultRepositoryLocation, repository);
705 // }
706 //}
707
708 // void CatalogueController::CatalogueControllerPrivate::saveEvent(
709 // std::shared_ptr<DBEvent> event, bool persist)
710 //{
711 // m_CatalogueDao.copyEvent(*event, toSyncRepository(event->getRepository()),
712 // true); if (persist)
713 // {
714 // savAllDB();
715 // }
716 //}
717
718 // void CatalogueController::CatalogueControllerPrivate::saveCatalogue(
719 // std::shared_ptr<DBCatalogue> catalogue, bool persist)
720 //{
721 // m_CatalogueDao.copyCatalogue(*catalogue,
722 // toSyncRepository(catalogue->getRepository()), true); if (persist)
723 // {
724 // savAllDB();
725 // }
726 //}
727
728 // std::shared_ptr<IRequestPredicate>
729 // CatalogueController::CatalogueControllerPrivate::createFinder(
730 // const QUuid& uniqId, const QString& repository, DBType type)
731 //{
732 // // update catalogue parameter
733 // auto uniqIdPredicate = std::make_shared<ComparaisonPredicate>(
734 // QString { "uniqId" }, uniqId, ComparaisonOperation::EQUALEQUAL);
735
736 // auto repositoryType = repository;
737 // switch (type)
738 // {
739 // case DBType::SYNC:
740 // repositoryType = toSyncRepository(repositoryType);
741 // break;
742 // case DBType::WORK:
743 // repositoryType = toWorkRepository(repositoryType);
744 // break;
745 // case DBType::TRASH:
746 // default:
747 // break;
748 // }
749
750 // auto repositoryPredicate = std::make_shared<ComparaisonPredicate>(
751 // QString { "repository" }, repositoryType,
752 // ComparaisonOperation::EQUALEQUAL);
753
754 // auto finderPred =
755 // std::make_shared<CompoundPredicate>(CompoundOperation::AND);
756 // finderPred->AddRequestPredicate(uniqIdPredicate);
757 // finderPred->AddRequestPredicate(repositoryPredicate);
758
759 // return finderPred;
760 //}
@@ -1,80 +1,81
1 #include <QString>
1 #include "pywrappers_common.h"
2 #include <QUuid>
3 #include <QDate>
2 #include <QDate>
3 #include <QString>
4 #include <QTime>
4 #include <QTime>
5 #include <QUuid>
5 #include <QVector>
6 #include <QVector>
6 #include <string>
7 #include <sstream>
8 #include "pywrappers_common.h"
9 #include <pybind11/pybind11.h>
10 #include <pybind11/operators.h>
7 #include <pybind11/operators.h>
8 #include <pybind11/pybind11.h>
9 #include <sstream>
10 #include <string>
11
11
12 #include <Catalogue/CatalogueController.h>
12 #include <Catalogue/CatalogueController.h>
13 #include <DBCatalogue.h>
13 #include <DBEvent.h>
14 #include <DBEvent.h>
14 #include <DBEventProduct.h>
15 #include <DBEventProduct.h>
15 #include <DBCatalogue.h>
16
16
17
17
18 namespace py = pybind11;
18 namespace py = pybind11;
19
19
20 std::ostream &operator <<(std::ostream& os, const DBEvent& e)
20 std::ostream& operator<<(std::ostream& os, const DBEvent& e)
21 {
21 {
22 os << std::endl;
22 os << std::endl;
23 return os;
23 return os;
24 }
24 }
25
25
26 #define JAVA_LIKE_PROPERTY(name, className) .def_property("##name", &className::get##name, &className::set##name)
26 #define JAVA_LIKE_PROPERTY(name, className) \
27 .def_property("##name", &className::get##name, &className::set##name)
27
28
28 PYBIND11_MODULE(pysciqlopcatalogs,m){
29 PYBIND11_MODULE(pysciqlopcatalogs, m)
30 {
29
31
30 py::class_<DBEventProduct, std::shared_ptr<DBEventProduct>>(m,"DBEventProduct")
32 // py::class_<DBEventProduct, std::shared_ptr<DBEventProduct>>(m,"DBEventProduct")
31 JAVA_LIKE_PROPERTY(TStart, DBEventProduct)
33 // JAVA_LIKE_PROPERTY(TStart, DBEventProduct)
32 JAVA_LIKE_PROPERTY(TEnd, DBEventProduct)
34 // JAVA_LIKE_PROPERTY(TEnd, DBEventProduct)
33 JAVA_LIKE_PROPERTY(ProductId, DBEventProduct)
35 // JAVA_LIKE_PROPERTY(ProductId, DBEventProduct)
34 JAVA_LIKE_PROPERTY(CreationDateTime, DBEventProduct)
36 // JAVA_LIKE_PROPERTY(CreationDateTime, DBEventProduct)
35 JAVA_LIKE_PROPERTY(ModificationDateTime, DBEventProduct)
37 // JAVA_LIKE_PROPERTY(ModificationDateTime, DBEventProduct)
36 JAVA_LIKE_PROPERTY(Event, DBEventProduct)
38 // JAVA_LIKE_PROPERTY(Event, DBEventProduct)
37 ;
39 // ;
38
40
39 py::class_<DBEvent, std::shared_ptr<DBEvent>>(m, "DBEvent")
41 // py::class_<DBEvent, std::shared_ptr<DBEvent>>(m, "DBEvent")
40 JAVA_LIKE_PROPERTY(Name,DBEvent)
42 // JAVA_LIKE_PROPERTY(Name,DBEvent)
41 JAVA_LIKE_PROPERTY(UniqId,DBEvent)
43 // JAVA_LIKE_PROPERTY(UniqId,DBEvent)
42 JAVA_LIKE_PROPERTY(Author,DBEvent)
44 // JAVA_LIKE_PROPERTY(Author,DBEvent)
43 JAVA_LIKE_PROPERTY(Repository,DBEvent)
45 // JAVA_LIKE_PROPERTY(Repository,DBEvent)
44 JAVA_LIKE_PROPERTY(CreationDateTime,DBEvent)
46 // JAVA_LIKE_PROPERTY(CreationDateTime,DBEvent)
45 JAVA_LIKE_PROPERTY(ModificationDateTime,DBEvent)
47 // JAVA_LIKE_PROPERTY(ModificationDateTime,DBEvent)
46 JAVA_LIKE_PROPERTY(EventProducts,DBEvent)
48 // JAVA_LIKE_PROPERTY(EventProducts,DBEvent)
47 .def_property_readonly("TStart", &DBEvent::getTStart)
49 // .def_property_readonly("TStart", &DBEvent::getTStart)
48 .def_property_readonly("TEnd", &DBEvent::getTEnd)
50 // .def_property_readonly("TEnd", &DBEvent::getTEnd)
49 .def("__repr__",__repr__<DBEvent>);
51 // .def("__repr__",__repr__<DBEvent>);
50
52
51 py::class_<DBCatalogue, std::shared_ptr<DBCatalogue>>(m,"DBEventProduct")
53 // py::class_<DBCatalogue, std::shared_ptr<DBCatalogue>>(m,"DBEventProduct")
52 JAVA_LIKE_PROPERTY(CatalogueId, DBCatalogue)
54 // JAVA_LIKE_PROPERTY(CatalogueId, DBCatalogue)
53 JAVA_LIKE_PROPERTY(UniqId, DBCatalogue)
55 // JAVA_LIKE_PROPERTY(UniqId, DBCatalogue)
54 JAVA_LIKE_PROPERTY(Name, DBCatalogue)
56 // JAVA_LIKE_PROPERTY(Name, DBCatalogue)
55 JAVA_LIKE_PROPERTY(Author, DBCatalogue)
57 // JAVA_LIKE_PROPERTY(Author, DBCatalogue)
56 JAVA_LIKE_PROPERTY(Repository, DBCatalogue)
58 // JAVA_LIKE_PROPERTY(Repository, DBCatalogue)
57 JAVA_LIKE_PROPERTY(CreationDateTime, DBCatalogue)
59 // JAVA_LIKE_PROPERTY(CreationDateTime, DBCatalogue)
58 JAVA_LIKE_PROPERTY(ModificationDateTime, DBCatalogue)
60 // JAVA_LIKE_PROPERTY(ModificationDateTime, DBCatalogue)
59 ;
61 // ;
60
62
61 py::class_<CatalogueController>(m, "CatalogueController")
63 // py::class_<CatalogueController>(m, "CatalogueController")
62 .def("addDB", &CatalogueController::addDB)
64 // .def("addDB", &CatalogueController::addDB)
63 .def("saveDB", &CatalogueController::saveDB)
65 // .def("saveDB", &CatalogueController::saveDB)
64 .def("addEvent", &CatalogueController::addEvent)
66 // .def("addEvent", &CatalogueController::addEvent)
65 .def("updateEvent", &CatalogueController::updateEvent)
67 // .def("updateEvent", &CatalogueController::updateEvent)
66 .def("updateEventProduct", &CatalogueController::updateEventProduct)
68 // .def("updateEventProduct", &CatalogueController::updateEventProduct)
67 .def("removeEvent", &CatalogueController::removeEvent)
69 // .def("removeEvent", &CatalogueController::removeEvent)
68 .def("saveEvent", &CatalogueController::saveEvent)
70 // .def("saveEvent", &CatalogueController::saveEvent)
69 .def("discardEvent", &CatalogueController::discardEvent)
71 // .def("discardEvent", &CatalogueController::discardEvent)
70 .def("eventHasChanges", &CatalogueController::eventHasChanges)
72 // .def("eventHasChanges", &CatalogueController::eventHasChanges)
71 .def("addCatalogue", &CatalogueController::addCatalogue)
73 // .def("addCatalogue", &CatalogueController::addCatalogue)
72 .def("updateCatalogue", &CatalogueController::updateCatalogue)
74 // .def("updateCatalogue", &CatalogueController::updateCatalogue)
73 .def("removeCatalogue", &CatalogueController::removeCatalogue)
75 // .def("removeCatalogue", &CatalogueController::removeCatalogue)
74 .def("saveCatalogue", &CatalogueController::saveCatalogue)
76 // .def("saveCatalogue", &CatalogueController::saveCatalogue)
75 .def("discardCatalogue", &CatalogueController::discardCatalogue)
77 // .def("discardCatalogue", &CatalogueController::discardCatalogue)
76 .def("saveAll", &CatalogueController::saveAll)
78 // .def("saveAll", &CatalogueController::saveAll)
77 .def("hasChanges", &CatalogueController::hasChanges)
79 // .def("hasChanges", &CatalogueController::hasChanges)
78 ;
80 // ;
79 }
81 }
80
@@ -47,3 +47,5 declare_test(TestDownloader TestDownloader Network/TestDownloader.cpp "sciqlopco
47 declare_test(TestVariableController2 TestVariableController2 Variable/TestVariableController2.cpp "sciqlopcore;TestUtils;Qt5::Test")
47 declare_test(TestVariableController2 TestVariableController2 Variable/TestVariableController2.cpp "sciqlopcore;TestUtils;Qt5::Test")
48 declare_test(TestVariableController2Async TestVariableController2Async Variable/TestVariableController2Async.cpp "sciqlopcore;TestUtils;Qt5::Test")
48 declare_test(TestVariableController2Async TestVariableController2Async Variable/TestVariableController2Async.cpp "sciqlopcore;TestUtils;Qt5::Test")
49 declare_test(TestVariableController2WithSync TestVariableController2WithSync Variable/TestVariableController2WithSync.cpp "sciqlopcore;TestUtils;Qt5::Test")
49 declare_test(TestVariableController2WithSync TestVariableController2WithSync Variable/TestVariableController2WithSync.cpp "sciqlopcore;TestUtils;Qt5::Test")
50
51 declare_test(TestCatalogueController TestCatalogueController CatalogueController/TestCatalogueController.cpp "sciqlopcore;TestUtils;Qt5::Test")
General Comments 0
You need to be logged in to leave comments. Login now