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

use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::ArtistContract::ArtistContractLicenseIncome;

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

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

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

    my $contractID = $args{artistContractID};

    $self->{ArtistContractLicenseIncome} = [];

    if ($contractID) {
        $self->{_contractID} = $contractID;
        my $collection = RPS::DB::Item::ArtistContractLicenseIncome->GetByContract($contractID);
        while ( $collection->hasNext() ) {
            my $obj = $collection->next();
            push @{ $self->{ArtistContractLicenseIncome} }, RPS::ArtistContract::ArtistContractLicenseIncome->new( _dbItem => $obj );
        }
    }

    return $self;
}

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

    my $valid  = 1;
    my %unique = ();
    foreach my $item ( @{ $self->{ArtistContractLicenseIncome} } ) {
        next if ( !defined $item || $item->Delete() || $item->Inactive() );

        if ( $item->LicenseIncomeTypeID() || $item->LicenseIncomeType && defined $item->Percent() ) {
            my $key = ( $item->LicenseIncomeTypeID() || $item->LicenseIncomeType() );
            if ( $unique{$key} ) {
                $self->setError(Common::FormObject::kErrDuplicate);
                $valid = 0;
            } else {
                $unique{$key} = 1;
            }
        }

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

    return $valid;
}

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

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

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

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

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

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

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

    # May need to dynamically add objects to the list...
    #
    if ( !defined $self->{ArtistContractLicenseIncome}[$index] ) {
        $self->{ArtistContractLicenseIncome}[$index] =
          RPS::ArtistContract::ArtistContractLicenseIncome->new( artistContractID => $self->{_contractID} );
    }

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

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

    return $self->{ArtistContractLicenseIncome};
}

###
1;    #
###

