#------------------------------------------------------------
# Copyright (C) 2007 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
#
#------------------------------------------------------------
# Validation Album Common Object.  Extends the Validate
# Rule class.  This object contains the method to do
# validation on piece of data.
#
# Test the target field to ensure all rows contain the same
# data.
#
# Base Class:
#   Validation::Rule
#
# Public methods:
#   + Metadata::Rule::Album::Common new();
#   + $fail_status         ok();
#   + $scalar              AUTOLOAD( $scalar );
#------------------------------------------------------------
package Metadata::Rule::Album::Common;
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 'Metadata::Rule::Album';

use Validate::Util qw( is_sequence );

use vars qw($AUTOLOAD);

###
# Default failure message if the OK method fails.
###
use constant kFailureMessage => "Row mismatch.  Values are not equal";

use constant kClassAttributes => qw( required_field );

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

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

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

#------------------------------------------------------------
# $fail_status ok( $target_data )
#
# Check for multually exclusive fields.
#
# 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.
#------------------------------------------------------------
sub ok {
    my ( $self, $target, %args ) = @_;
    my $fail =
      defined( $self->{_fail_status} )
      ? $self->{_fail_status}
      : Validate::kStatusDefault;

    my @sequence;
    my ( $minimum, $maximum );

    my $success = Validate::kStatusOK;
    my $dbo     = Common::RSApp::GetClientDB();

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

    return $success unless ( defined($target) );

    my $sql =
        "SELECT COUNT(DISTINCT(IF("
      . $self->target_field()
      . " IS NULL, 1, "
      . $self->target_field
      . "))) as target " . "FROM "
      . Metadata::DB::Item::ContentImportData::kTable . " "
      . "WHERE "
      . $self->unifying_field() . " = "
      . $dbo->DBQuote($target);

    if ( $self->required_field ) {
        $sql .= " AND " . $self->required_field . " IS NOT NULL ";
    }

    Common::Log::Debug("QUERY: $sql");
    my $sth = $dbo->DoCmd($sql);

    my $row = $sth->fetchrow_hashref();

    return $row->{target} <= 1 ? $success : $fail;
}

#------------------------------------------------------------
# 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->SUPER::_init(%args);

    $self->message( $args{message} ? $args{message} : kFailureMessage );

    return $self;
}
1;
