text-document-io 1.7.0

Import/export for text-document: plain text, Markdown, HTML, LaTeX, DOCX
Documentation
// Generated by Qleany v1.4.8 from feature_use_case_uow.tera

use crate::use_cases::import_djot_uc::{
    ImportDjotUnitOfWorkFactoryTrait, ImportDjotUnitOfWorkTrait,
};
use anyhow::{Ok, Result};
use common::database::CommandUnitOfWork;
use common::database::{db_context::DbContext, transactions::Transaction};
#[allow(unused_imports)]
use common::entities::{Block, Document, Frame, List, Root, Table, TableCell};
use common::event::{AllEvent, DirectAccessEntity, Event, EventBuffer, EventHub, Origin};
#[allow(unused_imports)]
use common::types;
#[allow(unused_imports)]
use common::types::EntityId;
use parking_lot::Mutex;
use std::sync::Arc;

pub struct ImportDjotUnitOfWork {
    context: DbContext,
    transaction: Mutex<Option<Transaction>>,
    event_hub: Arc<EventHub>,
    event_buffer: Mutex<EventBuffer>,
}

impl ImportDjotUnitOfWork {
    pub fn new(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Self {
        ImportDjotUnitOfWork {
            context: db_context.clone(),
            transaction: Mutex::new(None),
            event_hub: event_hub.clone(),
            event_buffer: Mutex::new(EventBuffer::new()),
        }
    }
}

impl CommandUnitOfWork for ImportDjotUnitOfWork {
    fn begin_transaction(&mut self) -> Result<()> {
        let mut transaction = self.transaction.lock();
        *transaction = Some(Transaction::begin_write_transaction(&self.context)?);
        self.event_buffer.lock().begin_buffering();
        Ok(())
    }

    fn commit(&mut self) -> Result<()> {
        let mut transaction = self.transaction.lock();
        transaction.take().unwrap().commit()?;
        drop(transaction); // release lock before flushing events
        for event in self.event_buffer.lock().flush() {
            self.event_hub.send_event(event);
        }
        Ok(())
    }

    fn rollback(&mut self) -> Result<()> {
        let mut transaction = self.transaction.lock();
        transaction.take().unwrap().rollback()?;
        drop(transaction);
        self.event_buffer.lock().discard();
        Ok(())
    }

    fn create_savepoint(&self) -> Result<types::Savepoint> {
        let transaction = self.transaction.lock();
        transaction.as_ref().unwrap().create_savepoint()
    }

    fn restore_to_savepoint(&mut self, savepoint: types::Savepoint) -> Result<()> {
        let mut transaction_guard = self.transaction.lock();
        let mut transaction = transaction_guard.take().unwrap();
        transaction.restore_to_savepoint(savepoint)?;

        // Discard buffered events — savepoint restore invalidated them
        self.event_buffer.lock().discard();

        // Send Reset immediately (not buffered — UI must refresh now)
        self.event_hub.send_event(Event {
            origin: Origin::DirectAccess(DirectAccessEntity::All(AllEvent::Reset)),
            ids: vec![],
            data: None,
        });

        *transaction_guard = Some(transaction);

        Ok(())
    }

    fn store(&self) -> std::sync::Arc<common::database::Store> {
        self.context.get_store().clone()
    }
}
#[macros::uow_action(entity = "Root", action = "Get", thread_safe = true)]
#[macros::uow_action(entity = "Root", action = "GetRelationship", thread_safe = true)]
#[macros::uow_action(entity = "Document", action = "Get", thread_safe = true)]
#[macros::uow_action(entity = "Document", action = "Update", thread_safe = true)]
#[macros::uow_action(entity = "Document", action = "GetRelationship", thread_safe = true)]
#[macros::uow_action(entity = "Frame", action = "Get", thread_safe = true)]
#[macros::uow_action(entity = "Frame", action = "Create", thread_safe = true)]
#[macros::uow_action(entity = "Frame", action = "Update", thread_safe = true)]
#[macros::uow_action(
    entity = "Frame",
    action = "UpdateWithRelationships",
    thread_safe = true
)]
#[macros::uow_action(entity = "Frame", action = "Remove", thread_safe = true)]
#[macros::uow_action(entity = "Frame", action = "GetRelationship", thread_safe = true)]
#[macros::uow_action(entity = "Block", action = "Create", thread_safe = true)]
#[macros::uow_action(entity = "Block", action = "SetRelationship", thread_safe = true)]
#[macros::uow_action(entity = "List", action = "Create", thread_safe = true)]
#[macros::uow_action(entity = "Table", action = "Create", thread_safe = true)]
#[macros::uow_action(entity = "TableCell", action = "Create", thread_safe = true)]
impl ImportDjotUnitOfWorkTrait for ImportDjotUnitOfWork {}

pub struct ImportDjotUnitOfWorkFactory {
    context: DbContext,
    event_hub: Arc<EventHub>,
}

impl ImportDjotUnitOfWorkFactory {
    pub fn new(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Self {
        ImportDjotUnitOfWorkFactory {
            context: db_context.clone(),
            event_hub: event_hub.clone(),
        }
    }
}

impl ImportDjotUnitOfWorkFactoryTrait for ImportDjotUnitOfWorkFactory {
    fn create(&self) -> Box<dyn ImportDjotUnitOfWorkTrait> {
        Box::new(ImportDjotUnitOfWork::new(&self.context, &self.event_hub))
    }
}