#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::Catalog::Command::ListBooks;
use strict;
use warnings;

use lib '/app/tools/bookpub/lib';
use BookPub::Catalog::BookList::WithAuthor;
use BookPub::Catalog::BookList::WithAuthor::ByImprintID;
use BookPub::Catalog::BookList::FullDisplay;
use BookPub::Catalog::BookList::FullDisplay::ByContributorID;
use BookPub::Catalog::ImprintList;
use BookPub::DB::Item::Book;
use BookPub::Command;
use base 'BookPub::Command';

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

sub pageName { return 'List Books'; }
sub cmd      { return 'list_books'; }
sub area     { return 'catalog'; }

use constant kTemplate => "/app/tools/bookpub/templates/catalog/list_books.xsl";

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

    my $response = $self->SUPER::execute();
    return $response if $response;

    # Not going to allow any sort of 'all' view - Just far too much data for that to ever work out...
    #
    my $page = $self->getParam('page');
    $page = 1 unless defined $page;

    my $list;
    my %listArgs;
    $listArgs{currentPage} = $page;

    if ( $self->getParam('ImprintID') ) {
        $listArgs{imprintID}  = $self->getParam('ImprintID');
        $listArgs{collection} = BookPub::DB::Item::Book->GetAllForImprintSortedByTitle( $self->getParam('ImprintID') );
        $list                 = BookPub::Catalog::BookList::WithAuthor::ByImprintID->new(%listArgs);
    } elsif ( $self->getParam('ContributorID') ) {
        $listArgs{contributorID} = $self->getParam('ContributorID');
        $listArgs{collection}    = BookPub::DB::Item::Book->GetAllForContributorSortedByTitle( $self->getParam('ContributorID') );

        # Going to try using a 'richer' Book class here.
        #
        $list = BookPub::Catalog::BookList::FullDisplay::ByContributorID->new(%listArgs);
    } else {
        $listArgs{collection} = BookPub::DB::Item::Book->GetAllSortedByTitle();
        $list = BookPub::Catalog::BookList::WithAuthor->new(%listArgs);
    }

    $self->{xml}{BookList} = $list;

    # Let's only show the imprint list if we're doing an imprint-only search.
    #
    if ( $self->getParam('ImprintID') ) {
        $self->{xml}{ImprintList} = BookPub::Catalog::ImprintList->new();
    }

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

1;
