Line data Source code
1 : // DUNE DAQ modification notice:
2 : // This file has been modified from the original ATLAS dbe source for the DUNE DAQ project.
3 : // Fork baseline commit: dbe-02-12-17 (2022-05-12).
4 : // Renamed since fork: no.
5 :
6 : /*
7 : * config_api_commands.cpp
8 : *
9 : * Created on: Apr 19, 2016
10 : * Author: Leonidas Georgopoulos
11 : */
12 :
13 : #include "dbe/Command.hpp"
14 : #include "dbe/Exceptions.hpp"
15 : #include "dbe/change_attribute.hpp"
16 : #include "dbe/change_class.hpp"
17 : #include "dbe/change_date.hpp"
18 : #include "dbe/change_enum.hpp"
19 : #include "dbe/change_time.hpp"
20 : #include "dbe/confaccessor.hpp"
21 : #include "dbe/config_api.hpp"
22 : #include "dbe/config_reference.hpp"
23 : #include "dbe/confobject_desc.hpp"
24 : #include "dbe/dbcontroller.hpp"
25 : #include "dbe/messenger.hpp"
26 :
27 : #include "conffwk/Schema.hpp"
28 : #include "ers/Issue.hpp"
29 :
30 : #include <QString>
31 : #include <QUuid>
32 :
33 : #include <stack>
34 : #include <string>
35 : #include <vector>
36 :
37 : //------------------------------------------------------------------------------------------
38 : // NAMESPACE DBE::CONFIG::API::COMMANDS
39 : //------------------------------------------------------------------------------------------
40 : /*
41 : * Purpose of this namespace is to group all commands that generate undo/redo objects
42 : * that acts on config related structures and objects .
43 : *
44 : * DBE user interface should access the database by appropriately calling methods in this
45 : * namespace. Direct access by accessing that database and retrieving ConfigObject directly
46 : * is strongly discouraged.
47 : */
48 : namespace dbe {
49 : namespace config {
50 : namespace api {
51 : namespace commands {
52 :
53 : //------------------------------------------------------------------------------------------
54 : void
55 0 : newobj(std::string const& fn,
56 : std::string const& cn,
57 : std::string const& name,
58 : dbe::t_config_object_preimage::type_attrmap const& attributes,
59 : dbe::t_config_object_preimage::type_relmap const& relations,
60 : QUuid const& src)
61 : {
62 0 : std::string description{ "Object created : " + name + "@" + cn };
63 :
64 0 : config_internal_change request{ config_internal_change::CREATED, description, name, cn, "" };
65 :
66 0 : confaccessor::get_commands()->push(
67 0 : new dbe::actions::object::create({ attributes, relations, { name, cn }, fn }, src));
68 :
69 0 : confaccessor::get_internal_change_stack()->push(request);
70 0 : }
71 : //------------------------------------------------------------------------------------------
72 :
73 : //------------------------------------------------------------------------------------------
74 : bool
75 0 : delobj(tref obj, QUuid const& src)
76 : {
77 0 : try {
78 0 : std::string const description{ "Object deleted : " + obj.UID() + "@" + obj.class_name() };
79 0 : config_internal_change request{ config_internal_change::DELETED, description, obj.UID(), obj.class_name(), "" };
80 0 : confaccessor::get_commands()->push(new dbe::actions::object::remove(obj, src));
81 0 : confaccessor::get_internal_change_stack()->push(request);
82 0 : } catch (daq::dbe::ObjectChangeWasNotSuccessful const& dbe_err) {
83 0 : INFO("Delete Object: The object could not be deleted!\n\n", dbe::config::errors::parse(dbe_err).c_str());
84 0 : return false;
85 0 : }
86 :
87 0 : return true;
88 : }
89 : //------------------------------------------------------------------------------------------
90 :
91 : //------------------------------------------------------------------------------------------
92 : bool
93 0 : renobj(tref obj, std::string const& newuuid, QUuid const& src)
94 : {
95 0 : try {
96 0 : std::string const description = { "Object renamed : " + obj.UID() + "@" + obj.class_name() + " to " + newuuid +
97 0 : "@" + obj.class_name() };
98 :
99 0 : config_internal_change request{ config_internal_change::RENAMED, description, obj.UID(), obj.class_name(), "" };
100 :
101 0 : confaccessor::get_commands()->push(new dbe::actions::object::rename(obj, newuuid, src));
102 :
103 0 : confaccessor::get_internal_change_stack()->push(request);
104 :
105 0 : return true;
106 0 : } catch (daq::dbe::ObjectChangeWasNotSuccessful const& dbe_err) {
107 0 : INFO("It was not possible to rename object ",
108 : dbe::config::errors::parse(dbe_err).c_str(),
109 : "\n\nObject UID:",
110 0 : obj.UID().c_str());
111 0 : return false;
112 0 : }
113 : }
114 : //------------------------------------------------------------------------------------------
115 :
116 : //------------------------------------------------------------------------------------------
117 : bool
118 0 : movobj(tref obj, std::string const& destination, QUuid const& src)
119 : {
120 0 : try {
121 0 : std::string const description{ "Object " + obj.UID() + "@" + obj.class_name() + " contained in " +
122 0 : obj.contained_in() + " moved to " + destination };
123 :
124 0 : config_internal_change request{ config_internal_change::MOVED, description, obj.UID(), obj.class_name(), "" };
125 :
126 0 : confaccessor::get_commands()->push(new dbe::actions::object::move(obj, destination, src));
127 0 : confaccessor::get_internal_change_stack()->push(request);
128 :
129 0 : return true;
130 0 : } catch (daq::dbe::ObjectChangeWasNotSuccessful const& dbe_err) {
131 0 : INFO("It was not possible to move object ",
132 : dbe::config::errors::parse(dbe_err).c_str(),
133 : "\n\nObject UID:",
134 0 : obj.UID().c_str());
135 0 : return false;
136 0 : }
137 : }
138 : //------------------------------------------------------------------------------------------
139 :
140 : //------------------------------------------------------------------------------------------
141 : void
142 0 : modobj(tref object, const dunedaq::conffwk::relationship_t& linkinfo, std::vector<std::string> const& others_names)
143 : {
144 0 : try {
145 0 : confaccessor::get_commands()->push(new dbe::actions::object::changerefs(object, linkinfo, others_names));
146 0 : std::string const description(others_names.size() != 1 ? "Multi Relationship : " + linkinfo.p_name
147 0 : : "Single Relationship : " + linkinfo.p_name);
148 :
149 0 : config_internal_change const change{
150 0 : config_internal_change::MODIFIED, description, object.UID(), object.class_name(), ""
151 0 : };
152 :
153 0 : confaccessor::get_internal_change_stack()->push(change);
154 0 : } catch (daq::dbe::ObjectChangeWasNotSuccessful const& dbe_err) {
155 0 : WARN("The object reference could not be changed", dbe::config::errors::parse(dbe_err).c_str());
156 0 : }
157 0 : }
158 : //------------------------------------------------------------------------------------------
159 :
160 : //------------------------------------------------------------------------------------------
161 : template<>
162 : void
163 0 : modobj<std::string>(tref Object, const dunedaq::conffwk::attribute_t& AttributeData, std::string Value)
164 : {
165 0 : try {
166 0 : config_internal_change Change;
167 0 : Change.uid = Object.UID();
168 0 : Change.classname = Object.class_name();
169 0 : Change.request = config_internal_change::MODIFIED;
170 0 : Change.description = QString("Attribute ").append(AttributeData.p_name.c_str()).toStdString();
171 :
172 0 : if (AttributeData.p_type == dunedaq::conffwk::string_type) {
173 0 : confaccessor::get_commands()->push(new dbe::actions::ChangeAttribute<std::string>(Object, AttributeData, Value));
174 : } else if (AttributeData.p_type == dunedaq::conffwk::enum_type) {
175 0 : confaccessor::get_commands()->push(new dbe::actions::ChangeEnum<std::string>(Object, AttributeData, Value));
176 : } else if (AttributeData.p_type == dunedaq::conffwk::class_type) {
177 0 : confaccessor::get_commands()->push(new dbe::actions::ChangeClass<std::string>(Object, AttributeData, Value));
178 : } else if (AttributeData.p_type == dunedaq::conffwk::date_type) {
179 :
180 0 : confaccessor::get_commands()->push(new dbe::actions::ChangeDate<std::string>(Object, AttributeData, Value));
181 : } else if (AttributeData.p_type == dunedaq::conffwk::time_type) {
182 0 : confaccessor::get_commands()->push(new dbe::actions::ChangeTime<std::string>(Object, AttributeData, Value));
183 : }
184 :
185 0 : confaccessor::get_internal_change_stack()->push(Change);
186 0 : } catch (daq::dbe::ObjectChangeWasNotSuccessful const& dbe_err) {
187 0 : WARN("The object attribute could not be changed", dbe::config::errors::parse(dbe_err).c_str());
188 0 : }
189 0 : }
190 : //------------------------------------------------------------------------------------------
191 :
192 : //------------------------------------------------------------------------------------------
193 : template<>
194 : void
195 0 : modobj<std::vector<std::string>>(tref Object,
196 : const dunedaq::conffwk::attribute_t& AttributeData,
197 : std::vector<std::string> Value)
198 : {
199 0 : try {
200 0 : config_internal_change Change;
201 0 : Change.uid = Object.UID();
202 0 : Change.classname = Object.class_name();
203 0 : Change.request = config_internal_change::MODIFIED;
204 0 : Change.description = QString("Attribute ").append(AttributeData.p_name.c_str()).toStdString();
205 :
206 0 : if (AttributeData.p_type == dunedaq::conffwk::string_type) {
207 0 : confaccessor::get_commands()->push(
208 0 : new dbe::actions::ChangeAttribute<std::vector<std::string>>(Object, AttributeData, Value));
209 : } else if (AttributeData.p_type == dunedaq::conffwk::enum_type) {
210 0 : confaccessor::get_commands()->push(
211 0 : new dbe::actions::ChangeEnum<std::vector<std::string>>(Object, AttributeData, Value));
212 : } else if (AttributeData.p_type == dunedaq::conffwk::class_type) {
213 0 : confaccessor::get_commands()->push(
214 0 : new dbe::actions::ChangeClass<std::vector<std::string>>(Object, AttributeData, Value));
215 : } else if (AttributeData.p_type == dunedaq::conffwk::date_type) {
216 0 : confaccessor::get_commands()->push(
217 0 : new dbe::actions::ChangeDate<std::vector<std::string>>(Object, AttributeData, Value));
218 : } else if (AttributeData.p_type == dunedaq::conffwk::time_type) {
219 0 : confaccessor::get_commands()->push(
220 0 : new dbe::actions::ChangeTime<std::vector<std::string>>(Object, AttributeData, Value));
221 : }
222 :
223 0 : confaccessor::get_internal_change_stack()->push(Change);
224 0 : } catch (daq::dbe::ObjectChangeWasNotSuccessful const& dbe_err) {
225 0 : WARN("The object attribute could not be changed", dbe::config::errors::parse(dbe_err).c_str());
226 0 : }
227 0 : }
228 : //------------------------------------------------------------------------------------------
229 :
230 : //------------------------------------------------------------------------------------------
231 :
232 : //------------------------------------------------------------------------------------------
233 : namespace file {
234 : void
235 0 : remove(QString const& db, QString const& fn)
236 : {
237 0 : std::string dbname = db.toStdString();
238 0 : std::string sinclude = fn.toStdString();
239 :
240 0 : try {
241 0 : config_internal_change Change;
242 0 : Change.filename = fn.toStdString();
243 0 : Change.request = config_internal_change::FILE_DELETED;
244 0 : Change.description = QString("File(deleted) : ").append(fn).toStdString();
245 :
246 0 : confaccessor::get_commands()->push(new dbe::actions::file::remove(dbname, sinclude));
247 0 : confaccessor::get_internal_change_stack()->push(Change);
248 0 : } catch (daq::dbe::DatabaseChangeNotSuccessful const& dbe_err) {
249 0 : WARN("File removal failure", dbe::config::errors::parse(dbe_err).c_str());
250 0 : }
251 0 : }
252 :
253 : void
254 0 : add(const QString& Database, const QString& IncludedFile)
255 : {
256 0 : std::string dbname = Database.toStdString();
257 0 : std::string sinclude = IncludedFile.toStdString();
258 :
259 0 : try {
260 0 : config_internal_change Change;
261 0 : Change.filename = IncludedFile.toStdString();
262 0 : Change.request = config_internal_change::FILE_INCLUDED;
263 0 : Change.description = QString("File(included) : ").append(IncludedFile).toStdString();
264 :
265 0 : confaccessor::get_commands()->push(new dbe::actions::file::add(dbname, sinclude));
266 0 : confaccessor::get_internal_change_stack()->push(Change);
267 0 : } catch (daq::dbe::DatabaseChangeNotSuccessful const& dbe_err) {
268 0 : WARN("File inclusion did not succeed", dbe::config::errors::parse(dbe_err).c_str());
269 0 : }
270 0 : }
271 :
272 : }
273 :
274 : }
275 : }
276 : }
277 : }
278 : //------------------------------------------------------------------------------------------
|