use crate::use_cases::import_html_uc::{
ImportHtmlUnitOfWorkFactoryTrait, ImportHtmlUnitOfWorkTrait,
};
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, InlineElement, List, Resource, 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 std::sync::Arc;
use std::sync::Mutex;
pub struct ImportHtmlUnitOfWork {
context: DbContext,
transaction: Mutex<Option<Transaction>>,
event_hub: Arc<EventHub>,
event_buffer: Mutex<EventBuffer>,
}
impl ImportHtmlUnitOfWork {
pub fn new(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Self {
ImportHtmlUnitOfWork {
context: db_context.clone(),
transaction: Mutex::new(None),
event_hub: event_hub.clone(),
event_buffer: Mutex::new(EventBuffer::new()),
}
}
}
impl CommandUnitOfWork for ImportHtmlUnitOfWork {
fn begin_transaction(&mut self) -> Result<()> {
let mut transaction = self.transaction.lock().unwrap();
*transaction = Some(Transaction::begin_write_transaction(&self.context)?);
self.event_buffer.lock().unwrap().begin_buffering();
Ok(())
}
fn commit(&mut self) -> Result<()> {
let mut transaction = self.transaction.lock().unwrap();
transaction.take().unwrap().commit()?;
drop(transaction); for event in self.event_buffer.lock().unwrap().flush() {
self.event_hub.send_event(event);
}
Ok(())
}
fn rollback(&mut self) -> Result<()> {
let mut transaction = self.transaction.lock().unwrap();
transaction.take().unwrap().rollback()?;
drop(transaction);
self.event_buffer.lock().unwrap().discard();
Ok(())
}
fn create_savepoint(&self) -> Result<types::Savepoint> {
let transaction = self.transaction.lock().unwrap();
transaction.as_ref().unwrap().create_savepoint()
}
fn restore_to_savepoint(&mut self, savepoint: types::Savepoint) -> Result<()> {
let mut transaction_guard = self.transaction.lock().unwrap();
let mut transaction = transaction_guard.take().unwrap();
transaction.restore_to_savepoint(savepoint)?;
self.event_buffer.lock().unwrap().discard();
self.event_hub.send_event(Event {
origin: Origin::DirectAccess(DirectAccessEntity::All(AllEvent::Reset)),
ids: vec![],
data: None,
});
*transaction_guard = Some(transaction);
Ok(())
}
}
#[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 = "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 = "InlineElement", action = "Create", thread_safe = true)]
#[macros::uow_action(entity = "List", action = "Create", thread_safe = true)]
#[macros::uow_action(entity = "Resource", 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 ImportHtmlUnitOfWorkTrait for ImportHtmlUnitOfWork {}
pub struct ImportHtmlUnitOfWorkFactory {
context: DbContext,
event_hub: Arc<EventHub>,
}
impl ImportHtmlUnitOfWorkFactory {
pub fn new(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Self {
ImportHtmlUnitOfWorkFactory {
context: db_context.clone(),
event_hub: event_hub.clone(),
}
}
}
impl ImportHtmlUnitOfWorkFactoryTrait for ImportHtmlUnitOfWorkFactory {
fn create(&self) -> Box<dyn ImportHtmlUnitOfWorkTrait> {
Box::new(ImportHtmlUnitOfWork::new(&self.context, &self.event_hub))
}
}