#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::DB::Item::CatalogImportItem::Book;

use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use base 'BookPub::DB::Item::CatalogImportItem';
use Common::Assert;
use Common::RSApp;
use Common::Util;

use constant kTable => 'catalog_import_book';

# !!! This would be better if it used subtitle, I'm thinking...
#
sub GetByTitlesAndCatalogImportID {
    my ( $class, %args ) = @_;
    assert($class);

    # Going to search using the _clean_ titles.
    #

    my $title = $args{title};
    assert($title);

    my $subtitle = $args{subtitle};

    my $titleClean;
    my $subtitleClean;

    $titleClean = Common::Util::clean_book_title($title);
    $subtitleClean = Common::Util::clean_book_title($subtitle) if $subtitle;

    my $catalogImportID = $args{catalogImportID};
    assert($catalogImportID);

    #	my $languageCode = $args{languageCode};
    #	my $imprintID = $args{catalogImportImprintID};

    my $dbo = Common::RSApp::GetClientDB();

    my @where;
    push @where, 'title_clean=' . $dbo->DBQuote($titleClean);
    push @where, 'catalog_import_id=' . $dbo->DBQuote($catalogImportID);

    #    push @where, 'language_code='.$dbo->DBQuote($languageCode);
    #    push @where, 'catalog_import_imprint_id='.$dbo->DBQuote($imprintID);

    if ($subtitle) {
        push @where, 'subtitle_clean=' . $dbo->DBQuote($subtitleClean);
    } else {
        push @where, '(subtitle IS NULL OR subtitle = \'\')';
    }

    my $sql = 'SELECT * FROM ' . kTable . ' WHERE ' . join( ' AND ', @where );
    return $class->GetAll($sql);
}

sub GetAllByCleanTitleAndSubtitle {
    my ( $class, $title, $subtitle ) = @_;
    assert($title);

    my $sql = 'SELECT * FROM ' . kTable . ' WHERE title_clean=' . $class->quote($title);

    if ( defined $subtitle ) {
        $sql .= ' AND subtitle_clean=' . $class->quote($subtitle);
    }

    return $class->GetAll($sql);
}

1;
