#!/usr/local/bin/perl
package Base;
my $DebugLevel = 0;
sub new {
my $this = shift;
my $class = ref($this) || $this;
my $self = {};
bless $self, $class;
if (@_ > 0) { $self->initialize(@_); }
return $self;
}
sub child_types
{
my $self = shift;
if (defined($self->{children}))
{
return(keys %{$self->{$children}});
}
}
sub parent
{
my $self = shift;
my $parent_type = shift;
my $parent_object = shift;
# if they don't ask for a kind, return the whole thing
if (!defined($parent_type)) { return $self->{parents}; }
# if they supply an object, set it as the parent object
if (defined($parent_object))
{
$self->{parents}{$parent_type} = $parent_object;
}
# return the parent object
$parent_object = $self->{parents}{$parent_type};
return $parent_object;
}
sub children
{
my $self = shift;
my $child_type = shift;
if (defined($child_type) && defined($self->{children}{$child_type}))
{
return @{$self->{children}{$child_type}};
}
return ();
}
sub child
{
my $self = shift;
my $child_type = shift;
my $child_object = shift;
#print "\nin child method for ".$self->id_param."\n";
# if they don't ask for a kind, return the whole thing
if (!defined($child_type))
{
#print "\nreturn all children\n";
return $self->{children};
}
# if they supply an object, set it as the child object
if (defined($child_object))
{
#print "\n set child 0 of type $child_type to ".$child_object->id_param."\n";
@{$self->{children}{$child_type}}[0] = $child_object;
}
# return the first child object
foreach (@{$self->{children}{$child_type}})
{
#print "\nreturning child 0 of type $child_type: [".$_->id_param."]\n";
return $_;
}
#print "\nno children of type $child_type\n";
return undef;
}
sub set {
my $self = shift;
my %params = @_;
my $key = "";
foreach (sort keys %params)
{
$key = $_;
$key =~ tr/A-Z/a-z/;
#print "
[$key]=[$params{$_}]\n";
$self->{$key} = $params{$_};
}
}
sub get {
my $self = shift;
my $total = @_;
if (!defined(@_) || !defined($total) || $total <= 0)
{
return(keys %{$self});
}
elsif ($total == 1)
{
my $field = shift;
if ($field =~ /^) { return $field; }
$field =~ tr/A-Z/a-z/;
my $result = defined($self->{$field}) ? $self->{$field} : "";
#print "key=$field value=$result
\n";
return $result;
}
my @results = ();
foreach (@_)
{
push(@results,$self->get($_));
}
#print "keys=";
#print @_;
#print "
\n";
#print "values=";
#print @results;
#print "
\n";
return(@results);
}
sub synonym {
my $self = shift;
my $key = "";
foreach (@_)
{
$key = $_;
$key =~ tr/A-Z/a-z/;
if (defined($self->{$key})) { return($key,$self->{$key}); }
}
return('none','');
}
sub fail {
my($self,$reason) = @_;
$self->set( 'Reason' => $self->get('Reason').$reason );
return(0);
}
sub print {
my $self = shift;
foreach $_ (sort keys %{$self}) { print "$_ = $self->{$_}\n"; }
}
sub initialize {
my $self = shift;
my %params = @_;
$self->set(%params);
}
sub set_flag
{
my $self = shift;
my $flag = shift;
$self->{$flag} = shift;
}
sub flag
{
my $self = shift;
my $flag = shift;
my $value = shift;
if (defined($value)) { $self->{$flag} = $value; }
if (!defined($flag)
|| !defined($self->{$flag})
|| $self->{$flag} eq ''
|| $self->{$flag} == 0
)
{
return 0;
}
return $self->{$flag};
}
sub debug_print
{
if ($DebugLevel > 0)
{
print @_;
}
}
sub alarm_handler {
die;
}
sub money
{
my $self = shift;
my $value = shift;
return(sprintf("%0.2f",$value));
}
1;