4 SGN::Controller::AJAX::Locations - a REST controller class to provide the
5 backend for managing Locations
13 package SGN
::Controller
::AJAX
::Locations
;
17 use CXGN
::BreedersToolbox
::Projects
;
18 use CXGN
::Location
::ParseUpload
;
23 BEGIN { extends
'Catalyst::Controller::REST' }
26 default => 'application/json',
28 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
31 sub get_all_locations
:Path
("/ajax/location/all") Args
(0) {
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) {
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;
59 $c->stash->{rest
} = { error
=> 'You must be logged in to add or edit a location.' };
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.' };
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,
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,
84 my $store = $location->store_location();
86 if ($store->{'error'}) {
87 $c->stash->{rest
} = { error
=> $store->{'error'} };
90 $c->stash->{rest
} = { success
=> $store->{'success'}, nd_geolocation_id
=> $store->{'nd_geolocation_id'} };
95 sub delete_location
:Path
('/ajax/location/delete') Args
(1) {
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." };
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." };
110 my $location_to_delete = CXGN
::Location
->new( {
111 bcs_schema
=> $c->dbic_schema("Bio::Chado::Schema"),
112 nd_geolocation_id
=> $location_id
115 my $delete = $location_to_delete->delete_location();
117 if ($delete->{'success'}) {
118 $c->stash->{rest
} = { success
=> $delete->{'success'} };
121 $c->stash->{rest
} = { error
=> $delete->{'error'} };
125 sub upload_locations
: Path
('/ajax/locations/upload') : ActionClass
('REST') { }
127 sub upload_locations_POST
: Args
(0) {
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);
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 };
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,
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 };
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 };
182 if ($parse_result->{'error'}) {
183 $c->stash->{rest
} = {filename
=> $upload_original_name, error
=> $parse_result->{'error'}};
187 foreach my $row (@
{$parse_result->{'success'}}) {
188 #get data from rows one at a time
190 my $location = CXGN
::Location
->new( {
191 bcs_schema
=> $schema,
192 nd_geolocation_id
=> undef,
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],
204 my $store = $location->store_location();
206 if ($store->{'error'}) {
207 $response{$data[0]} = $store->{'error'};
210 $response{$data[0]} = $store->{'success'};
214 $c->stash->{rest
} = \
%response;