#!/usr/bin/perl 
use lib("/www/opals/module");
use Opals::Context('/etc/opals/conf/tmp_test');
#use Opals::Context("/etc/opals/conf/_MY_SITE_");
use strict;
use Opals::Pathfinder qw(
    pf_getRecById
   );
use CGI;
use DBI;
use Opals::Pathfinder qw(
    pf_getRecById
    pf_parsePfXml
);

my $dbh = Opals::Context->dbh();
my $sth_rs=$dbh->prepare("insert into opl_reserveShelf set title=? , author=?, introduction=? ,createdDate=now(), reserveFrom='2014-01-01',reserveTo='2014-06-30'");
my $sth_rsSbj=$dbh->prepare("insert into opl_reserveShelfSubject set shelfId=?,subject=?");
my $sth_rsGrp=$dbh->prepare("insert into opl_reserveShelfGroup set shelfId=?,groupId=?,header=?,description=?");
my $sth_rsItem=$dbh->prepare("insert into opl_reserveShelfItems set shelfId=?,groupId=?,rid=?,callNumber=?");



my $sth=$dbh->prepare("select pfId,title,author,introduction,subjectArea,createdDate,xml from pf_record ");

$sth->execute();
while(my ($pfId,$title,$author,$introduction,$subjectArea,$createdDate,$xml)=$sth->fetchrow_array){

    my $pfRec=pf_parsePfXml($xml);
    
    $sth_rs->execute($title,$author,$introduction);
    my $shelfId = $dbh->{'mysql_insertid'};

   foreach my $s(@{$pfRec->{'subject'}}){
        $sth_rsSbj->execute($shelfId,$s->{'item'});
   }
   my $grpId=1;
   foreach my $bookRs(@{$pfRec->{'libraryRsList'}}){
       $sth_rsGrp->execute($shelfId,$grpId,$bookRs->{'header'},$bookRs->{'description'});
       foreach my $b (@{$bookRs->{'bookList'}}){
           $sth_rsItem->execute($shelfId,$grpId,$b->{'rid'},$b->{'callNumber'});
       }
       $grpId++;
   }

}


