use anyhow::{Ok, Result};
use common::database::{CommandUnitOfWork, QueryUnitOfWork};
use common::database::{db_context::DbContext, transactions::Transaction};
use common::direct_access::repository_factory;
use common::direct_access::use_cases;
use common::entities::InlineElement;
use common::event::{AllEvent, DirectAccessEntity, Event, EventBuffer, EventHub, Origin};
use common::snapshot::EntityTreeSnapshot;
use common::types;
use common::types::EntityId;
use std::cell::RefCell;
use std::sync::Arc;
pub struct InlineElementWriteUoW {
context: DbContext,
transaction: Option<Transaction>,
event_hub: Arc<EventHub>,
event_buffer: RefCell<EventBuffer>,
}
impl InlineElementWriteUoW {
pub fn new(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Self {
InlineElementWriteUoW {
context: db_context.clone(),
transaction: None,
event_hub: event_hub.clone(),
event_buffer: RefCell::new(EventBuffer::new()),
}
}
}
impl CommandUnitOfWork for InlineElementWriteUoW {
fn begin_transaction(&mut self) -> Result<()> {
self.transaction = Some(Transaction::begin_write_transaction(&self.context)?);
self.event_buffer.get_mut().begin_buffering();
Ok(())
}
fn commit(&mut self) -> Result<()> {
self.transaction.take().unwrap().commit()?;
for event in self.event_buffer.get_mut().flush() {
self.event_hub.send_event(event);
}
Ok(())
}
fn rollback(&mut self) -> Result<()> {
self.transaction.take().unwrap().rollback()?;
self.event_buffer.get_mut().discard();
Ok(())
}
fn create_savepoint(&self) -> Result<types::Savepoint> {
self.transaction.as_ref().unwrap().create_savepoint()
}
fn restore_to_savepoint(&mut self, savepoint: types::Savepoint) -> Result<()> {
let mut transaction = self.transaction.take().unwrap();
transaction.restore_to_savepoint(savepoint)?;
self.event_buffer.get_mut().discard();
self.event_hub.send_event(Event {
origin: Origin::DirectAccess(DirectAccessEntity::All(AllEvent::Reset)),
ids: vec![],
data: None,
});
self.transaction = Some(transaction);
Ok(())
}
}
impl use_cases::WriteUoW for InlineElementWriteUoW {
type Entity = InlineElement;
fn get(&self, id: &EntityId) -> Result<Option<InlineElement>> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let repo = repository_factory::write::create_inline_element_repository(transaction);
Ok(repo.get(id)?)
}
fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<InlineElement>>> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let repo = repository_factory::write::create_inline_element_repository(transaction);
Ok(repo.get_multi(ids)?)
}
fn get_all(&self) -> Result<Vec<InlineElement>> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let repo = repository_factory::write::create_inline_element_repository(transaction);
Ok(repo.get_all()?)
}
fn create_orphan_multi(&self, entities: &[InlineElement]) -> Result<Vec<InlineElement>> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let mut repo = repository_factory::write::create_inline_element_repository(transaction);
let mut event_buffer = self.event_buffer.borrow_mut();
Ok(repo.create_orphan_multi(&mut event_buffer, entities)?)
}
fn update_multi(&self, entities: &[InlineElement]) -> Result<Vec<InlineElement>> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let mut repo = repository_factory::write::create_inline_element_repository(transaction);
let mut event_buffer = self.event_buffer.borrow_mut();
Ok(repo.update_multi(&mut event_buffer, entities)?)
}
fn update_with_relationships_multi(
&self,
entities: &[InlineElement],
) -> Result<Vec<InlineElement>> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let mut repo = repository_factory::write::create_inline_element_repository(transaction);
let mut event_buffer = self.event_buffer.borrow_mut();
Ok(repo.update_with_relationships_multi(&mut event_buffer, entities)?)
}
fn remove(&self, id: &EntityId) -> Result<()> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let mut repo = repository_factory::write::create_inline_element_repository(transaction);
let mut event_buffer = self.event_buffer.borrow_mut();
Ok(repo.remove(&mut event_buffer, id)?)
}
fn remove_multi(&self, ids: &[EntityId]) -> Result<()> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let mut repo = repository_factory::write::create_inline_element_repository(transaction);
let mut event_buffer = self.event_buffer.borrow_mut();
Ok(repo.remove_multi(&mut event_buffer, ids)?)
}
fn snapshot(&self, ids: &[EntityId]) -> Result<EntityTreeSnapshot> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let repo = repository_factory::write::create_inline_element_repository(transaction);
Ok(repo.snapshot(ids)?)
}
fn restore(&self, snap: &EntityTreeSnapshot) -> Result<()> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let mut repo = repository_factory::write::create_inline_element_repository(transaction);
let mut event_buffer = self.event_buffer.borrow_mut();
Ok(repo.restore(&mut event_buffer, snap)?)
}
}
impl use_cases::OwnedWriteUoW for InlineElementWriteUoW {
fn create_multi(
&self,
entities: &[InlineElement],
owner_id: EntityId,
index: i32,
) -> Result<Vec<InlineElement>> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let mut repo = repository_factory::write::create_inline_element_repository(transaction);
let mut event_buffer = self.event_buffer.borrow_mut();
Ok(repo.create_multi(&mut event_buffer, entities, owner_id, index)?)
}
fn get_relationships_from_owner(&self, owner_id: &EntityId) -> Result<Vec<EntityId>> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let repo = repository_factory::write::create_inline_element_repository(transaction);
Ok(repo.get_relationships_from_owner(owner_id)?)
}
fn set_relationships_in_owner(&self, owner_id: &EntityId, ids: &[EntityId]) -> Result<()> {
let transaction = self.transaction.as_ref().expect("Transaction not started");
let mut repo = repository_factory::write::create_inline_element_repository(transaction);
let mut event_buffer = self.event_buffer.borrow_mut();
repo.set_relationships_in_owner(&mut event_buffer, owner_id, ids)?;
Ok(())
}
}
pub struct InlineElementWriteUoWFactory {
context: DbContext,
event_hub: Arc<EventHub>,
}
impl InlineElementWriteUoWFactory {
pub fn new(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Self {
InlineElementWriteUoWFactory {
context: db_context.clone(),
event_hub: event_hub.clone(),
}
}
}
impl use_cases::WriteUoWFactory for InlineElementWriteUoWFactory {
type Entity = InlineElement;
fn create(&self) -> Box<dyn use_cases::WriteUoW<Entity = InlineElement>> {
Box::new(InlineElementWriteUoW::new(&self.context, &self.event_hub))
}
}
impl use_cases::OwnedWriteUoWFactory for InlineElementWriteUoWFactory {
type Entity = InlineElement;
fn create(&self) -> Box<dyn use_cases::OwnedWriteUoW<Entity = InlineElement>> {
Box::new(InlineElementWriteUoW::new(&self.context, &self.event_hub))
}
}
pub struct InlineElementReadUoW {
context: DbContext,
transaction: RefCell<Option<Transaction>>,
}
impl InlineElementReadUoW {
pub fn new(db_context: &DbContext) -> Self {
InlineElementReadUoW {
context: db_context.clone(),
transaction: RefCell::new(None),
}
}
}
impl QueryUnitOfWork for InlineElementReadUoW {
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(())
}
}
impl use_cases::ReadUoW for InlineElementReadUoW {
type Entity = InlineElement;
fn get(&self, id: &EntityId) -> Result<Option<InlineElement>> {
let transaction = self.transaction.borrow();
let repo = repository_factory::read::create_inline_element_repository(
transaction.as_ref().unwrap(),
);
Ok(repo.get(id)?)
}
fn get_multi(&self, ids: &[EntityId]) -> Result<Vec<Option<InlineElement>>> {
let transaction = self.transaction.borrow();
let repo = repository_factory::read::create_inline_element_repository(
transaction.as_ref().unwrap(),
);
Ok(repo.get_multi(ids)?)
}
fn get_all(&self) -> Result<Vec<InlineElement>> {
let transaction = self.transaction.borrow();
let repo = repository_factory::read::create_inline_element_repository(
transaction.as_ref().unwrap(),
);
Ok(repo.get_all()?)
}
}
pub struct InlineElementReadUoWFactory {
context: DbContext,
}
impl InlineElementReadUoWFactory {
pub fn new(db_context: &DbContext) -> Self {
InlineElementReadUoWFactory {
context: db_context.clone(),
}
}
}
impl use_cases::ReadUoWFactory for InlineElementReadUoWFactory {
type Entity = InlineElement;
fn create(&self) -> Box<dyn use_cases::ReadUoW<Entity = InlineElement>> {
Box::new(InlineElementReadUoW::new(&self.context))
}
}