Merge pull request #5230 from solgenomics/topic/open_pollinated
[sgn.git] / lib / SGN / Controller / AJAX / DataCollectorDownload.pm
blob6cf1be24609e6ea4cbb44bdc23394a08f4a9b07e
2 =head1 NAME
4 SGN::Controller::AJAX::PhenotypesDownload - a REST controller class to provide the
5 backend for downloading phenotype spreadsheets
7 =head1 DESCRIPTION
9 Downloading Phenotype Spreadsheets
11 =head1 AUTHOR
13 Jeremy Edwards <jde22@cornell.edu>
15 =cut
17 package SGN::Controller::AJAX::DataCollectorDownload;
19 use Moose;
20 use Try::Tiny;
21 use DateTime;
22 use File::Slurp;
23 use File::Spec::Functions;
24 use File::Copy;
25 use File::Basename;
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' }
36 __PACKAGE__->config(
37 default => 'application/json',
38 stash_key => 'rest',
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) {
46 my $self = shift;
47 my $c = shift;
48 $c->forward('create_DataCollector_spreadsheet_POST');
51 sub create_DataCollector_spreadsheet_POST : Args(0) {
52 print STDERR "phenotype download controller\n";
53 my ($self, $c) = @_;
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." };
64 return;
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";
72 close($fh);
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,
81 format => $format,
82 data_level => $data_level,
83 trial_stock_type => $trial_stock_type,
84 });
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} };
91 return;
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;
106 my $json = new JSON;
107 if ($list_json) {
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;
113 else {
114 return;
118 #########
120 #########