@@ -1,131 +1,139 | |||||
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 <DBTag.h> |
|
11 | #include <DBTag.h> | |
12 | #include <IRequestPredicate.h> |
|
12 | #include <IRequestPredicate.h> | |
13 |
|
13 | |||
14 | #include <QMutex> |
|
14 | #include <QMutex> | |
15 | #include <QThread> |
|
15 | #include <QThread> | |
16 |
|
16 | |||
17 | #include <QDir> |
|
17 | #include <QDir> | |
18 | #include <QStandardPaths> |
|
18 | #include <QStandardPaths> | |
19 |
|
19 | |||
20 | Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController") |
|
20 | Q_LOGGING_CATEGORY(LOG_CatalogueController, "CatalogueController") | |
21 |
|
21 | |||
22 |
namespace |
|
22 | namespace { | |
23 |
|
23 | |||
24 | static QString REPOSITORY_WORK_SUFFIX = QString{"Work"}; |
|
24 | static QString REPOSITORY_WORK_SUFFIX = QString{"Work"}; | |
25 |
|
25 | |||
26 | } |
|
26 | } | |
27 |
|
27 | |||
28 | class CatalogueController::CatalogueControllerPrivate { |
|
28 | class CatalogueController::CatalogueControllerPrivate { | |
29 | public: |
|
29 | public: | |
30 | QMutex m_WorkingMutex; |
|
30 | QMutex m_WorkingMutex; | |
31 | CatalogueDao m_CatalogueDao; |
|
31 | CatalogueDao m_CatalogueDao; | |
32 |
|
32 | |||
33 | std::list<QString> m_RepositoryList; |
|
33 | std::list<QString> m_RepositoryList; | |
34 | }; |
|
34 | }; | |
35 |
|
35 | |||
36 | CatalogueController::CatalogueController(QObject *parent) |
|
36 | CatalogueController::CatalogueController(QObject *parent) | |
37 | : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>()} |
|
37 | : impl{spimpl::make_unique_impl<CatalogueControllerPrivate>()} | |
38 | { |
|
38 | { | |
39 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction") |
|
39 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController construction") | |
40 | << QThread::currentThread(); |
|
40 | << QThread::currentThread(); | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | CatalogueController::~CatalogueController() |
|
43 | CatalogueController::~CatalogueController() | |
44 | { |
|
44 | { | |
45 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction") |
|
45 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController destruction") | |
46 | << QThread::currentThread(); |
|
46 | << QThread::currentThread(); | |
47 | this->waitForFinish(); |
|
47 | this->waitForFinish(); | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | void CatalogueController::addDB(const QString &dbPath) |
|
50 | void CatalogueController::addDB(const QString &dbPath) | |
51 | { |
|
51 | { | |
52 | QDir dbDir(dbPath); |
|
52 | QDir dbDir(dbPath); | |
53 | if (dbDir.exists()) { |
|
53 | if (dbDir.exists()) { | |
54 | auto dirName = dbDir.dirName(); |
|
54 | auto dirName = dbDir.dirName(); | |
55 |
|
55 | |||
56 | if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName) |
|
56 | if (std::find(impl->m_RepositoryList.cbegin(), impl->m_RepositoryList.cend(), dirName) | |
57 | != impl->m_RepositoryList.cend()) { |
|
57 | != impl->m_RepositoryList.cend()) { | |
58 | qCCritical(LOG_CatalogueController()) |
|
58 | qCCritical(LOG_CatalogueController()) | |
59 | << tr("Impossible to addDB that is already loaded"); |
|
59 | << tr("Impossible to addDB that is already loaded"); | |
60 | } |
|
60 | } | |
61 |
|
61 | |||
62 | if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) { |
|
62 | if (!impl->m_CatalogueDao.addDB(dbPath, dirName)) { | |
63 | qCCritical(LOG_CatalogueController()) |
|
63 | qCCritical(LOG_CatalogueController()) | |
64 | << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath); |
|
64 | << tr("Impossible to addDB %1 from %2 ").arg(dirName, dbPath); | |
65 | } |
|
65 | } | |
66 | else |
|
66 | else { | |
67 | { |
|
67 | impl->m_RepositoryList.push_back(dirName); | |
68 | impl->m_RepositoryList << dirName; |
|
|||
69 | } |
|
68 | } | |
70 | } |
|
69 | } | |
71 | else { |
|
70 | else { | |
72 | qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ") |
|
71 | qCCritical(LOG_CatalogueController()) << tr("Impossible to addDB that not exists: ") | |
73 | << dbPath; |
|
72 | << dbPath; | |
74 | } |
|
73 | } | |
75 | } |
|
74 | } | |
76 |
|
75 | |||
77 | void CatalogueController::saveDB(const QString &destinationPath, const QString &repository) |
|
76 | void CatalogueController::saveDB(const QString &destinationPath, const QString &repository) | |
78 | { |
|
77 | { | |
79 | if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) { |
|
78 | if (!impl->m_CatalogueDao.saveDB(destinationPath, repository)) { | |
80 | qCCritical(LOG_CatalogueController()) |
|
79 | qCCritical(LOG_CatalogueController()) | |
81 | << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath); |
|
80 | << tr("Impossible to saveDB %1 from %2 ").arg(repository, destinationPath); | |
82 | } |
|
81 | } | |
83 | } |
|
82 | } | |
84 |
|
83 | |||
85 | std::list<std::shared_ptr<DBEvent> > |
|
84 | std::list<std::shared_ptr<DBEvent> > | |
86 | CatalogueController::retrieveEvents(const QString &repository) const |
|
85 | CatalogueController::retrieveEvents(const QString &repository) const | |
87 | { |
|
86 | { | |
88 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
87 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; | |
89 | auto events = impl->m_CatalogueDao.getEvents(repository); |
|
88 | auto events = impl->m_CatalogueDao.getEvents(repository); | |
90 | for (auto event : events) { |
|
89 | for (auto event : events) { | |
91 | eventsShared.push_back(std::make_shared<DBEvent>(event)); |
|
90 | eventsShared.push_back(std::make_shared<DBEvent>(event)); | |
92 | } |
|
91 | } | |
93 | return eventsShared; |
|
92 | return eventsShared; | |
94 | } |
|
93 | } | |
95 |
|
94 | |||
96 | std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const |
|
95 | std::list<std::shared_ptr<DBEvent> > CatalogueController::retrieveAllEvents() const | |
97 | { |
|
96 | { | |
98 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; |
|
97 | auto eventsShared = std::list<std::shared_ptr<DBEvent> >{}; | |
99 | for (auto repository : impl->m_RepositoryList) { |
|
98 | for (auto repository : impl->m_RepositoryList) { | |
100 | eventsShared.splice(eventsShared.end(), retrieveEvents(repository)); |
|
99 | eventsShared.splice(eventsShared.end(), retrieveEvents(repository)); | |
101 | } |
|
100 | } | |
102 |
|
101 | |||
103 | return eventsShared; |
|
102 | return eventsShared; | |
104 | } |
|
103 | } | |
105 |
|
104 | |||
106 | void CatalogueController::initialize() |
|
105 | void CatalogueController::initialize() | |
107 | { |
|
106 | { | |
108 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init") |
|
107 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init") | |
109 | << QThread::currentThread(); |
|
108 | << QThread::currentThread(); | |
110 | impl->m_WorkingMutex.lock(); |
|
109 | impl->m_WorkingMutex.lock(); | |
111 | impl->m_CatalogueDao.initialize(); |
|
110 | impl->m_CatalogueDao.initialize(); | |
112 |
auto defaultRepository |
|
111 | auto defaultRepositoryLocation | |
113 |
QStandardPaths:: |
|
112 | = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); | |
114 | QDir defaultRepositoryDir(defaultRepository); |
|
113 | ||
115 |
|
|
114 | QDir defaultRepositoryLocationDir; | |
116 | qCInfo(LOG_CatalogueController()) << tr("Persistant data loading from: ") |
|
115 | if (defaultRepositoryLocationDir.mkpath(defaultRepositoryLocation)) { | |
117 | << defaultRepository; |
|
116 | defaultRepositoryLocationDir.cd(defaultRepositoryLocation); | |
|
117 | auto defaultRepository = defaultRepositoryLocationDir.absoluteFilePath(REPOSITORY_DEFAULT); | |||
|
118 | qCInfo(LOG_CatalogueController()) | |||
|
119 | << tr("Persistant data loading from: ") << defaultRepository; | |||
118 | this->addDB(defaultRepository); |
|
120 | this->addDB(defaultRepository); | |
119 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END"); |
|
|||
120 | } |
|
121 | } | |
|
122 | else { | |||
|
123 | qCWarning(LOG_CatalogueController()) | |||
|
124 | << tr("Cannot load the persistent default repository from ") | |||
|
125 | << defaultRepositoryLocation; | |||
|
126 | } | |||
|
127 | ||||
|
128 | qCDebug(LOG_CatalogueController()) << tr("CatalogueController init END"); | |||
121 | } |
|
129 | } | |
122 |
|
130 | |||
123 | void CatalogueController::finalize() |
|
131 | void CatalogueController::finalize() | |
124 | { |
|
132 | { | |
125 | impl->m_WorkingMutex.unlock(); |
|
133 | impl->m_WorkingMutex.unlock(); | |
126 | } |
|
134 | } | |
127 |
|
135 | |||
128 | void CatalogueController::waitForFinish() |
|
136 | void CatalogueController::waitForFinish() | |
129 | { |
|
137 | { | |
130 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
138 | QMutexLocker locker{&impl->m_WorkingMutex}; | |
131 | } |
|
139 | } |
General Comments 0
You need to be logged in to leave comments.
Login now