#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2009 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#---------------------------------------------------------------
package BookPub::Catalog::BookSubject::WithLiteral;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::DB::Item::BookSubject;
use BookPub::DB::Item::BISACSubject;
use Common::RSApp;
use Common::Util;
use Common::Assert;
use Common::FormObject;

use base 'BookPub::Catalog::BookSubject';

# Extends the 'BookSubject' class to include the BISAC literal descriptor.
#
# JPK - I intend for this to be used in an auto-complete. And, I want to display both
# the BISAC code and the literal in the widget.
#
# So, I'm going to modify this class a bit, and provide a 'combo' element that
# combines both the code and the literal.  And _this_ will be what is edited/selected
# in the edit interface.  We'll have to do some parsing, though, on the back end.

sub _initProperties {
    my ( $self, $props ) = @_;

    $self->SUPER::_initProperties($props);

    my $comboValue;
    if ( $props->{subject_code} ) {
        my $bisacSubject = BookPub::DB::Item::BISACSubject->Lookup( bisac_subject_code => $props->{subject_code} );
        if ($bisacSubject) {
            $self->{Literal} = $bisacSubject->literal();
            $comboValue = $bisacSubject->bisac_subject_code() . " : " . $bisacSubject->literal();
        }
    }

    $self->{Combo} = Common::FormObject::Scalar::String->new( value => $comboValue );
}

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

    my $valid = $self->SUPER::validate();

    # Don't bother validating if we're deleting this dude...
    #
    if ( !$self->Delete() ) {

        # !!! Here's the thing:  We have essentially a 'fake' user-editable field, 'Combo'.
        # The REAL DEAL is the SubjectCode element, which is being populated via a hidden control.
        # So we want to validate that 'SubjectCode' is valid.  But if there is an error, we want to attach
        # the error state to 'Combo', so that the user will see it...
        #
        my $subjectCode = $self->SubjectCode();
        if ( defined $subjectCode ) {
            my $dbItem = BookPub::DB::Item::BISACSubject->Lookup( bisac_subject_code => $subjectCode );
            if ( !$dbItem ) {
                $self->{Combo}->setError(Common::FormObject::kErrFieldInvalid);
                $valid = 0;
            }
        } else {

            # Blank fields are only invalid for _existing_ subjects.
            #
            if ( !defined $self->Combo() && $self->SubjectID() ) {
                $self->{Combo}->setError(Common::FormObject::kErrFieldBlank);
                $valid = 0;
            } elsif ( defined $self->Combo() ) {
                $self->{Combo}->setError(Common::FormObject::kErrFieldInvalid);
                $valid = 0;
            }
        }
    }

    return $valid;
}

###
1;    #
###
