File: gui_search_runner.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 (345 lines) | stat: -rw-r--r-- 16,287 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
/* File: gui_search_runner.c; Copyright and License: see below */

#include "gui_search_runner.h"
#include "set/data_search_result_list.h"
#include "u8/u8_trace.h"
#include "u8/u8_log.h"
#include <assert.h>

void gui_search_runner_init ( gui_search_runner_t *this_,
                              gui_simple_message_to_user_t *message_to_user,
                              data_database_reader_t *db_reader,
                              data_database_t *database,
                              gui_sketch_area_t *result_consumer )
{
    U8_TRACE_BEGIN();
    assert ( message_to_user != NULL );
    assert ( db_reader != NULL );
    assert ( database != NULL );
    assert ( result_consumer != NULL );

    (*this_).message_to_user = message_to_user;
    (*this_).db_reader = db_reader;
    const u8_error_t d_err = data_database_text_search_init ( &((*this_).db_searcher), database );
    if ( U8_ERROR_NONE != d_err )
    {
        U8_LOG_WARNING_HEX( "data_database_text_search_t could not be constructed.", d_err );
    }
    (*this_).result_consumer = result_consumer;
    DATA_SEARCH_RESULT_LIST_INIT( &((*this_).temp_result_list), (*this_).temp_result_list_buf );

    U8_TRACE_END();
}

void gui_search_runner_destroy ( gui_search_runner_t *this_ )
{
    U8_TRACE_BEGIN();

    (*this_).message_to_user = NULL;
    (*this_).db_reader = NULL;
    const u8_error_t d_err = data_database_text_search_destroy ( &((*this_).db_searcher) );
    if ( U8_ERROR_NONE != d_err )
    {
        U8_LOG_WARNING_HEX( "data_database_text_search_t could not be destructed.", d_err );
    }
    (*this_).result_consumer = NULL;

    U8_TRACE_END();
}

