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

use strict;
use warnings;
use Data::Dumper;
use Carp;

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

use lib '/app/tools/rps/lib';
use RPS::License::BaseControlledComposition;
use RPS::DB::Item::CATrackLicense;
use RPS::DB::Item::CALicenseReserve;
use RPS::Publisher::CA::Publisher;
use RPS::DB::Item::CAMechanicalStatementLicense;

use RPS::License::CA;
use base 'RPS::License::BaseControlledComposition';


sub _getDBItem
{
    my ($self, $id) = @_;
    return RPS::License::CA::GetDBItem($id);
}


sub _homeCountryCode
{
    my ($self) = @_;
    return 'CA';
}


sub _type { return RPS::DB::Item::CATrackLicense::kControlledComposition; }


sub _getPublisherObject
{
    my ($self, $publisherID) = @_;
    assert($publisherID);

    my $pubObj = RPS::Publisher::CA::Publisher->new(publisherID => $publisherID, readOnly => 1);
    return $pubObj;
}


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

    $self->SUPER::_propertiesFromDBItem($hrProperties, $dbItem);
	$hrProperties->{ca_track_license_id} = $dbItem->ca_track_license_id,
	$hrProperties->{ca_publisher_id}	 = $dbItem->ca_publisher_id,
}


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

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

    $self->{TrackLicenseID}     = Common::FormObject::Scalar->new(value => $props->{ca_track_license_id}, readOnly => 1);
    $self->{PublisherID}	    = Common::FormObject::Scalar->new(value => $props->{ca_publisher_id});
    
    if ($props->{ca_publisher_id})
    {
	    $self->{Publisher} = _getPublisherObject($self, $props->{ca_publisher_id});
    }
    
    if ($props->{ca_track_license_id})
    {
    
        # We need to mark the license so that we can tell if it's ever been used in a run (that wasn't deleted).
        #
        my $inUse = 0;
        
        my $licenseCount = RPS::DB::Item::CAMechanicalStatementLicense->LicenseCount($props->{ca_track_license_id});
        if ($licenseCount > 0)
        {
            $inUse = 1;    
        }   
        
        # Let's also check for (historical) reserves, just to be safe
        
        if ($inUse == 0)
        {
            my $reserveCount = RPS::DB::Item::CALicenseReserve->CountByTrackLicenseID($props->{ca_track_license_id});
            if ($reserveCount > 0)
            {
                $inUse = 1;  
            }     
        }        
        
        
        $self->{InUse} = $inUse;
   
    }       
    
}

sub _setDBValues
{
    my ($self, $dbItem) = @_;
    assert($dbItem);
    $self->SUPER::_setDBValues($dbItem);
    
    $dbItem->ca_publisher_id($self->PublisherID());  
}



sub _setDBValues
{
    my ($self, $dbItem) = @_;
    assert($dbItem);
    $self->SUPER::_setDBValues($dbItem);
    
    $dbItem->ca_publisher_id($self->PublisherID());  
}


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

    my %countryCodeMap;
    my $similarLicenses = RPS::DB::Item::CATrackLicense->GetByTrackPublisherProductType($self->TrackID(),$self->PublisherID, $self->ProductTypeID());
    while (my $similarLicense = $similarLicenses->next())
    {
        # Skip over _this_ track license...
        #
        next if ($self->TrackLicenseID() && $similarLicense->ca_track_license_id == $self->TrackLicenseID());
        my $ccMapItems = RPS::DB::Item::RegionCountryMap->GetByRegionID($similarLicense->region_id());
        while (my $mapItem = $ccMapItems->next())
        {
            $countryCodeMap{$mapItem->country_code} = 1;
        }
    }


    return \%countryCodeMap;
}


###
1;#
###


