package Gestinanna::Shell::Site; use Gestinanna::SchemaManager; use Gestinanna::Shell::Base; use XML::LibXML; @ISA = qw(Gestinanna::Shell::Base); %EXPORT_COMMANDS = ( site => \&do_site, sites => \&do_list, ); %COMMANDS = ( create => \&do_create, clone => \&do_clone, select => \&do_select, delete => \&do_delete, list => \&do_list, uri => \&Gestinanna::Shell::Site::URI::do_uri, config => \&do_config, '?' => \&do_help, ); sub do_help { my($shell, $prefix, $arg) = @_; print "The following commands are available for `site': ", join(", ", sort grep { $_ ne '?' } keys %COMMANDS), "\n"; 1; } sub do_config { my($shell, $prefix, $arg) = @_; if($arg =~ /\?$/) { print < {site}) { print <' to select a site or `site create' to create a new site. EOF return 1; } my $config = $shell -> {site} -> {configuration}; my $new_config = $shell -> edit_xml($config); if($new_config eq $config) { print "Configuration unchanged.\n"; return 1; } my $site = $shell -> {alzabo_schema} -> {runtime_schema} -> table('Site') -> row_by_pk( pk => $shell -> {site} -> {site} ); $site -> update( configuration => $new_config ); $shell -> {site} -> {configuration} = $new_config; load_site_config($shell); } sub do_site { my($shell, $prefix, $arg) = @_; unless($arg =~ /\?$/ || defined $shell -> {alzabo_schema} -> {runtime_schema}) { warn "No schema has been loaded. Use `schema load ' first.\n"; return; } if($arg !~ /^\s*$/) { return __PACKAGE__ -> interpret($shell, $prefix, $arg); } else { if($shell -> {site} -> {name}) { print "Current site: (", $shell -> {site} -> {site}, ") ", $shell -> {site} -> {name}, "\n"; } else { print <' to select a site or `site create' to create a new site. EOF } } } sub do_list { my($shell, $prefix, $arg) = @_; if($arg =~ /\?$/) { print < {alzabo_schema} -> {runtime_schema} -> table("Site"); my $cursor = $table -> all_rows(order_by => $table -> column("site")); my $string = ''; my $site; $string .= join("\t", $site -> select(qw(site name))) . "\n" while $site = $cursor -> next; __PACKAGE__ -> page($string); } sub do_clone { my($shell, $prefix, $arg) = @_; unless($arg =~ /\?$/ || $shell -> {site} -> {name}) { print "No site has been selected. Please select a site. See `site select'.\n"; return; } if($arg =~ /\?$/) { print < This will create a new site with the name and the configuration copied from the currently selected site. Any uri mappings and embeddings specific to the currently selected site will also be copied to the new site. See also: site create, site select EOF return 1; } my $site = $shell -> {alzabo_schema} -> {runtime_schema} -> table("Site") -> insert( values => { name => $arg, configuration => '', parent => $shell -> {site} -> {site}, }, ); unless($site -> is_live) { die "Unable to create site.\n"; return; } print "New site: ", $site -> site, "\t", $site -> name, "\n"; } sub do_create { my($shell, $prefix, $arg) = @_; if($arg =~ /\?$/) { # do help print < This will create a new site with the name . See also: site select EOF return 1; } $shell -> {alzabo_schema} -> {runtime_schema} ->set_referential_integrity(0); my $site = $shell -> {alzabo_schema} -> {runtime_schema} -> table("Site") -> insert( values => { name => $arg, configuration => '', parent_site => 0, }, ); unless($site -> is_live) { die "Unable to create site.\n"; return; } $shell -> {alzabo_schema} -> {runtime_schema} ->set_referential_integrity(1); my $site_number = $site -> select('site'); my $schema_name = $shell -> {alzabo_schema} -> {runtime_schema} -> name; my $driver_name = $shell -> {alzabo_schema} -> {runtime_schema} -> driver -> driver_id; $site -> update( configuration => < production_tag EOXML ); print "New site: ", $site -> select('site'), "\t", $site -> select('name'), "\n"; } sub do_delete { my($shell, $prefix, $arg) = @_; if($arg =~ /\?$/) { print < {alzabo_schema} -> {runtime_schema} -> table("Site") -> row_by_pk(pk => $shell -> {site} -> {site}); return unless $site; $site -> delete; print "Site deleted\n"; delete $shell -> {site}; } sub do_select { my($shell, $prefix, $arg) = @_; if($arg =~ /\?$/) { print < This will select the site for further processing. EOF return 1; } my $site; if($arg =~ /^\d+$/) { # select by number $site = $shell -> {alzabo_schema} -> {runtime_schema} -> table("Site") -> row_by_pk(pk => $arg); } else { $site = $shell -> {alzabo_schema} -> {runtime_schema} -> table("Site") -> one_row( where => [ $shell -> {alzabo_schema} -> {runtime_schema} -> table("Site") -> column("name"), '=', $arg ], ); } return unless $site; $shell -> {site} -> {name} = $site -> select('name'); $shell -> {site} -> {site} = $site -> select('site'); $shell -> {site} -> {configuration} = $site -> select('configuration'); load_site_config($shell); # handles parent configs as well print "Site set to: (", $site -> select('site'), ") ", $site -> select('name'), "\n"; } sub load_site_config { my($shell) = @_; my @parents; my $s = $shell -> {alzabo_schema} -> {runtime_schema} -> table("Site") -> row_by_pk(pk => $shell -> {site} -> {site}); push @parents, $s; while($s -> select('parent_site')) { $s = $shell -> {alzabo_schema} -> {runtime_schema} -> table("Site") -> row_by_pk(pk => $s -> select('parent_site')); push @parents, $s; } my $sc; while(@parents) { my $s = pop @parents; $sc = Gestinanna::SiteConfiguration -> new(site => $s -> select('site'), parent => $sc); $sc -> parse_config($s -> select('configuration')); } $shell -> {site} -> {site_config} = $sc; $sc -> build_factory; } package Gestinanna::Shell::Site::URI; @ISA = qw(Gestinanna::Shell::Base); %COMMANDS = ( delete => \&do_delete, add => \&do_add, list => \&do_list, uri => \&Gestinanna::Shell::Site::URI::do_uri, '?' => \&do_help, ); sub do_help { my($shell, $prefix, $arg) = @_; print "The following commands are available for `site uri': ", join(", ", sort grep { $_ ne '?' } keys %COMMANDS), "\n"; 1; } sub do_uri { my($shell, $prefix, $arg) = @_; unless($arg =~ /\?$/ || $shell -> {site} -> {name}) { print "No site has been selected. Please select a site. See `site select'.\n"; return; } if($arg !~ /^\s*$/) { return __PACKAGE__ -> interpret($shell, $prefix, $arg); } # else { # if($shell -> {site} -> {name}) { # print "Current site: ", $shell -> {site} -> {name}, "\n"; # } # else { # print <' to select a #site or `site create' to create a new site. #EOF # } # } } sub do_list { my($shell, $prefix, $arg) = @_; if($arg =~ /\?$/) { print <{alzabo_schema} -> {runtime_schema} -> table("Uri_Map") -> rows_where( where => [ $shell->{alzabo_schema} -> {runtime_schema} -> table("Uri_Map") -> column("site"), '=', $shell -> {site} -> {site} ] ); my $string = ''; my $u; $string .= join("\t", $u->uri, $u -> file) . "\n" while $u = $cursor -> next; __PACKAGE__->page($string); } sub do_add { my($shell, $prefix, $arg) = @_; if($arg =~ /\?$/) { print < Adds the uri to filename mapping. EOF return; } my($uri, $type, $file) = split(/\s+/, $arg, 3); $uri = "/$uri" if $uri !~ m{^/}; my $u = $shell -> {alzabo_schema} -> {runtime_schema} -> table("Uri_Map") -> insert( values => { site => $shell -> {site} -> {site}, uri => $uri, file => $file, type => $type, }, ); } sub do_delete { my($shell, $prefix, $arg) = @_; if($arg =~ /\?$/) { print <; site uri delete ; site uri delete This will delete either the uri or all uris associated with a filename. Uris should start with a forward slash (/) to distinguish them from file names. EOF return; } if($arg =~ m{^/}) { my $u = $shell -> {alzabo_schema} -> {runtime_schema} -> table("Uri_Map") -> row_by_pk( pk => { site => $shell -> {site} -> {site}, uri => $arg, }, ); return unless $u; print "deleting ", $u -> uri, "\n"; $u -> delete; } else { my($type, $file) = split(/\s+/, $arg, 2); ($type, $file) = (undef, $file) if $file =~ m{^\s*$}; my $table = $shell -> {alzabo_schema} -> {runtime_schema} -> table("Uri_Map"); my $cursor = $table -> rows_where( where => [ [ $table -> column("site"), '=', $shell -> {site} -> {site} ], [ $table -> column("file"), '=', $file ], ( (defined $type) ? ( [ $table -> column("type"), '=', $type ] ) : () ), ], ); my $u; while($u = $cursor -> next) { print "deleting ", $u -> uri, "\n"; $u -> delete; } } } 1; __END__ =head1 NAME Gestinanna::Shell::Site - site commands =head1 SYNOPSIS perl -MGestinanna -e shell =head1 DESCRIPTION This module defines all the C commands in the Gestinanna shell. =head1 AUTHOR James G. Smith, =head1 COPYRIGHT Copyright (C) 2002 Texas A&M University. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.