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/domain/change_reason.hpp"
30#include "ores.dq/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> getReasonsForAmend(
96 const std::string& category_code) const;
97
104 std::vector<dq::domain::change_reason> getReasonsForDelete(
105 const std::string& category_code) const;
106
114 const std::string& code) const;
115
122 bool isValidReasonCode(const std::string& code) const;
123
127 const std::vector<dq::domain::change_reason_category>& allCategories() const {
128 return categories_;
129 }
130
131signals:
135 void loaded();
136
140 void loadError(const QString& error_message);
141
145 void refreshed();
146
147private slots:
148 void onReasonsLoaded();
149 void onCategoriesLoaded();
150 void onNotificationReceived(const QString& eventType,
151 const QDateTime& timestamp, const QStringList& entityIds);
152
153private:
154 void loadReasons();
155 void loadCategories();
156 void subscribeToEvents();
157
158 struct ReasonsResult {
159 bool success;
160 std::vector<dq::domain::change_reason> reasons;
161 };
162
163 struct CategoriesResult {
164 bool success;
165 std::vector<dq::domain::change_reason_category> categories;
166 };
167
168 ClientManager* clientManager_;
169
170 // Cached data
171 std::vector<dq::domain::change_reason> reasons_;
172 std::vector<dq::domain::change_reason_category> categories_;
173
174 // Index for fast lookup by code
175 std::unordered_map<std::string, std::size_t> reason_index_;
176
177 // Loading state
178 bool is_loaded_{false};
179 bool is_loading_{false};
180 bool reasons_loaded_{false};
181 bool categories_loaded_{false};
182
183 QFutureWatcher<ReasonsResult>* reasons_watcher_;
184 QFutureWatcher<CategoriesResult>* categories_watcher_;
185};
186
187}
188
189#endif
Implements logging infrastructure for ORE Studio.
Definition boost_severity.hpp:28
Qt-based graphical user interface for ORE Studio.
Definition AboutDialog.hpp:35
Defines a specific reason for record changes.
Definition change_reason.hpp:47
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:325
std::vector< dq::domain::change_reason > getReasonsForAmend(const std::string &category_code) const
Get change reasons filtered for amend operations.
Definition ChangeReasonCache.cpp:287
void refreshed()
Emitted when the cache is refreshed due to server event.
bool isValidReasonCode(const std::string &code) const
Check if a reason code is valid.
Definition ChangeReasonCache.cpp:335
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:127
std::vector< dq::domain::change_reason > getReasonsForDelete(const std::string &category_code) const
Get change reasons filtered for delete operations.
Definition ChangeReasonCache.cpp:306
void loadAll()
Load all change reasons and categories from the server.
Definition ChangeReasonCache.cpp:86
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 network client and IO context.
Definition ClientManager.hpp:90