#------------------------------------------------------------
# Copyright (C) 2008 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Raptor::Tracker::Job::ImportSalesFile;

use strict;
use warnings;

use Sys::Hostname;

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

use lib '/app/tools/raptor/lib';
use Raptor::Config;

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

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

use base 'Job::Job';

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

    # Let's go ahead and construct everything now.
    #
    my $clientID = $self->clientID();

    my $fileID = $self->getCommandLineArg('fileID');
    assert( $fileID, "ERROR - missing fileID" );

    my $config     = Raptor::Config->new();
    my $scriptPath = $config->get('sale_import_script_dir');

    my $commandLine = "nice $scriptPath/import_sales_file.pl $clientID $fileID";
    return $commandLine;
}

# We're going to want to access a File::File object at various points.
# This method gives us the flexibility to lazily fetch it when necessary.
#
sub _getFileObj {
    my ($self) = @_;

    if ( !$self->{_file} ) {
        my $clientID = $self->clientID();
        my $fileID   = $self->getCommandLineArg('fileID');
        assert($fileID);
        assert($clientID);

        $self->{_file} = RPS::File::File->new( file_id => $fileID, client_id => $clientID );
    }

    return $self->{_file};
}

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

    return 'import.log';
}

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

    # This will get called by the job wrapper, at a point where we don't have
    # a client-specific RSApp singleton.   So we need to create a temporary singleton
    # so that this File object can be retrieved.
    #
    my $tempApp = Common::RSApp->new_temporary( clientID => $self->clientID() );
    my $file = $self->_getFileObj();
    return $file->FileDir();
}

sub _defaultClass    { return 'Tracker'; }
sub _defaultSubclass { return 'ImportSalesFile'; }

#sub _defaultHostname    { return hostname(); }
sub _defaultPriority { return Job::Job::kPriorityHigh; }

sub enqueue {
    my ( $self, %queueArgs ) = @_;

    $self->SUPER::enqueue();

    my $file = $self->_getFileObj();
    $file->FileStatus(File::File::STATUS_IN_QUEUE);
    $file->Save();
}

###
1;    #
###

