#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package RPS::Catalog::DAAlbum;
use strict;
use warnings;

use lib '/app/tools/rps/lib';
use RPS::DB::Item::Album;
use RPS::DB::Item::Artist;
use RPS::Track::TrackListSimple;

use RPS::Catalog::AlbumSimple;
use base 'RPS::Catalog::AlbumSimple';


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

    # !!! This code is copied from Catalog::Album.
    # !!! Might be nice to add this stuff to the base class.
    #

	# -------------------------------
	# Save or create the artist
	# -------------------------------
	#
	my $artistObj;
	if($self->ArtistID())
	{
		$artistObj = RPS::DB::Item::Artist->Lookup(artist_id => $self->ArtistID());

		# changing the artist? see if it's a different artist
		if($self->ArtistName())
		{
			if(lc($artistObj->name) ne lc($self->ArtistName()))
			{
				$artistObj = RPS::DB::Item::Artist->Create(name => $self->ArtistName());
                ## only good place to set the clean name for artists atm
                $artistObj->name_clean(Common::Util::clean_name_catalog($self->ArtistName()));
				$artistObj->save();
			}
		}
	}
    elsif($self->ArtistName())
    {
        $artistObj = RPS::DB::Item::Artist->Lookup(name => $self->ArtistName());
        if (defined $artistObj && lc($artistObj->name) eq lc($self->ArtistName()))
        {
            $self->ArtistID($artistObj->artist_id);
        }
        else # Create a new artist.
        {
            $artistObj = RPS::DB::Item::Artist->Create(name => $self->ArtistName());
            ## only good place to set the clean name for artists atm
            $artistObj->name_clean(Common::Util::clean_name_catalog($self->ArtistName()));
            $artistObj->save();
        }
    }


	# -------------------------------
	# Save myself
	# -------------------------------
	#
	my $albumDBObj;
	if ($self->AlbumID())
	{
		$albumDBObj = RPS::DB::Item::Album->Lookup(album_id => $self->AlbumID());
	}
	else
	{
		$albumDBObj = RPS::DB::Item::Album->Create();
	}

	$albumDBObj->catalog_number($self->CatalogNumber());
	$albumDBObj->client_album_id($self->ClientAlbumID());
	$albumDBObj->title($self->Title());
	$albumDBObj->genre_primary_id($self->GenrePrimaryID());
	$albumDBObj->genre_secondary_id($self->GenreSecondaryID());
	$albumDBObj->c_line($self->CLine());
	$albumDBObj->p_line($self->PLine());
	$albumDBObj->title_version($self->TitleVersion());
	$albumDBObj->label_id($self->LabelID());
	$albumDBObj->status($self->Status());
	$albumDBObj->inactive($self->Inactive());
	$albumDBObj->artist_contract_id($self->ArtistContractID());
	$albumDBObj->company_id($self->CompanyID);
	$albumDBObj->vendor_id($self->VendorID);
	$albumDBObj->location_id($self->LocationID);
	$albumDBObj->custom_1($self->Custom1);
	$albumDBObj->custom_2($self->Custom2);
	$albumDBObj->custom_3($self->Custom3);

	$albumDBObj->artist_id($artistObj->artist_id);

    # Derrive the clean title from title
    #
    $albumDBObj->title_clean(Common::Util::clean_name_catalog($self->Title()));

	$albumDBObj->save();

	# reload object
	#
    $self->_init(_dbItem => $albumDBObj);
}

1;
