#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Support::JobQueue::Command::List;
use strict;
use warnings;

use lib '/app/tools/support/lib';
use base 'Support::Command::Submit';

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

use lib '/app/tools/job/lib';
use Job::Queue;
use Job::DB::Item::Job;

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

use constant kTemplate => "/app/tools/support/templates/job_queue/list.xsl";

#use constant kTemplate => "/app/tools/admin/templates/jobqueue/getqueue.xsl";

sub _argOrPref {
    my ( $self, $arg ) = @_;

    return $self->getParam($arg) if defined $self->getParam($arg);

    my $pref = Common::Preference::Get("jobqueue_$arg");
    return undef unless $pref;
    $self->setParam( $arg, $pref->value );
    return $pref->value;
}

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

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

    my $hideChildren = $self->_argOrPref('hideChildren');
    my $maxJobAge    = $self->_argOrPref('age');
    my $host         = $self->_argOrPref('host');
    my $class        = $self->_argOrPref('jobclass');
    my $subclass     = $self->_argOrPref('jobsubclass');
    my $client       = $self->_argOrPref('client');

    $maxJobAge = 24 unless $maxJobAge;

    # I want to also provide a list of all valid Classes and Subclasses - All that are in the table currently.
    #
    $self->{xml}{Classes} = Job::DB::Item::Job->GetAllClassesAndSubclasses();

    # ... and I want a list of hosts
    #
    $self->{xml}{Hosts} = Job::DB::Item::Job->GetAllRecentHosts();

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

    # Save last args as preferences, if they were specified
    #
    Common::Preference::SetPersistentCookie( 'jobqueue_age'          => $maxJobAge )    if ( defined $maxJobAge );
    Common::Preference::SetPersistentCookie( 'jobqueue_host'         => $host )         if ( defined $host );
    Common::Preference::SetPersistentCookie( 'jobqueue_jobclass'     => $class )        if ( defined $class );
    Common::Preference::SetPersistentCookie( 'jobqueue_jobsubclass'  => $subclass )     if ( defined $subclass );
    Common::Preference::SetPersistentCookie( 'jobqueue_client'       => $client )       if ( defined $client );
    Common::Preference::SetPersistentCookie( 'jobqueue_hideChildren' => $hideChildren ) if ( defined $hideChildren );
    Common::Preference::StoreCookies();

    return $response;
}

1;
