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
::DataCollectorDownload
;
23 use File
::Spec
::Functions
;
26 use List
::MoreUtils qw
/any /;
27 use SGN
::View
::ArrayElements qw
/array_elements_simple_view/;
28 use CXGN
::Stock
::StockTemplate
;
29 use JSON
-support_by_pp
;
30 use CXGN
::Phenotypes
::DataCollectorSpreadsheet
;
31 use CXGN
::Trial
::Download
;
32 use Tie
::UrlEncoder
; our(%urlencode);
34 BEGIN { extends
'Catalyst::Controller::REST' }
37 default => 'application/json',
39 map => { 'application/json' => 'JSON' },
43 sub create_DataCollector_spreadsheet
: Path
('/ajax/phenotype/create_DataCollector') : ActionClass
('REST') { }
45 sub create_DataCollector_spreadsheet_GET
: Args
(0) {
48 $c->forward('create_DataCollector_spreadsheet_POST');
51 sub create_DataCollector_spreadsheet_POST
: Args
(0) {
52 print STDERR
"phenotype download controller\n";
54 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
55 my $trial_id = $c->req->param('trial_id');
56 my $trait_list_ref = $c->req->param('trait_list');
57 my $format = $c->req->param('format') || "DataCollectorExcel";
58 my $data_level = $c->req->param('data_level') || "plots";
60 if ($data_level eq 'plants') {
61 my $trial = CXGN
::Trial
->new( { bcs_schema
=> $c->dbic_schema("Bio::Chado::Schema"), trial_id
=> $trial_id });
62 if (!$trial->has_plant_entries()) {
63 $c->stash->{rest
} = { error
=> "The requested trial (".$trial->get_name().") does not have plant entries. Please create the plant entries first." };
68 my @trait_list = @
{_parse_list_from_json
($c->req->param('trait_list'))};
69 my $dir = $c->tempfiles_subdir('download');
70 my ($fh, $tempfile) = $c->tempfile( TEMPLATE
=> 'download/'.$format.'_'.$trial_id.'_'.'XXXXX');
71 my $file_path = $c->config->{basepath
}."/".$tempfile.".xlsx";
73 move
($tempfile, $file_path);
74 my $trial_stock_type = $c->req->param('trial_stock_type');
75 my $create_spreadsheet = CXGN
::Trial
::Download
->new(
77 bcs_schema
=> $schema,
78 trial_id
=> $trial_id,
79 trait_list
=> \
@trait_list,
80 filename
=> $file_path,
82 data_level
=> $data_level,
83 trial_stock_type
=> $trial_stock_type,
86 my $spreadsheet_response = $create_spreadsheet->download();
88 if ($spreadsheet_response->{error
}) {
89 print STDERR
"Returning with error . . .\n";
90 $c->stash->{rest
} = { error
=> $spreadsheet_response->{error
} };
94 print STDERR
"DOWNLOAD FILENAME = ".$create_spreadsheet->filename()."\n";
95 print STDERR
"RELATIVE = $tempfile\n";
97 my $file_name = basename
($file_path);
98 print STDERR
"file name= $file_name\n";
100 $c->stash->{rest
} = { filename
=> $urlencode{$tempfile.".xlsx"} };
104 sub _parse_list_from_json
{
105 my $list_json = shift;
108 #my $decoded_list = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($list_json);
109 my $decoded_list = decode_json
($list_json);
110 my @array_of_list_items = @
{$decoded_list};
111 return \
@array_of_list_items;