4 SGN::Controller::AJAX::PhenotypesDownload - a REST controller class to provide the
5 backend for downloading phenotype spreadsheets
9 Downloading Phenotype Spreadsheets
13 Jeremy Edwards <jde22@cornell.edu>
17 package SGN
::Controller
::AJAX
::PhenotypesDownload
;
23 use File
::Spec
::Functions
;
25 use List
::MoreUtils qw
/any /;
26 use SGN
::View
::ArrayElements qw
/array_elements_simple_view/;
27 use CXGN
::Stock
::StockTemplate
;
28 use JSON
-support_by_pp
;
29 use CXGN
::Phenotypes
::CreateSpreadsheet
;
30 use CXGN
::Trial
::Download
;
31 use Tie
::UrlEncoder
; our(%urlencode);
33 use JSON
qw( decode_json );
35 BEGIN { extends
'Catalyst::Controller::REST' }
38 default => 'application/json',
40 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
44 sub create_phenotype_spreadsheet
: Path
('/ajax/phenotype/create_spreadsheet') : ActionClass
('REST') { }
46 sub create_phenotype_spreadsheet_GET
: Args
(0) {
49 $c->forward('create_phenotype_spreadsheet_POST');
52 sub create_phenotype_spreadsheet_POST
: Args
(0) {
53 print STDERR
"phenotype download controller\n";
55 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
56 my $trial_id = $c->req->param('trial_id');
57 my $trait_list_ref = $c->req->param('trait_list');
58 my $format = $c->req->param('format') || "ExcelBasic";
59 my $data_level = $c->req->param('data_level') || "plots";
60 my $sample_number = $c->req->param('sample_number');
61 if ($sample_number eq '') {$sample_number = undef};
62 my $predefined_columns = $c->req->param('predefined_columns');
63 if ($predefined_columns) {
64 $predefined_columns = decode_json
($predefined_columns);
67 #print STDERR Dumper $sample_number;
68 #print STDERR Dumper $predefined_columns;
70 if ($data_level eq 'plants') {
71 my $trial = CXGN
::Trial
->new( { bcs_schema
=> $c->dbic_schema("Bio::Chado::Schema"), trial_id
=> $trial_id });
72 if (!$trial->has_plant_entries()) {
73 $c->stash->{rest
} = { error
=> "The requested trial (".$trial->get_name().") does not have plant entries. Please create the plant entries first." };
78 my @trait_list = @
{_parse_list_from_json
($c->req->param('trait_list'))};
79 my $dir = $c->tempfiles_subdir('/download');
80 my $rel_file = $c->tempfile( TEMPLATE
=> 'download/downloadXXXXX');
81 my $tempfile = $c->config->{basepath
}."/".$rel_file.".xls";
83 my $create_spreadsheet = CXGN
::Trial
::Download
->new(
85 bcs_schema
=> $schema,
86 trial_id
=> $trial_id,
87 trait_list
=> \
@trait_list,
88 filename
=> $tempfile,
90 data_level
=> $data_level,
91 sample_number
=> $sample_number,
92 predefined_columns
=> $predefined_columns,
95 $create_spreadsheet->download();
97 print STDERR
"DOWNLOAD FILENAME = ".$create_spreadsheet->filename()."\n";
98 print STDERR
"RELATIVE = $rel_file\n";
101 #$c->stash->{rest} = { error => $error };
104 $c->stash->{rest
} = { filename
=> $urlencode{$rel_file.".xls"} };
108 sub _parse_list_from_json
{
109 my $list_json = shift;
112 my $decoded_list = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($list_json);
113 #my $decoded_list = decode_json($list_json);
114 my @array_of_list_items = @
{$decoded_list};
115 return \
@array_of_list_items;