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