File: ctrl_multi_step_changer.c

package info (click to toggle)
crystal-facet-uml 1.63.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 21,640 kB
  • sloc: ansic: 109,610; xml: 3,085; makefile: 138; sh: 113
file content (704 lines) | stat: -rw-r--r-- 27,994 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
/* File: ctrl_multi_step_changer.c; Copyright and License: see below */

#include "ctrl_multi_step_changer.h"
#include "u8/u8_trace.h"
#include "u8/u8_log.h"
#include <assert.h>

void ctrl_multi_step_changer_init ( ctrl_multi_step_changer_t *this_,
                                    ctrl_controller_t *controller,
                                    data_database_reader_t *db_reader )
{
    U8_TRACE_BEGIN();
    assert( NULL != controller );
    assert( NULL != db_reader );

    /* init member attributes */
    (*this_).controller = controller;
    (*this_).db_reader = db_reader;

    (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_START_NEW;

    U8_TRACE_END();
}

void ctrl_multi_step_changer_destroy ( ctrl_multi_step_changer_t *this_ )
{
    U8_TRACE_BEGIN();
    assert( NULL != (*this_).controller );
    assert( NULL != (*this_).db_reader );

    /* destroy member attributes */
    (*this_).controller = NULL;
    (*this_).db_reader = NULL;

    U8_TRACE_END();
}

/* ================================ delete sets of elements ================================ */

u8_error_t ctrl_multi_step_changer_delete_set ( ctrl_multi_step_changer_t *this_,
                                                const data_small_set_t *objects,
                                                data_stat_t *io_stat )
{
    U8_TRACE_BEGIN();
    assert ( NULL != io_stat );
    u8_error_t result = U8_ERROR_NONE;

    if ( data_small_set_is_empty( objects ) )
    {
        result = U8_ERROR_INPUT_EMPTY;
    }
    else
    {
        int index;

        ctrl_classifier_controller_t *const classifier_ctrl = ctrl_controller_get_classifier_control_ptr( (*this_).controller);
        ctrl_diagram_controller_t *const diagram_ctrl = ctrl_controller_get_diagram_control_ptr( (*this_).controller );

        /* STEP ZERO: Delete all objects that can be immediately deleted */

        for ( index = 0; index < data_small_set_get_count( objects ); index ++ )
        {
            data_id_t current_id;
            current_id = data_small_set_get_id( objects, index );
            switch ( data_id_get_table( &current_id ) )
            {
                case DATA_TABLE_CLASSIFIER:
                {
                    /* see step two */
                }
                break;

                case DATA_TABLE_FEATURE:
                {
                    result |= ctrl_classifier_controller_delete_feature ( classifier_ctrl,
                                                                          data_id_get_row_id( &current_id ),
                                                                          (*this_).is_first_step
                                                                        );
                    if ( result == U8_ERROR_NONE )
                    {
                        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
                    }
                    else
                    {
                        data_stat_inc_count( io_stat, DATA_STAT_TABLE_FEATURE, DATA_STAT_SERIES_ERROR );
                    }
                }
                break;

                case DATA_TABLE_RELATIONSHIP:
                {
                    result |= ctrl_classifier_controller_delete_relationship ( classifier_ctrl,
                                                                               data_id_get_row_id( &current_id ),
                                                                               (*this_).is_first_step
                                                                             );
                    if ( result == U8_ERROR_NONE )
                    {
                        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
                    }
                    else
                    {
                        data_stat_inc_count( io_stat, DATA_STAT_TABLE_RELATIONSHIP, DATA_STAT_SERIES_ERROR );
                    }
                }
                break;

                case DATA_TABLE_DIAGRAMELEMENT:
                {
                    /* see step one */
                }
                break;

                case DATA_TABLE_DIAGRAM:
                {
                    /* see step two */
                }
                break;

                default:
                {
                    result |= U8_ERROR_VALUE_OUT_OF_RANGE;
                }
                break;
            }
        }

        /* STEP ONE: Delete all objects that can be deleted after relationships and features are gone */

        for ( index = 0; index < data_small_set_get_count( objects ); index ++ )
        {
            data_id_t current_id;
            current_id = data_small_set_get_id( objects, index );
            switch ( data_id_get_table( &current_id ) )
            {
                case DATA_TABLE_CLASSIFIER:
                {
                    /* see step two */
                }
                break;

                case DATA_TABLE_FEATURE:
                {
                    /* see step zero */
                }
                break;

                case DATA_TABLE_RELATIONSHIP:
                {
                    /* see step zero */
                }
                break;

                case DATA_TABLE_DIAGRAMELEMENT:
                {
                    result |= ctrl_diagram_controller_delete_diagramelement ( diagram_ctrl,
                                                                              data_id_get_row_id( &current_id ),
                                                                              (*this_).is_first_step
                                                                            );
                    if ( result == U8_ERROR_NONE )
                    {
                        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
                    }
                    else
                    {
                        data_stat_inc_count( io_stat, DATA_STAT_TABLE_DIAGRAMELEMENT, DATA_STAT_SERIES_ERROR );
                    }
                }
                break;

                case DATA_TABLE_DIAGRAM:
                {
                    /* see step two */
                }
                break;

                default:
                {
                    /* see step zero */
                }
                break;
            }
        }

        /* STEP TWO: Delete all objects that can be deleted after step one */

        for ( index = 0; index < data_small_set_get_count( objects ); index ++ )
        {
            data_id_t current_id;
            current_id = data_small_set_get_id( objects, index );
            switch ( data_id_get_table( &current_id ) )
            {
                case DATA_TABLE_CLASSIFIER:
                {
                    result |= ctrl_classifier_controller_delete_classifier( classifier_ctrl,
                                                                            data_id_get_row_id( &current_id ),
                                                                            (*this_).is_first_step
                                                                          );
                    if ( result == U8_ERROR_NONE )
                    {
                        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
                    }
                    else
                    {
                        data_stat_inc_count( io_stat, DATA_STAT_TABLE_CLASSIFIER, DATA_STAT_SERIES_ERROR );
                    }
                }
                break;

                case DATA_TABLE_FEATURE:
                {
                    /* see step zero */
                }
                break;

                case DATA_TABLE_RELATIONSHIP:
                {
                    /* see step zero */
                }
                break;

                case DATA_TABLE_DIAGRAMELEMENT:
                {
                    /* see step one */
                }
                break;

                case DATA_TABLE_DIAGRAM:
                {
                    result |= ctrl_diagram_controller_delete_diagram ( diagram_ctrl,
                                                                       data_id_get_row_id( &current_id ),
                                                                       (*this_).is_first_step
                                                                     );
                    if ( result == U8_ERROR_NONE )
                    {
                        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
                    }
                    else
                    {
                        data_stat_inc_count( io_stat, DATA_STAT_TABLE_DIAGRAM, DATA_STAT_SERIES_ERROR );
                    }
                }
                break;

                default:
                {
                    /* see step zero */
                }
                break;
            }
        }

        /* update statistics based on undo redo list */
        ctrl_undo_redo_iterator_t iter;
        ctrl_undo_redo_iterator_init_empty( &iter );
        result |= ctrl_controller_get_undo_iterator( (*this_).controller, &iter );
        ctrl_undo_redo_iterator_collect_statistics( &iter, false /* NOT categorize as undo */, io_stat );
        ctrl_undo_redo_iterator_destroy( &iter );
    }

    U8_TRACE_END_ERR( result );
    return result;
}

/* ================================ create elements without duplicate ids ================================ */

u8_error_t ctrl_multi_step_changer_create_diagram ( ctrl_multi_step_changer_t *this_,
                                                    data_diagram_t *new_diagram,
                                                    u8_error_t* out_info )
{
    U8_TRACE_BEGIN();
    assert( NULL != new_diagram );
    assert( NULL != out_info );
    u8_error_t result = U8_ERROR_NONE;

    /* ensure that a uuid exists */
    if ( 0 == utf8string_get_length( data_diagram_get_uuid_const( new_diagram ) ) )
    {
        data_uuid_t new_uuid;
        data_uuid_init_new( &new_uuid );
        data_diagram_set_uuid( new_diagram, data_uuid_get_string( &new_uuid ) );
        data_uuid_destroy( &new_uuid );
    }

    ctrl_diagram_controller_t *const diagram_ctrl
        = ctrl_controller_get_diagram_control_ptr( (*this_).controller );

    data_row_t new_diagram_id;
    const u8_error_t create_err
        = ctrl_diagram_controller_create_diagram( diagram_ctrl,
                                                  new_diagram,
                                                  (*this_).is_first_step,
                                                  &new_diagram_id
                                                );
    *out_info = create_err;
    if ( create_err == U8_ERROR_NONE )
    {
        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
        data_diagram_set_row_id( new_diagram, new_diagram_id );
    }
    else if ( u8_error_contains( create_err, U8_ERROR_DUPLICATE ) )
    {
        data_diagram_set_row_id( new_diagram, DATA_ROW_VOID );
        const u8_error_t alt_create_err
            = ctrl_diagram_controller_create_diagram( diagram_ctrl,
                                                      new_diagram,
                                                      (*this_).is_first_step,
                                                      &new_diagram_id
                                                    );
        if ( alt_create_err == U8_ERROR_NONE )
        {
            (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
            data_diagram_set_row_id( new_diagram, new_diagram_id );
            *out_info |= U8_ERROR_DUPLICATE_ID;
        }
        else
        {
            result = alt_create_err;
        }
    }
    else
    {
        result = create_err;
    }

    U8_TRACE_END_ERR( result );
    return result;
}

u8_error_t ctrl_multi_step_changer_create_diagramelement ( ctrl_multi_step_changer_t *this_,
                                                           data_diagramelement_t *new_diagramelement,
                                                           u8_error_t* out_info )
{
    U8_TRACE_BEGIN();
    assert( NULL != new_diagramelement );
    assert( NULL != out_info );
    u8_error_t result = U8_ERROR_NONE;

    /* ensure that a uuid exists */
    if ( 0 == utf8string_get_length( data_diagramelement_get_uuid_const( new_diagramelement ) ) )
    {
        data_uuid_t new_uuid;
        data_uuid_init_new( &new_uuid );
        data_diagramelement_set_uuid( new_diagramelement, data_uuid_get_string( &new_uuid ) );
        data_uuid_destroy( &new_uuid );
    }

    ctrl_diagram_controller_t *const diagram_ctrl
        = ctrl_controller_get_diagram_control_ptr( (*this_).controller );

    data_row_t new_diagramelement_id;
    const u8_error_t create_err
        = ctrl_diagram_controller_create_diagramelement( diagram_ctrl,
                                                         new_diagramelement,
                                                         (*this_).is_first_step,
                                                         &new_diagramelement_id
                                                       );
    *out_info = create_err;
    if ( create_err == U8_ERROR_NONE )
    {
        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
        data_diagramelement_set_row_id( new_diagramelement, new_diagramelement_id );
    }
    else if ( u8_error_contains( create_err, U8_ERROR_DUPLICATE ) )
    {
        data_diagramelement_set_row_id( new_diagramelement, DATA_ROW_VOID );
        const u8_error_t alt_create_err
            = ctrl_diagram_controller_create_diagramelement( diagram_ctrl,
                                                             new_diagramelement,
                                                             (*this_).is_first_step,
                                                             &new_diagramelement_id
                                                           );
        if ( alt_create_err == U8_ERROR_NONE )
        {
            (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
            data_diagramelement_set_row_id( new_diagramelement, new_diagramelement_id );
            *out_info |= U8_ERROR_DUPLICATE_ID;
        }
        else
        {
            result = alt_create_err;
        }
    }
    else
    {
        result = create_err;
    }

    U8_TRACE_END_ERR( result );
    return result;
}

u8_error_t ctrl_multi_step_changer_create_classifier ( ctrl_multi_step_changer_t *this_,
                                                       data_classifier_t *new_classifier,
                                                       u8_error_t* out_info )
{
    U8_TRACE_BEGIN();
    assert( NULL != new_classifier );
    assert( NULL != out_info );
    u8_error_t result = U8_ERROR_NONE;

    /* ensure that a uuid exists */
    if ( 0 == utf8string_get_length( data_classifier_get_uuid_const( new_classifier ) ) )
    {
        data_uuid_t new_uuid;
        data_uuid_init_new( &new_uuid );
        data_classifier_set_uuid( new_classifier, data_uuid_get_string( &new_uuid ) );
        data_uuid_destroy( &new_uuid );
    }

    ctrl_classifier_controller_t *const classifier_ctrl
        = ctrl_controller_get_classifier_control_ptr( (*this_).controller);

    data_row_t new_classifier_id;
    const u8_error_t create_err
        = ctrl_classifier_controller_create_classifier( classifier_ctrl,
                                                        new_classifier,
                                                        (*this_).is_first_step,
                                                        &new_classifier_id
                                                      );
    *out_info = create_err;
    if ( create_err == U8_ERROR_NONE )
    {
        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
        data_classifier_set_row_id( new_classifier, new_classifier_id );
    }
    else if ( u8_error_contains( create_err, U8_ERROR_DUPLICATE ) )
    {
        data_classifier_set_row_id( new_classifier, DATA_ROW_VOID );
        const u8_error_t alt_create_err
            = ctrl_classifier_controller_create_classifier( classifier_ctrl,
                                                            new_classifier,
                                                            (*this_).is_first_step,
                                                            &new_classifier_id
                                                          );
        if ( alt_create_err == U8_ERROR_NONE )
        {
            (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
            data_classifier_set_row_id( new_classifier, new_classifier_id );
            *out_info |= U8_ERROR_DUPLICATE_ID;
        }
        else
        {
            result = alt_create_err;
        }
    }
    else
    {
        result = create_err;
    }

    if ( u8_error_contains( result, U8_ERROR_DUPLICATE ) )
    {
        *out_info |= result;
        result = U8_ERROR_NONE;

        /* find an alternative, unused name */
        char wish_name_buf[DATA_CLASSIFIER_MAX_NAME_SIZE];
        utf8stringbuf_t wish_name = UTF8STRINGBUF( wish_name_buf );
        result |= utf8stringbuf_copy_str( &wish_name, data_classifier_get_name_const( new_classifier ) );  /* error to be reported to caller */
        {
            bool name_ok = false;
            static const uint_fast16_t MAX_SEARCH_STEP = 10000;
            static const uint_fast16_t FIRST_STEP = 2;
            for ( uint_fast16_t search_step = FIRST_STEP; ( search_step < MAX_SEARCH_STEP )&&( ! name_ok )&&( result == U8_ERROR_NONE ); search_step ++ )
            {
                char new_name_buf[DATA_CLASSIFIER_MAX_NAME_SIZE];
                utf8stringbuf_t new_name = UTF8STRINGBUF( new_name_buf );
                const u8_error_t trunc_err
                    = ctrl_multi_step_changer_private_propose_classifier_name( this_,
                                                                               utf8stringbuf_get_string( &wish_name ),
                                                                               search_step,
                                                                               new_name
                                                                             );
                if ( trunc_err != U8_ERROR_NONE )
                {
                    U8_TRACE_INFO_STR("Name truncated at search for alternative:", utf8stringbuf_get_string( &new_name ) );
                }
                data_classifier_set_name( new_classifier, utf8stringbuf_get_string( &new_name ) );
                const u8_error_t retry_err
                    = ctrl_classifier_controller_create_classifier( classifier_ctrl,
                                                                    new_classifier,
                                                                    (*this_).is_first_step,
                                                                    &new_classifier_id
                                                                  );
                if ( u8_error_contains( retry_err, U8_ERROR_DUPLICATE ) )
                {
                    *out_info |= retry_err;
                }
                else if ( retry_err == U8_ERROR_NONE )
                {
                    name_ok = true;  /* name unused */
                    (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
                    data_classifier_set_row_id( new_classifier, new_classifier_id );
                    *out_info |= U8_ERROR_DUPLICATE_NAME;
                }
                else
                {
                    result |= retry_err;
                }
            }
        }
    }

    U8_TRACE_END_ERR( result );
    return result;
}

u8_error_t ctrl_multi_step_changer_create_feature ( ctrl_multi_step_changer_t *this_,
                                                    data_feature_t *new_feature,
                                                    u8_error_t* out_info )
{
    U8_TRACE_BEGIN();
    assert( NULL != new_feature );
    assert( NULL != out_info );
    u8_error_t result = U8_ERROR_NONE;

    /* ensure that a uuid exists */
    if ( 0 == utf8string_get_length( data_feature_get_uuid_const( new_feature ) ) )
    {
        data_uuid_t new_uuid;
        data_uuid_init_new( &new_uuid );
        data_feature_set_uuid( new_feature, data_uuid_get_string( &new_uuid ) );
        data_uuid_destroy( &new_uuid );
    }

    ctrl_classifier_controller_t *const classifier_ctrl
        = ctrl_controller_get_classifier_control_ptr( (*this_).controller);

    data_row_t new_feature_id;
    const u8_error_t create_err
        = ctrl_classifier_controller_create_feature( classifier_ctrl,
                                                     new_feature,
                                                     (*this_).is_first_step,
                                                     &new_feature_id
                                                   );
    *out_info = create_err;
    if ( create_err == U8_ERROR_NONE )
    {
        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
        data_feature_set_row_id( new_feature, new_feature_id );
    }
    else if ( u8_error_contains( create_err, U8_ERROR_DUPLICATE) )
    {
        data_feature_set_row_id( new_feature, DATA_ROW_VOID );
        const u8_error_t alt_create_err
            = ctrl_classifier_controller_create_feature( classifier_ctrl,
                                                         new_feature,
                                                         (*this_).is_first_step,
                                                         &new_feature_id
                                                       );
        if ( alt_create_err == U8_ERROR_NONE )
        {
            (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
            data_feature_set_row_id( new_feature, new_feature_id );
            *out_info |= U8_ERROR_DUPLICATE_ID;
        }
        else
        {
            result = alt_create_err;
        }
    }
    else
    {
        result = create_err;
    }

    U8_TRACE_END_ERR( result );
    return result;
}

u8_error_t ctrl_multi_step_changer_create_relationship ( ctrl_multi_step_changer_t *this_,
                                                         data_relationship_t *new_relationship,
                                                         u8_error_t* out_info )
{
    U8_TRACE_BEGIN();
    assert( NULL != new_relationship );
    assert( NULL != out_info );
    u8_error_t result = U8_ERROR_NONE;

    /* ensure that a uuid exists */
    if ( 0 == utf8string_get_length( data_relationship_get_uuid_const( new_relationship ) ) )
    {
        data_uuid_t new_uuid;
        data_uuid_init_new( &new_uuid );
        data_relationship_set_uuid( new_relationship, data_uuid_get_string( &new_uuid ) );
        data_uuid_destroy( &new_uuid );
    }

    ctrl_classifier_controller_t *const classifier_ctrl
        = ctrl_controller_get_classifier_control_ptr( (*this_).controller);

    data_row_t new_relationship_id;
    const u8_error_t create_err
        = ctrl_classifier_controller_create_relationship( classifier_ctrl,
                                                          new_relationship,
                                                          (*this_).is_first_step,
                                                          &new_relationship_id
                                                        );
    *out_info = create_err;
    if ( create_err == U8_ERROR_NONE )
    {
        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
        data_relationship_set_row_id( new_relationship, new_relationship_id );
    }
    else if ( u8_error_contains( create_err, U8_ERROR_DUPLICATE) )
    {
        data_relationship_set_row_id( new_relationship, DATA_ROW_VOID );
        const u8_error_t alt_create_err
            = ctrl_classifier_controller_create_relationship( classifier_ctrl,
                                                              new_relationship,
                                                              (*this_).is_first_step,
                                                              &new_relationship_id
                                                            );
        if ( alt_create_err == U8_ERROR_NONE )
        {
            (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
            data_relationship_set_row_id( new_relationship, new_relationship_id );
            *out_info |= U8_ERROR_DUPLICATE_ID;
        }
        else
        {
            result = alt_create_err;
        }
    }
    else
    {
        result = create_err;
    }

    U8_TRACE_END_ERR( result );
    return result;
}

/* ================================ update links of existing elements  ================================ */

u8_error_t ctrl_multi_step_changer_update_diagram_parent_id ( ctrl_multi_step_changer_t *this_,
                                                              data_row_t diagram_id,
                                                              data_row_t new_diagram_parent_id )
{
    U8_TRACE_BEGIN();
    u8_error_t result = U8_ERROR_NONE;

    ctrl_diagram_controller_t *const diagram_ctrl
        = ctrl_controller_get_diagram_control_ptr( (*this_).controller );

    result = ctrl_diagram_controller_update_diagram_parent_id( diagram_ctrl,
                                                               diagram_id,
                                                               new_diagram_parent_id,
                                                               (*this_).is_first_step
                                                             );
    if ( result == U8_ERROR_NONE )
    {
        (*this_).is_first_step = CTRL_UNDO_REDO_ACTION_BOUNDARY_APPEND;
    }

    U8_TRACE_END_ERR( result );
    return result;
}

/* ================================ propose names of classifiers ================================ */

u8_error_t ctrl_multi_step_changer_private_propose_classifier_name ( ctrl_multi_step_changer_t *this_,
                                                                     const char* base_classifier_name,
                                                                     uint_fast16_t iteration,
                                                                     utf8stringbuf_t out_name )
{
    U8_TRACE_BEGIN();
    const size_t RESERVED_FOR_NUMBER = 5;
    assert( NULL != base_classifier_name );
    assert( utf8stringbuf_get_size( &out_name ) > RESERVED_FOR_NUMBER );
    u8_error_t result = U8_ERROR_NONE;

    /* find an alternative, unused name */
    /* copy the base_classifier_name to newname_buf */
    {
        utf8stringbuf_t shortened_new_name
            = utf8stringbuf_new( utf8stringbuf_get_string( &out_name ), utf8stringbuf_get_size( &out_name ) - RESERVED_FOR_NUMBER );
        result |= utf8stringbuf_copy_str( &shortened_new_name, base_classifier_name );
        /* null termination is guaranteed, also this function does not cut an utf8 code point in the middle. */
    }
    /* append a separator and the iteration number */
    result |= utf8stringbuf_append_str( &out_name, "-" );
    result |= utf8stringbuf_append_int( &out_name, iteration );

    U8_TRACE_END_ERR( result );
    return result;
}


/*
Copyright 2016-2025 Andreas Warnke

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/