fix typo in conf file; add path to pars executable
[scpubgit/stemmaweb.git] / lib / stemmaweb / Controller / Stemweb.pm
index 8804c4c..3dc4ede 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
 
@@ -52,7 +56,7 @@ sub result :Local :Args(0) {
                        # Read in the file and parse that.
                        $c->log->debug( "Request body is in a temp file" );
                        open( POSTDATA, $c->request->body ) 
-                               or return _json_error( 500, "Failed to open post data file" );
+                               or return _json_error( $c, 500, "Failed to open post data file" );
                        binmode( POSTDATA, ':utf8' );
                        # JSON should be all one line
                        my $pdata = <POSTDATA>;
@@ -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 );
@@ -115,24 +140,17 @@ sub query :Local :Args(1) {
 ## Helper function for parsing Stemweb result data either by push or by pull
 sub _process_stemweb_result {
        my( $c, $answer ) = @_;
-       # Find a tradition with the defined Stemweb job ID.
-       # TODO: Maybe get Stemweb to pass back the tradition ID...
+       # Find the specified tradition and check its job ID.
        my $m = $c->model('Directory');
-       my @traditions;
-       ## STUPID HACK: unless we load the possible tradition owners
-       ## within scope of the scan, they will not exist when the affected
-       ## tradition is saved.
-       my @users;
-       $m->scan( sub{ push( @traditions, $_[0] )
-                                       if $_[0]->$_isa('Text::Tradition')
-                                       && $_[0]->has_stemweb_jobid 
-                                       && $_[0]->stemweb_jobid eq $answer->{jobid}; 
-                       push( @users, $_[0] ) if $_[0]->$_isa('Text::Tradition::User');
-                               } );
-       if( @traditions == 1 ) {
-               my $tradition = shift @traditions;
-               if( $answer->{status} == 0 ) {
-                       my $stemmata;
+       my $tradition = $m->tradition( $answer->{textid} );
+       unless( $tradition ) {
+               return _json_error( $c, 400, "No tradition found with ID "
+                       . $answer->{textid} );
+       }
+       if( $answer->{status} == 0 ) {
+               my $stemmata;
+               if( $tradition->has_stemweb_jobid 
+                       && $tradition->stemweb_jobid eq $answer->{jobid} ) {
                        try {
                                $stemmata = $tradition->record_stemweb_result( $answer );
                                $m->save( $tradition );
@@ -141,7 +159,14 @@ sub _process_stemweb_result {
                        } catch {
                                return _json_error( $c, 500, $@ );
                        }
+               } else {
+                       # It may be that we already received a callback meanwhile.
+                       # Check all stemmata for the given jobid and return them.
+                       @$stemmata = grep { $_->came_from_jobid && $_->from_jobid eq $answer->{jobid} } $tradition->stemmata;
+               }
+               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 ),
@@ -150,23 +175,20 @@ sub _process_stemweb_result {
                        $c->stash->{'result'} = { 
                                'status' => 'success',
                                'stemmata' => \@steminfo };
-               } elsif( $answer->{status} < 1 ) {
-                       $c->stash->{'result'} = { 'status' => 'running' };
                } else {
-                       return _json_error( $c, 500,
-                               "Stemweb failure not handled: " . $answer->{result} );
+                       # Hm, no stemmata found on this tradition with this jobid.
+                       # Clear the tradition jobid so that the user can try again.
+                       if( $tradition->has_stemweb_jobid ) {
+                               $tradition->_clear_stemweb_jobid;
+                               $m->save( $tradition );
+                       }
+                       $c->stash->{'result'} = { status => 'notfound' };
                }
-       } elsif( @traditions ) {
-               return _json_error( $c, 500, 
-                       "Multiple traditions with Stemweb job ID " . $answer->{jobid} . "!" );
+       } elsif( $answer->{status} == 1 ) {
+               $c->stash->{'result'} = { 'status' => 'running' };
        } else {
-               # Possible that the tradition got updated in the meantime...
-               if( $answer->{status} == 0 ) {
-                       $c->stash->{'result'} = { 'status' => 'notfound' };
-               } else {
-                       return _json_error( $c, 400, 
-                               "No tradition found with Stemweb job ID " . $answer->{jobid} );
-               }
+               return _json_error( $c, 500,
+                       "Stemweb failure not handled: " . $answer->{result} );
        }
        $c->forward('View::JSON');
 }
@@ -204,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 ) {
@@ -251,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 ) = @_;
@@ -258,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;