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

use strict;
use warnings;
use Data::Dumper;

use Carp;

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

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

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

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

sub _ArchiveTableName {
    return 'archive_market';
}

sub GetAllByProductID {
    my $class = shift;
    my $productID = shift || confess;

    return $class->SUPER::GetAll(
        "SELECT * FROM " . kTable . " INNER JOIN product_market USING (market_id) WHERE product_id = " . $class->quote($productID) );
}

sub GetAllByRegionIDs {
    my ( $class, @regions ) = @_;
    assert( scalar @regions, "ERROR - no regions provided" );

    my $sql = "SELECT * FROM " . kTable . " WHERE region_id IN ( " . join( ',', @regions ) . ")";
    return $class->SUPER::GetAll($sql);
}
1;
