#---------------------------------------------------------------
# ____                   _ _         ____  _                    
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___ 
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/                            
#
# Copyright (C) 2012 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------


# !!! This is the 'Final' class from ArtistRoyalty.   It assumes that the final product
# is a db table, which is not actually true for label royalties.
#
# I think I may make this a base class, then have two intermediate classes that overload Commit - 
# The DB classes will use mysqlimport, while the output file will just copy it to it's final destination
# (which I belive is on the shared filesystem).

package RPS::LabelRoyalty::Fast::Final::DB;

use strict;
use Data::Dumper;
use File::Path;

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

use IO::File;

use Common::Log;

use base 'RPS::LabelRoyalty::Fast::Final';

# We'll maintain some hashes to store derived class global data.
# The class package will be the hash key.
#
my %gFD;
my %gCurrentID;


sub Init
{
    my ($class, $outputDirectory) = @_;

    $class->SUPER::Init($outputDirectory);


    # Figure out the current max statement id, and store
    # that as our current id. We'll increment this every time we
    # instiate a new line object.
    #
    $gCurrentID{$class} = $class->GetCurrentMaxID();
}

sub GetCurrentMaxID
{
    my ($class) = @_;

    my $dbo = Common::RSApp::GetClientDB();
    my $tableName = $class->FileName();
    my $sql = "SHOW TABLE STATUS LIKE '".$tableName."'";
    my $sth = $dbo->DoCmd($sql);
    my $hr = $sth->fetchrow_hashref();
    my $id = $hr->{'Auto_increment'};
    assert($id);
    return ($id - 1);
}

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

    my $class = ref($self);
    return $class->_CurrentID();
}

sub _CurrentID
{
    my ($class) = @_;
    return $gCurrentID{$class};
}

sub _IncrementID
{
    my ($class) = @_;
    $gCurrentID{$class}++;
}

sub Commit
{
    my ($class, $dataPath) = @_;


    my $filename = "$dataPath/".$class->FileName();

    my $clientID = Common::RSApp::GetClientID();


    my $f = $class->Fields();
    my $fields = join(',', @$f);
    my @command = 
    (
        'mysqlimport',
        '--local',
        '--fields-optionally-enclosed-by=\"',
        "--columns=$fields",
        "--user=" . $Common::RSDB::CLIENT_DB{$clientID}->{username},
        "--password=" . $Common::RSDB::CLIENT_DB{$clientID}->{password},
        "--host=" . $Common::RSDB::CLIENT_DB{$clientID}->{server},
        $Common::RSDB::CLIENT_DB{$clientID}->{db_name},
        $filename,
    );
    Log->warn("COMMAND: " . join(' ', @command));

    system(@command);
}

1;
