#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::ArtistRoyalty::Job::CreateStatement;

use strict;
use warnings;

use Sys::Hostname;

use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/rps/lib';
use RPS::Config;

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

use base 'RPS::ArtistRoyalty::Job::ChildJob';


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

    my $statementID = $self->getCommandLineArg('statementID');
    assert($statementID);

    my $clientID = $self->clientID();
    assert($clientID);

    my $logLevel = $self->getCommandLineArg('logLevel');
    $logLevel = 1 unless $logLevel;

    # !!! Use the 'config' system to get the path to the royalty scripts.
    #
    my $rpsConfig = RPS::Config->new();
    my $scriptPath = $rpsConfig->get('royalty_script_dir');


    # !!! Note that this is a linux-only command line.
    # !!! If we end up wanting to run these on windows <shudder>, we'd have
    # !!! to create a 'Win32' subclass and override this method.
    # !!! Actually, we'd need to create a 'linux' subclass as well.
    #
    my $commandLine = "nice $scriptPath/create_artist_royalty_statement.pl -c $clientID -s $statementID -V $logLevel";

    return $commandLine;
}


# This will just be the path in which the file will live.
#
sub generateLogBasePath
{
    my ($self) = @_;

    my $clientID = $self->clientID();
    assert($clientID);
    my $runID = $self->getCommandLineArg('runID');

    return (ref($self))->LogFilePath($clientID, $runID);
}


sub _defaultSubclass
{
    return 'ArtistRoyalties';
}


sub _defaultClass
{
    return 'CreateStatement';
}


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

    my $statementID = $self->getCommandLineArg('statementID');
    assert($statementID);

    return "statement_$statementID";

}


sub LogFilePath
{
    my ($class, $clientID, $runID) = @_;
    assert($clientID);
    assert($runID);

    my $rpsConfig = RPS::Config->new();
    my $basePath = $rpsConfig->get('royalty_log_dir');

    my $clientData = Common::DB::Item::Client->Lookup(client_id => $clientID);

    $basePath .= '/' . $clientData->client_name_clean . "/artist/$runID";

    return $basePath;
}

# Going to introduce some randomness into the priority values
# This is to avoid having all the jobs for a single run stuck together
# in a single clump - Otherwise, huge runs will prevent smaller runs from
# going until the big run is done, which is not what I want.
#
sub _defaultPriority
{
    my $basePriority = Job::Job::kPriorityNormal;
    return ( $basePriority + int( rand($basePriority) ));
}


###
1;#
###
