#!/usr/bin/perl
use strict;

use DBI;
use CGI;
use RDFStore::NodeFactory;

$SIG{'SEGV'} = \&Carp::confess;

#processing order is: URL, file, free-text
my $query = new CGI;

my $text = $query->param("rdql_text");
my $asrdf = $query->param("asrdf");

my $factory = new RDFStore::NodeFactory();

open FH,"../../style/top.html";
my ($top,$header,$footer);
while (<FH>) {
        $top .= $_;
        };
close(FH);
open FH,"../../style/header.html";
while (<FH>) {
        $header .= $_;
        };
close(FH);
open FH,"../../style/footer.html";
while (<FH>) {
        $footer .= $_;
        };
close(FH);

my $results;
eval {
        $results = DBI->connect(
                "DBI:RDFStore:database=", 
                "pincopallino", 0, 
                { nodeFactory => $factory, results => { syntax => $asrdf } }
        );

        my $sth = $results->prepare( $text );
        $sth->execute();

        $asrdf = ($asrdf eq 'N-Triples') ? $asrdf : 'RDF/XML'
                if( $sth->func('getQueryStatement')->getQueryType ne 'SELECT' );

        if(! $asrdf) {
                print "Content-Type: text/html; charset=utf-8\r\n\r\n";

                print qq|$top
                <TITLE>RDFStore RDQL/SquishQL WWW2003 demo - query results</TITLE>
                        $header
|;
        } else {
                print "Content-Type: text/plain; charset=utf-8\r\n\r\n";
                };

        $text=~s/</\&lt;/g;
        $text=~s/>/\&gt;/g;

        if($asrdf) {
                if($asrdf !~ m/(RDF\/XML|N-Triples)/) {
                        while (my $xml = $sth->func($asrdf, 'fetchrow_XML' )) { print $xml; };
                } else {
                        while (my $rdf = $sth->func($asrdf, 'fetchsubgraph_serialize' )) { print $rdf; };
                        };
        } else {

                print "<BR><b class='title'>Your query:</b><BR><BR><FONT><PRE class='body'>".$text."</PRE></FONT><BR>";
                print "<b class='title'>returned the following results:</b><BR><BR>";

                print "<TABLE BORDER='1'>";
                print "<TR class='title'>";
                map { 
                        my $a=$_; $a=~s/^\?//; 
                        print "<TH><B>$a</B></TH>"; 
                } @{$sth->{'NAME'}};

                print "</TR>";

                while (my @row = $sth->fetchrow_array()) {
                        unless($asrdf) {
                                print "<TR VALIGN='top' class='body'>";
                                map {
                                        my $aa = ($_) ? $_->toString : undef;
                                        $aa=~s/(\w+\:\/\/.*)/<a href='$1'>$1<\/a>/;
                                        print "<TD VALIGN='top'>".$aa."</TD>";
                                } @row;
                                print "</TR>";
                                };
                        };
                print "</TABLE>";

                print qq|<BR><BR>$footer|;
                };
        $sth->finish();
};

my $err = $@;

if((defined $err) && ($err ne '') ) {
        $err=~s/</\&lt;/g;
        $err=~s/>/\&gt;/g;
        $err=~ s/\s*at \/(.*)//g;
        print '<BR><b><font color="#FF0000">An ERROR occurred while parsing the RDQL/SquishQL query or the XML/RDF content:</font><br><br><font color="#0000FF"><cite><pre>'.$err.'</pre></cite></font></BODY></HTML>';
};

Apache::exit();