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

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

use lib '/app/tools/bookpub/lib';
use BookPub::Tracker::File;
use BookPub::Tracker::Job::DumpAllPriceExceptions;
use BookPub::Tracker::Command::Main;
use BookPub::Sale::Report::PriceExceptions::AllFiles;

use base 'BookPub::Command';

sub cmd      { return "all_price_exceptions"; }
sub area     { return "tracker"; }
sub pageName { return "Create Price Exceptions Report"; }

use constant kTemplate => '/app/tools/bookpub/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 $report = BookPub::Sale::Report::PriceExceptions::AllFiles->new();

    my $baseDir = $report->_baseDir();    # !!! Perhaps this doesn't need to be private?
    if ( !-d $baseDir ) {
        File::Path::mkpath($baseDir) || die "ERROR - Unable to create directory $baseDir: $!";
    }

    my $fileOnDisk = $report->filePath();

    # Nuke the old file, then create the 'temp' file right away.
    # This will insure that the 'File generating...' message will appear immediately.
    #
    unlink $fileOnDisk if ( -e $fileOnDisk );
    my $tempFile = $fileOnDisk . '-tmp';
    `touch $tempFile`;

    my $jobArgs = BookPub::Tracker::Job::DumpAllPriceExceptions->new();
    my $job     = $jobArgs->enqueue();

    # Redirect back to the tracker page.
    #
    my $newCmd = BookPub::Tracker::Command::Main->new( page => $self->getParam('page') );
    return RSApache::Response::Redirect->new( $newCmd->getRedirect() );
}

1;
