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 SGN
::Model
::Cvterm
;
14 BEGIN { extends
'Catalyst::Controller::REST'; }
17 default => 'application/json',
19 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
23 sub get_trials
: Path
('/ajax/breeders/get_trials') Args
(0) {
27 my $p = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $c->dbic_schema("Bio::Chado::Schema") } );
29 my $projects = $p->get_breeding_programs();
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) {
44 my $tree_type = $c->req->param('type') || 'trial'; #can be 'trial' or 'genotyping_trial', 'cross'
46 my $schema = $c->dbic_schema("Bio::Chado::Schema");
47 my $p = CXGN
::BreedersToolbox
::Projects
->new( { schema
=> $schema } );
49 my $projects = $p->get_breeding_programs();
52 my $folder_obj = CXGN
::Trial
::Folder
->new( { bcs_schema
=> $schema, folder_id
=> @
$projects[0]->[0] });
54 print STDERR
"Starting get trials $tree_type at time ".localtime()."\n";
55 foreach my $project (@
$projects) {
56 my %project = ( "id" => $project->[0], "name" => $project->[1]);
57 $html .= $folder_obj->get_jstree_html(\
%project, $schema, 'breeding_program', $tree_type);
59 print STDERR
"Finished get trials $tree_type at time ".localtime()."\n";
61 my $dir = catdir
($c->site_cluster_shared_dir, "folder");
62 eval { make_path
($dir) };
64 print "Couldn't create $dir: $@";
66 my $filename = $dir."/entire_jstree_html_$tree_type.txt";
69 open $OUTFILE, '>', $filename or die "Error opening $filename: $!";
70 print { $OUTFILE } $html or croak
"Cannot write to $filename: $!";
71 close $OUTFILE or croak
"Cannot close $filename: $!";
73 $c->stash->{rest
} = { status
=> 1 };
76 sub get_trials_with_folders_cached
: Path
('/ajax/breeders/get_trials_with_folders_cached') Args
(0) {
79 my $tree_type = $c->req->param('type') || 'trial'; #can be 'trial' or 'genotyping_trial', 'cross'
81 my $dir = catdir
($c->site_cluster_shared_dir, "folder");
82 my $filename = $dir."/entire_jstree_html_$tree_type.txt";
84 open(my $fh, '<', $filename) or die "cannot open file $filename";
92 $c->stash->{rest
} = { html
=> $html };
95 sub trial_autocomplete
: Local
: ActionClass
('REST') { }
97 sub trial_autocomplete_GET
:Args
(0) {
100 my $term = $c->req->param('term');
102 print STDERR
"Term: $term\n";
103 $term =~ s/(^\s+|\s+)$//g;
106 my $trial_design_cvterm_id = SGN
::Model
::Cvterm
->get_cvterm_row($c->dbic_schema("Bio::Chado::Schema"), "design", "project_property")->cvterm_id();
108 my $q = "select distinct(name) from project join projectprop using(project_id) where project.name ilike ? and projectprop.type_id = ? ORDER BY name";
109 my $sth = $c->dbc->dbh->prepare($q);
110 $sth->execute('%'.$term.'%', $trial_design_cvterm_id);
111 while (my ($project_name) = $sth->fetchrow_array) {
112 push @response_list, $project_name;
114 #print STDERR Dumper \@response_list;
116 print STDERR
"Returning...\n";
117 $c->stash->{rest
} = \
@response_list;