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

#------------------------------------------------------------
# A validation response.  Result object return from a
# validate call using a Validate::Item object.  This object
# contains results for all rules executed on a given piece
# if data.
#
# Public methods:
#   + Validate::Response new();
#   + void               add_error( %args );
#   + bool               is_valid();
#   + @error_hashes      errors();
#   + int                error_code();
#   + $error_messages    error_as_string( $delimeter );
#
#------------------------------------------------------------

package Validate::Response;
use strict;

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

use Validate;

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

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

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

#------------------------------------------------------------
# void add_error( %args )
#
# Add an error to this object.  An error is added using a
# has with the following paramerters;
#
# PRE: All parameters are not null
#
# Parameters:
#   rule        -  The object type of the rule that generated
#                  the error.
#
#   message     -  The error message generated by the rule.
#
#   code        -  Error code returned by the rule.
#------------------------------------------------------------
sub add_error {
    my ( $self, %args ) = @_;
    assert($self);
    assert( $args{code},    "code required in response" );
    assert( $args{message}, "message required in response" );
    assert( $args{rule},    "rule required in response" );

    push( @{ $self->{_errors} }, \%args );
}

#------------------------------------------------------------
# @errors errors()
#
# Return all error hashes contained in this object.  Error
# hashs contain message, rule, and code keys.  An empty set
# is returned if the object doesn't contain any errors.
#------------------------------------------------------------
sub errors {
    my ($self) = @_;
    assert($self);

    return undef unless ( $self->{_errors} );

    return $_->{_errors};
}

#------------------------------------------------------------
# $error_code error_code
#
# Return the largest error code found in the error code list
# contained in this object.  If the list is empty return 0.
#------------------------------------------------------------
sub error_code {
    my ($self) = @_;
    assert($self);

    my $error_code = 0;

    return undef unless ( $self->{_errors} );

    foreach ( @{ $self->{_errors} } ) {
        $error_code = $_->{code}
          if ( $_->{code} && $_->{code} > $error_code );
    }

    return $error_code;
}

#------------------------------------------------------------
# $error_string error_as_string( $delimeter )
#
# Return all the error message joined together by a
# $delimeter.  The default delimeter is a space (', ').
#
# If the list is empty return undef.
#------------------------------------------------------------
sub error_as_string {
    my ( $self, $delimeter ) = @_;
    my @error;
    assert($self);
    $delimeter = ', ' unless ($delimeter);

    my $error_string;

    return undef unless ( $self->{_errors} );

    foreach ( @{ $self->{_errors} } ) {
        push( @error, $_->{message} ) if ( $_->{message} );
    }

    return @error ? join( $delimeter, @error ) : undef;
}

#------------------------------------------------------------
# bool is_valid()
#
# Return true if the response has no errors and false (undef)
# if the object contains errors.
#------------------------------------------------------------
sub is_valid {
    my $self = shift;
    assert($self);

    return 1 unless ( $self->{_errors} );

    return undef;
}

#------------------------------------------------------------
# Validate::Response _init()
#
# Class initializer.  This doesn't do anything for this
# object.  It's just here for convention.
#------------------------------------------------------------
sub _init {
    my ( $self, %args ) = @_;

    return $self;
}

1;
