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

use strict;
use warnings;
use lib '/app/tools/common/lib';
use Common::Assert;

use lib '/app/tools/rps/lib';
use RPS::License::BaseEditForm;
use RPS::License::CA::TrackLicenseWithLiquidationSchedule;
use RPS::License::CA::PublicDomain;
use RPS::License::CA::ControlledComposition;
use RPS::DB::Item::CATrackLicense;
use RPS::RoyaltyRun::RoyaltyRunStatusList;

use base 'RPS::License::BaseEditForm';


sub _init {
    my $self = shift;

	$self->SUPER::_init( @_ );

	my $payorID = $self->{TrackLicense}->PayorID()
	    if( $self->{TrackLicense} );

	$self->{RoyaltyRunStatus} = new RPS::RoyaltyRun::RoyaltyRunStatusList( caMechanical => 1 );

	#$self->{RoyaltyRunStatus}->{ByPayorID} = new RPS::RoyaltyRun::RoyaltyRunStatusList( caMechanical => 1, payorID => $payorID )
	#    if( $payorID );

    return $self;
}

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


    # If we're being passed an actual TrackLicenseID, then we can fetch the db item directly.
    # And by examining the 'type' column, that will let us know what class to use.
    #
    if ($args{trackLicenseID})
    {
        my $dbItem = RPS::DB::Item::CATrackLicense->Lookup(ca_track_license_id => $args{trackLicenseID});
        die "ERROR - unable to load track license with id " . $args{trackLicenseID} unless $dbItem;

        # !!! This logic used to be buried in the TrackLicense form object code.
        #
	    if (RPS::DB::Item::CATrackLicense::kPublishingLicense == $dbItem->type)
	    {
            return RPS::License::CA::TrackLicenseWithLiquidationSchedule->new(_dbItem => $dbItem);
        }

        if (RPS::DB::Item::CATrackLicense::kControlledComposition == $dbItem->type)
        {
            return RPS::License::CA::ControlledComposition->new(_dbItem => $dbItem);
        }

        if (RPS::DB::Item::CATrackLicense::kPublicDomain == $dbItem->type)
        {
            return RPS::License::CA::PublicDomain->new(_dbItem => $dbItem);
        }

        die "ERROR - unknown type for track license " . $args{trackLicenseID};
	}

    my $licenseType = $args{licenseType};

    if ($licenseType == RPS::DB::Item::TrackLicense::kPublicDomain)
    {
        return RPS::License::CA::PublicDomain->new(trackID => $args{trackID});
    }

    if ($args{controlledCompositionID})
    {
        return RPS::License::CA::ControlledComposition->new(trackID => $args{trackID});
    }

    return RPS::License::CA::TrackLicenseWithLiquidationSchedule->new(trackID => $args{trackID});
}



###
1;#
###
