#------------------------------------------------------------
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
# $Id$
#------------------------------------------------------------
package BookPub::Admin::Command::DeleteImprint;
use strict;
use warnings;

use lib '/app/tools/common/lib';
use lib '/app/tools/bookpub/lib';
use BookPub::Admin::Command::ListImprints;
use RSApache::Message;
use BookPub::Catalog::Imprint::WithBookCount;
use BookPub::DB::Item::Imprint;

use BookPub::Command;
use base 'BookPub::Command';

sub cmd      { return 'delete_imprint'; }
sub area     { return 'admin'; }
sub pageName { return 'Delete Imprint'; }

#use constant kTemplate => "/app/tools/bookpub/templates/admin/edit_contributor.xsl";

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

    my $response = $self->SUPER::execute();
    return $response if $response;

    my $imprintID = $self->getParam('ImprintID');
    my $dbItem = BookPub::DB::Item::Imprint->Lookup( imprint_id => $imprintID );
    die "ERROR - Invalid ImprintID '$imprintID'" unless $dbItem;

    my $imprint = BookPub::Catalog::Imprint::WithBookCount->new( dbItem => $dbItem );
    die "ERROR - Cannot delete imprints with non-0 book count" if $imprint->BookCount() > 0;

    $dbItem->delete();

    # We want to change views, so we want to redirect to the
    # Show command now.
    my $successMessage = RSApache::Message->new(
        type => "success",
        code => Common::FormObject::kSuccessDelete,
    );
    my %showParams = ( msg => $successMessage->toString(), );
    my $command = BookPub::Admin::Command::ListImprints->new(%showParams);
    return RSApache::Response::Redirect->new( $command->getRedirect() );
}

1;
