source: branches/help/MERGE/MG_names.cxx

Last change on this file was 16763, checked in by westram, 7 years ago
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : MG_names.cxx                                      //
4//   Purpose   :                                                   //
5//                                                                 //
6//   Institute of Microbiology (Technical University Munich)       //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "merge.hxx"
12
13#include <AW_rename.hxx>
14#include <aw_root.hxx>
15#include <aw_awar.hxx>
16#include <aw_msg.hxx>
17#include <aw_window.hxx>
18#include <arb_progress.h>
19
20// --------------------------------------------------------------------------------
21
22#define AWAR_ADDID_SRC  AWAR_MERGE_TMP_SRC "addid"
23#define AWAR_ADDID_DST  AWAR_MERGE_TMP_DST "addid"
24
25#define AWAR_ADDID_MATCH   AWAR_MERGE_TMP "addidmatch"
26#define AWAR_RENAME_STATUS AWAR_MERGE_TMP "renamestat"
27#define AWAR_ALLOW_DUPS    AWAR_MERGE_TMP "allowdups"
28#define AWAR_OVERRIDE      AWAR_MERGE_TMP "override"
29
30// --------------------------------------------------------------------------------
31
32void MG_create_rename_awars(AW_root *aw_root, AW_default aw_def) {
33    aw_root->awar_string(AWAR_ADDID_MATCH, "", aw_def);
34    aw_root->awar_string(AWAR_RENAME_STATUS, "", aw_def);
35    aw_root->awar_int(AWAR_ALLOW_DUPS, 0, aw_def);
36    aw_root->awar_int(AWAR_OVERRIDE, 0, aw_def);
37}
38
39// --------------------------------------------------------------------------------
40
41static const char *addids_match_info(AW_root *aw_root) {
42    char *src_addid = aw_root->awar(AWAR_ADDID_SRC)->read_string();
43    char *dst_addid = aw_root->awar(AWAR_ADDID_DST)->read_string();
44
45    const char *result = (strcmp(src_addid, dst_addid) == 0) ? "Ok" : "MISMATCH!";
46
47    free(dst_addid);
48    free(src_addid);
49
50    return result;
51}
52
53static void addids_match_info_refresh_cb(AW_root *aw_root) {
54    aw_root->awar(AWAR_ADDID_MATCH)->write_string(addids_match_info(aw_root));
55    MG_set_renamed(false, aw_root, "Needed (add.field changed)");
56}
57
58void MG_create_db_dependent_rename_awars(AW_root *aw_root, GBDATA *gb_src, GBDATA *gb_dst) {
59    static bool created = false;
60
61    if (!created) {
62        GB_transaction src_ta(gb_src);
63        GB_transaction dst_ta(gb_dst);
64        GB_ERROR       error = NULp;
65
66        // Awars for additional ID need to be mapped, cause they use same db-path in both DBs
67
68        GBDATA *gb_src_addid = GB_search(gb_src, AWAR_NAMESERVER_ADDID, GB_STRING);
69        GBDATA *gb_dst_addid = GB_search(gb_dst, AWAR_NAMESERVER_ADDID, GB_STRING);
70
71        const char *src_addid = gb_src_addid ? GB_read_char_pntr(gb_src_addid) : "";
72        const char *dst_addid = gb_dst_addid ? GB_read_char_pntr(gb_dst_addid) : "";
73
74        // use other as default (needed e.g. for import)
75        if (gb_src_addid && !gb_dst_addid) {
76            gb_dst_addid             = GB_create(gb_dst, AWAR_NAMESERVER_ADDID, GB_STRING);
77            if (!gb_dst_addid) error = GB_await_error();
78            else error               = GB_write_string(gb_dst_addid, src_addid);
79        }
80        else if (!gb_src_addid && gb_dst_addid) {
81            gb_src_addid             = GB_create(gb_src, AWAR_NAMESERVER_ADDID, GB_STRING);
82            if (!gb_src_addid) error = GB_await_error();
83            else error               = GB_write_string(gb_src_addid, dst_addid);
84        }
85
86        if (!error) {
87            AW_awar *awar_src_addid = aw_root->awar_string(AWAR_ADDID_SRC, "xxx", gb_src);
88            AW_awar *awar_dst_addid = aw_root->awar_string(AWAR_ADDID_DST, "xxx", gb_dst);
89
90            awar_src_addid->unmap(); awar_src_addid->map(gb_src_addid);
91            awar_dst_addid->unmap(); awar_dst_addid->map(gb_dst_addid);
92
93            awar_src_addid->add_callback(addids_match_info_refresh_cb);
94            awar_dst_addid->add_callback(addids_match_info_refresh_cb);
95
96            addids_match_info_refresh_cb(aw_root);
97        }
98
99        if (error) {
100            error = src_ta.close(error);
101            error = dst_ta.close(error);
102            aw_message(error);
103        }
104        else {
105        created = true;
106    }
107}
108}
109
110// --------------------------------------------------------------------------------
111
112static bool was_renamed = false;
113
114void MG_set_renamed(bool renamed, AW_root *aw_root, const char *reason) {
115    aw_root->awar(AWAR_RENAME_STATUS)->write_string(reason);
116    was_renamed = renamed;
117}
118
119GB_ERROR MG_expect_renamed() {
120    GB_ERROR error = NULp;
121    if (!was_renamed) error = "First you have to synchronize species IDs in both databases";
122    return error;
123}
124
125// --------------------------------------------------------------------------------
126
127static GB_ERROR renameDB(const char *which, GBDATA *gb_db, bool allowDups) {
128    arb_progress progress(GBS_global_string("Generating new names in %s database", which));
129    bool         isDuplicatesWarning;
130    GB_ERROR     error = AWTC_pars_names(gb_db, &isDuplicatesWarning);
131
132    if (error) {
133        error = GBS_global_string("While renaming %s DB:\n%s", which, error);
134        if (isDuplicatesWarning && allowDups) {
135            aw_message(error);
136            aw_message("Duplicates error ignored. Be careful during merge!");
137            error = NULp;
138        }
139    }
140
141    return error;
142}
143
144static void rename_both_databases(AW_window *aww) {
145    GB_ERROR  error     = NULp;
146    AW_root  *aw_root   = aww->get_root();
147    char     *match     = aw_root->awar(AWAR_ADDID_MATCH)->read_string();
148    bool      allowDups = aw_root->awar(AWAR_ALLOW_DUPS)->read_int();
149
150    if (strcmp(match, "Ok") == 0) {
151        error = renameDB("source", GLOBAL_gb_src, allowDups);
152        if (!error) error = renameDB("destination", GLOBAL_gb_dst, allowDups);
153    }
154    else {
155        error = "Denying rename - additional fields have to match!";
156    }
157    free(match);
158
159    if (error) {
160        aw_message(error);
161        MG_set_renamed(false, aw_root, "Failed");
162    }
163    else {
164        MG_set_renamed(true, aw_root, "Ok");
165    }
166}
167
168static void override_toggle_cb(AW_root *aw_root) {
169    bool override = aw_root->awar(AWAR_OVERRIDE)->read_int();
170    MG_set_renamed(override, aw_root, override ? "Overridden" : "Not renamed");
171}
172
173AW_window *MG_create_merge_names_window(AW_root *awr) {
174    AW_window_simple *aws = new AW_window_simple;
175
176    aws->init(awr, "MERGE_AUTORENAME_SPECIES", "Synchronize IDs");
177    aws->load_xfig("merge/names.fig");
178
179    aws->at("close");
180    aws->callback(AW_POPDOWN);
181    aws->create_button("CLOSE", "CLOSE", "C");
182
183    aws->at("help");
184    aws->callback(makeHelpCallback("mg_names.hlp"));
185    aws->create_button("HELP", "HELP", "H");
186
187    aws->at("addid1");
188    aws->create_input_field(AWAR_ADDID_SRC, 10);
189
190    aws->at("addid2");
191    aws->create_input_field(AWAR_ADDID_DST, 10);
192
193    aws->at("dups");
194    aws->label("Allow merging duplicates (dangerous! see HELP)");
195    aws->create_toggle(AWAR_ALLOW_DUPS);
196
197    aws->at("override");
198    aws->label("Override (even more dangerous! see HELP)");
199    awr->awar(AWAR_OVERRIDE)->add_callback(override_toggle_cb);
200    aws->create_toggle(AWAR_OVERRIDE);
201
202    aws->at("match");
203    aws->button_length(12);
204    aws->create_button(NULp, AWAR_ADDID_MATCH, NULp, "+");
205
206    aws->at("status");
207    aws->button_length(25);
208    aws->label("Status:");
209    aws->create_button(NULp, AWAR_RENAME_STATUS, NULp, "+");
210
211    aws->at("rename");
212    aws->callback(rename_both_databases);
213    aws->create_autosize_button("RENAME_DATABASES", "Synchronize");
214
215    aws->button_length(0);
216    aws->shadow_width(1);
217    aws->at("icon");
218    aws->callback(makeHelpCallback("mg_names.hlp"));
219    aws->create_button("HELP_MERGE", "#merge/icon.xpm");
220
221    return aws;
222}
Note: See TracBrowser for help on using the repository browser.