#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2010 RoyaltyShare, Inc.      All Rights Reserved
#---------------------------------------------------------------
package Job::SkipRuleList;

use strict;
use Data::Dumper;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::XMLObject;

use lib '/app/tools/job/lib';
use Job::DB::Item::JobSkipRule;
use Job::SkipRule;

use base 'Common::XMLObject';

# This class will contain a list of all the skip rules.
# The results are cached and are updated automatically if changes are detected in
# the database.
#
# This also contains a method to determine if a rule can be run based on skiprules

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

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

    $self->refresh();

    return $self;
}

sub refresh {
    my $self = shift;

    my $cacheTableMD5 = Job::DB::Item::JobSkipRule->getTableMd5( $self->{_last_table_md5} );

    # This means there has been an update since we last checked the cache
    unless ( $cacheTableMD5 == $self->{_last_table_md5} ) {
        Common::Log::Debug("Refreshing skiprule cache");
        $self->{SkipRule} = [];

        my $collection = Job::DB::Item::JobSkipRule->GetAll();

        while ( my $obj = $collection->next() ) {
            push @{ $self->{SkipRule} }, Job::SkipRule->new( dbItem => $obj );
        }

        $self->{_last_table_md5} = $cacheTableMD5;

        #Common::Log::Debug( "RESULT: \n" . Dumper $self->{Rule} );
    }
}

sub SkipRules {
    my $self = shift;
    $self->refresh();
    return $self->{SkipRule};
}

1;
