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

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

use lib '/app/tools/rps/lib';
use Common::FormObject;
use RPS::Master::Master;

use RPS::Command;
use base 'RPS::Command';

sub cmd  { return 'list'; }
sub area { return 'master'; }

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

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

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

    my $searchCollection;
    $searchCollection = RPS::DB::Item::Master->Get( order => $self->getParam("order") );

    my $page = $self->getParam("page") || 1;
    my $perPage = 50;

    my $count = 0;
    while ( $searchCollection->hasNext() ) {
        my $masterObj = $searchCollection->next();

        if ( $count >= ( $page - 1 ) * $perPage && $count < $page * $perPage ) {
            push @{ $self->{xml}{Master} }, RPS::Master::Master->new( _dbItem => $masterObj, loadSubs => 0 );
        }

        $count++;
    }

    # TODO: we probably need to add pagination xml so the template knows which links to display

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

1;
