From 1bc9fed70dc8b05f76f2280d3cb8bbc4e7bf6c17 Mon Sep 17 00:00:00 2001 From: nickmorales Date: Wed, 26 Apr 2017 15:08:07 +0300 Subject: [PATCH] download file of field layout from brapi --- lib/CXGN/BrAPI/v1/Studies.pm | 36 +++++++++++++++++++++++++++++++----- lib/SGN/Controller/AJAX/BrAPI.pm | 29 ++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 12 deletions(-) diff --git a/lib/CXGN/BrAPI/v1/Studies.pm b/lib/CXGN/BrAPI/v1/Studies.pm index 20536fc7b..6c7ff9661 100644 --- a/lib/CXGN/BrAPI/v1/Studies.pm +++ b/lib/CXGN/BrAPI/v1/Studies.pm @@ -360,7 +360,11 @@ sub studies_observation_variables { sub studies_layout { my $self = shift; - my $study_id = shift; + my $inputs = shift; + my $study_id = $inputs->{study_id}; + my $format = $inputs->{format} || 'json'; + my $file_path = $inputs->{file_path}; + my $file_uri = $inputs->{file_uri}; my $page_size = $self->page_size; my $page = $self->page; my $status = $self->status; @@ -407,10 +411,32 @@ sub studies_layout { } $count++; } - my %result = (data=>$plot_data); - my @data_files; - my $pagination = CXGN::BrAPI::Pagination->pagination_response($count,$page_size,$page); - return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Studies layout result constructed'); + my %result; + my @data_files; + if ($format eq 'json'){ + %result = (data=>$plot_data); + } elsif ($format eq 'tsv' || $format eq 'csv' || $format eq 'xls') { + # if xls or csv or tsv, create tempfile name and place to save it + + my @header_row = ('studyDbId', 'observationUnitDbId', 'observationUnitName', 'observationLevel', 'replicate', 'blockNumber', 'X', 'Y', 'entryType', 'germplasmName', 'germplasmDbId'); + my @data_out; + push @data_out, \@header_row; + foreach (@$plot_data){ + my @row = ($_->{studyDbId}, $_->{observationUnitDbId}, $_->{observationUnitName}, $_->{observationLevel}, $_->{replicate}, $_->{blockNumber}, $_->{X}, $_->{Y}, $_->{entryType}, $_->{germplasmName}, $_->{germplasmDbId}); + push @data_out, \@row; + } + + my $file_response = CXGN::BrAPI::FileResponse->new({ + absolute_file_path => $file_path, + absolute_file_uri => $inputs->{main_production_site_url}.$file_uri, + format => $format, + data => \@data_out + }); + @data_files = $file_response->get_datafiles(); + + } + my $pagination = CXGN::BrAPI::Pagination->pagination_response($count,$page_size,$page); + return CXGN::BrAPI::JSONResponse->return_success(\%result, $pagination, \@data_files, $status, 'Studies layout result constructed'); } diff --git a/lib/SGN/Controller/AJAX/BrAPI.pm b/lib/SGN/Controller/AJAX/BrAPI.pm index c0a9693d6..e3c993d43 100644 --- a/lib/SGN/Controller/AJAX/BrAPI.pm +++ b/lib/SGN/Controller/AJAX/BrAPI.pm @@ -1552,15 +1552,30 @@ sub studies_layout_POST { } sub studies_layout_GET { - my $self = shift; - my $c = shift; - #my $auth = _authenticate_user($c); + my $self = shift; + my $c = shift; + my $clean_inputs = $c->stash->{clean_inputs}; + #my $auth = _authenticate_user($c); + my $format = $clean_inputs->{format}->[0] || 'json'; + my $file_path; + my $uri; + if ($format eq 'tsv' || $format eq 'csv' || $format eq 'xls'){ + my $dir = $c->tempfiles_subdir('download'); + my $time_stamp = strftime "%Y-%m-%dT%H%M%S", localtime(); + my $temp_file_name = $time_stamp . "phenotype_download_$format"."_XXXX"; + ($file_path, $uri) = $c->tempfile( TEMPLATE => "download/$temp_file_name"); + } + my $brapi = $self->brapi_module; my $brapi_module = $brapi->brapi_wrapper('Studies'); - my $brapi_package_result = $brapi_module->studies_layout( - $c->stash->{study_id} - ); - _standard_response_construction($c, $brapi_package_result); + my $brapi_package_result = $brapi_module->studies_layout({ + study_id => $c->stash->{study_id}, + format => $format, + main_production_site_url => $c->config->{main_production_site_url}, + file_path => $file_path, + file_uri => $uri + }); + _standard_response_construction($c, $brapi_package_result); } -- 2.11.4.GIT