#!/usr/bin/perl -w
# This is a CGI-BIN script, invoked by a web server when an HTTP
# POST comes in, dispatching requests to the appropriate module
# (SADI web service).
#
# It includes some hard-coded paths - they were added during the
# generate service call.
#
# $Id: service-cgi-async.tt,v 1.24 2010-01-26 20:43:35 ubuntu Exp $
# Contact: Edward Kawas <edward.kawas@gmail.com>
# ---------------------------------------------------------------
[%# A template for a sadi service.
    ===================================

    Expected/recognized parameters:
      obj            ... a service definition, type SADI::Service::Instance
      services_dir   ... the directory to the impl files
      generated_dir  ... the directory to the 'generated' dir
      home_dir       ... the SADI-Perl user home directory
-%]

#-----------------------------------------------------------------
# Authority:    [% obj.Authority %]
# Service name: [% obj.ServiceName %]
# Generated:    [% USE Date (format = '%d-%b-%Y %H:%M:%S %Z') %][% Date.format %]
# Contact: Edward Kawas <edward.kawas@gmail.com>
#-----------------------------------------------------------------

use strict;


# --- during service generation --- 
# leave at the top!
use lib '[% generated_dir %]';
use lib '[% services_dir %]';
use lib '[% home_dir %]';

use CGI;
use CGI::Carp qw(fatalsToBrowser);

use SADI::Service::Instance;
use SADI::RDF::Core;
use SADI::Generators::GenServices;
use SADI::FileStore;

# the suggested wait time for polling in seconds
my $SUGGESTED_WAIT_TIME = 0.5*60;

# here we require the service module and add it to ISA hierarchy
use base 'Service::[% obj.ServiceName %]';

# if this is a GET, send the service interface
if ($ENV{REQUEST_METHOD} eq 'GET' or $ENV{REQUEST_METHOD} eq 'HEAD') {
    my $q = new CGI;
    # print the interface unless we are polling
    do {
        # send the signature for this service
        # instantiate a new SADI::RDF::Core object
        my $core = SADI::RDF::Core->new;
        $core->ContentType(__PACKAGE__->get_response_content_type());
        # set the Instance for $core
        $core->Signature(__PACKAGE__->get_service_signature('[% obj.ServiceName %]'));
        print $q->header(-type=>__PACKAGE__->get_response_content_type());
        print $core->getServiceInterface();
        exit();
    } unless $q->param('poll');
    
    # we are polling ... 
    # $poll is the id for our file store
    my $poll = $q->param('poll');
    $ENV{SADI_UID} = $poll;
    my $completed;
    eval {$completed = __PACKAGE__->completed($poll);};
    # do something if $@
    print $q->header(-status=>"404 nothing found for the given polling parameter" ) if $@;
    exit if $@;
    if ($completed) {
        # we are done
        eval {$completed = __PACKAGE__->retrieve($poll);};
        unless ($@) {
          print $q->header(-type=>__PACKAGE__->get_response_content_type());
          print $completed;
          exit;
        }
    } else {
        # still waiting
        my $signature = __PACKAGE__->get_service_signature('[% obj.ServiceName %]');
        print $q->redirect(-uri=>$signature->URL . "?poll=$poll", -status=>302, -Retry_After=>"$SUGGESTED_WAIT_TIME");
        exit;
    }
} else {
    # call the service

    # get the posted data
    my $data = join "",<STDIN>;

    # set the UID
    my $uid = SADI::FileStore->new(ServiceName => "[% obj.ServiceName %]")->generate_uid();
    $ENV{SADI_UID} = $uid;

    # call the service
    __PACKAGE__->[% obj.ServiceName %](
       $data
    );
   my $q = new CGI;
   print $q->header(
     -type=>__PACKAGE__->get_response_content_type(),
     -status=>202,
     -Retry_After=>"$SUGGESTED_WAIT_TIME"
   );
   print __PACKAGE__->get_polling_rdf();
}

__END__

