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#include "ores.qt/export.hpp"
32
33namespace ores::qt {
34
48class ORES_QT_API ChangeReasonCache final : public QObject {
49 Q_OBJECT
50
51private:
52 inline static std::string_view logger_name = "ores.qt.change_reason_cache";
53
54 [[nodiscard]] static auto& lg() {
55 using namespace ores::logging;
56 static auto instance = make_logger(logger_name);
57 return instance;
58 }
59
60public:
61 explicit ChangeReasonCache(ClientManager* clientManager,
62 QObject* parent = nullptr);
63 ~ChangeReasonCache() override = default;
64
71 void loadAll();
72
76 bool isLoaded() const { return is_loaded_; }
77
81 bool isLoading() const { return is_loading_; }
82
86 const std::vector<dq::domain::change_reason>& allReasons() const {
87 return reasons_;
88 }
89
96 std::vector<dq::domain::change_reason> getReasonsForNew(
97 const std::string& category_code) const;
98
105 std::vector<dq::domain::change_reason> getReasonsForAmend(
106 const std::string& category_code) const;
107
114 std::vector<dq::domain::change_reason> getReasonsForDelete(
115 const std::string& category_code) const;
116
123 const dq::domain::change_reason* getReasonByCode(
124 const std::string& code) const;
125
132 bool isValidReasonCode(const std::string& code) const;
133
137 const std::vector<dq::domain::change_reason_category>& allCategories() const {
138 return categories_;
139 }
140
141signals:
145 void loaded();
146
150 void loadError(const QString& error_message);
151
155 void refreshed();
156
157private slots:
158 void onReasonsLoaded();
159 void onCategoriesLoaded();
160 void onNotificationReceived(const QString& eventType,
161 const QDateTime& timestamp, const QStringList& entityIds,
162 const QString& tenantId);
163
164private:
165 void loadReasons();
166 void loadCategories();
167 void subscribeToEvents();
168
169 struct ReasonsResult {
170 bool success;
171 std::vector<dq::domain::change_reason> reasons;
172 };
173
174 struct CategoriesResult {
175 bool success;
176 std::vector<dq::domain::change_reason_category> categories;
177 };
178
179 ClientManager* clientManager_;
180
181 // Cached data
182 std::vector<dq::domain::change_reason> reasons_;
183 std::vector<dq::domain::change_reason_category> categories_;
184
185 // Index for fast lookup by code
186 std::unordered_map<std::string, std::size_t> reason_index_;
187
188 // Loading state
189 bool is_loaded_{false};
190 bool is_loading_{false};
191 bool reasons_loaded_{false};
192 bool categories_loaded_{false};
193
194 QFutureWatcher<ReasonsResult>* reasons_watcher_;
195 QFutureWatcher<CategoriesResult>* categories_watcher_;
196};
197
198}
199
200#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AccountController.hpp:32
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:48
const std::vector< dq::domain::change_reason > & allReasons() const
Get all change reasons.
Definition ChangeReasonCache.hpp:86
void loaded()
Emitted when reasons and categories have been loaded.
void refreshed()
Emitted when the cache is refreshed due to server event.
bool isLoading() const
Check if loading is in progress.
Definition ChangeReasonCache.hpp:81
const std::vector< dq::domain::change_reason_category > & allCategories() const
Get all change reason categories.
Definition ChangeReasonCache.hpp:137
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:76
Manages the lifecycle of the NATS client and login state.
Definition ClientManager.hpp:123