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

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::CatalogImport;
use Common::RSApp;
use Common::Util;
use Common::Assert;
use Common::FormObject;

use base 'Common::FormObject';

# For display purposes, a map of status code to an english description.
# Also putting a 'type' in here, to help the interface classify the states in some fashion.
# (I dunno, maybe put blinky lights around errors or something...)
#
my %gStatusMap = (
    BookPub::DB::Item::CatalogImport::kImportStatusFetchReady()    => { text => 'Waiting to Fetch',   type => 'wait', },
    BookPub::DB::Item::CatalogImport::kImportStatusStagingReady()  => { text => 'Waiting to Stage',   type => 'wait', },
    BookPub::DB::Item::CatalogImport::kImportStatusAnalysisReady() => { text => 'Waiting to Analyze', type => 'wait', },
    BookPub::DB::Item::CatalogImport::kImportStatusImportReady()   => { text => 'Waiting to Import',  type => 'wait', },
    BookPub::DB::Item::CatalogImport::kImportStatusFetchRun()      => { text => 'Fetching',           type => 'run', },
    BookPub::DB::Item::CatalogImport::kImportStatusStagingRun()    => { text => 'Staging',            type => 'run', },
    BookPub::DB::Item::CatalogImport::kImportStatusAnalysisRun()   => { text => 'Analyzing',          type => 'run', },
    BookPub::DB::Item::CatalogImport::kImportStatusImportRun()     => { text => 'Importing',          type => 'run', },
    BookPub::DB::Item::CatalogImport::kImportStatusErrorFetch()    => { text => 'Fetch Error',        type => 'error', },
    BookPub::DB::Item::CatalogImport::kImportStatusErrorStaging()  => { text => 'Staging Error',      type => 'error', },
    BookPub::DB::Item::CatalogImport::kImportStatusErrorAnalysis() => { text => 'Analysis Error',     type => 'error', },
    BookPub::DB::Item::CatalogImport::kImportStatusErrorImport()   => { text => 'Import Error',       type => 'error', },
    BookPub::DB::Item::CatalogImport::kImportStatusDone()          => { text => 'Complete',           type => 'done', },
    BookPub::DB::Item::CatalogImport::kImportStatusError()         => { text => 'Error',              type => 'error', },
    BookPub::DB::Item::CatalogImport::kImportStatusAborted()       => { text => 'Aborted',            type => 'error', },
);

# This static class method will return the catalog_import_id of the most
# recent catalog_import.
#
sub LastImportID {
    return BookPub::DB::Item::CatalogImport->LastCatalogImportID();
}

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

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

    my %properties;
    if ( $args{dbItem} ) {
        $self->_propertiesFromDBItem( \%properties, $args{dbItem} );
    } elsif ( $args{catalogImportID} ) {
        $self->_propertiesFromDB( \%properties, $args{catalogImportID} );
    } else {
        assert( 0, "ERROR - neither dbItem nor catalogImportID were specified!" );
    }

    $self->_initProperties( \%properties );

    return $self;
}

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

    my $dbItem = BookPub::DB::Item::CatalogImport->Lookup( catalog_import_id => $id );

    $self->_propertiesFromDBItem( $hrProperties, $dbItem );
}

sub _propertiesFromDBItem {
    my ( $self, $hrProperties, $dbItem ) = @_;

    $hrProperties->{catalog_import_id}   = $dbItem->catalog_import_id;
    $hrProperties->{status}              = $dbItem->status;
    $hrProperties->{date}                = $dbItem->date;
    $hrProperties->{fetch_start_time}    = $dbItem->fetch_start_time;
    $hrProperties->{fetch_end_time}      = $dbItem->fetch_end_time;
    $hrProperties->{fetch_job_id}        = $dbItem->fetch_job_id;
    $hrProperties->{staging_start_time}  = $dbItem->staging_start_time;
    $hrProperties->{staging_end_time}    = $dbItem->staging_end_time;
    $hrProperties->{staging_job_id}      = $dbItem->staging_job_id;
    $hrProperties->{analysis_start_time} = $dbItem->analysis_start_time;
    $hrProperties->{analysis_end_time}   = $dbItem->analysis_end_time;
    $hrProperties->{analysis_job_id}     = $dbItem->analysis_job_id;
    $hrProperties->{import_start_time}   = $dbItem->import_start_time;
    $hrProperties->{import_end_time}     = $dbItem->import_end_time;
    $hrProperties->{import_job_id}       = $dbItem->import_job_id;
}

sub _initProperties {
    my ( $self, $props ) = @_;

    # !!! I don't imagine that ANY of these fields will ever be editable.
    #
    $self->{CatalogImportID} = $props->{catalog_import_id};

    $self->{Status}     = $props->{status};
    $self->{StatusText} = $gStatusMap{ $props->{status} }{text};
    $self->{StatusType} = $gStatusMap{ $props->{status} }{type};

    $self->{Date} = Common::FormObject::Scalar::Date->new( value => $props->{date}, readOnly => 1 );

    $self->{FetchStartTime} = Common::FormObject::Scalar::DateTime->new( value => $props->{fetch_start_time}, readOnly => 1 );
    $self->{FetchEndTime}   = Common::FormObject::Scalar::DateTime->new( value => $props->{fetch_end_time},   readOnly => 1 );
    $self->{FetchJobID}     = $props->{fetch_job_id};

    $self->{StagingStartTime} = Common::FormObject::Scalar::DateTime->new( value => $props->{staging_start_time}, readOnly => 1 );
    $self->{StagingEndTime}   = Common::FormObject::Scalar::DateTime->new( value => $props->{staging_end_time},   readOnly => 1 );
    $self->{StagingJobID}     = $props->{staging_job_id};

    $self->{AnalysisStartTime} = Common::FormObject::Scalar::DateTime->new( value => $props->{analysis_start_time}, readOnly => 1 );
    $self->{AnalysisEndTime}   = Common::FormObject::Scalar::DateTime->new( value => $props->{analysis_end_time},   readOnly => 1 );
    $self->{AnalysisJobID}     = $props->{analysis_job_id};

    $self->{ImportStartTime} = Common::FormObject::Scalar::DateTime->new( value => $props->{import_start_time}, readOnly => 1 );
    $self->{ImportEndTime}   = Common::FormObject::Scalar::DateTime->new( value => $props->{import_end_time},   readOnly => 1 );
    $self->{ImportJobID}     = $props->{import_job_id};
}

###
1;    #
###
