Skip to main content

text_document_io/
document_io_controller.rs

1// Generated by Qleany v1.5.1 from feature_controller.tera
2
3use crate::ExportDjotDto;
4use crate::ExportDocxDto;
5use crate::ExportDocxResultDto;
6use crate::ExportHtmlDto;
7use crate::ExportLatexDto;
8use crate::ExportLatexResultDto;
9use crate::ExportMarkdownDto;
10use crate::ExportPlainTextDto;
11use crate::ImportDjotDto;
12use crate::ImportDjotResultDto;
13use crate::ImportHtmlDto;
14use crate::ImportHtmlResultDto;
15use crate::ImportMarkdownDto;
16use crate::ImportMarkdownResultDto;
17use crate::ImportPlainTextDto;
18use crate::units_of_work::export_djot_uow::ExportDjotUnitOfWorkFactory;
19use crate::units_of_work::export_docx_uow::ExportDocxUnitOfWorkFactory;
20use crate::units_of_work::export_html_uow::ExportHtmlUnitOfWorkFactory;
21use crate::units_of_work::export_latex_uow::ExportLatexUnitOfWorkFactory;
22use crate::units_of_work::export_markdown_uow::ExportMarkdownUnitOfWorkFactory;
23use crate::units_of_work::export_plain_text_uow::ExportPlainTextUnitOfWorkFactory;
24use crate::units_of_work::import_djot_uow::ImportDjotUnitOfWorkFactory;
25use crate::units_of_work::import_html_uow::ImportHtmlUnitOfWorkFactory;
26use crate::units_of_work::import_markdown_uow::ImportMarkdownUnitOfWorkFactory;
27use crate::units_of_work::import_plain_text_uow::ImportPlainTextUnitOfWorkFactory;
28use crate::use_cases::export_djot_uc::ExportDjotUseCase;
29use crate::use_cases::export_docx_uc::ExportDocxUseCase;
30use crate::use_cases::export_html_uc::ExportHtmlUseCase;
31use crate::use_cases::export_latex_uc::ExportLatexUseCase;
32use crate::use_cases::export_markdown_uc::ExportMarkdownUseCase;
33use crate::use_cases::export_plain_text_uc::ExportPlainTextUseCase;
34use crate::use_cases::import_djot_uc::ImportDjotUseCase;
35use crate::use_cases::import_html_uc::ImportHtmlUseCase;
36use crate::use_cases::import_markdown_uc::ImportMarkdownUseCase;
37use crate::use_cases::import_plain_text_uc::ImportPlainTextUseCase;
38use anyhow::Result;
39use common::event::{Event, Origin};
40use common::parser_tools::DjotExportOptions;
41
42use common::event::DocumentIoEvent::ExportDjot;
43use common::event::DocumentIoEvent::ExportHtml;
44use common::event::DocumentIoEvent::ExportLatex;
45use common::event::DocumentIoEvent::ExportMarkdown;
46use common::event::DocumentIoEvent::ExportPlainText;
47use common::event::DocumentIoEvent::ImportPlainText;
48
49use common::long_operation::{LongOperationManager, OperationProgress};
50use common::{database::db_context::DbContext, event::EventHub};
51use std::sync::Arc;
52
53pub fn import_plain_text(
54    db_context: &DbContext,
55    event_hub: &Arc<EventHub>,
56    dto: &ImportPlainTextDto,
57) -> Result<()> {
58    let uow_context = ImportPlainTextUnitOfWorkFactory::new(db_context, event_hub);
59    let mut uc = ImportPlainTextUseCase::new(Box::new(uow_context));
60    uc.execute(dto)?;
61    // Notify that the handling manifest has been loaded
62    event_hub.send_event(Event {
63        origin: Origin::DocumentIo(ImportPlainText),
64        ids: vec![],
65        data: None,
66    });
67    Ok(())
68}
69
70pub fn export_plain_text(
71    db_context: &DbContext,
72    event_hub: &Arc<EventHub>,
73) -> Result<ExportPlainTextDto> {
74    let uow_context = ExportPlainTextUnitOfWorkFactory::new(db_context);
75    let mut uc = ExportPlainTextUseCase::new(Box::new(uow_context));
76    let return_dto = uc.execute()?;
77    // Notify that the handling manifest has been loaded
78    event_hub.send_event(Event {
79        origin: Origin::DocumentIo(ExportPlainText),
80        ids: vec![],
81        data: None,
82    });
83    Ok(return_dto)
84}
85
86pub fn import_markdown(
87    db_context: &DbContext,
88    event_hub: &Arc<EventHub>,
89    long_operation_manager: &mut LongOperationManager,
90    dto: &ImportMarkdownDto,
91) -> Result<String> {
92    let uow_context = ImportMarkdownUnitOfWorkFactory::new(db_context, event_hub);
93    let uc = ImportMarkdownUseCase::new(Box::new(uow_context), dto);
94    let operation_id = long_operation_manager.start_operation(uc);
95    Ok(operation_id)
96}
97
98pub fn get_import_markdown_progress(
99    long_operation_manager: &LongOperationManager,
100    operation_id: &str,
101) -> Option<OperationProgress> {
102    long_operation_manager.get_operation_progress(operation_id)
103}
104
105pub fn get_import_markdown_result(
106    long_operation_manager: &LongOperationManager,
107    operation_id: &str,
108) -> Result<Option<ImportMarkdownResultDto>> {
109    // Get the operation result as a JSON string
110    let result_json = long_operation_manager.get_operation_result(operation_id);
111
112    // If there's no result, return None
113    if result_json.is_none() {
114        return Ok(None);
115    }
116    // Parse the JSON string into a ImportMarkdownResultDto
117    let result_dto: ImportMarkdownResultDto = serde_json::from_str(&result_json.unwrap())?;
118
119    Ok(Some(result_dto))
120}
121
122pub fn export_markdown(
123    db_context: &DbContext,
124    event_hub: &Arc<EventHub>,
125) -> Result<ExportMarkdownDto> {
126    let uow_context = ExportMarkdownUnitOfWorkFactory::new(db_context);
127    let mut uc = ExportMarkdownUseCase::new(Box::new(uow_context));
128    let return_dto = uc.execute()?;
129    // Notify that the handling manifest has been loaded
130    event_hub.send_event(Event {
131        origin: Origin::DocumentIo(ExportMarkdown),
132        ids: vec![],
133        data: None,
134    });
135    Ok(return_dto)
136}
137
138pub fn import_djot(
139    db_context: &DbContext,
140    event_hub: &Arc<EventHub>,
141    long_operation_manager: &mut LongOperationManager,
142    dto: &ImportDjotDto,
143) -> Result<String> {
144    let uow_context = ImportDjotUnitOfWorkFactory::new(db_context, event_hub);
145    let uc = ImportDjotUseCase::new(Box::new(uow_context), dto);
146    let operation_id = long_operation_manager.start_operation(uc);
147    Ok(operation_id)
148}
149
150pub fn get_import_djot_progress(
151    long_operation_manager: &LongOperationManager,
152    operation_id: &str,
153) -> Option<OperationProgress> {
154    long_operation_manager.get_operation_progress(operation_id)
155}
156
157pub fn get_import_djot_result(
158    long_operation_manager: &LongOperationManager,
159    operation_id: &str,
160) -> Result<Option<ImportDjotResultDto>> {
161    let result_json = long_operation_manager.get_operation_result(operation_id);
162    if result_json.is_none() {
163        return Ok(None);
164    }
165    let result_dto: ImportDjotResultDto = serde_json::from_str(&result_json.unwrap())?;
166    Ok(Some(result_dto))
167}
168
169pub fn export_djot(
170    db_context: &DbContext,
171    event_hub: &Arc<EventHub>,
172    options: &DjotExportOptions,
173) -> Result<ExportDjotDto> {
174    let uow_context = ExportDjotUnitOfWorkFactory::new(db_context);
175    let mut uc = ExportDjotUseCase::new(Box::new(uow_context));
176    let return_dto = uc.execute(options)?;
177    event_hub.send_event(Event {
178        origin: Origin::DocumentIo(ExportDjot),
179        ids: vec![],
180        data: None,
181    });
182    Ok(return_dto)
183}
184
185pub fn import_html(
186    db_context: &DbContext,
187    event_hub: &Arc<EventHub>,
188    long_operation_manager: &mut LongOperationManager,
189    dto: &ImportHtmlDto,
190) -> Result<String> {
191    let uow_context = ImportHtmlUnitOfWorkFactory::new(db_context, event_hub);
192    let uc = ImportHtmlUseCase::new(Box::new(uow_context), dto);
193    let operation_id = long_operation_manager.start_operation(uc);
194    Ok(operation_id)
195}
196
197pub fn get_import_html_progress(
198    long_operation_manager: &LongOperationManager,
199    operation_id: &str,
200) -> Option<OperationProgress> {
201    long_operation_manager.get_operation_progress(operation_id)
202}
203
204pub fn get_import_html_result(
205    long_operation_manager: &LongOperationManager,
206    operation_id: &str,
207) -> Result<Option<ImportHtmlResultDto>> {
208    // Get the operation result as a JSON string
209    let result_json = long_operation_manager.get_operation_result(operation_id);
210
211    // If there's no result, return None
212    if result_json.is_none() {
213        return Ok(None);
214    }
215    // Parse the JSON string into a ImportHtmlResultDto
216    let result_dto: ImportHtmlResultDto = serde_json::from_str(&result_json.unwrap())?;
217
218    Ok(Some(result_dto))
219}
220
221pub fn export_html(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Result<ExportHtmlDto> {
222    let uow_context = ExportHtmlUnitOfWorkFactory::new(db_context);
223    let mut uc = ExportHtmlUseCase::new(Box::new(uow_context));
224    let return_dto = uc.execute()?;
225    // Notify that the handling manifest has been loaded
226    event_hub.send_event(Event {
227        origin: Origin::DocumentIo(ExportHtml),
228        ids: vec![],
229        data: None,
230    });
231    Ok(return_dto)
232}
233
234pub fn export_latex(
235    db_context: &DbContext,
236    event_hub: &Arc<EventHub>,
237    dto: &ExportLatexDto,
238) -> Result<ExportLatexResultDto> {
239    let uow_context = ExportLatexUnitOfWorkFactory::new(db_context);
240    let mut uc = ExportLatexUseCase::new(Box::new(uow_context));
241    let return_dto = uc.execute(dto)?;
242    // Notify that the handling manifest has been loaded
243    event_hub.send_event(Event {
244        origin: Origin::DocumentIo(ExportLatex),
245        ids: vec![],
246        data: None,
247    });
248    Ok(return_dto)
249}
250
251pub fn export_docx(
252    db_context: &DbContext,
253    _event_hub: &Arc<EventHub>,
254    long_operation_manager: &mut LongOperationManager,
255    dto: &ExportDocxDto,
256) -> Result<String> {
257    let uow_context = ExportDocxUnitOfWorkFactory::new(db_context);
258    let uc = ExportDocxUseCase::new(Box::new(uow_context), dto);
259    let operation_id = long_operation_manager.start_operation(uc);
260    Ok(operation_id)
261}
262
263/// Build the in-memory DOCX document for `db_context` without writing a file.
264///
265/// This runs the exact same builder used by [`export_docx`] but returns the
266/// assembled [`docx_rs::Docx`] instead of packing it to disk, so callers can
267/// inspect the produced structure. Intended for tests; the type is re-exported
268/// as [`crate::docx_rs`] so callers can name it.
269#[doc(hidden)]
270pub fn build_docx_document(db_context: &DbContext, dto: &ExportDocxDto) -> Result<docx_rs::Docx> {
271    let uow_context = ExportDocxUnitOfWorkFactory::new(db_context);
272    let uc = ExportDocxUseCase::new(Box::new(uow_context), dto);
273    let (docx, _paragraph_count) = uc.build_document()?;
274    Ok(docx)
275}
276
277pub fn get_export_docx_progress(
278    long_operation_manager: &LongOperationManager,
279    operation_id: &str,
280) -> Option<OperationProgress> {
281    long_operation_manager.get_operation_progress(operation_id)
282}
283
284pub fn get_export_docx_result(
285    long_operation_manager: &LongOperationManager,
286    operation_id: &str,
287) -> Result<Option<ExportDocxResultDto>> {
288    // Get the operation result as a JSON string
289    let result_json = long_operation_manager.get_operation_result(operation_id);
290
291    // If there's no result, return None
292    if result_json.is_none() {
293        return Ok(None);
294    }
295    // Parse the JSON string into a ExportDocxResultDto
296    let result_dto: ExportDocxResultDto = serde_json::from_str(&result_json.unwrap())?;
297
298    Ok(Some(result_dto))
299}