[Bio] / FigWebServices / WS_MGRAST.pm Repository:
ViewVC logotype

View of /FigWebServices/WS_MGRAST.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Mon May 11 18:20:55 2009 UTC (10 years, 6 months ago) by jared
Branch: MAIN
CVS Tags: mgrast_dev_08112011, rast_rel_2009_05_18, mgrast_dev_08022011, rast_rel_2014_0912, myrast_rel40, mgrast_dev_05262011, mgrast_dev_04082011, rast_rel_2010_0928, mgrast_version_3_2, mgrast_dev_12152011, mgrast_dev_06072011, rast_rel_2009_0925, rast_rel_2010_0526, rast_rel_2014_0729, mgrast_dev_02212011, rast_rel_2010_1206, mgrast_release_3_0, mgrast_dev_03252011, rast_rel_2010_0118, rast_rel_2011_0119, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, mgrast_dev_04012011, rast_rel_2009_07_09, rast_rel_2010_0827, myrast_33, rast_rel_2011_0928, mgrast_dev_04052011, mgrast_dev_02222011, mgrast_dev_10262011, HEAD
inital version

package FigWebServices::WS_MGRAST;

use WebApplicationDBHandle;
use MGRAST::MetagenomeAnalysis;
use DBMaster;
use strict;
use warnings;

use base qw( Exporter );
our @EXPORT = qw ( get_mgdb get_public_metagenomes get_all_jobs_for_user get_job_for_user );

1;

############################
#
# Accessor functions
#
############################

sub get_mgdb {
  my ($job) = @_;  
  unless (ref $job and $job->isa('MG_jobcache::Job')) {
    warn "ERROR get_database_handle must be called with job object";
    return undef;
  } else {
    return MGRAST::MetagenomeAnalysis->new($job);
  }
}

sub get_public_metagenomes {
   return &_jobdb()->Job->get_objects({public => 1});
}

sub get_all_jobs_for_user {
  my ($token) = @_;
  my $user = &_token_to_user($token);
  if(defined $user){
    return &_jobdb()->Job->get_jobs_for_user($user, "view");
  } else {
    warn "ERROR no user found.\n";
    return undef;
  }
}

sub get_job_for_user {
  my ($token, $jobnum ) = @_;
  
  my $job = &_get_job($jobnum);
  if(defined $job){
    my $user = &_token_to_user($token);
    if(defined $user){
      if($user->has_right(undef, "view", "metagenome", $job->genome_id())){
	return $job;
      }
    } else {
      warn "ERROR no user found.\n";
      return undef;
    }
  } else {
    warn "ERROR no job found.\n";
    return undef;
  }
}


############################
#
# Internal functions
#
############################

sub _get_job {
  my ($jobnum) = @_;
  my $jobcache = &_jobdb();
  my ($job) = $jobcache->Job->get_objects({id=>$jobnum})->[0];
  if (defined $job){
    return $job;
  } else {
    return undef;
  }
}

sub _token_to_user {
  my ($token) = @_;

  my ($dbmaster, $error) = WebApplicationDBHandle->new();
    
  # check if we got a dbmaster
  if ($error) {
    print $error."\n";
    exit 0;
  }
  
  my $user = WebApplicationDBHandle::authenticate_user($dbmaster, $token);
  if ($user) {
    return $user;
  } else {
    return undef; 
  }
}


sub _jobdb {
  my $handle;
  eval {
    $handle = DBMaster->new( -database => $FIG_Config::mgrast_jobcache_db || 'JobCacheMGRast',
			     -host     => $FIG_Config::mgrast_jobcache_host,
			     -user     => $FIG_Config::mgrast_jobcache_user,
			     -password => $FIG_Config::mgrast_jobcache_password );
  };
  if ($@) {
    warn "Unable to connect to MGRAST database: $@\n";
    $handle = undef;
  }
  
  return $handle;
}


MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3