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

package RPS::LabelRoyalty::Fast::Static::LabelTerms;

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 Data::Dumper;
use DB_File;

use Common::Log;

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


use constant kLabelID               => 0;
use constant kLabelContractTermID   => 1;
use constant kLabelContractID       => 2;
use constant kLabelPayeeID          => 3;
use constant kDistributionFee       => 4;


sub FileName { 'label_terms' }
sub CacheQueryTableList { "'label_contract_group_term','label_contract_group_term_label','label_contract'"};


sub _Create
{
    my ($class, $filename, $payorID) = @_;

    # We just need a one-to-many mapping.
    # Could use BTREE with allow_dups, I suppose.
    # Or just serialize the array.
    # Let's try using DB_BTREE.

    my %hash;
    $DB_BTREE->{'flags'} = R_DUP;
    tie %hash, "DB_File", $filename, O_RDWR | O_CREAT, 0666, $DB_BTREE or die "Error opening $filename: $!\n";


    my $dbo = Common::RSApp::GetClientDB();

    my $sql = "SELECT"
     . " label_contract_group_term_label.label_id,"
     . " label_contract_group_term.label_contract_group_term_id,"
     . " label_contract_group_term.label_contract_id,"
     . " label_contract.label_payee_id,"
     . " label_contract_group_term.distribution_fee"
     . " FROM label_contract_group_term"
     . " JOIN label_contract USING (label_contract_id)"
     . " JOIN label_contract_group_term_label USING (label_contract_group_term_id)"
     . " JOIN label_payee ON (label_contract.label_payee_id = label_payee.label_payee_id)"
     . " WHERE label_payee.status in (1,2)"
     . " AND payor_id = $payorID";

    # !!! The data structure handles dups, so no need to sort the query result.

    my $sth = $dbo->DoCmd($sql);

    while (my $row = $sth->fetchrow_arrayref())
    {
        # Note that because this is a tied array, backed by a DB_BTREE with R_DUP enabled, we can store
        # multiple values in a single key.
        #
        $hash{ $row->[kLabelID] } = join(',', @$row);
    }

    untie %hash;
}


my %gHash;
sub GetLabelTermData
{
    my ($class, $dataPath) = @_;

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

    # We have to return the '$dbObj' reference in order to allow us to call the 'get_dup' interface to retrieve
    # multiple items per key. 
    # !!! I don't know whether we can allow the hash to go out of scope, so I am using a slimy global variable.
    #
    # I am making an assumption here that this is faster than using a 'normal' DB_HASH, and just serializing an array.
    # Probably work testing someday...
    #
    my $dbObj = tie %gHash, "DB_File", $filename, O_RDONLY, 0666, $DB_BTREE or die "Error opening $filename: $!\n";

    return $dbObj;
}



1;
