clean
[sgn.git] / lib / SGN / Controller / AJAX / PhenotypesUpload.pm
blob6b97349bd89b896c7ce093e02b6a860eca9d2cea
2 =head1 NAME
4 SGN::Controller::AJAX::PhenotypesUpload - a REST controller class to provide the
5 backend for uploading phenotype spreadsheets
7 =head1 DESCRIPTION
9 Uploading Phenotype Spreadsheets
11 =head1 AUTHOR
13 Jeremy Edwards <jde22@cornell.edu>
14 Naama Menda <nm249@cornell.edu>
15 Alex Ogbonna <aco46@cornell.edu>
16 Nicolas Morales <nm529@cornell.edu>
18 =cut
20 package SGN::Controller::AJAX::PhenotypesUpload;
22 use Moose;
23 use Try::Tiny;
24 use DateTime;
25 use File::Slurp;
26 use File::Spec::Functions;
27 use File::Copy;
28 use Data::Dumper;
30 BEGIN { extends 'Catalyst::Controller::REST' }
32 __PACKAGE__->config(
33 default => 'application/json',
34 stash_key => 'rest',
35 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
39 sub upload_phenotype_verify : Path('/ajax/phenotype/upload_verify') : ActionClass('REST') { }
40 sub upload_phenotype_verify_POST : Args(1) {
41 my ($self, $c, $file_type) = @_;
42 my $store_phenotypes = CXGN::Phenotypes::StorePhenotypes->new();
43 my $warning_status;
45 my ($success_status, $error_status, $parsed_data, $plots, $traits, $phenotype_metadata, $timestamp_included, $data_level, $overwrite_values, $image_zip) = _prep_upload($c, $file_type);
46 if (scalar(@$error_status)>0) {
47 $c->stash->{rest} = {success => $success_status, error => $error_status };
48 return;
51 my ($verified_warning, $verified_error) = $store_phenotypes->verify($c, $plots, $traits, $parsed_data, $phenotype_metadata, $timestamp_included, $image_zip);
52 if ($verified_error) {
53 push @$error_status, $verified_error;
54 $c->stash->{rest} = {success => $success_status, error => $error_status };
55 return;
57 if ($verified_warning) {
58 push @$warning_status, $verified_warning;
60 push @$success_status, "File data verified. Plot names and trait names are valid.";
62 $c->stash->{rest} = {success => $success_status, warning => $warning_status, error => $error_status};
65 sub upload_phenotype_store : Path('/ajax/phenotype/upload_store') : ActionClass('REST') { }
66 sub upload_phenotype_store_POST : Args(1) {
67 my ($self, $c, $file_type) = @_;
68 my $store_phenotypes = CXGN::Phenotypes::StorePhenotypes->new();
70 my ($success_status, $error_status, $parsed_data, $plots, $traits, $phenotype_metadata, $timestamp_included, $data_level, $overwrite_values, $image_zip) = _prep_upload($c, $file_type);
71 if (scalar(@$error_status)>0) {
72 $c->stash->{rest} = {success => $success_status, error => $error_status };
73 return;
76 #upload_phenotype_store function redoes the same verification that upload_phenotype_verify does before actually uploading. maybe this should be commented out.
77 #my ($verified_warning, $verified_error) = $store_phenotypes->verify($c,$plots,$traits, $parsed_data, $phenotype_metadata);
78 #if ($verified_error) {
79 #push @$error_status, $verified_error;
80 #$c->stash->{rest} = {success => $success_status, error => $error_status };
81 #return;
83 #push @$success_status, "File data verified. Plot names and trait names are valid.";
86 my $size = scalar(@$plots) * scalar(@$traits);
87 my $stored_phenotype_error = $store_phenotypes->store($c, $size, $plots, $traits, $parsed_data, $phenotype_metadata, $data_level, $overwrite_values, $image_zip);
89 if ($stored_phenotype_error) {
90 push @$error_status, $stored_phenotype_error;
91 $c->stash->{rest} = {success => $success_status, error => $error_status};
92 return;
94 push @$success_status, "Metadata saved for archived file.";
95 push @$success_status, "File data successfully stored.";
97 $c->stash->{rest} = {success => $success_status, error => $error_status};
100 sub _prep_upload {
101 my ($c, $file_type) = @_;
102 my $uploader = CXGN::UploadFile->new();
103 my $parser = CXGN::Phenotypes::ParseUpload->new();
104 my @success_status;
105 my @error_status;
106 my $timestamp_included;
107 my $upload;
108 my $subdirectory;
109 my $validate_type;
110 my $metadata_file_type;
111 my $data_level;
112 my $image_zip;
113 if ($file_type eq "spreadsheet") {
114 print STDERR "Spreadsheet \n";
115 $subdirectory = "spreadsheet_phenotype_upload";
116 $validate_type = "phenotype spreadsheet";
117 $metadata_file_type = "spreadsheet phenotype file";
118 $timestamp_included = $c->req->param('upload_spreadsheet_phenotype_timestamp_checkbox');
119 $data_level = $c->req->param('upload_spreadsheet_phenotype_data_level') || 'plots';
120 $upload = $c->req->upload('upload_spreadsheet_phenotype_file_input');
122 elsif ($file_type eq "fieldbook") {
123 print STDERR "Fieldbook \n";
124 $subdirectory = "tablet_phenotype_upload";
125 $validate_type = "field book";
126 $metadata_file_type = "tablet phenotype file";
127 $timestamp_included = 1;
128 $upload = $c->req->upload('upload_fieldbook_phenotype_file_input');
129 $image_zip = $c->req->upload('upload_fieldbook_phenotype_images_zipfile');
130 $data_level = $c->req->param('upload_fieldbook_phenotype_data_level') || 'plots';
132 elsif ($file_type eq "datacollector") {
133 print STDERR "Datacollector \n";
134 $subdirectory = "data_collector_phenotype_upload";
135 $validate_type = "datacollector spreadsheet";
136 $metadata_file_type = "data collector phenotype file";
137 $timestamp_included = $c->req->param('upload_datacollector_phenotype_timestamp_checkbox');
138 $upload = $c->req->upload('upload_datacollector_phenotype_file_input');
141 my $overwrite_values = $c->req->param('phenotype_upload_overwrite_values');
142 if ($overwrite_values) {
143 my $user_type = $c->user()->get_object->get_user_type();
144 print STDERR $user_type."\n";
145 if ($user_type ne 'curator') {
146 push @error_status, 'Must be a curator to overwrite values! Please contact us!';
150 my $upload_original_name = $upload->filename();
151 my $upload_tempfile = $upload->tempname;
152 my %phenotype_metadata;
153 my $time = DateTime->now();
154 my $timestamp = $time->ymd()."_".$time->hms();
156 my $archived_filename_with_path = $uploader->archive($c, $subdirectory, $upload_tempfile, $upload_original_name, $timestamp);
157 my $md5 = $uploader->get_md5($archived_filename_with_path);
158 if (!$archived_filename_with_path) {
159 push @error_status, "Could not save file $upload_original_name in archive.";
160 } else {
161 push @success_status, "File $upload_original_name saved in archive.";
163 unlink $upload_tempfile;
164 #print STDERR "Archived Phenotype File: $archived_filename_with_path\n";
166 my $archived_image_zipfile_with_path;
167 if ($image_zip) {
168 my $upload_original_name = $image_zip->filename();
169 my $upload_tempfile = $image_zip->tempname;
170 my %phenotype_metadata;
171 my $time = DateTime->now();
173 $archived_image_zipfile_with_path = $uploader->archive($c, $subdirectory, $upload_tempfile, $upload_original_name, $timestamp);
174 my $md5 = $uploader->get_md5($archived_image_zipfile_with_path);
175 if (!$archived_image_zipfile_with_path) {
176 push @error_status, "Could not save images zipfile $upload_original_name in archive.";
177 } else {
178 push @success_status, "Images Zip File $upload_original_name saved in archive.";
180 unlink $upload_tempfile;
181 #print STDERR "Archived Zipfile: $archived_image_zipfile_with_path\n";
184 ## Validate and parse uploaded file
185 my $validate_file = $parser->validate($validate_type, $archived_filename_with_path, $timestamp_included, $data_level);
186 if (!$validate_file) {
187 push @error_status, "Archived file not valid: $upload_original_name.";
189 if ($validate_file == 1){
190 push @success_status, "File valid: $upload_original_name.";
191 } else {
192 if ($validate_file->{'error'}) {
193 push @error_status, $validate_file->{'error'};
197 ## Set metadata
198 $phenotype_metadata{'archived_file'} = $archived_filename_with_path;
199 $phenotype_metadata{'archived_file_type'} = $metadata_file_type;
200 my $operator = $c->user()->get_object()->get_username();
201 $phenotype_metadata{'operator'} = $operator;
202 $phenotype_metadata{'date'} = $timestamp;
204 my $parsed_file = $parser->parse($validate_type, $archived_filename_with_path, $timestamp_included);
205 if (!$parsed_file) {
206 push @error_status, "Error parsing file $upload_original_name.";
208 if ($parsed_file->{'error'}) {
209 push @error_status, $parsed_file->{'error'};
211 my %parsed_data;
212 my @plots;
213 my @traits;
214 if (scalar(@error_status) == 0) {
215 if ($parsed_file && !$parsed_file->{'error'}) {
216 %parsed_data = %{$parsed_file->{'data'}};
217 @plots = @{$parsed_file->{'plots'}};
218 @traits = @{$parsed_file->{'traits'}};
219 push @success_status, "File data successfully parsed.";
223 return (\@success_status, \@error_status, \%parsed_data, \@plots, \@traits, \%phenotype_metadata, $timestamp_included, $data_level, $overwrite_values, $archived_image_zipfile_with_path);
226 #########
228 #########