#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::Catalog::Import::Process;
use strict;
use warnings;
use Data::Dumper;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use Common::RSApp;
use Common::Util;
use Common::Assert;
use Common::Process;
use Common::Locale;

use base 'Common::Process';

# Putting several common methods in here.

sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    $self->{_importRecord} = $args{importRecord};
    assert( $self->{_importRecord} );

    return $self;
}

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

    return $self->{_importRecord}->date();
}

sub _config {
    my ($self) = @_;
    if ( !$self->{_config} ) {
        $self->{_config} = BookPub::Config->new();
    }

    return $self->{_config};
}

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

    return $self->{_importRecord}->catalog_import_id();
}

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

    if ( !$self->{_baseStagingPath} ) {
        $self->_report( "  generating the path from config", 3 );

        # Get the base path from the config system.
        # Then tack on the client clean name, and the catalog_import_id.
        #
        my $config = $self->_config();
        my $client = Common::Client::Current();

        my $basePath  = $config->get('catalog_import_staging_dir');
        my $cleanName = $client->ClientNameClean();
        my $id        = $self->_catalogImportID();

        $self->{_baseStagingPath} = "$basePath/$cleanName/$id";
    }

    $self->_report( "  returning this path:'" . $self->{_baseStagingPath} . "'", 4 );
    return $self->{_baseStagingPath};
}

# Unlike the regular logging interface,  this method is intended for messages
# that will be emailed to interested parties after the entire process is completed.
# We'll just use a log file, and at the end slurp up the contents to mail.
#
sub errorFilePath {
    my ($self) = @_;
    if ( !$self->{_errorFilePath} ) {
        my $basePath = $self->_getBaseStagingPath();
        $self->{_errorFilePath} = $basePath . "/ERROR_LOG";
    }

    return $self->{_errorFilePath};
}

sub _writeError {
    my ( $self, $message ) = @_;

    my $errorFilePath = $self->errorFilePath();
    open ERROR_LOG, ">> $errorFilePath" or die "FATAL ERROR - Unable to open $errorFilePath for updating: $!";
    print ERROR_LOG "$message\n";
    close ERROR_LOG;
}

# Cache
sub _getFromCache ($$;@) {
    my ($self, $key, @args) = @_;

    unless ( exists $self->{_cache}{$key} ) {
        return $self->_loadCache($key, @args);
    }

    return $self->{_cache}{$key};
}

sub _loadCache ($$;@) {
    my ($self, $key, @args) = @_;

    my %preparedMap = (
        a_common_all_languages         => \&Common::DB::Item::Language::getAllLanguages,
        h_common_countries             => \&Common::DB::Item::Country::getAllWithCustom,
        h_client_all_contributor       => \&BookPub::DB::Item::CatalogImportItem::Contributor::getSimpleAsCatalogImportIdCleanName,
        h_client_all_contributor_role  => \&BookPub::DB::Item::ContributorRole::getAllAsOnixCode,
        a_client_find_matching_region  => \&BookPub::DB::Item::CatalogImportItem::Region::getAllForMatchingRegion,
        a_common_find_matching_region  => \&BookPub::DB::Item::Region::getAllForMatchingRegion,
        h_client_countries_territories => \&BookPub::DB::Item::CatalogImportItem::Region::getAllCountryAndTerritory,
    );

    if ( exists $preparedMap{$key} ) {
        my $method = $preparedMap{$key};
        delete $self->{_cache}{$key};
        $self->{_cache}{$key} = &$method(@args);
    }

    return $self->{_cache}{$key};
}

sub _clearCache ($;$) {
    my ($self, $key) = @_;
    delete $self->{_cache} unless defined $key;
    delete $self->{_cache}{$key} if exists $self->{_cache}{$key};
    return 1;
}


1;
