#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::LabelContract::GroupTermExceptionHash;

use strict;
use warnings;

use Data::Dumper;

use lib '/app/tools/rps/lib';
use RPS::LabelContract::GroupTermException;
use RPS::DB::Item::LabelContract;
use RPS::DB::Item::LabelContractGroupTermException;

use lib '/app/tools/common/lib';

use Common::FormHash;
use base 'Common::FormHash';

sub _tagName {
    return 'GroupTermException';
}

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

    if ( !$args{labelContractGroupTermID} ) {
        return undef;
    }

    my $collection = RPS::DB::Item::LabelContractGroupTermException->GetByLabelContractGroupTermID( $args{labelContractGroupTermID} );
    return $collection;
}

sub _keyMethodName {
    return 'LabelContractGroupTermExceptionID';
}

sub _getNewObject {
    my ($self) = @_;

    if ( $self->{_labelContractGroupTermID} ) {
        return RPS::LabelContract::GroupTermException->new( labelContractGroupTermID => $self->{_labelContractGroupTermID} );
    }

    return RPS::LabelContract::GroupTermException->new();
}

sub _getObjectFromDBItem {
    my ( $self, $dbItem ) = @_;

    return RPS::LabelContract::GroupTermException->new( _dbItem => $dbItem );
}

sub _addObject {
    my ( $self, $object ) = @_;

    $self->SUPER::_addObject($object);

    if ( $self->{_dependency} && exists $self->{_dependency}{ $object->LabelContractGroupTermExceptionID() } ) {
        $object->setDependency();
    }
}

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

    # We want to keep track of the contract ID for later.
    #
    if ( $args{labelContractGroupTermID} ) {

        #$self->{_dependency} = RPS::DB::Item::LabelContractTerm->GetTermDependencyByLabelContractID($args{labelContractID});
        $self->{_labelContractGroupTermID} = $args{labelContractGroupTermID};
    }

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

sub getKeys {
    my ($self) = @_;

    my $keys = $self->getAllKeysSortedByServiceAndCountry();

    return $keys;
}

sub getAllKeysSortedByServiceAndCountry {
    my ($self) = @_;

    my @keys = sort {
             compareServiceNames( $self->{GroupTermException}{$a}->ServiceName(), $self->{GroupTermException}{$b}->ServiceName() )
          || compareCountryNames( $self->{GroupTermException}{$a}->CountryName(), $self->{GroupTermException}{$b}->CountryName() )
    } keys %{ $self->{GroupTermException} };
    return \@keys;
}

sub compareServiceNames {
    my $a = shift;
    my $b = shift;

    if ( uc($a) eq 'ALL' && uc($b) eq 'ALL' ) {
        return 0;
    } elsif ( uc($a) eq 'ALL' ) {
        return 1;
    } elsif ( uc($b) eq 'ALL' ) {
        return -1;
    } else {
        return ( uc($a) cmp uc($b) );
    }
}

sub compareCountryNames {
    my $a = shift;
    my $b = shift;

    if ( uc($a) eq 'ALL' ) {
        return 1;
    } elsif ( uc($b) eq 'ALL' ) {
        return -1;
    } else {
        return ( uc($a) cmp uc($b) );
    }
}

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

    my $keysSortedByServiceAndCountry = $self->getAllKeysSortedByServiceAndCountry();

    foreach my $groupTermExceptionID (@$keysSortedByServiceAndCountry) {
        my $groupTermExceptionObj = $self->{GroupTermException}{$groupTermExceptionID};
        $groupTermExceptionObj->save();
    }
}

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

    my $keysSortedByServiceAndCountry = $self->getAllKeysSortedByServiceAndCountry();

    foreach my $groupTermExceptionID (@$keysSortedByServiceAndCountry) {
        my $groupTermExceptionObj = $self->{GroupTermException}{$groupTermExceptionID};
        $groupTermExceptionObj->delete();
    }
}

sub validate {
    my ($self) = @_;

    my $valid = 1;
    foreach my $termID ( keys %{ $self->{GroupTermException} } ) {
        my $term = $self->{GroupTermException}{$termID};
        next if ( !defined $term );

        if ( !$term->validate() ) {
            $valid = 0;
        }
    }

    return $valid;
}

sub LabelContractGroupTermID {
    my ($self) = @_;

    return $self->{_labelContractGroupTermID};
}

###
1;    #
###
