#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Raptor::Tracker::Command::AllExceptionsReport;
use strict;
use warnings;

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

use lib '/app/tools/raptor/lib';
use Raptor::Tracker::Command::Main;
use Raptor::Tracker::Job::DumpAllExceptions;

use Raptor::Command;
use base 'Raptor::Tracker::Command::Base';

sub cmd  { return "allexceptions"; }
sub area { return "tracker"; }

use constant kTemplate => '/app/tools/raptor/templates/tracker.xsl';

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

    # Give any inherited methods the first crack at this.
    # (This checks for login and basic access permissions).
    #
    my $response = $self->SUPER::execute();
    return $response if $response;

    my $filename = $self->getAllExceptionsFilename;

    ## delete file if it exists
    unlink $filename if ( -e $filename );
    ## delete temp file if it exists
    unlink $filename . "-tmp" if ( -e $filename . "-tmp" );

    # I'm doing this so that the "File generating..." message shows up immediately.
    #
    my $tmpFilename = $filename . "-tmp";
    `touch $tmpFilename`;

    # Create the new job, and put it in the queue.
    #
    my $jobArgs = Raptor::Tracker::Job::DumpAllExceptions->new( fileName => $filename );
    my $job = $jobArgs->enqueue();

    ## start script running
    #my $script = "/app/tools/sale_import/bin/dump_all_exceptions.pl";
    #my $args   = Common::RSApp::GetClientID()." ".$filename;
    #system("$script $args &");
    #sleep 1;

    my $page = $self->getParam('page');

    my %showParams = ( page => $page, );
    my $command = Raptor::Tracker::Command::Main->new(%showParams);
    return RSApache::Response::Redirect->new( $command->getRedirect() );
}

1;
