#------------------------------------------------------------
# Copyright (C) 2007 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------

#------------------------------------------------------------
# Validation Rule ISRCRequired Object.
#
# Parameter Requirement:
#
#    The parameter used in config_rule must be a:
#       <not used>
#
# Base Class:
#   Validation::Rule
#
# Public methods:
#   + Validate::Rule::ISRC new();
#   + $fail_status         ok();
#   + $scalar              AUTOLOAD( $scalar );
#------------------------------------------------------------
package Validate::Rule::Constraint::ISRCRequired;
use strict;

use lib '/app/tools/common/lib';
use Common::Log;
use Common::RSApp;
use Common::Assert;

use lib '/app/tools/metadata/lib';
use base 'Validate';
use base 'Validate::Rule';

use vars qw($AUTOLOAD);

###
# Define addition public accessors
###
use constant kClassAttributes => qw( message paramter fail_status );

###
# Default failure message if the OK method fails.
###
use constant kFailureMessage => "required for digital products";

#------------------------------------------------------------
# Public Methods
#------------------------------------------------------------

#------------------------------------------------------------
# Constructor
#------------------------------------------------------------
sub new {
    my ( $class, %args ) = @_;
    my $self = bless {}, $class;

    return $self->_init(%args);
}

#------------------------------------------------------------
# $scalar AUTOLOAD( $scalar )
#
# Accessor methods for this object.  Methods to access the
# private data in this object.  Valid members are defined in
# the kClassAttributes constant.
#
# If a scalar is passed in this method will set the
# corrisponding private data member.
#
# This method always returns the value of the corrisponding
# private data member.
#
# This method is almost identical base class AUTOLOAD method
# except the method calls the parent AUTOLOAD instead of
# dying when an unknown attribute was seen.
#
# Parameters:
#   $scalar - Set the private data member to this value.
#             ** optional **
#
# Return:
#   $scalar - private memeber value
#------------------------------------------------------------
sub AUTOLOAD {
    my $self  = shift;
    my $value = shift;
    my $field = $AUTOLOAD;
    $field =~ s/.*://;    # Strips fully-qualified portion.

    my $attr = "_$field";

    return if $field eq 'DESTROY';    # skip DESTROY and all-cap methods

    unless ( $self->{_attributes}->{$field} ) {
        my $superior = "SUPER::$field";
        $self->$superior(@_);
    }

    # All preconditions are validated.  Let's do some work.
    $self->{$attr} = $value
      if ( defined($value) );

    return $self->{$attr};
}

#------------------------------------------------------------
# $fail_status ok( $target_data )
#
# Run external code source.  If the code returned a true
# value we return a success otherwise we return a failure.
#
# The function's first parameter must be a single scalar,
# the target data, then the rest of the argumenst are passed
# in via a hash.
#
#   i.e. is_vaild_region( $target_data, country => 'US' );
#
# PRE:
#   The parameter for this object must be a code reference.
# Parameters:
#   $target_data - Data we will be testing.
#
# Return:
#   $fail_status - kStatusOK (0) if the test passes, any
#                  non-zero should be considered a failure.
#
#                  NOTE:  If $target_data is undefined then
#                         return kStatusOK.
#------------------------------------------------------------
sub ok {
    my ( $self, $target, %args ) = @_;
    my $fail =
      defined( $self->{_fail_status} )
      ? $self->{_fail_status}
      : Validate::kStatusDefault;
    my $success = Validate::kStatusOK;

    return undef unless ( $self->is_configured() );

    if ( $args{digital_upc} ) {
        return $fail unless ($target);
    }

    elsif ( RPS::DB::Item::Master->HasDigitalProduct( isrc => $args{isrc} ) ) {
        return $fail unless ($target);
    }

    return $success;
}

#------------------------------------------------------------
# Private Methods
#------------------------------------------------------------

#------------------------------------------------------------
# Validate::Rule _init()
# Initialize the object.
#
# - Build accessor method hash
# - set internal private data ( message, fail_status, and
#   parameter ). See the base class for parameter defintions.
#
#------------------------------------------------------------
sub _init {
    my ( $self, %args ) = @_;

    # Build a hash with our valid class attributes
    foreach ( (kClassAttributes) ) {
        $self->{_attributes}->{$_}++;
    }

    $self->message( $args{message}                    ? $args{message}     : kFailureMessage );
    $self->fail_status( defined( $args{fail_status} ) ? $args{fail_status} : Validate::kStatusDefault );
    $self->parameter( $args{parameter} ) if ( $args{parameter} );

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

    return $self;
}

1;
