package Item;

use strict;

#use warnings;
use lib '/app/tools/common/lib';
use Common::RSDB;
use HTML::Entities;

sub new {
    my $class  = shift;
    my %params = @_;

    my $self = {};
    bless $self, $class;

    # use existing database connection if passed in
    #
    # otherwise client_id may be passed in, but typically only from
    # standalone scripts. if not, RSDB will take care of it.
    if ( $params{dbo} ) {
        $self->{dbo} = $params{dbo};
    } else {
        if ( defined $params{client_id} && 0 == $params{client_id} ) {
            $self->{dbo} = Common::RSApp::GetCommonDB();
        } else {
            $self->{dbo} = Common::RSApp::GetClientDB();
        }
    }

    if ( defined $params{client_id} ) {
        $self->{client_id} = $params{client_id};
    } else {
        $self->{client_id} = $ENV{CLIENT_ID} || $self->{dbo}->ClientID;
    }

    return $self;
}

sub ClientID {
    my $self = shift;
    return $self->{client_id};
}

sub _trimCtrl {
    my ($string) = @_;
    $string =~ tr/\000-\037/ /;
    return $string;
}

# the derived classes will pass their attributes array
# to this function, which will return a hashref to be
# used as the xml object.
sub GetObjectXML {
    my $self = shift;

    my $href = {};

    #	map $href->{$_} = $self->$_, @_;
    map $href->{$_} = _trimCtrl( $self->$_ ), @_;

    return $href;
}

sub _reverse_lookup {
    my $self    = shift;
    my $value   = lc shift;
    my $hashref = shift;

    my %reverse_hash = reverse %$hashref;

    my $key = ( exists $reverse_hash{$value} ) ? $reverse_hash{$value} : undef;

    return $key;
}

###
1;    # Play nicely.
###
