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

use strict;
use warnings;

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

use Common::FormObject;
use base 'Common::FormObject';

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

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

    $self->{LicenseIncome} = [];

    if ( $args{parentID} && $args{parentType} ) {
        $self->{_parentID}   = $args{parentID};
        $self->{_parentType} = $args{parentType};

        my $collection = RPS::DB::Item::LicenseIncome->GetByParent( $args{parentID}, $args{parentType} );
        while ( $collection->hasNext() ) {
            my $obj = $collection->next();
            push @{ $self->{LicenseIncome} }, RPS::LicenseIncome::LicenseIncome->new( _dbItem => $obj );
        }
    }

    return $self;
}

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

    my $valid = 1;
    foreach my $item ( @{ $self->{LicenseIncome} } ) {
        next if ( !defined $item );

        if ( !$item->validate() ) {
            $valid = 0;
        }
    }

    return $valid;
}

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

    foreach my $item ( @{ $self->{LicenseIncome} } ) {
        if ( defined $item && defined $item->Amount() ) {
            $item->save();
        }
    }
}

sub _listProperties {
    return { LicenseIncome => 1 };
}

sub _propertyIsEditable {
    my ( $self, $propertyName ) = @_;

    if ( $propertyName eq 'LicenseIncome' ) {
        return 1;
    }
    return 0;
}

# This is the special sort of list accessor.
#
sub LicenseIncome {
    my ( $self, $index, $methodAddressList, $newValue ) = @_;

    if ( 0 == scalar @$methodAddressList ) {
        if ( defined $newValue ) {
            $self->{LicenseIncome}[$index] = $newValue;
        }
        return 1;
    }

    # May need to dynamically add objects to the list...
    #
    if ( !defined $self->{LicenseIncome}[$index] ) {
        $self->{LicenseIncome}[$index] =
          RPS::LicenseIncome::LicenseIncome->new( parentID => $self->{_parentID}, parentType => $self->{_parentType} );
    }

    return $self->{LicenseIncome}[$index]->_accessProperty( $methodAddressList, $newValue );
}

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

    return $self->{LicenseIncome};
}

###
1;    #
###

