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