The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
[%# A template for a service base code.
    ===================================

    Expected/recognized parameters:

      obj         ... a service definition, type SADI::Service::Instance
      module_name ... the name of the module (scalar string)
-%]
#-----------------------------------------------------------------
# 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>
#-----------------------------------------------------------------

package [% module_name %];

use FindBin qw( $Bin );
use lib $Bin;

use SADI::Base;
use SADI::Service::ServiceBase;
use SADI::RDF::Core;
use SADI::Service::Instance;
use RDF::Notation3::RDFCore;

no strict;

use vars qw( @ISA );
@ISA = qw( SADI::Service::ServiceBase );

use strict;

#-----------------------------------------------------------------
# [% obj.ServiceName %]
#   the main method; corresponds to the name of this SADI web service
#----------------------------------------------------------------- 
sub [% obj.ServiceName %] {
    my ($self, $data) = @_;
    
    # instantiate a new SADI::RDF::Core object
    my $core = SADI::RDF::Core->new;
    # set the incoming content type
    $core->ContentType($self->get_request_content_type());

    # set the Instance for $core
    $core->Signature($self->get_service_signature('[% obj.ServiceName %]'));

    Log::Log4perl::NDC->push ($$);
    $LOG->info ('*** REQUEST START *** ' . "\n" . $self->log_request);

    no warnings 'newline';
    my $in_testing_mode = (-f $data);
    use warnings 'newline';
    if ($in_testing_mode) {
	     open (RAWXML, "<$data") or $LOG->logdie ("Cannot open $data: $!\n");
	     $data = join ('', <RAWXML>);
	     close RAWXML;
	     $LOG->debug ("Input raw data:\n$data\n") if ($LOG->is_debug);
     } else {
        $LOG->debug ("Input raw data (first 1000 characters):\n" . substr ($data, 0, 1000)) if $LOG->is_debug;
     }

    $self->default_throw_with_stack (0) unless $in_testing_mode;

    # get/parse the incoming RDF
    eval {
	    $core->Prepare($data) 
	      or $self->throw("Error parsing the input RDF. Couldn't create a model!");
    };
    # set the content type that the client wants us to return
    $core->ContentType($self->get_response_content_type());

    # error in creating parser, or parsing input
    if ($@) {
		$LOG->logdie ($@) if $in_testing_mode;
		# construct an outgoing message
		my $stack = $self->format_stack ($@);
        $core->_add_error($@, 'Error parsing input message for sadi service!', $stack);
        $LOG->error ($stack);
		$LOG->info ('*** FATAL ERROR RESPONSE BACK ***');
		Log::Log4perl::NDC->pop();
		return $core->serializeOutputModel();
    }
	
    # do something (this service main task)
    eval { 
    	my @inputs = $core->getInputNodes();
    	$self->process_it (\@inputs, $core); 
    };
    # error thrown by the implementation class
    if ($@) {
		$LOG->logdie ($@) if $in_testing_mode;
		# TODO how to report errors?
		my $stack = $self->format_stack ($@);
		$core->_add_error($@, 'Error running sadi service!', $stack);
		$LOG->error ($stack);
		$LOG->info ('*** REQUEST TERMINATED RESPONSE BACK ***');
		Log::Log4perl::NDC->pop();
		return $core->serializeOutputModel ();
    }

    # return result
    $LOG->info ('*** RESPONSE READY *** ');
    if ($in_testing_mode) {
		Log::Log4perl::NDC->pop();
	    return $core->serializeOutputModel();
    } else {
		if ($LOG->is_debug) {
		    my $xml_output = $core->serializeOutputModel();
		    $LOG->debug ("Output raw data (first 1000 characters): " .
				 substr ($xml_output,0,1000));
		    Log::Log4perl::NDC->pop();
		    return $xml_output;
		} else {
		    Log::Log4perl::NDC->pop();
		    return $core->serializeOutputModel();
		}
    }
}

1;