void gui_search_runner_run ( gui_search_runner_t *this_, const char* search_string )
{
    U8_TRACE_BEGIN();

    if ( search_string != NULL )
    {
        data_id_t search_id;
        data_id_init_by_string ( &search_id, search_string );
        data_id_trace ( &search_id );

        gui_simple_message_to_user_hide( (*this_).message_to_user );

        data_search_result_list_clear( &((*this_).temp_result_list) );
        const data_row_t search_row_id = data_id_get_row_id(&search_id);
        u8_error_t d_err = U8_ERROR_NONE;

        if ( data_id_is_valid( &search_id ))
        {
            switch ( data_id_get_table(&search_id) )
            {
                case DATA_TABLE_CLASSIFIER:
                {
                    d_err = data_database_reader_get_classifier_by_id ( (*this_).db_reader,
                                                                        search_row_id,
                                                                        &((*this_).temp_classifier)
                                                                      );
                    if ( d_err == U8_ERROR_NONE )
                    {
                        data_search_result_t half_initialized;
                        data_search_result_init_classifier( &half_initialized,
                                                            search_row_id,
                                                            data_classifier_get_main_type( &((*this_).temp_classifier) ),
                                                            data_classifier_get_name_const( &((*this_).temp_classifier) ),
                                                            DATA_ROW_VOID /* diagram_id */
                                                        );
                        gui_search_runner_private_add_diagrams_of_classifier( this_, &half_initialized, &((*this_).temp_result_list) );

                        data_classifier_destroy( &((*this_).temp_classifier) );
                        data_search_result_destroy( &half_initialized );
                    }
                    else
                    {
                        U8_TRACE_INFO( "classifier does not exist or database not open." );
                    }
                }
                break;

                case DATA_TABLE_FEATURE:
                {
                    d_err = data_database_reader_get_feature_by_id ( (*this_).db_reader,
                                                                     search_row_id,
                                                                     &((*this_).temp_feature)
                                                                   );
                    if ( d_err == U8_ERROR_NONE )
                    {
                        data_row_t classifier_id = data_feature_get_classifier_row_id( &((*this_).temp_feature) );
                        data_search_result_t half_initialized;
                        data_search_result_init_feature( &half_initialized,
                                                         data_feature_get_row_id( &((*this_).temp_feature) ),
                                                         data_feature_get_main_type( &((*this_).temp_feature) ),
                                                         data_feature_get_key_const( &((*this_).temp_feature) ),
                                                         classifier_id,
                                                         DATA_ROW_VOID /* diagram_id */
                                                       );
                        gui_search_runner_private_add_diagrams_of_classifier( this_, &half_initialized, &((*this_).temp_result_list) );

                        data_feature_destroy( &((*this_).temp_feature) );
                        data_search_result_destroy( &half_initialized );
                    }
                    else
                    {
                        U8_TRACE_INFO( "feature does not exist or database not open." );
                    }
                }
                break;

                case DATA_TABLE_RELATIONSHIP:
                {
                    d_err = data_database_reader_get_relationship_by_id ( (*this_).db_reader,
                                                                          search_row_id,
                                                                          &((*this_).temp_relationship)
                                                                        );
                    if ( d_err == U8_ERROR_NONE )
                    {
                        data_row_t classifier_id = data_relationship_get_from_classifier_row_id( &((*this_).temp_relationship) );
                        data_search_result_t half_initialized;
                        data_search_result_init_relationship( &half_initialized,
                                                              data_relationship_get_row_id( &((*this_).temp_relationship) ),
                                                              data_relationship_get_main_type( &((*this_).temp_relationship) ),
                                                              data_relationship_get_name_const( &((*this_).temp_relationship) ),
                                                              classifier_id,
                                                              data_relationship_get_to_classifier_row_id( &((*this_).temp_relationship) ),
                                                              DATA_ROW_VOID /* diagram_id */
                                                            );
                        gui_search_runner_private_add_diagrams_of_classifier( this_, &half_initialized, &((*this_).temp_result_list) );

                        data_relationship_destroy( &((*this_).temp_relationship) );
                        data_search_result_destroy( &half_initialized );
                    }
                    else
                    {
                        U8_TRACE_INFO( "relationship does not exist or database not open." );
                    }
                }
                break;

                case DATA_TABLE_DIAGRAMELEMENT:
                {
                    d_err = data_database_reader_get_diagramelement_by_id ( (*this_).db_reader,
                                                                            search_row_id,
                                                                            &((*this_).temp_diagramelement)
                                                                          );
                    if ( d_err == U8_ERROR_NONE )
                    {
                        data_search_result_t half_initialized;
                        data_search_result_init_classifier( &half_initialized,
                                                            data_diagramelement_get_classifier_row_id(&((*this_).temp_diagramelement)),
                                                            0 /* match_type is unknown */,
                                                            "" /* match_name */,
                                                            data_diagramelement_get_diagram_row_id(&((*this_).temp_diagramelement))
                                                          );
                        const u8_error_t err = data_search_result_list_add( &((*this_).temp_result_list), &half_initialized );
                        if ( err != U8_ERROR_NONE )
                        {
                            /*d_err = U8_ERROR_ARRAY_BUFFER_EXCEEDED;*/
                            U8_LOG_WARNING( "U8_ERROR_ARRAY_BUFFER_EXCEEDED at inserting search result to list" );
                        }

                        data_diagramelement_destroy( &((*this_).temp_diagramelement) );
                        data_search_result_destroy( &half_initialized );
                    }
                    else
                    {
                        U8_TRACE_INFO( "diagramelement does not exist or database not open." );
                    }
                }
                break;

                case DATA_TABLE_DIAGRAM:
                {
                    d_err = data_database_reader_get_diagram_by_id ( (*this_).db_reader, search_row_id, &((*this_).temp_diagram) );
                    if ( d_err == U8_ERROR_NONE )
                    {
                        data_search_result_t half_initialized;
                        data_search_result_init_diagram( &half_initialized,
                                                         search_row_id,
                                                         data_diagram_get_diagram_type( &((*this_).temp_diagram) ),
                                                         data_diagram_get_name_const( &((*this_).temp_diagram) )
                                                       );
                        const u8_error_t err = data_search_result_list_add( &((*this_).temp_result_list), &half_initialized );
                        if ( err != U8_ERROR_NONE )
                        {
                            /*d_err = U8_ERROR_ARRAY_BUFFER_EXCEEDED;*/
                            U8_LOG_WARNING( "U8_ERROR_ARRAY_BUFFER_EXCEEDED at inserting search result to list" );
                        }

                        data_diagram_destroy( &((*this_).temp_diagram) );
                        data_search_result_destroy( &half_initialized );
                    }
                    else
                    {
                        U8_TRACE_INFO( "diagram does not exist or database not open." );
                    }
                }
                break;

                default:
                {
                    assert(false);  /* data_id_is_valid should have been false already */
                }
                break;
            }
        }
        else
        {
            U8_LOG_EVENT_STR( "User search input is not an id", search_string );
        }

        /* free text search */
        d_err = data_database_text_search_get_objects_by_textfragment ( &((*this_).db_searcher),
                                                                        search_string,
                                                                        &((*this_).temp_result_list)
                                                                      );
        if ( U8_ERROR_NONE != d_err )
        {
            U8_LOG_ERROR_HEX( "data_database_text_search_t could not search.", d_err );
        }

        gui_sketch_area_show_result_list ( (*this_).result_consumer, &((*this_).temp_result_list) );
        data_search_result_list_clear( &((*this_).temp_result_list) );
    }
    else
    {
        assert(false);
    }

    U8_TRACE_END();
}

