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

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::ChapterProduct;
use BookPub::DB::Item::ProductType;
use Common::RSApp;
use Common::Util;
use Common::Assert;
use Common::FormObject;

use base 'BookPub::Catalog::Product';

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

    my $dbItem = BookPub::DB::Item::ChapterProduct->Lookup( product_id => $productID );

    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

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

    # !!! Don't need to set 'product_id' : That is handled in the base class.
    #
    $hrProperties->{chapter_id}     = $dbItem->chapter_id;
    $hrProperties->{book_format_id} = $dbItem->book_format_id;

    # Invoke the inherited method with a _different_ db item.
    #
    my $baseDBItem = BookPub::DB::Item::Product->Lookup( product_id => $dbItem->product_id );
    $self->SUPER::_propertiesFromDBItem( $hrProperties, $baseDBItem );
}

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

    # By _definition_, the product_type_id for a book product is 'book product'.
    # So we'll force that value before we call the inherited init method.
    $props->{product_type_id} = BookPub::DB::Item::ProductType::kChapter;

    # Invoke the inherited method first.
    #
    $self->SUPER::_initProperties($props);

    $self->{ChapterID}    = Common::FormObject::Scalar->new( value => $props->{chapter_id} );
    $self->{BookFormatID} = Common::FormObject::Scalar->new( value => $props->{book_format_id} );
}

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

    # Before doing anything, see if we have a product_id yet.  If we don't, we're creating
    # a new record. (Actually, _two_ new records - product and book_product).
    #
    my $productID = $self->ProductID();

    # Now invoke the inherited method.
    # This will create and/or update the 'product' table.
    # Assuming that works out, we'll then get passed back a DB::Item::Product reference
    # from which we can get the product_id (if necessary).
    #
    my $productDBObj = $self->SUPER::_saveProduct();

    # Now we can update OUR table.
    #
    my $chapterProductDBObj;
    if ($productID) {
        $chapterProductDBObj = BookPub::DB::Item::ChapterProduct->Lookup( product_id => $productID );
    } else {
        $productID = $productDBObj->product_id;
        $chapterProductDBObj = BookPub::DB::Item::ChapterProduct->Create( product_id => $productID );
    }

    $chapterProductDBObj->chapter_id( $self->ChapterID() );
    $chapterProductDBObj->book_format_id( $self->BookFormatID() );

    $chapterProductDBObj->save();

    return $chapterProductDBObj;

}

###
1;    #
###
