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

use strict;
use warnings;
use Carp;

use Data::Dumper;
use lib '/app/tools/common/lib';
use Common::FormObject;
use Common::Assert;

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

use base 'Common::FormObject';

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

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

    $self->{McpsStatement} = [];

    my $runID = $args{ukMechanicalRunID};
    assert($runID);

    my $filterBy = $args{filterBy};

    # Do we even have filters?
    #

    my $collection = RPS::DB::Item::McpsStatement->GetByRunID($runID);

    #    my $collection = RPS::DB::Item::McpsStatement->Get
    #    (
    #        runID => $runID,
    #        payorID => $payorID,
    #        sortByPublisherName => 1,
    #        filterBy => $filterBy,
    #    );

    if ($collection) {
        while ( $collection->hasNext() ) {
            my $obj = $collection->next();

            # Calling an abstract member function to actually create the correct
            # subclass of BaseUKMechanicalStatement.
            #
            push @{ $self->{McpsStatement} }, $self->_makeStatement($obj);
        }
    }

    return $self;
}

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

# !!! This method may not be necessary.
#
sub McpsStatement {
    my ( $self, $index, $methodAddressList, $newValue ) = @_;

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

    # May need to dynamically add objects to the list...
    #
    if ( !defined $self->{McpsStatement}[$index] ) {
        die "ERROR - no mcps statement with index $index";
    }

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

# Have to override validate to allow it to validate all the members in my list.
#
# !!! Why do we need to validate?
#
sub validate {
    my ( $self, $skipRequiredFields ) = @_;

    print STDERR ref($self) . "::validate\n";

    # A recursive validation mechanism.
    # Subclasses should override this to perform specific
    # validation, if they care too, then call the inherited
    # method (this).  This base method implements the
    # recursive descent.
    #
    my $valid = 1;

    foreach my $item ( @{ $self->{McpsStatement} } ) {
        if ( !$item->validate($skipRequiredFields) ) {
            $valid = 0;
        }
    }

    return $valid;
}

# !!! This is abstract, so I won't _really_ implement this here.
# You _must_ instiantiate this method in subclasses.
#
#sub _makeStatement
#{
#    my ($self, $dbItem) = @_;
#}

1;
