Added eval; site now shows clean dataset missing message instead of server error...
[sgn.git] / lib / SGN / Controller / Search / Cross.pm
blobc896b68b4180e239964b92162e86dddac724218b
2 package SGN::Controller::Search::Cross;
4 use Moose;
6 use File::Basename;
7 use File::Slurp qw | read_file |;
8 use URI::FromHash 'uri';
9 use CXGN::Trial::Download;
10 use DateTime;
11 use Data::Dumper;
13 BEGIN { extends 'Catalyst::Controller' };
15 sub search_page : Path('/search/cross') Args(0) {
16 my $self = shift;
17 my $c = shift;
19 my $user = $c->user();
20 if (!$user) {
21 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
22 return;
25 $c->stash->{template} = '/search/cross.mas';
30 sub search_progenies_using_female : Path('/search/progenies_using_female') Args(0) {
31 my $self = shift;
32 my $c = shift;
34 $c->stash->{template} = '/search/cross/progeny_search_using_female.mas';
39 sub search_progenies_using_male : Path('/search/progenies_using_male') Args(0) {
40 my $self = shift;
41 my $c = shift;
43 $c->stash->{template} = '/search/cross/progeny_search_using_male.mas';
48 sub search_crosses_using_female : Path('/search/crosses_using_female') Args(0) {
49 my $self = shift;
50 my $c = shift;
52 $c->stash->{template} = '/search/cross/cross_search_using_female.mas';
57 sub search_crosses_using_male : Path('/search/crosses_using_male') Args(0) {
58 my $self = shift;
59 my $c = shift;
61 $c->stash->{template} = '/search/cross/cross_search_using_male.mas';
66 sub download_cross_entries : Path('/search/download_cross_entries') Args(0) {
67 my $self = shift;
68 my $c = shift;
70 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
71 my $user = $c->user();
72 if (!$user) {
73 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
74 return;
77 my $cross_properties_string = $c->config->{cross_properties};
78 my @cross_properties = split ',', $cross_properties_string;
79 my $file_format = "xlsx";
81 my $time = DateTime->now();
82 my $timestamp = $time->ymd();
83 my $dir = $c->tempfiles_subdir('download');
84 my $temp_file_name = "cross_entries". "XXXX";
85 my $rel_file = $c->tempfile( TEMPLATE => "download/$temp_file_name");
86 $rel_file = $rel_file . ".$file_format";
87 my $tempfile = $c->config->{basepath}."/".$rel_file;
88 # print STDERR "TEMPFILE : $tempfile\n";
90 my $download = CXGN::Trial::Download->new({
91 bcs_schema => $schema,
92 filename => $tempfile,
93 format => 'CrossEntriesXLS',
94 field_crossing_data_order => \@cross_properties
95 });
97 my $error = $download->download();
99 my $file_name = "cross_entries" . "_" . "$timestamp" . ".$file_format";
100 $c->res->content_type('Application/'.$file_format);
101 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
103 my $output = read_file($tempfile);
105 $c->res->body($output);
111 sub download_all_accessions_with_pedigree : Path('/search/download_all_accessions_with_pedigree') Args(0) {
112 my $self = shift;
113 my $c = shift;
115 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
116 my $user = $c->user();
117 if (!$user) {
118 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
119 return;
122 my $file_format = "xlsx";
124 my $time = DateTime->now();
125 my $timestamp = $time->ymd();
126 my $dir = $c->tempfiles_subdir('download');
127 my $temp_file_name = "accessions_with_pedigree". "XXXX";
128 my $rel_file = $c->tempfile( TEMPLATE => "download/$temp_file_name");
129 $rel_file = $rel_file . ".$file_format";
130 my $tempfile = $c->config->{basepath}."/".$rel_file;
131 # print STDERR "TEMPFILE : $tempfile\n";
133 my $download = CXGN::Trial::Download->new({
134 bcs_schema => $schema,
135 filename => $tempfile,
136 format => 'AccessionsWithPedigreeXLSX',
139 my $error = $download->download();
141 my $file_name = "all_accessions_with_pedigree" . "_" . "$timestamp" . ".$file_format";
142 $c->res->content_type('Application/'.$file_format);
143 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
145 my $output = read_file($tempfile);
147 $c->res->body($output);
151 sub download_parents_and_numbers_of_progenies : Path('/search/download_parents_and_numbers_of_progenies') Args(0) {
152 my $self = shift;
153 my $c = shift;
155 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
156 my $user = $c->user();
157 if (!$user) {
158 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
159 return;
162 my $parent_type = $c->req->param("parent_type");
163 my $plugin_type;
164 if ($parent_type eq 'female_parent') {
165 $plugin_type = 'FemaleParentsAndNumbersOfProgeniesXLSX';
166 } elsif ($parent_type eq 'male_parent') {
167 $plugin_type = 'MaleParentsAndNumbersOfProgeniesXLSX';
170 my $file_format = "xlsx";
172 my $time = DateTime->now();
173 my $timestamp = $time->ymd();
174 my $dir = $c->tempfiles_subdir('download');
176 my $temp_file_name;
177 if ($parent_type eq 'female_parent') {
178 $temp_file_name = "female_parents_and_numbers_of_progenies". "XXXX";
179 } elsif ($parent_type eq 'male_parent') {
180 $temp_file_name = "male_parents_and_numbers_of_progenies". "XXXX";
183 my $rel_file = $c->tempfile( TEMPLATE => "download/$temp_file_name");
184 $rel_file = $rel_file . ".$file_format";
185 my $tempfile = $c->config->{basepath}."/".$rel_file;
186 # print STDERR "TEMPFILE : $tempfile\n";
188 my $download = CXGN::Trial::Download->new({
189 bcs_schema => $schema,
190 filename => $tempfile,
191 format => $plugin_type,
193 my $error = $download->download();
195 my $file_name;
196 if ($parent_type eq 'female_parent') {
197 $file_name = "female_parents_and_numbers_of_progenies" . "_" . "$timestamp" . ".$file_format";
198 } elsif ($parent_type eq 'male_parent') {
199 $file_name = "male_parents_and_numbers_of_progenies" . "_" . "$timestamp" . ".$file_format";
201 $c->res->content_type('Application/'.$file_format);
202 $c->res->header('Content-Disposition', qq[attachment; filename="$file_name"]);
204 my $output = read_file($tempfile);
206 $c->res->body($output);