#!/usr/bin/perl 
#
# Created date:  Thu, Sep 27, 2012 @ 12:06:16 EDT
# This script is used to generate recFormat  and insert into opl_marcRecord..
#
=item
command to update all the sites:

for i in `ls /etc/opals/conf/`; do cp -p /www/opals/script/update/update20110704/updateRecFormatTbl /tmp/urt; perl -pi -e "s/_MY_SITE_/$i/" /tmp/urt; sudo /tmp/urt; done

=cut


use lib '/www/opals/module';
use Opals::Context("/etc/opals/conf/_MY_SITE_");
#use Opals::Context("/etc/opals/conf/ztest");
#use Opals::Context("/etc/opals/conf/odev");

use strict;
use DBI;
use Getopt::Std;
use POSIX qw(
    ceil
    floor
);
use Opals::Utility qw(
  util_getXmlRecord
);
use XML::SAX;
use base qw( XML::SAX::Base );
use Opals::MarcXmlParser;

#my %options = ();
#getopts("c:",\%options);
#my $configFile = $options{c};
#if (!$configFile || ! -f $configFile) {
#    print "Usage: $0 -c CONFIG_FILE\n";
#    exit 1;
#}
#
my $dbh = Opals::Context->dbh();
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}

$| = 1;


my $sth_updateRecForm=$dbh->prepare(<<_SQL_);
update  opl_marcRecord 
set     literaryForm=?,recFormat=?
where rid=?
_SQL_


my $sth = $dbh->prepare(<<_SQL_);
select      distinct rid
from        opl_item
where       rid=2791 
order by    rid asc 
_SQL_
    $sth->execute;

    my $marcXmlParser=undef;
    my $dir;
    my $rStat;
   $marcXmlParser=Opals::MarcXmlParser->new() if (!defined $marcXmlParser);;
    while (my ($rid) = $sth->fetchrow_array) {
        my $xml = util_getXmlRecord($rid);
        my $rec = $marcXmlParser->getRecInfoGeneral($xml);
        my $form=$rec->{'literaryForm'};
        my $format="";
        my $fmtList =$rec->{'format'} ;
        # foreach my $f (@$fmtList){
        #print "$f->{'item'} \n";
        #    $format .= "," if($format ne '');
        #    $format .=  $f->{'item'} ;            
        #}
        my $format=@{$rec->{'format'}}[0]->{'item'};
        $sth_updateRecForm->execute($form,$format,$rid);
       
        
    }
    $sth_updateRecForm->finish;
    $sth->finish;
 

exit 0;