void gui_search_runner_private_add_diagrams_of_classifier ( gui_search_runner_t *this_,
                                                            data_search_result_t *classifier_template,
                                                            data_search_result_list_t *io_list
                                                          )
{
    U8_TRACE_BEGIN();
    assert( classifier_template != NULL );
    assert( io_list != NULL );
    u8_error_t d_err = U8_ERROR_NONE;

    data_row_t classifier_row_id;
    if ( DATA_TABLE_CLASSIFIER == data_id_get_table( data_search_result_get_match_id_const( classifier_template )))
    {
        classifier_row_id = data_id_get_row_id( data_search_result_get_match_id_const( classifier_template ));
    }
    else
    {
        classifier_row_id = data_id_get_row_id( data_search_result_get_src_classifier_id_const( classifier_template ));
    }

    data_diagram_iterator_t diagram_iterator;
    d_err |= data_diagram_iterator_init_empty( &diagram_iterator );
    d_err |= data_database_reader_get_diagrams_by_classifier_id( (*this_).db_reader,
                                                                classifier_row_id,
                                                                &diagram_iterator
                                                              );

    if ( d_err == U8_ERROR_NONE )
    {
        while ( data_diagram_iterator_has_next( &diagram_iterator ) )
        {
            d_err |= data_diagram_iterator_next( &diagram_iterator, &((*this_).temp_diagram) );
            const data_row_t diagram_row_id = data_diagram_get_row_id( &((*this_).temp_diagram) );
            data_id_reinit( data_search_result_get_diagram_id_ptr( classifier_template ), DATA_TABLE_DIAGRAM, diagram_row_id );

            bool filter = false;
            switch ( data_id_get_table( data_search_result_get_match_id_const( classifier_template ) ) )
            {
                case DATA_TABLE_FEATURE:
                {
                    /* if a user searches explicitly for a feature-id, which feature/classifiers should be filtered? */
                    /* and how? till here, the classifier type is not yet loaded. */
                }
                break;

                case DATA_TABLE_RELATIONSHIP:
                {
                    /* if a user searches explicitly for a relationship-id, which ones should be filtered? */
                    /* and how? till here, the classifier type is not yet loaded. */
                }
                break;

                default:
                {
                    /* do not filter classifiers (or other things?) */
                }
                break;
            }

            if ( ! filter )
            {
                const u8_error_t err = data_search_result_list_add( io_list, classifier_template );
                if ( err != U8_ERROR_NONE )
                {
                    /*d_err |= U8_ERROR_ARRAY_BUFFER_EXCEEDED;*/
                    U8_LOG_WARNING( "U8_ERROR_ARRAY_BUFFER_EXCEEDED at inserting search result to list" );
                }
            }

            data_diagram_destroy( &((*this_).temp_diagram) );
        }
    }
    else
    {
        U8_TRACE_INFO( "diagram does not exist or database not open." );
    }
    d_err |= data_diagram_iterator_destroy( &diagram_iterator );

    U8_TRACE_END();
}


/*
Copyright 2020-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.
*/