#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Catalog::Command::SearchProduct;
use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::LabelList;
use RPS::Catalog::Command::Search;
use RPS::Product::TypeList;

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use Common::Preference;

use RPS::SearchCommand;
use base 'RPS::SearchCommand';

sub cmd  { return 'search_product'; }
sub area { return 'catalog'; }

use constant kTemplate => "/app/tools/rps/templates/catalog/index.xsl";

sub pageSizePrefName  { 'catalog_search_pagesize' };

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

    my $pagesize = $self->setPagesize();

	my $response = $self->SUPER::execute(dbClass => 'RPS::DB::Item::Product',
										 xmlClass => 'RPS::Product::Product',
										 xmlTag => 'Product',
										 getAllMethod => 'GetAllExceptDigitalTrackProducts',
										 pageSize => $pagesize );
	return $response if $response;
	
	$self->{xml}{ProductCount} = $self->totalCount();

    $self->{xml}{TrackCount} = $self->getTrackCount();

	$self->{xml}{AlbumCount} = $self->getAlbumCount();
	
	$self->{xml}{TypeList}   = RPS::Product::TypeList->new();

	$response = RSApache::Response::XSLT->new($self->{xml}, kTemplate);
	return $response;
}

sub getAlbumCount {
	my $self = shift;

	my $collection;

    if( $self->getParam( 'Query' ) ) {
        $collection = RPS::DB::Item::Album->Search( $self->getParam( 'Query' ) );
    } else {
        $collection = RPS::DB::Item::Album->GetAll();
    }

	return $collection ? $collection->totalSize() : 0;
}

sub getTrackCount {
	my $self = shift;

	my $collection;

    if( $self->getParam( 'Query' ) ) {
        $collection = RPS::DB::Item::Track->Search( $self->getParam( 'Query' ) );
    } else {
        $collection = RPS::DB::Item::Track->GetAll();
    }

	return $collection ? $collection->totalSize() : 0;
}


1;
