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

use Carp;
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::ProductFormat;

use RPS::XMLObject;
use base 'RPS::XMLObject';


sub _init
{
    my ($self, %args) = @_;

    $self->SUPER::_init(%args);

    my %properties;

    if ($args{_dbItem})
    {   
        $self->_propertiesFromDBItem(\%properties, $args{_dbItem});
    }
    elsif ($args{productFormatID})
    {   
        $self->_propertiesFromDB(\%properties, $args{productFormatID});
    }
    else
    {   
        $self->_propertiesFromDefaults(\%properties);
    }


    $self->_initProperties(\%properties);
    $self->_initSubs(\%properties);

    return $self;
}


sub _propertiesFromDB
{   
    my ($self, $hrProperties, $id) = @_;

    my $dbItem = RPS::DB::Item::ProductFormat->Lookup(product_format_id => $id);
    return $self->_propertiesFromDBItem($hrProperties, $dbItem);
}


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

    $hrProperties->{product_format_id} = $dbItem->product_format_id;
    $hrProperties->{parent_id} = $dbItem->parent_id;
    $hrProperties->{description} = $dbItem->description;
    $hrProperties->{name} = $dbItem->name;
}


sub _initProperties
{
    my ($self, $props) = @_;

    $self->{ProductFormatID}	= RPS::XMLObject::Scalar::Integer->new(value => $props->{product_format_id}, readOnly => 1);
    $self->{ParentID}       	= RPS::XMLObject::Scalar::Integer->new(value => $props->{parent_id}, readOnly => 1);
    $self->{Name}			    = RPS::XMLObject::Scalar::String->new(value => $props->{name}, maxlength => 64);
    $self->{Description}	    = RPS::XMLObject::Scalar::String->new(value => $props->{description});

}


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

}


sub _propertiesFromDefaults
{
    my ($self, $hrProperties) = @_;
}


sub validate
{
    my ($self) = @_;

	my $valid = 1;

	if(!$self->Name())
	{
		$self->{Name}->setError(RPS::XMLObject::kErrFieldBlank);
		$valid = 0;
	}

    return $valid;
}

sub save
{
    my ($self) = @_;

	my $dbObj;
	if($self->ProductFormatID())
	{
		$dbObj = RPS::DB::Item::ProductFormat->Lookup(product_format_id => $self->ProductFormatID());
	}
	else
	{
		$dbObj = RPS::DB::Item::ProductFormat->Create();
	}

	# set fields here
	#
    $dbObj->parent_id($self->ParentID());
	$dbObj->name($self->Name());
	$dbObj->description($self->Description());

	# save db item
	#
	$dbObj->save();

	# reload object
	#
	$self->_init(_dbItem => $dbObj);
}

sub delete
{
	my $self = shift;

	my $myDBObj = RPS::DB::Item::ProductFormat->Lookup(product_format_id => $self->ProductFormatID());
	$myDBObj->delete();
}


###
1;#
###




