seedlot upload with accession synonyms. seedlot upload works to update existing seedlots
[sgn.git] / lib / SGN / Controller / AJAX / Locations.pm
blob8d855aabaac3e07f639b85c9c6465d16f9277c35
2 =head1 NAME
4 SGN::Controller::AJAX::Locations - a REST controller class to provide the
5 backend for managing Locations
7 =head1 DESCRIPTION
9 Managing Locations
11 =cut
13 package SGN::Controller::AJAX::Locations;
15 use Moose;
16 use CXGN::Location;
17 use CXGN::BreedersToolbox::Projects;
18 use CXGN::Location::ParseUpload;
19 use Data::Dumper;
20 use Try::Tiny;
21 use JSON;
23 BEGIN { extends 'Catalyst::Controller::REST' }
25 __PACKAGE__->config(
26 default => 'application/json',
27 stash_key => 'rest',
28 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
31 sub get_all_locations :Path("/ajax/location/all") Args(0) {
32 my $self = shift;
33 my $c = shift;
35 my $location = CXGN::BreedersToolbox::Projects->new( { schema => $c->dbic_schema("Bio::Chado::Schema") });
37 my $all_locations = $location->get_location_geojson();
38 #print STDERR "Returning with all locations: ".$all_locations."\n";
39 $c->stash->{rest} = { data => $all_locations };
43 sub store_location :Path("/ajax/location/store") Args(0) {
44 my $self = shift;
45 my $c = shift;
46 my $params = $c->request->parameters();
47 my $id = $params->{id} || undef;
48 my $name = $params->{name};
49 my $abbreviation = $params->{abbreviation};
50 my $country_name = $params->{country_name};
51 my $country_code = $params->{country_code};
52 my $program = $params->{program};
53 my $type = $params->{type};
54 my $latitude = $params->{latitude} || undef;
55 my $longitude = $params->{longitude} || undef;
56 my $altitude = $params->{altitude} || undef;
58 if (! $c->user()) {
59 $c->stash->{rest} = { error => 'You must be logged in to add or edit a location.' };
60 return;
63 if (! $c->user->check_roles("submitter") && !$c->user->check_roles("curator")) {
64 $c->stash->{rest} = { error => 'You do not have the necessary privileges to add or edit locations.' };
65 return;
68 print STDERR "Creating location object\n";
70 my $location = CXGN::Location->new( {
71 bcs_schema => $c->dbic_schema("Bio::Chado::Schema"),
72 nd_geolocation_id => $id,
73 name => $name,
74 abbreviation => $abbreviation,
75 country_name => $country_name,
76 country_code => $country_code,
77 breeding_program => $program,
78 location_type => $type,
79 latitude => $latitude,
80 longitude => $longitude,
81 altitude => $altitude
82 });
84 my $store = $location->store_location();
86 if ($store->{'error'}) {
87 $c->stash->{rest} = { error => $store->{'error'} };
89 else {
90 $c->stash->{rest} = { success => $store->{'success'}, nd_geolocation_id => $store->{'nd_geolocation_id'} };
95 sub delete_location :Path('/ajax/location/delete') Args(1) {
96 my $self = shift;
97 my $c = shift;
98 my $location_id = shift;
100 if (!$c->user) { # require login
101 $c->stash->{rest} = { error => "You need to be logged in to delete a location." };
102 return;
105 if (! ($c->user->check_roles('curator') || $c->user->check_roles('submitter'))) { # require curator or submitter roles
106 $c->stash->{rest} = { error => "You don't have the privileges to delete a location." };
107 return;
110 my $location_to_delete = CXGN::Location->new( {
111 bcs_schema => $c->dbic_schema("Bio::Chado::Schema"),
112 nd_geolocation_id => $location_id
113 } );
115 my $delete = $location_to_delete->delete_location();
117 if ($delete->{'success'}) {
118 $c->stash->{rest} = { success => $delete->{'success'} };
120 else {
121 $c->stash->{rest} = { error => $delete->{'error'} };
125 sub upload_locations : Path('/ajax/locations/upload') : ActionClass('REST') { }
127 sub upload_locations_POST : Args(0) {
128 my ($self, $c) = @_;
129 my $schema = $c->dbic_schema("Bio::Chado::Schema");
131 my $upload = $c->req->upload('locations_upload_file');
132 my $upload_original_name = $upload->filename();
133 my $upload_tempfile = $upload->tempname;
135 my $time = DateTime->now();
136 my $timestamp = $time->ymd()."_".$time->hms();
137 my (@errors, %response);
140 if (!$c->user()) {
141 print STDERR "User not logged in... not uploading locations.\n";
142 push @errors, "You need to be logged in to upload locations.";
143 $c->stash->{rest} = {filename => $upload_original_name, error => \@errors };
144 return;
147 my $user_id = $c->user()->get_object()->get_sp_person_id();
148 my $user_role = $c->user->get_object->get_user_type();
150 my $uploader = CXGN::UploadFile->new({
151 tempfile => $upload_tempfile,
152 subdirectory => 'location_upload',
153 archive_path => $c->config->{archive_path},
154 archive_filename => $upload_original_name,
155 timestamp => $timestamp,
156 user_id => $user_id,
157 user_role => $user_role
160 ## Store uploaded temporary file in archive
161 my $archived_filename_with_path = $uploader->archive();
162 my $md5 = $uploader->get_md5($archived_filename_with_path);
163 if (!$archived_filename_with_path) {
164 push @errors, "Could not save file $upload_original_name in archive";
165 $c->stash->{rest} = {filename => $upload_original_name, error => \@errors };
166 return;
168 unlink $upload_tempfile;
170 #parse uploaded file with appropriate plugin
171 my $type = 'location excel';
172 my $parser = CXGN::Location::ParseUpload->new();
173 my $parse_result = $parser->parse($type, $archived_filename_with_path, $schema);
175 print STDERR "Dump of parsed result:\t" . Dumper($parse_result) . "\n";
177 if (!$parse_result) {
178 push @errors, "Error parsing file.";
179 $c->stash->{rest} = {filename => $upload_original_name, error => \@errors };
180 return;
182 if ($parse_result->{'error'}) {
183 $c->stash->{rest} = {filename => $upload_original_name, error => $parse_result->{'error'}};
184 return;
187 foreach my $row (@{$parse_result->{'success'}}) {
188 #get data from rows one at a time
189 my @data = @$row;
190 my $location = CXGN::Location->new( {
191 bcs_schema => $schema,
192 nd_geolocation_id => undef,
193 name => $data[0],
194 abbreviation => $data[1],
195 country_code => $data[2],
196 country_name => $data[3],
197 breeding_program => $data[4],
198 location_type => $data[5],
199 latitude => $data[6],
200 longitude => $data[7],
201 altitude => $data[8]
204 my $store = $location->store_location();
206 if ($store->{'error'}) {
207 $response{$data[0]} = $store->{'error'};
209 else {
210 $response{$data[0]} = $store->{'success'};
214 $c->stash->{rest} = \%response;