package Opals::Indexer;

require Exporter;
@ISA       = qw(Exporter);
# Symbols to be exported by default
#@EXPORT    = qw(
#    opl_
#);
# Symbols to be exported on request
@EXPORT_OK = qw(
    ind_updateEbook
);

# Version number
$VERSION   = 0.01;

use Time::localtime;

use Opals::Date qw(
 date_text
 );
#use utf8;

use LWP::UserAgent;
use HTTP::Request::Common;
use URI::Escape;
use Opals::Utility qw(
    util_escapeXml
);
use strict;
use constant IND_UPDATE_BATCH_SIZE =>10;
use constant IND_DELETE_BATCH_SIZE =>10;
########################################################################################
sub ind_updateEbook{
    my($dbh)=@_;
    _updateEbook($dbh);
    _deleteEbook($dbh);
}
########################################################################################
sub _updateEbook{
    my($dbh)=@_;
    my ($ebSolrDoc,$bidList)=getEbSolrDocList_update($dbh,IND_UPDATE_BATCH_SIZE);
    while(scalar(@$bidList)>0){
        if($ebSolrDoc ne ""){
            if(postRequest("<add>$ebSolrDoc</add>") &&  postRequest("<commit/>")){
                my $bidListStr=join(",",@$bidList);
                $dbh->do("update eb_record set isIndexUpdated=1 where bid in($bidListStr)");
                ($ebSolrDoc,$bidList)=getEbSolrDocList_update($dbh,IND_UPDATE_BATCH_SIZE);
            }
        
            else{
                last;
            }
        }
    }
}
########################################################################################
sub _deleteEbook{
    my($dbh)=@_;
    my $bidList=getEbSolrDocList_delete($dbh,IND_DELETE_BATCH_SIZE);
    while(scalar(@$bidList)>0){
        my $bidCondArr=[];
        foreach my $e(@$bidList){
            push @$bidCondArr,"bid:$e";
        }
        my $bidCond= join(" OR ",@$bidCondArr);
        my $bidListStr=join(",",@$bidList);
        if(postRequest("<delete><query>db:ebook AND($bidCond)</query></delete>")){
            postRequest("<commit/>");
            $dbh->do("update eb_record set isIndexUpdated=1 where bid in($bidListStr)");
            $bidList=getEbSolrDocList_delete($dbh,IND_DELETE_BATCH_SIZE);
        }
        else{
            last;
        }
   }
}
########################################################################################
sub getEbSolrDocList_update{
    my($dbh,$size)=@_;
    $size=IND_UPDATE_BATCH_SIZE if(!defined $size || $size<1);
    my @fields= qw(bid title author ISBN description publisher pubDate language);
    my $sth=$dbh->prepare("select * from eb_record where deleted=0 && isIndexUpdated=0 limit $size");
    my $sth_toc=$dbh->prepare("select label from eb_toc where bid=?");
    $sth->execute();
    my $xmlDocs="";
    my $bidList=[];
    while( my $rec =$sth->fetchrow_hashref){
        push @$bidList,$rec->{'bid'};
        my $recData=[
                    {field=>"id",data=>"EB_" .$rec->{'bid'}},
                    {field=>"rid",data=>$rec->{'rid'}}];
        foreach my $f(@fields){
            push @$recData,{field=>$f,data=>util_escapeXml($rec->{$f})};
        }
        $sth_toc->execute($rec->{'bid'});
        while(my ($chapter) =$sth_toc->fetchrow_array){
             push  @$recData,{field=>"chapter",data=>util_escapeXml($chapter)};
        }
        $xmlDocs .= createEbSolrDoc($recData);

    }
    $sth->finish;
    $sth_toc->finish;
    return ($xmlDocs,$bidList);
}
########################################################################################
sub getEbSolrDocList_delete{
    my($dbh,$size)=@_;
    my $bidList=[];
    my $sth=$dbh->prepare("select bid from eb_record where deleted=1 && isIndexUpdated=0 limit $size");
    $sth->execute();
    while(my ($bid)=$sth->fetchrow_array){
        push @$bidList,$bid;
    }
    $sth->finish;
    return $bidList;
}
########################################################################################
sub createEbSolrDoc{
    my($rec)=@_;
    my $xmlDoc="";
    my $indexFieldMap={
        id          =>"id",
        bid         =>"bid",
        rid         =>"rid",
        title       =>"title",
        author      =>"author",
        language    =>"language",
        ISBN        =>"isbn",
        description =>"summary",
        publisher   =>"namePublisher",
        pubDate     =>"datePublication",    
        chapter     =>"chapterTitle"
     };
     my $copyField={title   =>["title_main","title_sort"],
                    author  =>["author_main"],
                    ISBN    =>["isbn_first"]};
                
     foreach my $r(@$rec){
         $xmlDoc .= sprintf("<field name=\"%s\">%s</field>\n",$indexFieldMap->{$r->{'field'}},$r->{'data'});
         if(defined $copyField->{$r->{'field'}}){
            my $data =$r->{'data'};
            foreach my $cf(@{$copyField->{$r->{'field'}}}){
                if($cf eq "title_sort" ){
                    $data  =~ s/^the|^a//gi;
                    $data  =~ s/^\s//gi;
                }
                $xmlDoc .= sprintf("<field name=\"%s\">%s</field>\n",$cf,$data);
            }
         }
     }
    
   
    return "<doc><field name=\"db\">ebook</field>\n$xmlDoc</doc>";
}


#////////////////////////////////////////////////////////////////////////////
sub postRequest{
    my ($xml)=@_;
    my($sHost,$sPort,$sDatabase) =getSolrSrvInfo();

    my $url="http://$sHost:$sPort/solr/$sDatabase/update" ;

    my $req = HTTP::Request->new( POST => $url );
    my $timeout = 600;
    my $ua        = LWP::UserAgent->new(agent => 'OPALS');
    $ua->timeout($timeout);
    $ua->agent("SolrHTTPUpdateHandlerAgent");
    $req->content_type('Content-type:text/xml; charset=utf-8');
    $req->content($xml);
    my $res = $ua->request($req);
    return $res->is_success;

    
}
#////////////////////////////////////////////////////////////////////////////
sub getSolrSrvInfo{
    my $sHost   = Opals::Context->config('sHost');
    my $sPort   = Opals::Context->config('sPort');
    my $sDatabase = Opals::Context->config('zDatabase');

    return ($sHost,$sPort,$sDatabase);

}

1;
