#---------------------------------------------------------------
# ____                   _ _         ____  _
#|  _ \ ___  _   _  __ _| | |_ _   _/ ___|| |__   __ _ _ __ ___
#| |_) / _ \| | | |/ _` | | __| | | \___ \| '_ \ / _` | '__/ _ \
#|  _ < (_) | |_| | (_| | | |_| |_| |___) | | | | (_| | | |  __/
#|_| \_\___/ \__, |\__,_|_|\__|\__, |____/|_| |_|\__,_|_|  \___|
#            |___/             |___/
#
# Copyright (C) 2011 RoyaltyShare, Inc.   All Rights Reserved
#---------------------------------------------------------------
package BookPub::RDAS::Process::CacheProductURL;

use strict;

use lib '/app/tools/bookpub/lib';
use BookPub::RDAS::Request::ProductPrice;
use BookPub::DB::Item::BookProduct;

use base 'BookPub::RDAS::Process';

sub _processService {
    my $self      = shift;
    my $serviceID = shift;
    my $productID = $self->param('product_id');
    my $sleep     = $self->_calculateSleep();

    Log->info(" - Processing service id: $serviceID");

    my $collection = BookPub::DB::Item::BookProduct->GetAllWithMissingURL( serviceID => $serviceID, productID => $productID );

    while ( my $dbitem = $collection->next() ) {
        my $request = BookPub::RDAS::Request::ProductPrice->GetRequestObject( $serviceID, productID => $dbitem->product_id );

        if ($request) {
            Log->debug( "Fetching URL for product id: " . $dbitem->product_id );
            my $url = $request->url( productID => $dbitem->product_id );

            if ($url) {
                Log->debug($url);
            } else {
                Log->debug( "No URL found for product ID: " . $self->param('product_id') );
            }
        } else {
            Log->error( "URL not found for service id: $serviceID, product id: " . $dbitem->product_id );
        }

        if ($sleep) {
            Log->debug("Sleeping $sleep seconds");
            Time::HiRes::sleep($sleep);
        }
    }
}

1;
