#!/usr/bin/perl 
use lib("/www/opals/module");
use Opals::Context('/etc/opals/conf/odev');
use strict;
use Opals::Constant; 
use Date::Calc::Object qw(
    :all
);
use Digest::SHA qw(
    sha1_hex
    sha512_hex
);
use Opals::XML::SAX::LicenseListHandler;
use Time::localtime;
use Opals::WebServiceEbookClient qw( 
    wsc_ebookAuth
    wsc_uploadEbook
    wsc_getEbook
    wsc_updateEbookLicense
    wsc_deleteEbookLicense
    wsc_getLicense
    wsc_getLicenseList
    wsc_deleteEbook
    wsc_searchEbook
);
use CGI;
use DBI;
my $dbh = Opals::Context->dbh();
END {
    if ($dbh) {
        $dbh->disconnect();
    }
}
my $ssid =wsc_ebookAuth({host =>"ebook-opals.dione.scoolaid.net",
               libCode=>'wpc_wpc',
               accessKey=>'1122334455'});


my $params={ host            =>"ebook-opals.dione.scoolaid.net",
              ssid          => "$ssid",
              title         => "calm",
              offset        =>0,
              size          =>3
             };

my $rs =wsc_searchEbook($params);

print $rs->{'hits'}, "\n\n";
  my $recs=  $rs->{'recordList'};
    foreach my $r(@$recs){
        printf "bid:%s\ntitle:%s\nISBN:%s\nlicense:%s::%s\n",$r->{'bid'},$r->{'title'},$r->{'lid'},$r->{'licenseType'},$r->{'totalLicense'};

    }

=item
  
########################
my $params={ host            =>"ebook-opals.dione.scoolaid.net",
               ssid            => "$ssid",
               bid             => 10,
             };

my $ebook = wsc_getEbook($params);
print $ebook->{'title'}, "\n";




########################

  my $params={ host            =>"ebook-opals.dione.scoolaid.net",
               ssid            => "$ssid",
               bid             => 4,
             };

my $delEk= wsc_deleteEbook($params);
print $delEk->{'lid'}, "\n";


########################

  my $params={ host            =>"ebook-opals.dione.scoolaid.net",
               ssid            => "$ssid",
               bid             => 10,
             };

my $licList= wsc_getLicenseList($params);
print $licList->{'bid'}, "\n";

my $list=$licList->{'licenseList'};
foreach my $lic(@$list){
    print "=======================\n";
    foreach my $f qw(licenseID licenseType noLicense datePurchase poNum vendor){
        printf "    %s:%s\n", $f,$lic->{$f};
    }

}

  
  ########################
  
  my $delLic={
      host            =>"ebook-opals.dione.scoolaid.net",
      ssid            => "$ssid",
      licId           => 5
  };
  my $rs =wsc_deleteEbookLicense($delLic);
  print $rs->{'errorCode'} ,"\n";

  ########################
     my $licInfo={
                host            =>"ebook-opals.dione.scoolaid.net",
                ssid            => "$ssid",
                bid             => 10,
                licId           => 5,
                licenseType     => 'paid',
                noLicense       => 3,
                datePurchase    => '2013-06-10',
                poNum           => 'PO00214',
                vendor          => 'OPALS',
                note            => 'for testing'
        };

my $lic =wsc_updateEbookLicense($licInfo);
print $lic->{'errorCode'} ,"\n";



  ########################
my $fileName ="/tmp/The_Brilliant_Book_of_Calm.epub";
my $rs = wsc_uploadEbook({
            host     => 'ebook-opals.dione.scoolaid.net' , 
            ssid     => "$ssid",
            note     =>"upload from test script by Ha Tran",
            epubFile =>[$fileName]});
print $rs->{"bid"} ,"\n";
=cut

=item
my $xml="<response>
    <errorCode>errorCode</errorCode>
    
    <bid>bid</bid>
    <licenseList> 
        <licenseInfo>
            <licenseID>licId1</licenseID>
            <licenseType>licenseType</licenseType>
            <noLicense>noLicense</noLicense>
            <datePurchase>datePurchase</datePurchase>
            <poNum>poNum</poNum>
            <vendor>vendor</vendor>
            <startDate>startDate</startDate>
            <endDate>endDate</endDate>
            <note>note</note>
        </licenseInfo>
        <licenseInfo>
            <licenseID>licId2</licenseID>
            <licenseType>licenseType</licenseType>
            <noLicense>noLicense</noLicense>
            <datePurchase>datePurchase</datePurchase>
            <poNum>poNum</poNum>
            <vendor>vendor</vendor>
            <startDate>startDate</startDate>
            <endDate>endDate</endDate>
            <note>note</note>
        </licenseInfo>
    </licenseList> 
</response>";

 
    my $parser = XML::SAX::ParserFactory->parser(
        Handler => Opals::XML::SAX::LicenseListHandler->new
    );
    $parser->parse_string($xml);
    my $licList= $parser->{'Handler'};
=cut
