Skip to main content

text_document_io/
document_io_controller.rs

1// Generated by Qleany v1.4.8 from feature_controller.tera
2
3use crate::ExportDocxDto;
4use crate::ExportDocxResultDto;
5use crate::ExportHtmlDto;
6use crate::ExportLatexDto;
7use crate::ExportLatexResultDto;
8use crate::ExportMarkdownDto;
9use crate::ExportPlainTextDto;
10use crate::ImportHtmlDto;
11use crate::ImportHtmlResultDto;
12use crate::ImportMarkdownDto;
13use crate::ImportMarkdownResultDto;
14use crate::ImportPlainTextDto;
15use crate::units_of_work::export_docx_uow::ExportDocxUnitOfWorkFactory;
16use crate::units_of_work::export_html_uow::ExportHtmlUnitOfWorkFactory;
17use crate::units_of_work::export_latex_uow::ExportLatexUnitOfWorkFactory;
18use crate::units_of_work::export_markdown_uow::ExportMarkdownUnitOfWorkFactory;
19use crate::units_of_work::export_plain_text_uow::ExportPlainTextUnitOfWorkFactory;
20use crate::units_of_work::import_html_uow::ImportHtmlUnitOfWorkFactory;
21use crate::units_of_work::import_markdown_uow::ImportMarkdownUnitOfWorkFactory;
22use crate::units_of_work::import_plain_text_uow::ImportPlainTextUnitOfWorkFactory;
23use crate::use_cases::export_docx_uc::ExportDocxUseCase;
24use crate::use_cases::export_html_uc::ExportHtmlUseCase;
25use crate::use_cases::export_latex_uc::ExportLatexUseCase;
26use crate::use_cases::export_markdown_uc::ExportMarkdownUseCase;
27use crate::use_cases::export_plain_text_uc::ExportPlainTextUseCase;
28use crate::use_cases::import_html_uc::ImportHtmlUseCase;
29use crate::use_cases::import_markdown_uc::ImportMarkdownUseCase;
30use crate::use_cases::import_plain_text_uc::ImportPlainTextUseCase;
31use anyhow::Result;
32use common::event::{Event, Origin};
33
34use common::event::DocumentIoEvent::ExportHtml;
35use common::event::DocumentIoEvent::ExportLatex;
36use common::event::DocumentIoEvent::ExportMarkdown;
37use common::event::DocumentIoEvent::ExportPlainText;
38use common::event::DocumentIoEvent::ImportPlainText;
39
40use common::long_operation::{LongOperationManager, OperationProgress};
41use common::{database::db_context::DbContext, event::EventHub};
42use std::sync::Arc;
43
44pub fn import_plain_text(
45    db_context: &DbContext,
46    event_hub: &Arc<EventHub>,
47    dto: &ImportPlainTextDto,
48) -> Result<()> {
49    let uow_context = ImportPlainTextUnitOfWorkFactory::new(db_context, event_hub);
50    let mut uc = ImportPlainTextUseCase::new(Box::new(uow_context));
51    uc.execute(dto)?;
52    // Notify that the handling manifest has been loaded
53    event_hub.send_event(Event {
54        origin: Origin::DocumentIo(ImportPlainText),
55        ids: vec![],
56        data: None,
57    });
58    Ok(())
59}
60
61pub fn export_plain_text(
62    db_context: &DbContext,
63    event_hub: &Arc<EventHub>,
64) -> Result<ExportPlainTextDto> {
65    let uow_context = ExportPlainTextUnitOfWorkFactory::new(db_context);
66    let mut uc = ExportPlainTextUseCase::new(Box::new(uow_context));
67    let return_dto = uc.execute()?;
68    // Notify that the handling manifest has been loaded
69    event_hub.send_event(Event {
70        origin: Origin::DocumentIo(ExportPlainText),
71        ids: vec![],
72        data: None,
73    });
74    Ok(return_dto)
75}
76
77pub fn import_markdown(
78    db_context: &DbContext,
79    event_hub: &Arc<EventHub>,
80    long_operation_manager: &mut LongOperationManager,
81    dto: &ImportMarkdownDto,
82) -> Result<String> {
83    let uow_context = ImportMarkdownUnitOfWorkFactory::new(db_context, event_hub);
84    let uc = ImportMarkdownUseCase::new(Box::new(uow_context), dto);
85    let operation_id = long_operation_manager.start_operation(uc);
86    Ok(operation_id)
87}
88
89pub fn get_import_markdown_progress(
90    long_operation_manager: &LongOperationManager,
91    operation_id: &str,
92) -> Option<OperationProgress> {
93    long_operation_manager.get_operation_progress(operation_id)
94}
95
96pub fn get_import_markdown_result(
97    long_operation_manager: &LongOperationManager,
98    operation_id: &str,
99) -> Result<Option<ImportMarkdownResultDto>> {
100    // Get the operation result as a JSON string
101    let result_json = long_operation_manager.get_operation_result(operation_id);
102
103    // If there's no result, return None
104    if result_json.is_none() {
105        return Ok(None);
106    }
107    // Parse the JSON string into a ImportMarkdownResultDto
108    let result_dto: ImportMarkdownResultDto = serde_json::from_str(&result_json.unwrap())?;
109
110    Ok(Some(result_dto))
111}
112
113pub fn export_markdown(
114    db_context: &DbContext,
115    event_hub: &Arc<EventHub>,
116) -> Result<ExportMarkdownDto> {
117    let uow_context = ExportMarkdownUnitOfWorkFactory::new(db_context);
118    let mut uc = ExportMarkdownUseCase::new(Box::new(uow_context));
119    let return_dto = uc.execute()?;
120    // Notify that the handling manifest has been loaded
121    event_hub.send_event(Event {
122        origin: Origin::DocumentIo(ExportMarkdown),
123        ids: vec![],
124        data: None,
125    });
126    Ok(return_dto)
127}
128
129pub fn import_html(
130    db_context: &DbContext,
131    event_hub: &Arc<EventHub>,
132    long_operation_manager: &mut LongOperationManager,
133    dto: &ImportHtmlDto,
134) -> Result<String> {
135    let uow_context = ImportHtmlUnitOfWorkFactory::new(db_context, event_hub);
136    let uc = ImportHtmlUseCase::new(Box::new(uow_context), dto);
137    let operation_id = long_operation_manager.start_operation(uc);
138    Ok(operation_id)
139}
140
141pub fn get_import_html_progress(
142    long_operation_manager: &LongOperationManager,
143    operation_id: &str,
144) -> Option<OperationProgress> {
145    long_operation_manager.get_operation_progress(operation_id)
146}
147
148pub fn get_import_html_result(
149    long_operation_manager: &LongOperationManager,
150    operation_id: &str,
151) -> Result<Option<ImportHtmlResultDto>> {
152    // Get the operation result as a JSON string
153    let result_json = long_operation_manager.get_operation_result(operation_id);
154
155    // If there's no result, return None
156    if result_json.is_none() {
157        return Ok(None);
158    }
159    // Parse the JSON string into a ImportHtmlResultDto
160    let result_dto: ImportHtmlResultDto = serde_json::from_str(&result_json.unwrap())?;
161
162    Ok(Some(result_dto))
163}
164
165pub fn export_html(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Result<ExportHtmlDto> {
166    let uow_context = ExportHtmlUnitOfWorkFactory::new(db_context);
167    let mut uc = ExportHtmlUseCase::new(Box::new(uow_context));
168    let return_dto = uc.execute()?;
169    // Notify that the handling manifest has been loaded
170    event_hub.send_event(Event {
171        origin: Origin::DocumentIo(ExportHtml),
172        ids: vec![],
173        data: None,
174    });
175    Ok(return_dto)
176}
177
178pub fn export_latex(
179    db_context: &DbContext,
180    event_hub: &Arc<EventHub>,
181    dto: &ExportLatexDto,
182) -> Result<ExportLatexResultDto> {
183    let uow_context = ExportLatexUnitOfWorkFactory::new(db_context);
184    let mut uc = ExportLatexUseCase::new(Box::new(uow_context));
185    let return_dto = uc.execute(dto)?;
186    // Notify that the handling manifest has been loaded
187    event_hub.send_event(Event {
188        origin: Origin::DocumentIo(ExportLatex),
189        ids: vec![],
190        data: None,
191    });
192    Ok(return_dto)
193}
194
195pub fn export_docx(
196    db_context: &DbContext,
197    _event_hub: &Arc<EventHub>,
198    long_operation_manager: &mut LongOperationManager,
199    dto: &ExportDocxDto,
200) -> Result<String> {
201    let uow_context = ExportDocxUnitOfWorkFactory::new(db_context);
202    let uc = ExportDocxUseCase::new(Box::new(uow_context), dto);
203    let operation_id = long_operation_manager.start_operation(uc);
204    Ok(operation_id)
205}
206
207pub fn get_export_docx_progress(
208    long_operation_manager: &LongOperationManager,
209    operation_id: &str,
210) -> Option<OperationProgress> {
211    long_operation_manager.get_operation_progress(operation_id)
212}
213
214pub fn get_export_docx_result(
215    long_operation_manager: &LongOperationManager,
216    operation_id: &str,
217) -> Result<Option<ExportDocxResultDto>> {
218    // Get the operation result as a JSON string
219    let result_json = long_operation_manager.get_operation_result(operation_id);
220
221    // If there's no result, return None
222    if result_json.is_none() {
223        return Ok(None);
224    }
225    // Parse the JSON string into a ExportDocxResultDto
226    let result_dto: ExportDocxResultDto = serde_json::from_str(&result_json.unwrap())?;
227
228    Ok(Some(result_dto))
229}