Line data Source code
1 : /* File: io_export_set_traversal.c; Copyright and License: see below */
2 :
3 : #include "io_export_set_traversal.h"
4 :
5 : #include "json/json_element_reader.h"
6 : #include "utf8stringbuf/utf8string.h"
7 : #include "u8stream/universal_memory_output_stream.h"
8 : #include "u8/u8_trace.h"
9 : #include <assert.h>
10 : #include <gtk/gtk.h>
11 : #include <stdbool.h>
12 :
13 0 : void io_export_set_traversal_init ( io_export_set_traversal_t *this_,
14 : data_database_reader_t *db_reader,
15 : data_stat_t *io_export_stat,
16 : io_element_writer_t *out_element_writer )
17 : {
18 0 : U8_TRACE_BEGIN();
19 0 : assert( NULL != db_reader );
20 0 : assert( NULL != io_export_stat );
21 0 : assert( NULL != out_element_writer );
22 :
23 0 : (*this_).db_reader = db_reader;
24 0 : (*this_).export_stat = io_export_stat;
25 0 : (*this_).element_writer = out_element_writer;
26 :
27 0 : U8_TRACE_END();
28 0 : }
29 :
30 0 : void io_export_set_traversal_destroy ( io_export_set_traversal_t *this_ )
31 : {
32 0 : U8_TRACE_BEGIN();
33 :
34 0 : (*this_).db_reader = NULL;
35 0 : (*this_).export_stat = NULL;
36 0 : (*this_).element_writer = NULL;
37 :
38 0 : U8_TRACE_END();
39 0 : }
40 :
41 0 : u8_error_t io_export_set_traversal_export_set( io_export_set_traversal_t *this_,
42 : const data_small_set_t *set_to_be_exported )
43 : {
44 0 : U8_TRACE_BEGIN();
45 0 : assert( NULL != set_to_be_exported );
46 :
47 0 : u8_error_t serialize_error = U8_ERROR_NONE;
48 :
49 0 : const char *const document_title = "";
50 0 : serialize_error |= io_element_writer_start_main( (*this_).element_writer, document_title );
51 :
52 : /* first pass: serialize the diagram(s) if there is one/some */
53 0 : for ( int index = 0; index < data_small_set_get_count( set_to_be_exported ); index ++ )
54 : {
55 : data_id_t current_id;
56 0 : current_id = data_small_set_get_id( set_to_be_exported, index );
57 0 : switch ( data_id_get_table( ¤t_id ) )
58 : {
59 0 : case DATA_TABLE_DIAGRAM:
60 : {
61 0 : serialize_error |= io_export_set_traversal_private_export_diagram( this_, current_id );
62 : }
63 0 : break;
64 :
65 0 : case DATA_TABLE_DIAGRAMELEMENT:
66 : {
67 0 : serialize_error |= io_export_set_traversal_private_export_diagramelement( this_, current_id );
68 : }
69 0 : break;
70 :
71 0 : case DATA_TABLE_CLASSIFIER:
72 : {
73 0 : serialize_error |= io_export_set_traversal_private_export_classifier( this_, current_id );
74 : }
75 0 : break;
76 :
77 0 : case DATA_TABLE_FEATURE:
78 : {
79 0 : serialize_error |= io_export_set_traversal_private_export_feature( this_, current_id );
80 : }
81 0 : break;
82 :
83 0 : case DATA_TABLE_RELATIONSHIP:
84 : {
85 0 : serialize_error |= io_export_set_traversal_private_export_relationship( this_, current_id );
86 : }
87 0 : break;
88 :
89 0 : default:
90 0 : break;
91 : }
92 : }
93 :
94 0 : serialize_error |= io_element_writer_end_main( (*this_).element_writer );
95 :
96 0 : U8_TRACE_END_ERR(serialize_error);
97 0 : return serialize_error;
98 : }
99 :
100 0 : u8_error_t io_export_set_traversal_private_export_diagram( io_export_set_traversal_t *this_, data_id_t id )
101 : {
102 0 : U8_TRACE_BEGIN();
103 0 : assert( data_id_is_valid( &id ) );
104 0 : assert( DATA_TABLE_DIAGRAM == data_id_get_table( &id ) );
105 0 : u8_error_t serialize_error = U8_ERROR_NONE;
106 : u8_error_t read_error;
107 :
108 : data_diagram_t out_diagram;
109 0 : read_error = data_database_reader_get_diagram_by_id ( (*this_).db_reader,
110 : data_id_get_row_id( &id ),
111 : &out_diagram
112 : );
113 :
114 0 : if ( read_error == U8_ERROR_NONE )
115 : {
116 : /* write diagram */
117 0 : const char *const diagram_file_base_name = "";
118 0 : serialize_error |= io_element_writer_start_diagram( (*this_).element_writer, &out_diagram );
119 0 : serialize_error |= io_element_writer_assemble_diagram( (*this_).element_writer,
120 : NULL, /* parent, currently not needed here */
121 : &out_diagram,
122 : diagram_file_base_name
123 : );
124 0 : serialize_error |= io_element_writer_end_diagram( (*this_).element_writer, &out_diagram );
125 : }
126 : else
127 : {
128 : /* program internal error */
129 0 : U8_LOG_ERROR( "io_export_set_traversal_export_set could not read all data of the set." );
130 0 : data_stat_inc_count ( (*this_).export_stat, DATA_STAT_TABLE_DIAGRAM, DATA_STAT_SERIES_ERROR );
131 : }
132 :
133 0 : U8_TRACE_END_ERR(serialize_error);
134 0 : return serialize_error;
135 : }
136 :
137 0 : u8_error_t io_export_set_traversal_private_export_diagramelement( io_export_set_traversal_t *this_, data_id_t id )
138 : {
139 0 : U8_TRACE_BEGIN();
140 0 : assert( data_id_is_valid( &id ) );
141 0 : assert( DATA_TABLE_DIAGRAMELEMENT == data_id_get_table( &id ) );
142 0 : u8_error_t serialize_error = U8_ERROR_NONE;
143 : u8_error_t read_error;
144 :
145 : data_classifier_t out_classifier;
146 : data_diagramelement_t out_diagramelement;
147 : /* data_diagram_t out_diagram; */
148 :
149 0 : read_error = data_database_reader_get_diagramelement_by_id( (*this_).db_reader,
150 : data_id_get_row_id( &id ),
151 : &out_diagramelement
152 : );
153 :
154 0 : if ( read_error == U8_ERROR_NONE )
155 : {
156 : /* get classifier */
157 0 : const data_row_t classifier_id = data_diagramelement_get_classifier_row_id( &out_diagramelement );
158 0 : read_error = data_database_reader_get_classifier_by_id( (*this_).db_reader,
159 : classifier_id,
160 : &out_classifier
161 : );
162 :
163 : /* get focused_feature */
164 : /*
165 : assert ( IO_EXPORT_SET_TRAVERSAL_MAX_FEATURES >= 1 );
166 : const data_row_t focused_feature_id = data_diagramelement_get_focused_feature_row_id( &out_diagramelement );
167 : if ( DATA_ROW_VOID == focused_feature_id )
168 : {
169 : data_feature_init_empty( &((*this_).temp_features[0]) );
170 : }
171 : else
172 : {
173 : read_error |= data_database_reader_get_feature_by_id ( (*this_).db_reader,
174 : focused_feature_id,
175 : &((*this_).temp_features[0])
176 : );
177 : }
178 : */
179 :
180 : /* get diagram */
181 : /*
182 : const data_row_t diag_id = data_diagramelement_get_diagram_row_id( &out_diagramelement );
183 : read_error |= data_database_reader_get_diagram_by_id ( (*this_).db_reader,
184 : diag_id,
185 : &out_diagram
186 : );
187 : */
188 :
189 0 : if ( read_error == U8_ERROR_NONE )
190 : {
191 : /* intentionally not supported: diagramelements */
192 0 : U8_TRACE_INFO( "io_export_set_traversal_export_set does not copy single diagramelements, only referenced classifiers." );
193 : /*
194 : serialize_error |= io_element_writer_start_diagramelement( (*this_).element_writer,
195 : &out_diagram,
196 : &out_diagramelement
197 : );
198 :
199 : serialize_error |= io_element_writer_assemble_diagramelement( (*this_).element_writer,
200 : &out_diagram,
201 : &out_diagramelement,
202 : &out_classifier,
203 : &((*this_).temp_features[0]
204 : );
205 :
206 : serialize_error |= io_element_writer_end_diagramelement( (*this_).element_writer,
207 : &out_diagram,
208 : &out_diagramelement
209 : );
210 : */
211 : }
212 :
213 : data_feature_iterator_t feature_iterator;
214 0 : read_error |= data_feature_iterator_init_empty( &feature_iterator );
215 0 : read_error |= data_database_reader_get_features_by_classifier_id ( (*this_).db_reader, classifier_id, &feature_iterator );
216 0 : if ( read_error == U8_ERROR_NONE )
217 : {
218 : /* write classifier */
219 0 : serialize_error |= io_element_writer_start_classifier( (*this_).element_writer,
220 : DATA_CLASSIFIER_TYPE_VOID, /* host_type */
221 : &out_classifier
222 : );
223 0 : serialize_error |= io_element_writer_assemble_classifier( (*this_).element_writer,
224 : DATA_CLASSIFIER_TYPE_VOID, /* host_type */
225 : &out_classifier
226 : );
227 :
228 : /* write classifier-features */
229 0 : while ( data_feature_iterator_has_next( &feature_iterator ) )
230 : {
231 0 : read_error |= data_feature_iterator_next( &feature_iterator, &((*this_).temp_feature_0) );
232 0 : const data_classifier_type_t parent_type = data_classifier_get_main_type( &out_classifier );
233 0 : serialize_error |= io_element_writer_start_feature( (*this_).element_writer,
234 : parent_type,
235 0 : &((*this_).temp_feature_0)
236 : );
237 :
238 0 : serialize_error |= io_element_writer_assemble_feature( (*this_).element_writer,
239 : &out_classifier,
240 0 : &((*this_).temp_feature_0)
241 : );
242 :
243 0 : serialize_error |= io_element_writer_end_feature( (*this_).element_writer,
244 : parent_type,
245 0 : &((*this_).temp_feature_0)
246 : );
247 : }
248 :
249 0 : serialize_error |= io_element_writer_end_classifier( (*this_).element_writer,
250 : DATA_CLASSIFIER_TYPE_VOID, /* host_type */
251 : &out_classifier
252 : );
253 : }
254 : else
255 : {
256 : /* program internal error */
257 0 : U8_LOG_ERROR( "io_export_set_traversal_export_set could not read referenced data of the set." );
258 0 : data_stat_inc_count ( (*this_).export_stat, DATA_STAT_TABLE_CLASSIFIER, DATA_STAT_SERIES_ERROR );
259 : }
260 0 : read_error |= data_feature_iterator_destroy( &feature_iterator );
261 : }
262 : else
263 : {
264 : /* program internal error */
265 0 : U8_LOG_ERROR( "io_export_set_traversal_export_set could not read all data of the set." );
266 0 : data_stat_inc_count ( (*this_).export_stat, DATA_STAT_TABLE_CLASSIFIER, DATA_STAT_SERIES_ERROR );
267 : }
268 :
269 0 : U8_TRACE_END_ERR(serialize_error);
270 0 : return serialize_error;
271 : }
272 :
273 0 : u8_error_t io_export_set_traversal_private_export_classifier( io_export_set_traversal_t *this_, data_id_t id )
274 : {
275 0 : U8_TRACE_BEGIN();
276 0 : assert( data_id_is_valid( &id ) );
277 0 : assert( DATA_TABLE_CLASSIFIER == data_id_get_table( &id ) );
278 0 : u8_error_t serialize_error = U8_ERROR_NONE;
279 : u8_error_t read_error;
280 :
281 : data_classifier_t out_classifier;
282 0 : read_error = data_database_reader_get_classifier_by_id( (*this_).db_reader,
283 : data_id_get_row_id( &id ),
284 : &out_classifier
285 : );
286 :
287 : data_feature_iterator_t feature_iterator;
288 0 : read_error |= data_feature_iterator_init_empty( &feature_iterator );
289 0 : read_error |= data_database_reader_get_features_by_classifier_id ( (*this_).db_reader, data_id_get_row_id( &id ), &feature_iterator );
290 0 : if ( read_error == U8_ERROR_NONE )
291 : {
292 : /* write classifier */
293 0 : serialize_error |= io_element_writer_start_classifier( (*this_).element_writer,
294 : DATA_CLASSIFIER_TYPE_VOID, /* host_type */
295 : &out_classifier
296 : );
297 0 : serialize_error |= io_element_writer_assemble_classifier( (*this_).element_writer,
298 : DATA_CLASSIFIER_TYPE_VOID, /* host_type */
299 : &out_classifier
300 : );
301 :
302 : /* write classifier-features */
303 0 : while ( data_feature_iterator_has_next( &feature_iterator ) )
304 : {
305 0 : read_error |= data_feature_iterator_next( &feature_iterator, &((*this_).temp_feature_0) );
306 0 : const data_classifier_type_t parent_type = data_classifier_get_main_type( &out_classifier );
307 0 : serialize_error |= io_element_writer_start_feature( (*this_).element_writer,
308 : parent_type,
309 0 : &((*this_).temp_feature_0)
310 : );
311 :
312 0 : serialize_error |= io_element_writer_assemble_feature( (*this_).element_writer,
313 : &out_classifier,
314 0 : &((*this_).temp_feature_0)
315 : );
316 :
317 0 : serialize_error |= io_element_writer_end_feature( (*this_).element_writer,
318 : parent_type,
319 0 : &((*this_).temp_feature_0)
320 : );
321 : }
322 :
323 0 : serialize_error |= io_element_writer_end_classifier( (*this_).element_writer,
324 : DATA_CLASSIFIER_TYPE_VOID, /* host_type */
325 : &out_classifier
326 : );
327 : }
328 : else
329 : {
330 : /* program internal error */
331 0 : U8_LOG_ERROR( "io_export_set_traversal_export_set could not read all data of the set." );
332 0 : data_stat_inc_count ( (*this_).export_stat, DATA_STAT_TABLE_CLASSIFIER, DATA_STAT_SERIES_ERROR );
333 : }
334 0 : read_error |= data_feature_iterator_destroy( &feature_iterator );
335 :
336 0 : U8_TRACE_END_ERR(serialize_error);
337 0 : return serialize_error;
338 : }
339 :
340 0 : u8_error_t io_export_set_traversal_private_export_feature( io_export_set_traversal_t *this_, data_id_t id )
341 : {
342 0 : U8_TRACE_BEGIN();
343 0 : assert( data_id_is_valid( &id ) );
344 0 : assert( DATA_TABLE_FEATURE == data_id_get_table( &id ) );
345 0 : u8_error_t serialize_error = U8_ERROR_NONE;
346 : u8_error_t read_error;
347 :
348 : data_feature_t out_feature;
349 0 : read_error = data_database_reader_get_feature_by_id ( (*this_).db_reader,
350 : data_id_get_row_id( &id ),
351 : &out_feature
352 : );
353 :
354 0 : if ( read_error == U8_ERROR_NONE )
355 : {
356 0 : const data_feature_type_t feat_type = data_feature_get_main_type( &out_feature );
357 0 : const data_stat_table_t feat_or_lifeline
358 0 : = ( feat_type == DATA_FEATURE_TYPE_LIFELINE ) ? DATA_STAT_TABLE_LIFELINE : DATA_STAT_TABLE_FEATURE;
359 :
360 : /* intentionally not supported */
361 0 : U8_TRACE_INFO( "io_export_set_traversal_export_set does not copy single features, only complete classifiers." );
362 0 : data_stat_inc_count ( (*this_).export_stat, feat_or_lifeline, DATA_STAT_SERIES_IGNORED );
363 : }
364 : else
365 : {
366 : /* program internal error */
367 0 : U8_LOG_ERROR( "io_export_set_traversal_export_set could not read all data of the set." );
368 0 : data_stat_inc_count ( (*this_).export_stat, DATA_STAT_TABLE_FEATURE, DATA_STAT_SERIES_ERROR );
369 : }
370 :
371 0 : U8_TRACE_END_ERR(serialize_error);
372 0 : return serialize_error;
373 : }
374 :
375 0 : u8_error_t io_export_set_traversal_private_export_relationship( io_export_set_traversal_t *this_, data_id_t id )
376 : {
377 0 : U8_TRACE_BEGIN();
378 0 : assert( data_id_is_valid( &id ) );
379 0 : assert( DATA_TABLE_RELATIONSHIP == data_id_get_table( &id ) );
380 0 : u8_error_t serialize_error = U8_ERROR_NONE;
381 : u8_error_t read_error;
382 :
383 : data_relationship_t out_relation;
384 0 : read_error = data_database_reader_get_relationship_by_id ( (*this_).db_reader,
385 : data_id_get_row_id( &id ),
386 : &out_relation
387 : );
388 :
389 0 : if ( read_error == U8_ERROR_NONE )
390 : {
391 : data_classifier_t from_classifier;
392 : data_classifier_t to_classifier;
393 :
394 : /* get source */
395 0 : read_error |= data_database_reader_get_classifier_by_id ( (*this_).db_reader,
396 : data_relationship_get_from_classifier_row_id( &out_relation ),
397 : &from_classifier
398 : );
399 0 : if ( DATA_ROW_VOID == data_relationship_get_from_feature_row_id( &out_relation ) )
400 : {
401 0 : data_feature_init_empty( &((*this_).temp_feature_0) );
402 : }
403 : else
404 : {
405 0 : read_error |= data_database_reader_get_feature_by_id ( (*this_).db_reader,
406 : data_relationship_get_from_feature_row_id( &out_relation ),
407 : &((*this_).temp_feature_0)
408 : );
409 : }
410 :
411 : /* get destination */
412 0 : read_error |= data_database_reader_get_classifier_by_id ( (*this_).db_reader,
413 : data_relationship_get_to_classifier_row_id( &out_relation ),
414 : &to_classifier
415 : );
416 0 : if ( DATA_ROW_VOID == data_relationship_get_to_feature_row_id( &out_relation ) )
417 : {
418 0 : data_feature_init_empty( &((*this_).temp_feature_1) );
419 : }
420 : else
421 : {
422 0 : read_error |= data_database_reader_get_feature_by_id ( (*this_).db_reader,
423 : data_relationship_get_to_feature_row_id( &out_relation ),
424 : &((*this_).temp_feature_1)
425 : );
426 : }
427 :
428 : /* serialize */
429 0 : if ( read_error == U8_ERROR_NONE )
430 : {
431 : /* write relationship */
432 0 : serialize_error |= io_element_writer_start_relationship( (*this_).element_writer,
433 : DATA_CLASSIFIER_TYPE_VOID, /* host_type */
434 : &out_relation
435 : );
436 0 : serialize_error |= io_element_writer_assemble_relationship( (*this_).element_writer,
437 : NULL, /* host */
438 : &out_relation,
439 : &from_classifier,
440 0 : &((*this_).temp_feature_0),
441 : &to_classifier,
442 0 : &((*this_).temp_feature_1)
443 : );
444 0 : serialize_error |= io_element_writer_end_relationship( (*this_).element_writer,
445 : DATA_CLASSIFIER_TYPE_VOID, /* host_type */
446 : &out_relation
447 : );
448 : }
449 : else
450 : {
451 : /* program internal error */
452 0 : U8_LOG_ERROR( "io_export_set_traversal_export_set could not read referenced data of the set." );
453 0 : data_stat_inc_count ( (*this_).export_stat, DATA_STAT_TABLE_RELATIONSHIP, DATA_STAT_SERIES_ERROR );
454 : }
455 : }
456 : else
457 : {
458 : /* program internal error */
459 0 : U8_LOG_ERROR( "io_export_set_traversal_export_set could not read all data of the set." );
460 0 : data_stat_inc_count ( (*this_).export_stat, DATA_STAT_TABLE_RELATIONSHIP, DATA_STAT_SERIES_ERROR );
461 : }
462 :
463 0 : U8_TRACE_END_ERR(serialize_error);
464 0 : return serialize_error;
465 : }
466 :
467 :
468 : /*
469 : Copyright 2016-2024 Andreas Warnke
470 :
471 : Licensed under the Apache License, Version 2.0 (the "License");
472 : you may not use this file except in compliance with the License.
473 : You may obtain a copy of the License at
474 :
475 : http://www.apache.org/licenses/LICENSE-2.0
476 :
477 : Unless required by applicable law or agreed to in writing, software
478 : distributed under the License is distributed on an "AS IS" BASIS,
479 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
480 : See the License for the specific language governing permissions and
481 : limitations under the License.
482 : */
|