ORE Studio 0.0.4
Loading...
Searching...
No Matches
ChangeReasonCache.hpp
1/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * Copyright (C) 2025 Marco Craveiro <marco.craveiro@gmail.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 3 of the License, or (at your option) any later
8 * version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 *
19 */
20#ifndef ORES_QT_CHANGE_REASON_CACHE_HPP
21#define ORES_QT_CHANGE_REASON_CACHE_HPP
22
23#include <vector>
24#include <unordered_map>
25#include <QObject>
26#include <QFutureWatcher>
27#include "ores.qt/ClientManager.hpp"
28#include "ores.logging/make_logger.hpp"
29#include "ores.dq.api/domain/change_reason.hpp"
30#include "ores.dq.api/domain/change_reason_category.hpp"
31
32namespace ores::qt {
33
47class ChangeReasonCache final : public QObject {
48 Q_OBJECT
49
50private:
51 inline static std::string_view logger_name = "ores.qt.change_reason_cache";
52
53 [[nodiscard]] static auto& lg() {
54 using namespace ores::logging;
55 static auto instance = make_logger(logger_name);
56 return instance;
57 }
58
59public:
60 explicit ChangeReasonCache(ClientManager* clientManager,
61 QObject* parent = nullptr);
62 ~ChangeReasonCache() override = default;
63
70 void loadAll();
71
75 bool isLoaded() const { return is_loaded_; }
76
80 bool isLoading() const { return is_loading_; }
81
85 const std::vector<dq::domain::change_reason>& allReasons() const {
86 return reasons_;
87 }
88
95 std::vector<dq::domain::change_reason> getReasonsForNew(
96 const std::string& category_code) const;
97
104 std::vector<dq::domain::change_reason> getReasonsForAmend(
105 const std::string& category_code) const;
106
113 std::vector<dq::domain::change_reason> getReasonsForDelete(
114 const std::string& category_code) const;
115
123 const std::string& code) const;
124
131 bool isValidReasonCode(const std::string& code) const;
132
136 const std::vector<dq::domain::change_reason_category>& allCategories() const {
137 return categories_;
138 }
139
140signals:
144 void loaded();
145
149 void loadError(const QString& error_message);
150
154 void refreshed();
155
156private slots:
157 void onReasonsLoaded();
158 void onCategoriesLoaded();
159 void onNotificationReceived(const QString& eventType,
160 const QDateTime& timestamp, const QStringList& entityIds,
161 const QString& tenantId);
162
163private:
164 void loadReasons();
165 void loadCategories();
166 void subscribeToEvents();
167
168 struct ReasonsResult {
169 bool success;
170 std::vector<dq::domain::change_reason> reasons;
171 };
172
173 struct CategoriesResult {
174 bool success;
175 std::vector<dq::domain::change_reason_category> categories;
176 };
177
178 ClientManager* clientManager_;
179
180 // Cached data
181 std::vector<dq::domain::change_reason> reasons_;
182 std::vector<dq::domain::change_reason_category> categories_;
183
184 // Index for fast lookup by code
185 std::unordered_map<std::string, std::size_t> reason_index_;
186
187 // Loading state
188 bool is_loaded_{false};
189 bool is_loading_{false};
190 bool reasons_loaded_{false};
191 bool categories_loaded_{false};
192
193 QFutureWatcher<ReasonsResult>* reasons_watcher_;
194 QFutureWatcher<CategoriesResult>* categories_watcher_;
195};
196
197}
198
199#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:29
Defines a specific reason for record changes.
Definition change_reason.hpp:48
Shared cache for change reasons used across all entity dialogs.
Definition ChangeReasonCache.hpp:47
const std::vector< dq::domain::change_reason > & allReasons() const
Get all change reasons.
Definition ChangeReasonCache.hpp:85
void loaded()
Emitted when reasons and categories have been loaded.
const dq::domain::change_reason * getReasonByCode(const std::string &code) const
Get a reason by its code.
Definition ChangeReasonCache.cpp:274
std::vector< dq::domain::change_reason > getReasonsForAmend(const std::string &category_code) const
Get change reasons filtered for amend operations.
Definition ChangeReasonCache.cpp:236
void refreshed()
Emitted when the cache is refreshed due to server event.
std::vector< dq::domain::change_reason > getReasonsForNew(const std::string &category_code) const
Get change reasons filtered for new record creation.
Definition ChangeReasonCache.cpp:218
bool isValidReasonCode(const std::string &code) const
Check if a reason code is valid.
Definition ChangeReasonCache.cpp:284
bool isLoading() const
Check if loading is in progress.
Definition ChangeReasonCache.hpp:80
const std::vector< dq::domain::change_reason_category > & allCategories() const
Get all change reason categories.
Definition ChangeReasonCache.hpp:136
std::vector< dq::domain::change_reason > getReasonsForDelete(const std::string &category_code) const
Get change reasons filtered for delete operations.
Definition ChangeReasonCache.cpp:255
void loadAll()
Load all change reasons and categories from the server.
Definition ChangeReasonCache.cpp:82
void loadError(const QString &error_message)
Emitted when an error occurs during loading.
bool isLoaded() const
Check if reasons have been loaded.
Definition ChangeReasonCache.hpp:75
Manages the lifecycle of the NATS client and login state.
Definition ClientManager.hpp:109