2 package SGN
::Controller
::AJAX
::Trials
;
6 use CXGN
::BreedersToolbox
::Projects
;
7 use CXGN
::Trial
::Folder
;
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'; }
18 default => 'application/json',
20 map => { 'application/json' => 'JSON' },
24 sub get_trials
: Path
('/ajax/breeders/get_trials') Args
(0) {
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();
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) {
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) };
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) {
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) };
71 print "Couldn't create $dir: $@";
73 my $filename = $dir."/entire_jstree_html_$tree_type.txt";
75 open(my $fh, '< :encoding(UTF-8)', $filename) or warn "cannot open file $filename $!";
83 $html = _write_cached_folder_tree
($schema, $tree_type, $filename);
87 $c->stash->{rest
} = { html
=> $html };
90 sub _write_cached_folder_tree
{
92 my $tree_type = shift;
94 my $p = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $schema } );
96 my $projects = $p->get_breeding_programs();
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";
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: $!";
116 sub trial_autocomplete
: Local
: ActionClass
('REST') { }
118 sub trial_autocomplete_GET
:Args
(0) {
121 my $term = $c->req->param('term');
123 print STDERR
"Term: $term\n";
124 $term =~ s/(^\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();
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) {
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"};
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' }
161 $trial_id = $rs->project_id() if $rs;
164 if ( !$trial_id || $trial_id eq '' ) {
165 $c->stash->{rest
} = {error
=> "Trial not found"};
169 $c->stash->{rest
} = { trial_id
=> $trial_id };