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

use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::Assert;
use Common::RSApp;
use BookPub::DB::Item::ContributorRole;
use BookPub::DB::Item::Archive::BookContributor;

use base 'BookPub::DB::MetadataItem';

sub _NewArchiveItem {
    my ( $class, %args ) = @_;
    return BookPub::DB::Item::Archive::BookProductContributor->Create(%args);
}

sub _ArchiveTableName {
    return 'archive_book_product_contributor';
}

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

sub GetAllByProductID {
    my ( $class, $bookProductID ) = @_;
    assert($bookProductID);

    my $sql = "SELECT * FROM " . kTable . " WHERE product_id=$bookProductID ORDER BY sequence";
    return $class->GetAll($sql);
}

sub DeleteAllByProductID {
    my ( $class, $productID ) = @_;
    assert($productID);
    my $dbo = Common::RSApp::GetClientDB();
    $dbo->DoCmd( "DELETE FROM " . kTable . " WHERE product_id=$productID" );
}

sub GetAllByRoleID {
    my ( $class, $roleID ) = @_;
    assert($roleID);

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

    my $sql = "SELECT * FROM " . kTable . " WHERE contributor_role_id=" . $dbo->DBQuote($roleID) . " ORDER BY sequence";

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

sub GetDistinctByRoleID {
    my ( $class, $roleID ) = @_;
    assert($roleID);

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

    my $sql =
      "SELECT DISTINCT contributor_id FROM " . kTable . " WHERE contributor_role_id=" . $dbo->DBQuote($roleID) . " ORDER BY sequence";

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

1;
