| 1 | # the contents of this file are Copyright (c) 2004-2009 David Blood | ||
| 2 | # This program is free software; you can redistribute it and/or | ||
| 3 | # modify it under the terms of the GNU General Public License as | ||
| 4 | # published by the Free Software Foundation. | ||
| 5 | |||
| 6 | |||
| 7 | package DBR::Admin::Window::SchemaList; | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use Class::Std; | ||
| 11 | use Data::Dumper; | ||
| 12 | |||
| 13 | use lib '/drj/tools/perl-dbr/lib'; | ||
| 14 | use DBR::Admin::Window; | ||
| 15 | use DBR::Admin::Utility; | ||
| 16 | use DBR::Admin::Window::TableList; | ||
| 17 | use DBR::Admin::Window::InstanceList; | ||
| 18 | use vars qw($VERSION $PKG); | ||
| 19 | |||
| 20 | $VERSION = 1.0; | ||
| 21 | |||
| 22 | $PKG = __PACKAGE__; | ||
| 23 | |||
| 24 | use base qw(DBR::Admin::Window); | ||
| 25 | |||
| 26 | |||
| 27 | |||
| 28 | { | ||
| 29 | my %schemas_of : ATTR( :get<schemas> :set<schemas>); | ||
| 30 | my %schema_listbox_of : ATTR( :get<schema_listbox> :set<schema_listbox>); | ||
| 31 | |||
| 32 | #################### | ||
| 33 | sub BUILD { | ||
| 34 | |||
| 35 | my ($self, $ident, $_args) = @_; | ||
| 36 | |||
| 37 | my $new_schema_button = $self->get_win->add( | ||
| 38 | 'newschema', 'Buttonbox', | ||
| 39 | -buttons => [ | ||
| 40 | { | ||
| 41 | -label => '< Add New Schema >', | ||
| 42 | -value => 1, | ||
| 43 | -shortcut => 1 , | ||
| 44 | -onpress => sub {$self->add_edit_schema(add => 1)} | ||
| 45 | |||
| 46 | }, | ||
| 47 | ], | ||
| 48 | -x => 40, | ||
| 49 | -y => 1, | ||
| 50 | |||
| 51 | ); | ||
| 52 | |||
| 53 | $new_schema_button->draw(); | ||
| 54 | |||
| 55 | |||
| 56 | my $listbox = $self->get_win->add( | ||
| 57 | 'schemalistbox', 'Listbox', | ||
| 58 | -y => 2, | ||
| 59 | -width => 25, | ||
| 60 | -vscrollbar => 1, | ||
| 61 | -onchange => sub {$self->listbox_item_options(listbox => shift);} | ||
| 62 | ); | ||
| 63 | |||
| 64 | $listbox->onFocus(sub {$listbox->clear_selection}); | ||
| 65 | $self->set_schema_listbox($listbox); | ||
| 66 | $self->load_schema_list(); | ||
| 67 | $self->get_schema_listbox->focus(); | ||
| 68 | $self->get_schema_listbox->focus(); | ||
| 69 | $self->get_win->set_focusorder('schemalistbox', 'newschema', 'close'); | ||
| 70 | } | ||
| 71 | |||
| 72 | ####################### | ||
| 73 | # get the list from the database | ||
| 74 | sub get_schema_list { | ||
| 75 | |||
| 76 | my ($self, %_args) = @_; | ||
| 77 | |||
| 78 | my $dbrh = DBR::Admin::Utility::get_dbrh(); | ||
| 79 | |||
| 80 | my $data = $dbrh->select( | ||
| 81 | -table => 'dbr_schemas', | ||
| 82 | -fields => 'schema_id handle display_name', | ||
| 83 | ) or throw DBR::Admin::Exception( | ||
| 84 | message => "failed to select from dbr_schema $!", | ||
| 85 | root_window => $self->get_win->root() | ||
| 86 | ); | ||
| 87 | |||
| 88 | my %menu_list; | ||
| 89 | my %schemas; | ||
| 90 | |||
| 91 | foreach my $e (@$data) { | ||
| 92 | $menu_list{$e->{schema_id}} = $e->{display_name}; | ||
| 93 | $schemas{$e->{schema_id}} = $e; | ||
| 94 | } | ||
| 95 | |||
| 96 | $self->set_schemas(\%schemas); | ||
| 97 | return \%menu_list; | ||
| 98 | } | ||
| 99 | |||
| 100 | ################# | ||
| 101 | # load the schema list into the | ||
| 102 | # schema listbox | ||
| 103 | sub load_schema_list{ | ||
| 104 | |||
| 105 | my ($self, %_args) = @_; | ||
| 106 | |||
| 107 | my $schema_listbox = $self->get_schema_listbox(); | ||
| 108 | my $menu_list = $self->get_schema_list(); | ||
| 109 | my @menu_values = keys %{$menu_list}; | ||
| 110 | $schema_listbox->values(\@menu_values); | ||
| 111 | $schema_listbox->labels($menu_list); | ||
| 112 | $self->set_schema_listbox($schema_listbox); | ||
| 113 | } | ||
| 114 | |||
| 115 | ########################## | ||
| 116 | # this is the options listbox that appears when a | ||
| 117 | # schema is chosen | ||
| 118 | sub listbox_item_options { | ||
| 119 | |||
| 120 | my ($self, %_args) = @_; | ||
| 121 | |||
| 122 | if ($self->get_win->getobj('schemalistbox_options')) { | ||
| 123 | $self->get_win->delete('schemalistbox_options'); | ||
| 124 | } | ||
| 125 | |||
| 126 | my $listbox_options = $self->get_win->add( | ||
| 127 | 'schemalistbox_options', 'Listbox', | ||
| 128 | -y => ($_args{listbox}->get_active_id() + 2) , | ||
| 129 | -x => 30, | ||
| 130 | -width => 25, | ||
| 131 | -values => ['Tables', 'Instances', 'Edit'], | ||
| 132 | -onchange => sub { $self->listbox_option_select( | ||
| 133 | listbox => shift, | ||
| 134 | schema_id => $_args{listbox}->get(), | ||
| 135 | schema_listbox => $_args{listbox} | ||
| 136 | ); }, | ||
| 137 | -onblur => sub {$self->get_win->delete('schemalistbox_options');} | ||
| 138 | ); | ||
| 139 | |||
| 140 | $listbox_options->focus(); | ||
| 141 | $listbox_options->onFocus(sub {$listbox_options->clear_selection}); | ||
| 142 | } | ||
| 143 | |||
| 144 | ########################## | ||
| 145 | # called when an option is selected | ||
| 146 | sub listbox_option_select { | ||
| 147 | |||
| 148 | my ($self, %_args) = @_; | ||
| 149 | |||
| 150 | #print STDERR $_args->get(); | ||
| 151 | |||
| 152 | |||
| 153 | if ($_args{listbox}->get eq 'Tables') { | ||
| 154 | DBR::Admin::Window::TableList->new( | ||
| 155 | {id => 'tables', | ||
| 156 | parent => $self->get_win, | ||
| 157 | schema_id => $_args{schema_id}, | ||
| 158 | parent_title => ucfirst($self->get_id) | ||
| 159 | } | ||
| 160 | ); | ||
| 161 | } | ||
| 162 | elsif ($_args{listbox}->get eq 'Instances'){ | ||
| 163 | DBR::Admin::Window::InstanceList->new( | ||
| 164 | {id => 'instances', | ||
| 165 | parent => $self->get_win, | ||
| 166 | schema_id => $_args{schema_id}, | ||
| 167 | schema => $self, | ||
| 168 | parent_title => ucfirst($self->get_id) | ||
| 169 | } | ||
| 170 | ); | ||
| 171 | } | ||
| 172 | |||
| 173 | elsif ($_args{listbox}->get eq 'Edit'){ | ||
| 174 | $self->add_edit_schema(schema_id => $_args{schema_id}) | ||
| 175 | } | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | ####################### | ||
| 180 | # add or edit window | ||
| 181 | sub add_edit_schema { | ||
| 182 | |||
| 183 | my ($self, %_args) = @_; | ||
| 184 | |||
| 185 | my $dbrh = DBR::Admin::Utility::get_dbrh(); | ||
| 186 | my $schemas = $self->get_schemas; | ||
| 187 | |||
| 188 | my $edit_window = $self->get_win->add( | ||
| 189 | 'schemaeditwindow', 'Window', | ||
| 190 | -border => 1, | ||
| 191 | -y => 1, | ||
| 192 | -bfg => 'blue', | ||
| 193 | -title => $_args{add} ? 'Add New Schema' : 'Edit Schema', | ||
| 194 | -titlereverse => 0, | ||
| 195 | ); | ||
| 196 | |||
| 197 | |||
| 198 | my $label; | ||
| 199 | my $schema_id_box; | ||
| 200 | my $schema_override_id_box; | ||
| 201 | |||
| 202 | my $x = 5; | ||
| 203 | my $y = 1; | ||
| 204 | |||
| 205 | ##### | ||
| 206 | # only show the schema_id if it's an edit | ||
| 207 | if (!$_args{add}) { | ||
| 208 | |||
| 209 | ####### | ||
| 210 | # schema_id | ||
| 211 | $label = $edit_window->add( | ||
| 212 | 'schema_id_label', 'Label', | ||
| 213 | -text => 'schema_id:', | ||
| 214 | -x => $x, | ||
| 215 | -y => $y | ||
| 216 | ); | ||
| 217 | |||
| 218 | $label->draw; | ||
| 219 | |||
| 220 | $schema_id_box = $edit_window->add( | ||
| 221 | 'schema_id_box', 'TextEditor', | ||
| 222 | -sbborder => 1, | ||
| 223 | -y => $y, | ||
| 224 | -x => $x + 30, | ||
| 225 | -width => 6, | ||
| 226 | -readonly => 1, | ||
| 227 | -singleline => 1, | ||
| 228 | -text => $_args{schema_id} | ||
| 229 | ); | ||
| 230 | $schema_id_box->draw(); | ||
| 231 | $y += 1; | ||
| 232 | |||
| 233 | |||
| 234 | } | ||
| 235 | |||
| 236 | ##### | ||
| 237 | # name | ||
| 238 | $label = $edit_window->add( | ||
| 239 | 'schema_name_label', 'Label', | ||
| 240 | -text => 'display_name: ', | ||
| 241 | -x => $x, | ||
| 242 | -y => $y | ||
| 243 | ); | ||
| 244 | |||
| 245 | $label->draw; | ||
| 246 | |||
| 247 | my $schema_display_name_box = $edit_window->add( | ||
| 248 | 'schema_name_box', 'TextEditor', | ||
| 249 | -sbborder => 1, | ||
| 250 | -y => $y, | ||
| 251 | -x => $x + 30, | ||
| 252 | -width => 25, | ||
| 253 | -singleline => 1, | ||
| 254 | -text => $schemas->{$_args{schema_id}}->{display_name} | ||
| 255 | ); | ||
| 256 | $schema_display_name_box->draw(); | ||
| 257 | $y += 1; | ||
| 258 | |||
| 259 | ##### | ||
| 260 | # handle | ||
| 261 | $label = $edit_window->add( | ||
| 262 | 'schema_handle_label', 'Label', | ||
| 263 | -text => 'handle:', | ||
| 264 | -x => $x, | ||
| 265 | -y => $y | ||
| 266 | ); | ||
| 267 | |||
| 268 | $label->draw; | ||
| 269 | |||
| 270 | |||
| 271 | my $schema_handle_box = $edit_window->add( | ||
| 272 | 'schema_handle_box', 'TextEditor', | ||
| 273 | -sbborder => 1, | ||
| 274 | -y => $y, | ||
| 275 | -x => $x + 30, | ||
| 276 | -width => 25, | ||
| 277 | -singleline => 1, | ||
| 278 | -text => $schemas->{$_args{schema_id}}->{handle} | ||
| 279 | ); | ||
| 280 | |||
| 281 | $schema_handle_box->draw(); | ||
| 282 | $y += 3; | ||
| 283 | |||
| 284 | ##### | ||
| 285 | # buttons | ||
| 286 | my $submit_button = $edit_window->add( | ||
| 287 | 'submit', 'Buttonbox', | ||
| 288 | -buttons => [ | ||
| 289 | { | ||
| 290 | -label => '< Submit >', | ||
| 291 | -value => 1, | ||
| 292 | -shortcut => 1 , | ||
| 293 | -onpress => sub { | ||
| 294 | $self->submit_add_edit( | ||
| 295 | schema_id => $_args{schema_id}, | ||
| 296 | display_name => $schema_display_name_box->get(), | ||
| 297 | handle => $schema_handle_box->get(), | ||
| 298 | edit_window => $edit_window, | ||
| 299 | add => $_args{add} | ||
| 300 | ); | ||
| 301 | } | ||
| 302 | }, | ||
| 303 | { | ||
| 304 | -label => '< Cancel >', | ||
| 305 | -value => 2, | ||
| 306 | -shortcut => 2 , | ||
| 307 | -onpress => sub {$self->close_edit_window(edit_window => $edit_window)} | ||
| 308 | } | ||
| 309 | |||
| 310 | ], | ||
| 311 | -x => $x, | ||
| 312 | -y => $y, | ||
| 313 | |||
| 314 | ); | ||
| 315 | $submit_button->draw(); | ||
| 316 | |||
| 317 | |||
| 318 | |||
| 319 | |||
| 320 | ##### | ||
| 321 | $schema_display_name_box->focus(); | ||
| 322 | } | ||
| 323 | |||
| 324 | ####################### | ||
| 325 | # called when the submit button on the | ||
| 326 | # add/edit window is selected | ||
| 327 | sub submit_add_edit { | ||
| 328 | |||
| 329 | my ($self, %_args) = @_; | ||
| 330 | |||
| 331 | #print STDERR "submit edit:\n"; | ||
| 332 | #print STDERR Dumper \%_args; | ||
| 333 | |||
| 334 | # check duplicate | ||
| 335 | my $duplicate = 0; | ||
| 336 | my $schemas = $self->get_schemas(); | ||
| 337 | foreach my $e (keys %{$schemas}) { | ||
| 338 | |||
| 339 | # don't check against myself | ||
| 340 | next if ($_args{schema_id} == $schemas->{$e}->{schema_id}); | ||
| 341 | |||
| 342 | if ( ($_args{display_name} eq $schemas->{$e}->{display_name}) || | ||
| 343 | ($_args{handle} eq $schemas->{$e}->{handle}) ){ | ||
| 344 | $duplicate = 1; | ||
| 345 | last; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | if ($duplicate) { | ||
| 350 | my $confirm = $self->get_win->root->dialog( | ||
| 351 | -message => "The Display Name or Handle you entered is already taken. Please try another.", | ||
| 352 | -title => "Duplicate Entry", | ||
| 353 | -buttons => [ | ||
| 354 | { | ||
| 355 | -label => '< OK >', | ||
| 356 | -value => 1, | ||
| 357 | -shortcut => 1 | ||
| 358 | } | ||
| 359 | ] | ||
| 360 | ); | ||
| 361 | return; | ||
| 362 | } | ||
| 363 | |||
| 364 | my $dbrh = DBR::Admin::Utility::get_dbrh(); | ||
| 365 | my $ret; | ||
| 366 | |||
| 367 | if ($_args{add}) { | ||
| 368 | $ret = $dbrh->insert( | ||
| 369 | -table => 'dbr_schemas', | ||
| 370 | -fields => { | ||
| 371 | display_name => $_args{display_name}, | ||
| 372 | handle => $_args{handle} | ||
| 373 | }, | ||
| 374 | ) or throw DBR::Admin::Exception( | ||
| 375 | message => "failed to insert into dbr_schemas $!", | ||
| 376 | root_window => $self->get_win->root() | ||
| 377 | ); | ||
| 378 | |||
| 379 | } else { | ||
| 380 | $ret = $dbrh->update( | ||
| 381 | -table => 'dbr_schemas', | ||
| 382 | -fields => { | ||
| 383 | display_name => $_args{display_name}, | ||
| 384 | handle => $_args{handle} | ||
| 385 | }, | ||
| 386 | -where => {schema_id => $_args{schema_id}} | ||
| 387 | ) or throw DBR::Admin::Exception( | ||
| 388 | message => "failed to update dbr_schemas $!", | ||
| 389 | root_window => $self->get_win->root() | ||
| 390 | ); | ||
| 391 | } | ||
| 392 | |||
| 393 | #success dialog | ||
| 394 | my $confirm = $self->get_win->root->dialog( | ||
| 395 | -message => $_args{add} ? 'Schema successfully added' :"This schema has been successfully updated.", | ||
| 396 | -title => "Success", | ||
| 397 | -buttons => [ | ||
| 398 | { | ||
| 399 | -label => '< OK >', | ||
| 400 | -value => 1, | ||
| 401 | -shortcut => 1 | ||
| 402 | } | ||
| 403 | ] | ||
| 404 | ); | ||
| 405 | |||
| 406 | # reset the schema list | ||
| 407 | $self->load_schema_list(); | ||
| 408 | |||
| 409 | # close window | ||
| 410 | $self->close_edit_window(%_args); | ||
| 411 | |||
| 412 | } | ||
| 413 | |||
| 414 | ####################### | ||
| 415 | sub close_edit_window { | ||
| 416 | |||
| 417 | my ($self, %_args) = @_; | ||
| 418 | |||
| 419 | $_args{edit_window}->parent->delete('schemaeditwindow'); | ||
| 420 | $_args{edit_window}->parent->draw(); | ||
| 421 | $_args{edit_window}->parent->focus(); | ||
| 422 | } | ||
| 423 | |||
| 424 | |||
| 425 | } | ||
| 426 | |||
| 427 | 1; |