Merge pull request #5243 from solgenomics/topic/observations_upload_catch_error
[sgn.git] / lib / SGN / Controller / AJAX / Trials.pm
blobe746235e6e28dd35f96def0efed0dca47914d538
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 File::Slurp qw | read_file |;
13 use SGN::Model::Cvterm;
15 BEGIN { extends 'Catalyst::Controller::REST'; }
17 __PACKAGE__->config(
18 default => 'application/json',
19 stash_key => 'rest',
20 map => { 'application/json' => 'JSON' },
24 sub get_trials : Path('/ajax/breeders/get_trials') Args(0) {
25 my $self = shift;
26 my $c = shift;
27 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
28 my $p = CXGN::BreedersToolbox::Projects->new( { schema => $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id) } );
30 my $projects = $p->get_breeding_programs();
32 my %data = ();
33 foreach my $project (@$projects) {
34 my $trials = $p->get_trials_by_breeding_program($project->[0]);
35 $data{$project->[1]} = $trials;
39 $c->stash->{rest} = \%data;
42 sub get_trials_with_folders : Path('/ajax/breeders/get_trials_with_folders') Args(0) {
43 my $self = shift;
44 my $c = shift;
45 my $tree_type = $c->req->param('type') || 'trial'; #can be 'trial' or 'genotyping_trial', 'cross'
46 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
47 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
49 my $dir = catdir($c->config->{static_content_path}, "folder");
50 eval { make_path($dir) };
51 if ($@) {
52 print STDERR "Couldn't create $dir: $@";
54 my $filename = $dir."/entire_jstree_html_$tree_type.txt";
56 _write_cached_folder_tree($schema, $tree_type, $filename);
58 $c->stash->{rest} = { status => 1 };
61 sub get_trials_with_folders_cached : Path('/ajax/breeders/get_trials_with_folders_cached') Args(0) {
62 my $self = shift;
63 my $c = shift;
64 my $tree_type = $c->req->param('type') || 'trial'; #can be 'trial','genotyping_trial', 'cross', 'genotyping_project', 'activity'
65 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
66 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
68 my $dir = catdir($c->config->{static_content_path}, "folder");
69 eval { make_path($dir) };
70 if ($@) {
71 print "Couldn't create $dir: $@";
73 my $filename = $dir."/entire_jstree_html_$tree_type.txt";
74 my $html = '';
75 open(my $fh, '< :encoding(UTF-8)', $filename) or warn "cannot open file $filename $!";
77 local $/;
78 $html = <$fh>;
80 close($fh);
82 if (!$html) {
83 $html = _write_cached_folder_tree($schema, $tree_type, $filename);
86 #print STDERR $html;
87 $c->stash->{rest} = { html => $html };
90 sub _write_cached_folder_tree {
91 my $schema = shift;
92 my $tree_type = shift;
93 my $filename = shift;
94 my $p = CXGN::BreedersToolbox::Projects->new( { schema => $schema } );
96 my $projects = $p->get_breeding_programs();
98 my $html = "";
99 my $folder_obj = CXGN::Trial::Folder->new( { bcs_schema => $schema, folder_id => @$projects[0]->[0] });
101 print STDERR "Starting trial tree refresh for $tree_type at time ".localtime()."\n";
102 foreach my $project (@$projects) {
103 my %project = ( "id" => $project->[0], "name" => $project->[1]);
104 $html .= $folder_obj->get_jstree_html(\%project, $schema, 'breeding_program', $tree_type);
106 print STDERR "Finished trial tree refresh for $tree_type at time ".localtime()."\n";
108 my $OUTFILE;
109 open $OUTFILE, '> :encoding(UTF-8)', $filename or die "Error opening $filename: $!";
110 print { $OUTFILE } $html or croak "Cannot write to $filename: $!";
111 close $OUTFILE or croak "Cannot close $filename: $!";
113 return $html;
116 sub trial_autocomplete : Local : ActionClass('REST') { }
118 sub trial_autocomplete_GET :Args(0) {
119 my ($self, $c) = @_;
121 my $term = $c->req->param('term');
123 print STDERR "Term: $term\n";
124 $term =~ s/(^\s+|\s+)$//g;
125 $term =~ s/\s+/ /g;
126 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
127 my $trial_design_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id), "design", "project_property")->cvterm_id();
128 my @response_list;
129 my $q = "select distinct(name) from project join projectprop using(project_id) where project.name ilike ? and projectprop.type_id = ? and projectprop.value not in ('genotyping_plate', 'genotype_data_project', 'pcr_genotype_data_project') ORDER BY name";
130 my $sth = $c->dbc->dbh->prepare($q);
131 $sth->execute('%'.$term.'%', $trial_design_cvterm_id);
132 while (my ($project_name) = $sth->fetchrow_array) {
133 push @response_list, $project_name;
135 #print STDERR Dumper \@response_list;
137 print STDERR "Returning...\n";
138 $c->stash->{rest} = \@response_list;
142 sub trial_lookup : Path('/ajax/breeders/trial_lookup') Args(0) {
143 my $self = shift;
144 my $c = shift;
145 my $trial_name = $c->req->param('name');
146 my $sp_person_id = $c->user() ? $c->user->get_object()->get_sp_person_id() : undef;
147 my $schema = $c->dbic_schema("Bio::Chado::Schema", undef, $sp_person_id);
149 if ( !$trial_name || $trial_name eq '' ) {
150 $c->stash->{rest} = {error => "Trial name required"};
151 $c->detach();
154 # Get trial id by name
155 my $trial_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "phenotyping_trial", "project_type")->cvterm_id();
156 my $rs = $schema->resultset("Project::Project")->find(
157 { 'name' => $trial_name, 'projectprops.type_id' => $trial_type_id },
158 { join => 'projectprops' }
160 my $trial_id;
161 $trial_id = $rs->project_id() if $rs;
163 # Trial not found
164 if ( !$trial_id || $trial_id eq '' ) {
165 $c->stash->{rest} = {error => "Trial not found"};
166 $c->detach();
169 $c->stash->{rest} = { trial_id => $trial_id };