fix Stemweb reporting bug
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Stemweb.pm
index 33c717f..a9e7755 100644 (file)
@@ -5,13 +5,17 @@ use Encode qw/ decode_utf8 /;
 use JSON;
 use LWP::UserAgent;
 use Safe::Isa;
+use Scalar::Util qw/ looks_like_number /;
 use TryCatch;
 use URI;
 
 BEGIN { extends 'Catalyst::Controller' }
 
-## TODO Move the /algorithms/available function to the Stemweb module
-my $STEMWEB_BASE_URL = 'http://slinkola.users.cs.helsinki.fi';
+has stemweb_url => (
+       is => 'ro',
+       isa => 'Str',
+       default => 'http://slinkola.users.cs.helsinki.fi',
+       );
 
 =head1 NAME
 
@@ -75,6 +79,27 @@ sub result :Local :Args(0) {
        }
 }
 
+=head2 available
+
+ GET algorithms/available
+Queries the Stemweb server for available stemma generation algorithms and their 
+parameters. Returns the JSON answer as obtained from Stemweb.
+
+=cut
+
+sub available :Local :Args(0) {
+       my( $self, $c ) = @_;
+       my $ua = LWP::UserAgent->new();
+       my $resp = $ua->get( $self->stemweb_url . '/algorithms/available' );
+       if( $resp->is_success ) {
+               $c->stash->{'result'} = decode_json( $resp->content );
+       } else {
+               $c->stash->{'result'} = {};
+       }
+       $c->forward('View::JSON');
+}
+
 =head2 query
 
  GET stemweb/query/<jobid>
@@ -88,7 +113,7 @@ indicate that the job is still running.
 sub query :Local :Args(1) {
        my( $self, $c, $jobid ) = @_;
        my $ua = LWP::UserAgent->new();
-       my $resp = $ua->get( $STEMWEB_BASE_URL . "/algorithms/jobstatus/$jobid" );
+       my $resp = $ua->get( $self->stemweb_url . "/algorithms/jobstatus/$jobid" );
        if( $resp->is_success ) {
                # Process it
                my $response = decode_utf8( $resp->content );
@@ -139,9 +164,9 @@ sub _process_stemweb_result {
                        # Check all stemmata for the given jobid and return them.
                        @$stemmata = grep { $_->came_from_jobid && $_->from_jobid eq $answer->{jobid} } $tradition->stemmata;
                }
-       $DB::single = 1;
                if( @$stemmata ) {
                        # If we got here, success!
+                       # TODO Use helper in Root.pm to do this
                        my @steminfo = map { { 
                                        name => $_->identifier, 
                                        directed => _json_bool( !$_->is_undirected ),
@@ -159,7 +184,7 @@ sub _process_stemweb_result {
                        }
                        $c->stash->{'result'} = { status => 'notfound' };
                }
-       } elsif( $answer->{status} < 1 ) {
+       } elsif( $answer->{status} == -1 ) {
                $c->stash->{'result'} = { 'status' => 'running' };
        } else {
                return _json_error( $c, 500,
@@ -201,12 +226,12 @@ sub request :Local :Args(0) {
                data => $t->collation->as_tsv({noac => 1}),
                userid => $c->user->get_object->email,
                textid => $tid,
-               parameters => $reqparams };
+               parameters => _cast_nonstrings( $reqparams ) };
                
        # Call to the appropriate URL with the request parameters.
        my $ua = LWP::UserAgent->new();
        $c->log->debug( 'Sending request to Stemweb: ' . to_json( $stemweb_request ) ); 
-       my $resp = $ua->post( $STEMWEB_BASE_URL . "/algorithms/process/$algorithm/",
+       my $resp = $ua->post( $self->stemweb_url . "/algorithms/process/$algorithm/",
                'Content-Type' => 'application/json; charset=utf-8', 
                'Content' => encode_json( $stemweb_request ) ); 
        if( $resp->is_success ) {
@@ -248,6 +273,20 @@ sub _check_permission {
        return _json_error( $c, 403, 'You do not have permission to view this tradition.' );
 }
 
+# QUICK HACK to deal with strict Stemweb validation.
+sub _cast_nonstrings {
+       my $params = shift;
+       foreach my $k ( keys %$params ) {
+               my $v = $params->{$k};
+               if( looks_like_number( $v ) ) {
+                       $params->{$k} = $v * 1;
+               } elsif ( !defined $v || $v eq 'true' ) {
+                       $params->{$k} = _json_bool( $v );
+               }
+       }
+       return $params;
+}
+
 # Helper to throw a JSON exception
 sub _json_error {
        my( $c, $code, $errmsg ) = @_;
@@ -255,7 +294,7 @@ sub _json_error {
        $c->stash->{'result'} = { 'error' => $errmsg };
        $c->forward('View::JSON');
        return 0;
-}
+}              
 
 sub _json_bool {
        return $_[0] ? JSON::true : JSON::false;