Skip to main content

text_document_common/direct_access/use_cases/
set_relationship.rs

1// Generated by Qleany v1.4.8 from common_da_use_cases_set_relationship.tera
2
3use super::traits::WriteRelUoWFactory;
4use crate::types::{EntityId, Savepoint};
5use crate::undo_redo::UndoRedoCommand;
6use anyhow::Result;
7use std::any::Any;
8use std::collections::VecDeque;
9
10// ---------------------------------------------------------------------------
11// Non-undoable set-relationship
12// ---------------------------------------------------------------------------
13
14pub struct SetRelationshipUseCase<RF, F: WriteRelUoWFactory<RF>> {
15    uow_factory: F,
16    _phantom: std::marker::PhantomData<RF>,
17}
18
19impl<RF, F: WriteRelUoWFactory<RF>> SetRelationshipUseCase<RF, F> {
20    pub fn new(uow_factory: F) -> Self {
21        SetRelationshipUseCase {
22            uow_factory,
23            _phantom: std::marker::PhantomData,
24        }
25    }
26
27    pub fn execute(&mut self, id: &EntityId, field: &RF, right_ids: &[EntityId]) -> Result<()> {
28        let mut uow = self.uow_factory.create();
29        uow.begin_transaction()?;
30        uow.set_relationship(id, field, right_ids)?;
31        uow.commit()?;
32        Ok(())
33    }
34}
35
36// ---------------------------------------------------------------------------
37// Undoable set-relationship
38// ---------------------------------------------------------------------------
39
40pub struct UndoableSetRelationshipUseCase<RF: Clone, F: WriteRelUoWFactory<RF>> {
41    uow_factory: F,
42    undo_stack: VecDeque<Savepoint>,
43    redo_stack: VecDeque<(EntityId, RF, Vec<EntityId>)>,
44}
45
46impl<RF: Clone, F: WriteRelUoWFactory<RF>> UndoableSetRelationshipUseCase<RF, F> {
47    pub fn new(uow_factory: F) -> Self {
48        UndoableSetRelationshipUseCase {
49            uow_factory,
50            undo_stack: VecDeque::new(),
51            redo_stack: VecDeque::new(),
52        }
53    }
54
55    pub fn execute(&mut self, id: &EntityId, field: &RF, right_ids: &[EntityId]) -> Result<()> {
56        let mut uow = self.uow_factory.create();
57        uow.begin_transaction()?;
58        let savepoint = uow.create_savepoint()?;
59        uow.set_relationship(id, field, right_ids)?;
60        uow.commit()?;
61        self.undo_stack.push_back(savepoint);
62        self.redo_stack
63            .push_back((*id, field.clone(), right_ids.to_vec()));
64        Ok(())
65    }
66}
67
68impl<RF: Clone + Send + 'static, F: WriteRelUoWFactory<RF> + Send + 'static> UndoRedoCommand
69    for UndoableSetRelationshipUseCase<RF, F>
70{
71    fn undo(&mut self) -> Result<()> {
72        if let Some(savepoint) = self.undo_stack.pop_back() {
73            let mut uow = self.uow_factory.create();
74            uow.begin_transaction()?;
75            uow.restore_to_savepoint(savepoint)?;
76            uow.commit()?;
77        }
78        Ok(())
79    }
80
81    fn redo(&mut self) -> Result<()> {
82        if let Some((id, field, right_ids)) = self.redo_stack.pop_back() {
83            let mut uow = self.uow_factory.create();
84            uow.begin_transaction()?;
85            let savepoint = uow.create_savepoint()?;
86            uow.set_relationship(&id, &field, &right_ids)?;
87            uow.commit()?;
88            self.undo_stack.push_back(savepoint);
89        }
90        Ok(())
91    }
92
93    fn as_any(&self) -> &dyn Any {
94        self
95    }
96}