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 : #include "dbe/Command.hpp"
7 : #include "dbe/config_api_graph.hpp" // checked
8 : #include "dbe/config_api_set.hpp" // checked
9 : #include "dbe/config_reference_copy.hpp"
10 : #include "dbe/confobject_desc.hpp" // checked
11 : #include "dbe/confobject_extra.hpp"
12 : #include "dbe/dbcontroller.hpp"
13 : #include "dbe/messenger.hpp"
14 :
15 : #include "conffwk/Configuration.hpp"
16 :
17 : #include <QFileInfo>
18 :
19 : #include <algorithm>
20 :
21 : //------------------------------------------------------------------------------------------
22 0 : dbe::actions::object::changerefs::changerefs(tref object,
23 : dunedaq::conffwk::relationship_t const& relation,
24 : std::vector<std::string> const& object_names_tolink,
25 : QUuid const& src,
26 0 : QUndoCommand* parent)
27 : : onobject(object, parent, src)
28 0 : , this_relation(relation)
29 0 : , this_current_neighbors(dbe::config::api::graph::linked::through::relation<std::vector<tref>>(object, relation))
30 : {
31 0 : try {
32 0 : for (std::string const& oname : object_names_tolink) {
33 0 : try {
34 0 : this_target_neighbors.push_back(inner::dbcontroller::get({ oname, relation.p_type }));
35 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& ex) {
36 : // Actually there is no need to handle this error here ,
37 : // since the object will not be added to the result list of references,
38 : // and can be safely ignored
39 0 : }
40 : }
41 0 : } catch (dunedaq::conffwk::Exception const& e) {
42 0 : failed();
43 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
44 0 : }
45 :
46 0 : setText(
47 0 : QObject::tr("Relation %1 of object %2 was updated.").arg(this_relation.p_name.c_str()).arg(object.UID().c_str()));
48 0 : }
49 : //------------------------------------------------------------------------------------------
50 :
51 : //------------------------------------------------------------------------------------------
52 : void
53 0 : dbe::actions::object::changerefs::redo()
54 : {
55 0 : try {
56 0 : if (redoable()) {
57 0 : failed();
58 0 : dbe::config::api::set::noactions::relation(checkedref(), this_relation, this_target_neighbors);
59 0 : confaccessor::ref().force_emit_object_changed(source().toString(), checkedref());
60 0 : toggle();
61 : }
62 0 : } catch (dunedaq::conffwk::Exception const& e) {
63 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, config::errors::parse(e));
64 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& ex) {
65 0 : FAIL("Operation did not complete because a lookup in the underlying database failed", ex.what());
66 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, ex);
67 0 : }
68 0 : }
69 : //------------------------------------------------------------------------------------------
70 :
71 : //------------------------------------------------------------------------------------------
72 : void
73 0 : dbe::actions::object::changerefs::undo()
74 : {
75 0 : try {
76 0 : if (undoable()) {
77 0 : failed();
78 0 : dbe::config::api::set::noactions::relation(checkedref(), this_relation, this_current_neighbors);
79 0 : confaccessor::ref().force_emit_object_changed(source().toString(), checkedref());
80 0 : toggle();
81 : }
82 0 : } catch (dunedaq::conffwk::Exception const& e) {
83 0 : failed();
84 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
85 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& ex) {
86 0 : FAIL("Operation did not complete because a lookup in the underlying database failed", ex.what());
87 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, ex);
88 0 : }
89 0 : }
90 : //------------------------------------------------------------------------------------------
91 :
92 : //------------------------------------------------------------------------------------------
93 0 : dbe::actions::file::add::add(std::string const& db_file,
94 : std::string const& include_file,
95 : QUuid const& src,
96 0 : QUndoCommand* parent)
97 : : state(parent, src)
98 0 : , m_db_file(db_file)
99 0 : , m_include_file(include_file)
100 : {
101 0 : setText(QObject::tr("Add Include File %1 to %2")
102 0 : .arg(QFileInfo(QString(m_include_file.c_str())).fileName())
103 0 : .arg(QFileInfo(QString(m_db_file.c_str())).fileName()));
104 0 : }
105 : //------------------------------------------------------------------------------------------
106 :
107 : //------------------------------------------------------------------------------------------
108 : void
109 0 : dbe::actions::file::add::undo()
110 : {
111 0 : try {
112 0 : if (undoable()) {
113 0 : confaccessor::ref().removefile(m_db_file, m_include_file);
114 : }
115 0 : } catch (dunedaq::conffwk::Exception const& e) {
116 0 : failed();
117 0 : throw daq::dbe::DatabaseChangeNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
118 0 : }
119 0 : }
120 : //------------------------------------------------------------------------------------------
121 :
122 : //------------------------------------------------------------------------------------------
123 : void
124 0 : dbe::actions::file::add::redo()
125 : {
126 0 : try {
127 0 : if (redoable()) {
128 0 : failed();
129 0 : confaccessor::ref().addfile(m_db_file, m_include_file);
130 0 : toggle();
131 : }
132 0 : } catch (dunedaq::conffwk::Exception const& e) {
133 0 : throw daq::dbe::DatabaseChangeNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
134 0 : }
135 0 : }
136 : //------------------------------------------------------------------------------------------
137 :
138 : //------------------------------------------------------------------------------------------
139 0 : dbe::actions::file::remove::remove(std::string& db_file,
140 : std::string& include_file,
141 : QUuid const& src,
142 0 : QUndoCommand* parent)
143 : : state(parent, src)
144 0 : , m_db_file(db_file)
145 0 : , m_include_file(include_file)
146 : {
147 0 : setText(QObject::tr("Remove Include File %1 from %2")
148 0 : .arg(QFileInfo(QString(m_include_file.c_str())).fileName())
149 0 : .arg(QFileInfo(QString(m_db_file.c_str())).fileName()));
150 0 : }
151 :
152 : void
153 0 : dbe::actions::file::remove::undo()
154 : {
155 0 : try {
156 0 : if (undoable()) {
157 0 : failed();
158 0 : confaccessor::ref().addfile(m_db_file, m_include_file);
159 0 : toggle();
160 : }
161 0 : } catch (dunedaq::conffwk::Exception const& e) {
162 0 : throw daq::dbe::DatabaseChangeNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
163 0 : }
164 0 : }
165 :
166 : void
167 0 : dbe::actions::file::remove::redo()
168 : {
169 0 : try {
170 0 : if (redoable()) {
171 0 : failed();
172 0 : confaccessor::ref().removefile(m_db_file, m_include_file);
173 0 : toggle();
174 : }
175 0 : } catch (dunedaq::conffwk::Exception const& e) {
176 0 : throw daq::dbe::DatabaseChangeNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
177 0 : }
178 0 : }
179 : //------------------------------------------------------------------------------------------
180 :
181 : //------------------------------------------------------------------------------------------
182 0 : dbe::actions::object::create::create(dbe::t_config_object_preimage const& img, QUuid const& src, QUndoCommand* parent)
183 : : state(parent, src)
184 0 : , this_object_key(img)
185 : {
186 0 : setText(QObject::tr("Creation of : Object %2@%1 ")
187 0 : .arg(this_object_key.ref.this_class.c_str())
188 0 : .arg(this_object_key.ref.this_name.c_str()));
189 0 : }
190 :
191 : void
192 0 : dbe::actions::object::create::undo()
193 : {
194 0 : try {
195 0 : if (undoable()) {
196 0 : failed();
197 : // notify for the objects that have been actually removed
198 0 : inner::configobject::gref<config_object_aggregator>::config_action_notifiable deletion = [&](dref const obj) {
199 0 : confaccessor::ref().force_emit_object_deleted(source().toString(), obj);
200 0 : };
201 :
202 0 : inner::dbcontroller::delete_object_request<config_object_aggregator>(
203 0 : inner::dbcontroller::get(this_object_key.ref), deletion);
204 0 : toggle();
205 0 : }
206 0 : } catch (dunedaq::conffwk::Exception const& e) {
207 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
208 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& e) {
209 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, e);
210 0 : }
211 0 : }
212 :
213 : void
214 0 : dbe::actions::object::create::redo()
215 : {
216 0 : try {
217 0 : if (redoable()) {
218 0 : failed();
219 0 : inner::dbcontroller::create_object_request(this_object_key);
220 0 : confaccessor::ref().force_emit_object_created(source().toString(), inner::dbcontroller::get(this_object_key.ref));
221 0 : toggle();
222 : }
223 0 : } catch (dunedaq::conffwk::Exception const& e) {
224 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
225 0 : }
226 0 : }
227 : //------------------------------------------------------------------------------------------
228 :
229 : //------------------------------------------------------------------------------------------
230 0 : dbe::actions::object::remove::remove(tref anobject, QUuid const& uuid, QUndoCommand* parent)
231 0 : : onobject(anobject, parent, uuid)
232 : {
233 0 : setText(QObject::tr("Delete object of class %1 with ID %2")
234 0 : .arg((this->checkedref().class_name()).c_str())
235 0 : .arg((this->checkedref().UID()).c_str()));
236 0 : }
237 :
238 0 : dbe::actions::object::remove::~remove() {}
239 :
240 : void
241 0 : dbe::actions::object::remove::undo()
242 : {
243 0 : try {
244 0 : if (undoable()) {
245 0 : failed();
246 0 : gref::config_action_notifiable creation = [&](dref obj) {
247 0 : confaccessor::ref().force_emit_object_created(source().toString(), obj);
248 0 : };
249 :
250 0 : inner::dbcontroller::create_object_request(this_remainder, creation);
251 0 : toggle();
252 0 : }
253 0 : } catch (dunedaq::conffwk::Exception const& e) {
254 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
255 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& ex) {
256 0 : FAIL("Operation did not complete because a lookup in the underlying database failed", ex.what());
257 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, ex);
258 0 : }
259 0 : }
260 :
261 : void
262 0 : dbe::actions::object::remove::redo()
263 : {
264 0 : try {
265 0 : if (redoable() and not checkedref().is_null()) {
266 0 : failed();
267 : // notify for the objects that have been actually removed
268 0 : gref::config_action_notifiable deletion = [&](dref const obj) {
269 0 : confaccessor::ref().force_emit_object_deleted(source().toString(), obj);
270 0 : };
271 :
272 0 : this_remainder =
273 0 : inner::dbcontroller::delete_object_request<decltype(this_remainder)::t_extractor>(checkedref(), deletion);
274 0 : toggle();
275 0 : }
276 0 : } catch (dunedaq::conffwk::Exception const& e) {
277 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
278 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& ex) {
279 0 : FAIL("Operation did not complete because a lookup in the underlying database failed", ex.what());
280 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, ex);
281 0 : }
282 0 : }
283 :
284 : //------------------------------------------------------------------------------------------
285 0 : dbe::actions::object::rename::rename(tref object, std::string const& Id, QUuid const& src, QUndoCommand* parent)
286 : : onobject(object, parent, src)
287 0 : , oldname(this->checkedref().UID())
288 0 : , newname(Id)
289 : {
290 0 : setText(QObject::tr("Object changed ID from %1 to %2").arg(this->checkedref().UID().c_str()).arg(newname.c_str()));
291 0 : }
292 : //------------------------------------------------------------------------------------------
293 :
294 : //------------------------------------------------------------------------------------------
295 : void
296 0 : dbe::actions::object::rename::redo()
297 : {
298 0 : try {
299 0 : if (redoable()) {
300 0 : failed();
301 0 : dref object_before_change(checkedref());
302 0 : dbe::inner::dbcontroller::rename_object_request(checkedref(), newname);
303 :
304 0 : confaccessor::ref().force_emit_object_renamed(source().toString(), object_before_change);
305 :
306 0 : std::vector<tref> tonotify{ this->checkedref().referenced_by("*", false) };
307 :
308 0 : std::for_each(tonotify.begin(), tonotify.end(), [&](tref const& x) {
309 0 : confaccessor::ref().force_emit_object_changed(source().toString(), x);
310 0 : });
311 0 : toggle();
312 0 : }
313 0 : } catch (dunedaq::conffwk::Exception const& e) {
314 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
315 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& e) {
316 0 : FAIL("Operation did not complete because a lookup in the underlying database failed", e.what());
317 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, e);
318 0 : }
319 0 : }
320 : //------------------------------------------------------------------------------------------
321 :
322 : //------------------------------------------------------------------------------------------
323 : void
324 0 : dbe::actions::object::rename::undo()
325 : {
326 0 : try {
327 0 : if (undoable()) {
328 0 : failed();
329 0 : dref object_before_rename(checkedref());
330 0 : dbe::inner::dbcontroller::rename_object_request(checkedref(), oldname);
331 0 : confaccessor::ref().force_emit_object_renamed(source().toString(), object_before_rename);
332 :
333 0 : std::vector<tref> tonotify = checkedref().referenced_by("*", false);
334 :
335 0 : std::for_each(tonotify.begin(), tonotify.end(), [&](tref const& x) {
336 0 : confaccessor::ref().force_emit_object_changed(source().toString(), x);
337 0 : });
338 0 : toggle();
339 0 : }
340 0 : } catch (dunedaq::conffwk::Exception const& e) {
341 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
342 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& e) {
343 0 : FAIL("Operation did not complete because a lookup in the underlying database failed", e.what());
344 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, e);
345 0 : }
346 0 : }
347 : //------------------------------------------------------------------------------------------
348 :
349 : //------------------------------------------------------------------------------------------
350 0 : dbe::actions::object::move::move(tref object, std::string const& filename, QUuid const& src, QUndoCommand* parent)
351 : : onobject(object, parent, src)
352 0 : , source_file(this->checkedref().contained_in())
353 0 : , destination_file(filename)
354 : {
355 0 : setText(QObject::tr("Object changed from file %1 to %2").arg(source_file.c_str()).arg(destination_file.c_str()));
356 0 : }
357 : //------------------------------------------------------------------------------------------
358 :
359 : //------------------------------------------------------------------------------------------
360 0 : dbe::actions::object::move::~move() {}
361 : //------------------------------------------------------------------------------------------
362 :
363 : //------------------------------------------------------------------------------------------
364 : void
365 0 : dbe::actions::object::move::redo()
366 : {
367 0 : try {
368 0 : if (redoable()) {
369 0 : failed();
370 0 : inner::dbcontroller::move_object_request(this->checkedref(), destination_file);
371 0 : emit confaccessor::ref().force_emit_object_changed(source().toString(), this -> checkedref());
372 0 : toggle();
373 : }
374 0 : } catch (dunedaq::conffwk::Exception const& e) {
375 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
376 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& e) {
377 0 : FAIL("Operation did not complete because a lookup in the underlying database failed", e.what());
378 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, e);
379 0 : }
380 0 : }
381 : //------------------------------------------------------------------------------------------
382 :
383 : //------------------------------------------------------------------------------------------
384 : void
385 0 : dbe::actions::object::move::undo()
386 : {
387 0 : try {
388 0 : if (undoable()) {
389 0 : failed();
390 0 : inner::dbcontroller::move_object_request(this->checkedref(), source_file);
391 0 : toggle();
392 : }
393 0 : } catch (dunedaq::conffwk::Exception const& e) {
394 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, dbe::config::errors::parse(e));
395 0 : } catch (daq::dbe::config_object_retrieval_result_is_null const& e) {
396 0 : FAIL("Operation did not complete because a lookup in the underlying database failed", e.what());
397 0 : throw daq::dbe::ObjectChangeWasNotSuccessful(ERS_HERE, e);
398 0 : }
399 0 : }
400 : //------------------------------------------------------------------------------------------
401 :
402 : //------------------------------------------------------------------------------------------
403 0 : dbe::actions::onobject::onobject(tref obj, QUndoCommand* parent, QUuid const& uuid)
404 : : dbe::actions::state(parent, uuid)
405 0 : , this_object(obj)
406 : {
407 0 : }
408 : //------------------------------------------------------------------------------------------
409 :
410 : //------------------------------------------------------------------------------------------
411 : dbe::tref
412 0 : dbe::actions::onobject::checkedref() const
413 : {
414 0 : return this_object.ref();
415 : }
416 : //------------------------------------------------------------------------------------------
417 :
418 : //------------------------------------------------------------------------------------------
419 0 : dbe::actions::state::state(QUndoCommand* parent, QUuid const& uuid)
420 : : QUndoCommand(parent)
421 0 : , uuid(uuid)
422 0 : , rewind(true)
423 0 : , forward(true)
424 0 : , invalid(false)
425 : {
426 0 : }
427 :
428 : void
429 0 : dbe::actions::state::toggle()
430 : {
431 0 : invalid = not invalid;
432 0 : rewind = not rewind;
433 0 : forward = not forward;
434 0 : }
435 :
436 : void
437 0 : dbe::actions::state::failed() const
438 : {
439 0 : invalid = true;
440 0 : rewind = false;
441 0 : forward = false;
442 0 : }
443 :
444 : bool
445 0 : dbe::actions::state::isvalid() const
446 : {
447 0 : return not invalid;
448 : }
449 :
450 : void
451 0 : dbe::actions::state::setundoable(bool state) const
452 : {
453 0 : rewind = state;
454 0 : }
455 :
456 : void
457 0 : dbe::actions::state::setredoable(bool state) const
458 : {
459 0 : forward = state;
460 0 : }
461 :
462 : bool
463 0 : dbe::actions::state::undoable() const
464 : {
465 0 : return rewind and not invalid;
466 : }
467 :
468 : bool
469 0 : dbe::actions::state::redoable() const
470 : {
471 0 : return forward and not invalid;
472 : }
473 :
474 : QUuid const&
475 0 : dbe::actions::state::source() const
476 : {
477 0 : return uuid;
478 : }
479 :
480 : bool
481 0 : dbe::actions::state::canRedo() const
482 : {
483 0 : return redoable();
484 : }
485 :
486 : bool
487 0 : dbe::actions::state::canUndo() const
488 : {
489 0 : return undoable();
490 : }
491 :
492 : void
493 0 : dbe::actions::state::reload() const
494 : {
495 : // TODO evaluate: is this command that reloads the configuration object from the database really needed?
496 0 : }
497 :
498 : //------------------------------------------------------------------------------------------
|