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) {
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();
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) };
63 print "Couldn't create $dir: $@";
65 my $filename = $dir."/entire_jstree_html.txt";
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) {
79 my $dir = catdir
($c->site_cluster_shared_dir, "folder");
80 my $filename = $dir."/entire_jstree_html.txt";
82 open(my $fh, '<', $filename) or die "cannot open file $filename";
90 $c->stash->{rest
} = { html
=> $html };
93 sub trial_autocomplete
: Local
: ActionClass
('REST') { }
95 sub trial_autocomplete_GET
:Args
(0) {
98 my $term = $c->req->param('term');
100 print STDERR
"Term: $term\n";
101 $term =~ s/(^\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();
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;