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

use Data::Dumper;

use lib '/app/tools/support/lib';
use Support::JobQueue::SkipRuleForm;
use Support::JobQueue::RunRuleForm;
use Support::JobQueue::Command::EditRules;

use base 'Support::Command::Submit';

use lib '/app/tools/common/lib';
use RSApache::Response::XSLT;
use RSApache::Message;
use Common::Client::ClientList;

use lib '/app/tools/job/lib';

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

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

sub write_command { 1; }

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

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

    my $hostname = $self->getParam('host');
    my $skipForm = Support::JobQueue::SkipRuleForm->new();

    if ( $skipForm->assignCGIParams( 'SkipRuleForm', $self->getParams() ) && $skipForm->validate() ) {
        $skipForm->save();
        my $successMessage = RSApache::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
        my %showParams = (
            msg  => $successMessage->toString(),
            host => $hostname,
        );
        my $command = Support::JobQueue::Command::EditRules->new(%showParams);
        return RSApache::Response::Redirect->new( $command->getRedirect() );

    } else {
        $self->{xml}{Error} = "Validation Failed";
    }

    my $runForm = Support::JobQueue::RunRuleForm->new( hostname => $hostname );

    # We'll want to list all the hosts that have any entries of any sort in the job_run_rule table.
    #
    my @hosts = Job::DB::Item::JobRunRule->GetAllHosts();
    $self->{xml}{HostList}{Host} = \@hosts;

    $self->{xml}{SkipRuleForm} = $skipForm;
    $self->{xml}{RunRuleForm}  = $runForm;

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

    return $response;
}

1;
