#------------------------------------------------------------
# Copyright (C) 2007 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package Metadata::Rule::Album::UniqueClientID;
use strict;

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

use lib '/app/tools/metadata/lib';
use Metadata::DB::Item::ContentImportData;

use base 'Metadata::Rule::Album';

use vars qw($AUTOLOAD);

###
# Default failure message if the OK method fails.
###
use constant kFailureMessage => "Duplicate client id detected in import file";

use constant kClassAttributes => qw();
use constant kClientIDField   => 'client_album_id';

#------------------------------------------------------------
# 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 $success = Validate::kStatusOK;
    my $fail =
      defined( $self->{_fail_status} )
      ? $self->{_fail_status}
      : Validate::kStatusDefault;

    return $success unless ( $self->target_field() );
    my $field = $self->target_field();

    my $oImport = Metadata::DB::Item::ContentImportData->Lookup( content_import_data_id => $args{id} );
    return $success unless ($oImport);

    my $target_data = $oImport->$field();
    return $success unless ( defined($target_data) && length($target_data) );

    return $success unless ($target_data);

    my @fields = (kClientIDField);

    my $has_dups = Metadata::DB::Item::ContentImportData::HasImportDuplicates(
        fields   => \@fields,
        target   => $target_data,
        group_by => $self->unifying_field()
    );

    return $has_dups ? $fail : $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->SUPER::_init(%args);

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

    return $self;
}
1;
