seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / DataCollectorDownload.pm
blob333c9c091bb6f7d9a22010f3e657b61dd5986729
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', 'text/html' => '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.".xls";
72 close($fh);
73 move($tempfile, $file_path);
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 });
85 my $spreadsheet_response = $create_spreadsheet->download();
87 if ($spreadsheet_response->{error}) {
88 print STDERR "Returning with error . . .\n";
89 $c->stash->{rest} = { error => $spreadsheet_response->{error} };
90 return;
93 print STDERR "DOWNLOAD FILENAME = ".$create_spreadsheet->filename()."\n";
94 print STDERR "RELATIVE = $tempfile\n";
96 my $file_name = basename($file_path);
97 print STDERR "file name= $file_name\n";
99 $c->stash->{rest} = { filename => $urlencode{$tempfile.".xls"} };
103 sub _parse_list_from_json {
104 my $list_json = shift;
105 my $json = new JSON;
106 if ($list_json) {
107 my $decoded_list = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($list_json);
108 #my $decoded_list = decode_json($list_json);
109 my @array_of_list_items = @{$decoded_list};
110 return \@array_of_list_items;
112 else {
113 return;
117 #########
119 #########