#------------------------------------------------------------
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package BookPub::DB::Item::ChapterRelated;
use strict;
use warnings;

#
# modeled after BookPub::DB::Item::BookMatch
#

use lib '/app/tools/common/lib';
use Common::Assert;
use base 'Common::DB::Item';

use constant kTable => 'NONE';
use constant kDB    => Common::DB::Item::kClientDB;

sub _GenerateClassConfig {
    my ($class) = @_;

    return {
        product_id     => { _readOnly => 1 },
        release_date   => { _readOnly => 1 },
        book_format_id => { _readOnly => 1 },
        book_id        => { _readOnly => 1 },
        book_title     => { _readOnly => 1 },
        chapter_id     => { _readOnly => 1 },
        chapter_title  => { _readOnly => 1 },
        ordinal        => { _readOnly => 1 },
        num_pages      => { _readOnly => 1 },
        language_code  => { _readOnly => 1 },
        author_id      => { _readOnly => 1 },
        author_name    => { _readOnly => 1 },
    };
}

sub Lookup {
    my ( $class, %args ) = @_;
    my $sql = <<EofSQL;
SELECT product.product_id, product.release_date,
    chapter_product.book_format_id, chapter_product.chapter_id
    chapter.title as chapter_title, chapter.ordinal, chapter.num_pages,
    book.book_id, book.title as book_title,
    author.author_id, author.name as author_name
FROM product, chapter_product, chapter, book, author
WHERE product.product_id=chapter_product.product_id
    AND chapter_product.chapter_id=chapter.chapter_id
    AND chapter.book_id=book.book_id
    AND author.author_id=chapter.author_id
    AND
EofSQL
    my ( @where, @values );

    # gotta at least provide the product_id
    assert( $args{product_id} );
    push @where,  'chapter_product.product_id=?';
    push @values, $args{product_id};

    $sql .= join( ' AND ', @where );

    return $class->SUPER::Lookup( _sql => $sql, _values => \@values );
}

1;
