package RSApache::Response::ReportDownload;

use strict;
use warnings;

use Apache2::Const qw(OK);

use CGI;

use lib '/app/tools/common/lib';
use Common::WriteXML;
use Common::Assert;
use Common::Log;
use RSApache::Response::XML;

use RSApache::Response;
use base 'RSApache::Response';

sub _init {
    my ( $self, $report, $fileName ) = @_;
    assert( defined $report );

    $fileName = 'RoyaltyShareFileDownload' unless $fileName;

    $self->{report}   = $report;
    $self->{fileName} = $fileName;
}

sub respond {
    my ( $self, $apache ) = @_;

    my $cgi = new CGI;

    $apache->content_type("application/octet-stream");
    $apache->headers_out->set( 'Content-Disposition' => "attachment; filename=\"$self->{fileName}\"" );

    $apache->send_http_header();

    print $self->report->getHeader() . "\n";

    while ( my $row = $self->report->getNextRow() ) {
        print "$row\n";
    }

    return OK;
}

sub report { shift->{report} }

###
1;    # Play nicely.
###
