#!/usr/bin/perl -w

# Script      : sushiclientIrus.pl
# Author      : Paul A S Needham
# Description : sample SUSHI client for harvesting IR1 reports from IRUS-UK 
# Date        : Aug 2013
# Version     : 0.1

use strict;
use warnings;
use SOAP::Lite;
# useful for debugging, uncomment if needed:
#SOAP::Lite->import(+trace => 'all');

# set the following 5 parameters before running the script:
my $RequestorID = "your requestor id";                   # RequestorID: mandatory. Contact IRUS-UK (help@jisc.ac.uk)to obtain your RequestorID                           
my $RequestorName = 'your name';                         # RequestorName: optional. Recommend setting to your name
my $RequestorEmail = 'your email address';               # RequestorEmail: optional. Recommend setting to your email address
my $CustomerReferenceID = "your customer reference id";  # CustomerReferenceID: mandatory - set to the same value as RequestorID
my $CustomerReferenceName = "your repository name";      # CustomerReferenceName: optional. Recommend setting to your institutional repository name 

##### take a look at the script parameters #####
if ($#ARGV != 2 ) {
  print "usage: sushiclientIrus.pl type startdate enddate\n";
  print "       type: IR1\n";
  print "       startdate: yyyy-mm-dd\n";
  print "       enddate: yyyy-mm-dd\n";
  exit;
}

my $ReportName=$ARGV[0];
my $Begin = $ARGV[1];
my $End = $ARGV[2];
my $ServiceProvider = "IRUS-UK";
my $ServiceURL = "http://www.jusp.mimas.ac.uk/irus/sushiserver/";
my $createdDate = sub{sprintf '%04d-%02d-%02d',$_[5]+1900, $_[4]+1, $_[3]}->(localtime);

my $OutputFile = $RequestorID.'_'.$ReportName.'_'.$ServiceProvider.'_'.$Begin.'_'.$End.'.xml';

my $soap = SOAP::Lite
  ->readable(1)
  ->outputxml(1)
  ->proxy($ServiceURL)
  ->on_action( sub { 'SushiService:GetReportIn' });

my $Requestor = SOAP::Data->name('Requestor' => \SOAP::Data->value(
   SOAP::Data->name('ID' => $RequestorID),
   SOAP::Data->name('Name' => $RequestorName),
   SOAP::Data->name('Email' => $RequestorEmail)
   )
);

my $CustomerReference = SOAP::Data-> name('CustomerReference' => \SOAP::Data->value(
   SOAP::Data->name('ID' => $CustomerReferenceID),
   SOAP::Data->name('Name' => $CustomerReferenceName)
   )
);

my $ReportDefinition = SOAP::Data->name('ReportDefinition' => \SOAP::Data->value(
   SOAP::Data->name('Filters' => \SOAP::Data->value(
    SOAP::Data->name('UsageDateRange' => \SOAP::Data->value(
     SOAP::Data->name('Begin' => $Begin),
     SOAP::Data->name('End' => $End)
     )
    )
   )
  )
 )
) -> attr({"Name" => $ReportName, "Release" => '3', 'xmlns' => 'http://www.niso.org/schemas/sushi'});

my $result = $soap->call(
   SOAP::Data-> attr({"Created" => $createdDate, "ID" => '1', 'xmlns' => 'http://www.niso.org/schemas/sushi/counter'})->name('ReportRequest')
   => $Requestor,
   => $CustomerReference,
   => $ReportDefinition
);

open(OUTXML, '>'.$OutputFile) or die "Cannot write $OutputFile\n";
print OUTXML $result;
close(OUTXML);

print "Data retrieved from $ServiceProvider for the period from $Begin to $End\n";
print "Check out the contents of '$OutputFile'\n";
