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

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

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

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

sub cmd  { return "license_income"; }
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 $fileID = $self->getParam('file');

    my $fileObj = Raptor::Tracker::LicenseIncomeFile->new( fileID => $fileID );

    if ( $self->getParam('formSubmit') ) {
        if ( $fileObj->assignCGIParams( 'LicenseIncomeFile', $self->getParams() ) && $fileObj->validate() ) {
            $fileObj->save();

            # We want to change views, so we want to redirect to the Show command now.
            #
            my $successMessage = RSApache::Message->new( type => "success", code => Common::FormObject::kSuccessFormSubmit );
            my %showParams = ( msg => $successMessage->toString(), );

            # I'd like to redirect back to the main Tracker page.
            #
            my $command = Raptor::Tracker::Command::Main->new(%showParams);
            return RSApache::Response::Redirect->new( $command->getRedirect() );
        } else {
            $fileObj->setError(Common::FormObject::kErrFormSubmit);
            $self->addMessageXML( type => "error", code => Common::FormObject::kErrFormSubmit );
        }
    }

    $self->{xml}{LicenseIncomeFile} = $fileObj;

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

    return $response;
}

1;
