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


# This is the base class for all the 'static' data sources.
# We're not going to try for a ton of modularity here, but
# I do want to share the caching strategy.
#

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

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;


sub Create
{
    my ($class, $dataPath, $payorID, $endDate) = @_;

    mkpath($dataPath) unless -d $dataPath;
    my $path = "$dataPath/".$class->FileName();
    $class->_Create($path, $payorID, $endDate);
}

# JPK - This caching code is interesting, but I am going to disable it for now.
#
sub DEPRECATED_Create
{
    my ($class, $dataPath, $payorID) = @_;

    
    my $md5 = $class->DBChecksum();

    # JPK - there is a bit of a problem with this scheme.
    # It's cumbersome to manage multiple files with similar but
    # not identical names...
    #
    # Instead, let's use the current fileName as another directory level.
    # The file can always be named 'data', and we'll store the checksum in a 'csm' file.
    #
    # It's knowing which one to use that seems challenging.
    #
    my $path = "$dataPath/".$class->FileName();
    mkpath($path) unless -d $path;

    
    my $filename = "$path/data";
        
    if (! -e $filename)
    {
        $class->_Create($filename, $payorID);
        open MD5, "> $path/csm" or die "Error $path/csm - $!";
        print MD5 "$md5\n";
        close MD5;
    }
    else
    {
        # Read in the previous checksum
        #
        my $oldMD5;
        if (-e "$path/csm")
        {
            open IN, "< $path/csm" or die "ERROR READING $path/csm - $!";
            $oldMD5 = <IN>;
            chomp $oldMD5;
            close IN;
        }

        
        # Rebuild if it doesn't match the current checksum.
        # Note that if it does match, we just exit - The cache is current.
        #
        if ($oldMD5 != $md5)
        {
            $class->_Create($filename, $payorID);
            open MD5, "> $path/csm" or die "Error $path/csm - $!";
            print MD5 "$md5\n";
            close MD5;
        }
    }


}



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

    # Each class needs to specify the tables it relies on, as
    # a comma-seperated list.
    # 
    my $tableList = $class->CacheQueryTableList();

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

    my $schema = $Common::RSDB::CLIENT_DB{$clientID}{'db_name'};

    my $sql = "SELECT MD5(GROUP_CONCAT(UPDATE_TIME)) FROM information_schema.tables WHERE TABLE_SCHEMA='$schema' AND TABLE_NAME IN ($tableList)";

    my $sth = $dbo->DoCmd($sql);
    my $md5 = $sth->fetchrow_arrayref->[0];

    return $md5;
}


sub EscapeArray
{
    my ($class, $ar) = @_;

    my @outArray;
    foreach my $field (@$ar)
    {
        push @outArray, $class->EscapeString($field);
    }

    return \@outArray;
}


sub EscapeString
{
    my ($class, $string) = @_;

    # We want to escape:
    # carriage returns
    # tabs
    # double-quotes
    # I suppose 'naked' backslashes.
    #

    $string =~ s/\\/\\\\/g;
    $string =~ s/\"/\\"/g;
    $string =~ s/\t/\\t/g;
    $string =~ s/\n/\\n/g;
    $string =~ s/\r/\\r/g;

    return $string;
}



1;
