#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Publisher::BasePublisherSimple;

use strict;
use warnings;
use Carp;

use lib '/app/tools/common/lib';
use Common::Assert;


use lib '/app/tools/rps/lib';
use Common::FormObject;
use base 'Common::FormObject';

sub _init
{
	my ($self, %args) = @_;
	
	$self->SUPER::_init(%args);

    # JPK - Changing the framework a bit.
    # I want to make the generation of initial property values
    # more distict from the initialization of XMLObject members.
    # This is to make inheritance easier...
    #

    my %properties;

	if ($args{_dbItem})
	{
        $self->_propertiesFromDBItem(\%properties, $args{_dbItem});
	}
	elsif ($args{publisherID})
	{
        $self->_propertiesFromDB(\%properties, $args{publisherID});
	}
	else
	{
	    # This is deprecated.
	    #
		#$self->_propertiesFromDefaults(\%properties);
        #if (defined $args{publisherID} && 0 == $args{publisherID})
        #{
            # JPK - This is perhaps a bit of a hack...
            #
        #    $properties{publisher_name} = '<Unknown>';
        #}
	}

    return $self->_initProperties(\%properties);
}


sub _propertiesFromDB
{
	my ($self, $hrProperties, $publisherID) = @_;
    my $timer = Common::Timer->new();

	my $dbItem = $self->_publisherDB()->Lookup($self->_publisherID() => $publisherID);
	$self->_propertiesFromDBItem($hrProperties, $dbItem);
}


sub _propertiesFromDBItem
{
	my ($self, $hrProperties, $dbItem) = @_;

    my $publisherID = $self->_publisherID();
    $hrProperties->{$publisherID}           = $dbItem->$publisherID;
    $hrProperties->{publisher_name}         = $dbItem->publisher_name;
    $hrProperties->{admin_id}               = $dbItem->admin_id;
    $hrProperties->{agent_id}               = $dbItem->agent_id;
    $hrProperties->{is_agency}              = $dbItem->is_agency;
    $hrProperties->{is_admin}               = $dbItem->is_admin;
    $hrProperties->{client_account_id}      = $dbItem->client_account_id;    
}


sub _propertiesFromDefaults
{
	my ($self, $hrProperties) = @_;
    my $timer = Common::Timer->new();

    my $defaultValues = RPS::DB::Item::FormDefaults->GetFormPropertyValues('RPS::Publisher::Publisher');
    while (my $item = $defaultValues->next())
    {
        $hrProperties->{$item->property_name} = $item->property_value;
    }
}


sub _initProperties
{
	my ($self, $props) = @_;
    my $timer = Common::Timer->new();

	$self->{PublisherID} =      Common::FormObject::Scalar->new(value => $props->{$self->_publisherID()}, readOnly => 1);
	$self->{PublisherName} =    Common::FormObject::Scalar::String->new(value => $props->{publisher_name}, required => 1);
	$self->{AgentID} =          Common::FormObject::Scalar->new(value => $props->{agent_id});
	$self->{AdminID} =          Common::FormObject::Scalar->new(value => $props->{admin_id});
	$self->{IsAgency} =         Common::FormObject::Scalar::Boolean->new(value => $props->{is_agency});
	$self->{IsAdmin} =          Common::FormObject::Scalar::Boolean->new(value => $props->{is_admin});
	$self->{ClientAccountID} =  Common::FormObject::Scalar::String->new(value => $props->{client_account_id});	

	if ($props->{agent_id})
	{
		my $agent = $self->_publisherDB()->Lookup($self->_publisherID() => $props->{agent_id});
		$self->{AgentName} = Common::FormObject::Scalar::String->new(value => $agent->publisher_name, readOnly => 1) if($agent);
	}
	if ($props->{admin_id})
	{
		my $admin = $self->_publisherDB()->Lookup($self->_publisherID() => $props->{admin_id});
		$self->{AdminName} = Common::FormObject::Scalar::String->new(value => $admin->publisher_name, readOnly => 1) if($admin);
	}

    # If this is the 'unknown publisher', with an ID of 0, then manually set the name.
    #
    if (defined $props->{$self->_publisherID()} && ! $props->{$self->_publisherID()})
    {
        $self->{PublisherName} = Common::FormObject::Scalar::String->new(value => '<Unknown>');
    }

	return $self;
}


###
1;#
###

