ARB
MG_names.cxx
Go to the documentation of this file.
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 
32 void 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 
41 static 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 
53 static void addids_match_info_refresh_cb(AW_root *aw_root) {
55  MG_set_renamed(false, aw_root, "Needed (add.field changed)");
56 }
57 
58 void 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);
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 
95 
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 
112 static bool was_renamed = false;
113 
114 void 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 
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 
127 static 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 
144 static 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 
168 static 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 
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)");
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 }
#define AWAR_ADDID_SRC
Definition: MG_names.cxx:22
const char * GB_ERROR
Definition: arb_core.h:25
static void override_toggle_cb(AW_root *aw_root)
Definition: MG_names.cxx:168
string result
#define AWAR_NAMESERVER_ADDID
Definition: AW_rename.hxx:21
#define AWAR_ALLOW_DUPS
Definition: MG_names.cxx:27
GB_ERROR GB_write_string(GBDATA *gbd, const char *s)
Definition: arbdb.cxx:1387
void load_xfig(const char *file, bool resize=true)
Definition: AW_window.cxx:720
AW_window * MG_create_merge_names_window(AW_root *awr)
Definition: MG_names.cxx:173
long read_int() const
Definition: AW_awar.cxx:184
static bool was_renamed
Definition: MG_names.cxx:112
const char * GBS_global_string(const char *templat,...)
Definition: arb_msg.cxx:203
GB_ERROR AWTC_pars_names(GBDATA *gb_main, bool *isWarningPtr)
Definition: AW_rename.cxx:465
void AW_POPDOWN(AW_window *window)
Definition: AW_window.cxx:52
#define AWAR_ADDID_DST
Definition: MG_names.cxx:23
#define AWAR_OVERRIDE
Definition: MG_names.cxx:28
GBDATA * GLOBAL_gb_dst
Definition: MG_main.cxx:32
AW_awar * add_callback(const RootCallback &cb)
Definition: AW_awar.cxx:231
static const char * addids_match_info(AW_root *aw_root)
Definition: MG_names.cxx:41
GB_ERROR GB_await_error()
Definition: arb_msg.cxx:342
WindowCallback makeHelpCallback(const char *helpfile)
Definition: aw_window.hxx:106
#define AWAR_RENAME_STATUS
Definition: MG_names.cxx:26
GBDATA * GB_create(GBDATA *father, const char *key, GB_TYPES type)
Definition: arbdb.cxx:1781
static void addids_match_info_refresh_cb(AW_root *aw_root)
Definition: MG_names.cxx:53
static void error(const char *msg)
Definition: mkptypes.cxx:96
void MG_create_rename_awars(AW_root *aw_root, AW_default aw_def)
Definition: MG_names.cxx:32
#define AWAR_ADDID_MATCH
Definition: MG_names.cxx:25
char * read_string() const
Definition: AW_awar.cxx:198
AW_awar * awar(const char *awar)
Definition: AW_root.cxx:554
static GB_ERROR renameDB(const char *which, GBDATA *gb_db, bool allowDups)
Definition: MG_names.cxx:127
AW_awar * unmap()
Definition: AW_awar.cxx:596
GBDATA * GLOBAL_gb_src
Definition: MG_main.cxx:31
AW_awar * awar_int(const char *var_name, long default_value=0, AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:580
GB_ERROR close(GB_ERROR error)
Definition: arbdbpp.cxx:35
AW_awar * map(const char *awarn)
Definition: AW_awar.cxx:521
GB_ERROR MG_expect_renamed()
Definition: MG_names.cxx:119
void aw_message(const char *msg)
Definition: AW_status.cxx:1142
AW_root * get_root()
Definition: aw_window.hxx:359
static void rename_both_databases(AW_window *aww)
Definition: MG_names.cxx:144
#define NULp
Definition: cxxforward.h:116
void MG_set_renamed(bool renamed, AW_root *aw_root, const char *reason)
Definition: MG_names.cxx:114
GB_ERROR write_string(const char *aw_string)
GB_CSTR GB_read_char_pntr(GBDATA *gbd)
Definition: arbdb.cxx:904
AW_awar * awar_string(const char *var_name, const char *default_value="", AW_default default_file=AW_ROOT_DEFAULT)
Definition: AW_root.cxx:570
GBDATA * GB_search(GBDATA *gbd, const char *fieldpath, GB_TYPES create)
Definition: adquery.cxx:531
void MG_create_db_dependent_rename_awars(AW_root *aw_root, GBDATA *gb_src, GBDATA *gb_dst)
Definition: MG_names.cxx:58