clean
[sgn.git] / lib / SGN / Controller / AJAX / PhenotypesDownload.pm
blob91f2b75a7b789d1dd659b1e8625ae74f38e54296
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::PhenotypesDownload;
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 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);
32 use Data::Dumper;
33 use JSON qw( decode_json );
35 BEGIN { extends 'Catalyst::Controller::REST' }
37 __PACKAGE__->config(
38 default => 'application/json',
39 stash_key => 'rest',
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) {
47 my $self = shift;
48 my $c = shift;
49 $c->forward('create_phenotype_spreadsheet_POST');
52 sub create_phenotype_spreadsheet_POST : Args(0) {
53 print STDERR "phenotype download controller\n";
54 my ($self, $c) = @_;
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." };
74 return;
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,
89 format => $format,
90 data_level => $data_level,
91 sample_number => $sample_number,
92 predefined_columns => $predefined_columns,
93 });
95 $create_spreadsheet->download();
97 print STDERR "DOWNLOAD FILENAME = ".$create_spreadsheet->filename()."\n";
98 print STDERR "RELATIVE = $rel_file\n";
100 #if ($error) {
101 #$c->stash->{rest} = { error => $error };
102 #return;
104 $c->stash->{rest} = { filename => $urlencode{$rel_file.".xls"} };
108 sub _parse_list_from_json {
109 my $list_json = shift;
110 my $json = new JSON;
111 if ($list_json) {
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;
117 else {
118 return;
122 #########
124 #########