#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::Catalog::ChapterContributorHash;

use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use BookPub::Catalog::ChapterContributor;
use BookPub::DB::Item::ChapterContributor;

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

use Data::Dumper;

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

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

sub _tagName { 'Contributor' }

sub _keyMethodName { 'ChapterContributorID' }

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

    my $chapterID = $args{chapterID};
    return undef unless $chapterID;

    return BookPub::DB::Item::ChapterContributor->GetAllByChapterID($chapterID);
}

sub _getNewObject {
    return BookPub::Catalog::ChapterContributor->new();
}

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

    return BookPub::Catalog::ChapterContributor->new( dbItem => $dbItem );
}

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

    my $tag = $self->_tagName();

    foreach my $key ( keys %{ $self->{$tag} } ) {
        $self->{$tag}->{$key}->save( %args, key => $key );
    }
}

sub validate {
    my ( $self, %args ) = @_;
    my $valid = 1;

    my $tag = $self->_tagName();

    foreach my $key ( keys %{ $self->{$tag} } ) {
        $valid = undef
          unless ( $self->{$tag}->{$key}->validate( %args, key => $key ) );
    }

    return $valid;
}

1;
