seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / Trials.pm
blob4c26ef46ea853bc6d74099c32bee738c5cad291e
2 package SGN::Controller::AJAX::Trials;
4 use Moose;
6 use CXGN::BreedersToolbox::Projects;
7 use CXGN::Trial::Folder;
8 use Data::Dumper;
9 use Carp;
10 use File::Path qw(make_path);
11 use File::Spec::Functions qw / catfile catdir/;
12 use SGN::Model::Cvterm;
14 BEGIN { extends 'Catalyst::Controller::REST'; }
16 __PACKAGE__->config(
17 default => 'application/json',
18 stash_key => 'rest',
19 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
23 sub get_trials : Path('/ajax/breeders/get_trials') Args(0) {
24 my $self = shift;
25 my $c = shift;
27 my $p = CXGN::BreedersToolbox::Projects->new( { schema => $c->dbic_schema("Bio::Chado::Schema") } );
29 my $projects = $p->get_breeding_programs();
31 my %data = ();
32 foreach my $project (@$projects) {
33 my $trials = $p->get_trials_by_breeding_program($project->[0]);
34 $data{$project->[1]} = $trials;
38 $c->stash->{rest} = \%data;
41 sub get_trials_with_folders : Path('/ajax/breeders/get_trials_with_folders') Args(0) {
42 my $self = shift;
43 my $c = shift;
45 my $schema = $c->dbic_schema("Bio::Chado::Schema");
46 my $p = CXGN::BreedersToolbox::Projects->new( { schema => $schema } );
48 my $projects = $p->get_breeding_programs();
50 my $html = "";
51 my $folder_obj = CXGN::Trial::Folder->new( { bcs_schema => $schema, folder_id => @$projects[0]->[0] });
53 print STDERR "Starting get trials at time ".localtime()."\n";
54 foreach my $project (@$projects) {
55 my %project = ( "id" => $project->[0], "name" => $project->[1]);
56 $html .= $folder_obj->get_jstree_html(\%project, $schema, 'breeding_program', 'trial');
58 print STDERR "Finished get trials at time ".localtime()."\n";
60 my $dir = catdir($c->site_cluster_shared_dir, "folder");
61 eval { make_path($dir) };
62 if ($@) {
63 print "Couldn't create $dir: $@";
65 my $filename = $dir."/entire_jstree_html.txt";
67 my $OUTFILE;
68 open $OUTFILE, '>', $filename or die "Error opening $filename: $!";
69 print { $OUTFILE } $html or croak "Cannot write to $filename: $!";
70 close $OUTFILE or croak "Cannot close $filename: $!";
72 $c->stash->{rest} = { status => 1 };
75 sub get_trials_with_folders_cached : Path('/ajax/breeders/get_trials_with_folders_cached') Args(0) {
76 my $self = shift;
77 my $c = shift;
79 my $dir = catdir($c->site_cluster_shared_dir, "folder");
80 my $filename = $dir."/entire_jstree_html.txt";
81 my $html = '';
82 open(my $fh, '<', $filename) or die "cannot open file $filename";
84 local $/;
85 $html = <$fh>;
87 close($fh);
89 #print STDERR $html;
90 $c->stash->{rest} = { html => $html };
93 sub trial_autocomplete : Local : ActionClass('REST') { }
95 sub trial_autocomplete_GET :Args(0) {
96 my ($self, $c) = @_;
98 my $term = $c->req->param('term');
100 print STDERR "Term: $term\n";
101 $term =~ s/(^\s+|\s+)$//g;
102 $term =~ s/\s+/ /g;
104 my $trial_design_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($c->dbic_schema("Bio::Chado::Schema"), "design", "project_property")->cvterm_id();
105 my @response_list;
106 my $q = "select distinct(name) from project join projectprop using(project_id) where project.name ilike ? and projectprop.type_id = ? ORDER BY name";
107 my $sth = $c->dbc->dbh->prepare($q);
108 $sth->execute('%'.$term.'%', $trial_design_cvterm_id);
109 while (my ($project_name) = $sth->fetchrow_array) {
110 push @response_list, $project_name;
112 #print STDERR Dumper \@response_list;
114 print STDERR "Returning...\n";
115 $c->stash->{rest} = \@response_list;