// Generated by Qleany v1.4.8 from feature_use_case_uow.tera

use crate::use_cases::get_document_stats_uc::{
    GetDocumentStatsUnitOfWorkFactoryTrait, GetDocumentStatsUnitOfWorkTrait,
};
use anyhow::{Ok, Result};
use common::database::QueryUnitOfWork;
use common::database::{db_context::DbContext, transactions::Transaction};
#[allow(unused_imports)]
use common::entities::{Block, Document, Frame, Root};
#[allow(unused_imports)]
use common::types;
#[allow(unused_imports)]
use common::types::EntityId;
use std::cell::RefCell;

// Unit of work for GetDocumentStats

pub struct GetDocumentStatsUnitOfWork {
    context: DbContext,
    transaction: RefCell<Option<Transaction>>,
}

impl GetDocumentStatsUnitOfWork {
    pub fn new(db_context: &DbContext) -> Self {
        GetDocumentStatsUnitOfWork {
            context: db_context.clone(),
            transaction: RefCell::new(None),
        }
    }
}

impl QueryUnitOfWork for GetDocumentStatsUnitOfWork {
    fn begin_transaction(&self) -> Result<()> {
        self.transaction
            .replace(Some(Transaction::begin_read_transaction(&self.context)?));
        Ok(())
    }

    fn end_transaction(&self) -> Result<()> {
        self.transaction.take().unwrap().end_read_transaction()?;
        Ok(())
    }

    fn store(&self) -> std::sync::Arc<common::database::Store> {
        self.context.get_store().clone()
    }
}

#[macros::uow_action(entity = "Root", action = "GetRO")]
#[macros::uow_action(entity = "Root", action = "GetRelationshipRO")]
#[macros::uow_action(entity = "Document", action = "GetRO")]
#[macros::uow_action(entity = "Document", action = "GetRelationshipRO")]
#[macros::uow_action(entity = "Frame", action = "GetMultiRO")]
#[macros::uow_action(entity = "Frame", action = "GetRelationshipRO")]
#[macros::uow_action(entity = "Block", action = "GetMultiRO")]
#[macros::uow_action(entity = "Block", action = "GetRelationshipRO")]
impl GetDocumentStatsUnitOfWorkTrait for GetDocumentStatsUnitOfWork {}

pub struct GetDocumentStatsUnitOfWorkFactory {
    context: DbContext,
}

impl GetDocumentStatsUnitOfWorkFactory {
    pub fn new(db_context: &DbContext) -> Self {
        GetDocumentStatsUnitOfWorkFactory {
            context: db_context.clone(),
        }
    }
}

impl GetDocumentStatsUnitOfWorkFactoryTrait for GetDocumentStatsUnitOfWorkFactory {
    fn create(&self) -> Box<dyn GetDocumentStatsUnitOfWorkTrait> {
        Box::new(GetDocumentStatsUnitOfWork::new(&self.context))
    }
}
