debugging
[sgn.git] / lib / SGN / Controller / AJAX / TrackingActivity.pm
blob89a451c16fd7fd2eedadfae73af4b4b18528dfb4
2 package SGN::Controller::AJAX::TrackingActivity;
4 use Moose;
5 use CXGN::Stock::Order;
6 use CXGN::Stock::OrderBatch;
7 use Data::Dumper;
8 use JSON;
9 use DateTime;
10 use CXGN::People::Person;
11 use CXGN::Contact;
12 use CXGN::Trial::Download;
13 use CXGN::Stock::TrackingActivity::TrackingIdentifier;
14 use CXGN::Stock::TrackingActivity::ActivityInfo;
15 use CXGN::TrackingActivity::AddActivityProject;
16 use CXGN::TrackingActivity::ActivityProject;
17 use SGN::Model::Cvterm;
18 use CXGN::Location::LocationLookup;
19 use CXGN::List;
21 use File::Basename qw | basename dirname|;
22 use File::Copy;
23 use File::Slurp;
24 use File::Spec::Functions;
25 use Digest::MD5;
26 use File::Path qw(make_path);
27 use File::Spec::Functions qw / catfile catdir/;
29 use LWP::UserAgent;
30 use LWP::Simple;
31 use HTML::Entities;
32 use URI::Encode qw(uri_encode uri_decode);
33 use Tie::UrlEncoder; our(%urlencode);
36 BEGIN { extends 'Catalyst::Controller::REST' }
38 __PACKAGE__->config(
39 default => 'application/json',
40 stash_key => 'rest',
41 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
45 sub create_activity_project : Path('/ajax/tracking_activity/create_activity_project') : ActionClass('REST'){ }
47 sub create_activity_project_POST : Args(0) {
48 my $self = shift;
49 my $c = shift;
51 if (!$c->user()) {
52 $c->stash->{rest} = { error => "You must be logged in to add project." };
53 return;
55 if (!($c->user()->has_role('submitter') or $c->user()->has_role('curator'))) {
56 $c->stash->{rest} = { error => "You do not have sufficient privileges to create activity project." };
57 return;
60 my $user_id = $c->user()->get_object()->get_sp_person_id();
61 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
62 my $dbh = $c->dbc->dbh;
64 my $project_name = $c->req->param("project_name");
65 my $activity_type = $c->req->param("activity_type");
66 my $breeding_program_id = $c->req->param("breeding_program");
67 my $project_location = $c->req->param("project_location");
68 my $year = $c->req->param("year");
69 my $project_description = $c->req->param("project_description");
71 my $geolocation_lookup = CXGN::Location::LocationLookup->new(schema =>$schema);
72 $geolocation_lookup->set_location_name($project_location);
73 if(!$geolocation_lookup->get_geolocation()){
74 $c->stash->{rest}={error => "Location not found"};
75 return;
78 my $error;
79 eval{
80 my $add_activity_project = CXGN::TrackingActivity::AddActivityProject->new({
81 bcs_schema => $schema,
82 dbh => $dbh,
83 breeding_program_id => $breeding_program_id,
84 year => $year,
85 project_description => $project_description,
86 activity_project_name => $project_name,
87 activity_type => $activity_type,
88 nd_geolocation_id => $geolocation_lookup->get_geolocation()->nd_geolocation_id(),
89 owner_id => $user_id
90 });
92 my $return = $add_activity_project->save_activity_project();
93 print STDERR "RETURN =".Dumper($return)."\n";
95 if ($return->{error}){
96 $error = $return->{error};
100 if ($@) {
101 $c->stash->{rest} = {error => $@};
102 return;
106 if ($error){
107 $c->stash->{rest} = {error => $error};
108 } else {
109 $c->stash->{rest} = {success => 1};
115 sub generate_tracking_identifiers : Path('/ajax/tracking_activity/generate_tracking_identifiers') : ActionClass('REST'){ }
117 sub generate_tracking_identifiers_POST : Args(0) {
118 my $self = shift;
119 my $c = shift;
121 if (!$c->user()) {
122 $c->stash->{rest} = { error => "You must be logged in to generate tracking identifiers." };
123 return;
125 if (!($c->user()->has_role('submitter') or $c->user()->has_role('curator'))) {
126 $c->stash->{rest} = { error => "You do not have sufficient privileges to generate tracking identifiers." };
127 return;
130 my $user_id = $c->user()->get_object()->get_sp_person_id();
131 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
132 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema");
133 my $dbh = $c->dbc->dbh();
135 my $project_name = $c->req->param("project_name");
136 my $list_id = $c->req->param("list_id");
137 my $project_id;
138 my $project_rs = $schema->resultset("Project::Project")->find( { name => $project_name });
139 if (!$project_rs) {
140 print STDERR "Error! Project name: $project_name was not found in the database.\n";
141 } else {
142 $project_id = $project_rs->project_id();
145 my $list = CXGN::List->new( { dbh=>$dbh, list_id=>$list_id });
146 my $material_names = $list->elements();
148 my @check_identifier_names;
149 my @tracking_identifiers;
150 foreach my $name (sort @$material_names) {
151 push @tracking_identifiers, [$project_name.":".$name, $name];
152 push @check_identifier_names, $project_name.":".$name;
155 foreach my $identifier_info (@tracking_identifiers) {
156 my $tracking_identifier = $identifier_info->[0];
157 my $material = $identifier_info->[1];
159 my $tracking_obj = CXGN::Stock::TrackingActivity::TrackingIdentifier->new({
160 schema => $schema,
161 phenome_schema => $phenome_schema,
162 tracking_identifier => $tracking_identifier,
163 material => $material,
164 project_id => $project_id,
165 user_id => $user_id
168 my $return = $tracking_obj->store();
169 if (!$return) {
174 $c->stash->{rest} = { success => 1};
179 sub activity_info_save : Path('/ajax/tracking_activity/save') : ActionClass('REST'){ }
181 sub activity_info_save_POST : Args(0) {
182 my $self = shift;
183 my $c = shift;
185 if (!$c->user()) {
186 $c->stash->{rest} = { error => "You must be logged in to add properties." };
187 return;
189 if (!($c->user()->has_role('submitter') or $c->user()->has_role('curator'))) {
190 $c->stash->{rest} = { error => "You do not have sufficient privileges to record activity info." };
191 return;
194 my $user_id = $c->user()->get_object()->get_sp_person_id();
195 print STDERR "USER ID =".Dumper($user_id)."\n";
197 my $tracking_identifier = $c->req->param("tracking_identifier");
198 my $selected_type = $c->req->param("selected_type");
199 my $input = $c->req->param("input");
200 my $record_timestamp = $c->req->param("record_timestamp");
202 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
203 my $check_tracking_identifier = $schema->resultset("Stock::Stock")->find({uniquename => $tracking_identifier});
204 print STDERR "AJAX IDENTIFIER =".Dumper($tracking_identifier)."\n";
205 print STDERR "AJAX SELECTED TYPE =".Dumper($selected_type)."\n";
206 print STDERR "AJAX INPUT =".Dumper($input)."\n";
207 print STDERR "AJAX TIMESTAMP =".Dumper($record_timestamp)."\n";
208 print STDERR "OPERATOR ID =".Dumper($user_id)."\n";
211 my $add_activity_info = CXGN::Stock::TrackingActivity::ActivityInfo->new({
212 schema => $schema,
213 tracking_identifier => $tracking_identifier,
214 selected_type => $selected_type,
215 input => $input,
216 timestamp => $record_timestamp,
217 operator_id => $user_id,
219 $add_activity_info->add_info();
220 print STDERR "ADD INFO =".Dumper ($add_activity_info->add_info())."\n";
222 if (!$add_activity_info->add_info()){
223 $c->stash->{rest} = {error_string => "Error saving info",};
224 return;
227 $c->stash->{rest} = { success => 1};
232 sub get_activity_details :Path('/ajax/tracking_activity/details') :Args(1) {
233 my $self = shift;
234 my $c = shift;
235 my $identifier_id = shift;
236 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
237 my $dbh = $c->dbc->dbh;
239 my @details;
240 my $tracking_activities = $c->config->{tracking_activities};
241 my @activity_types = split ',',$tracking_activities;
243 my $tracking_data_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'tracking_tissue_culture_json', 'stock_property')->cvterm_id();
244 my $activity_info_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $identifier_id, type_id => $tracking_data_json_cvterm_id});
245 if ($activity_info_rs) {
246 my $activity_json = $activity_info_rs->value();
247 my $info = JSON::Any->jsonToObj($activity_json);
248 my %info_hash = %{$info};
249 foreach my $type (@activity_types){
250 my $empty_string;
251 my @each_type_details = ();
252 my $each_timestamp_string;
253 my $each_type_string;
254 if ($info_hash{$type}) {
255 my @each_type_details = ();
256 my $details = {};
257 my %details_hash = ();
258 $details = $info_hash{$type};
259 %details_hash = %{$details};
260 # print STDERR "DETAILS HASH =".Dumper(\%details_hash);
261 foreach my $timestamp (keys %details_hash) {
262 my @each_timestamp_details = ();
263 push @each_timestamp_details, "timestamp".":"."".$timestamp;
264 my $operator_id = $details_hash{$timestamp}{'operator_id'};
266 my $person= CXGN::People::Person->new($dbh, $operator_id);
267 my $operator_name = $person->get_first_name()." ".$person->get_last_name();
269 push @each_timestamp_details, "operator".":"."".$operator_name;
270 my $input = $details_hash{$timestamp}{'input'};
271 push @each_timestamp_details, "count".":"."".$input;
272 push @each_timestamp_details, $empty_string;
274 $each_timestamp_string = join("<br>", @each_timestamp_details);
275 push @each_type_details, $each_timestamp_string;
278 $each_type_string = join("<br>", @each_type_details);
279 # print STDERR "EACH TYPE STRING =".Dumper($each_type_string)."\n";
280 push @details, $each_type_string;
281 } else {
282 my $empty_string;
283 push @details, $empty_string;
286 } else {
287 foreach my $type (@activity_types) {
288 push @details, 'NA';
292 my @all_details;
293 push @all_details, [@details];
295 # print STDERR "ALL DETAILS =".Dumper(\@all_details)."\n";
297 $c->stash->{rest} = { data => \@all_details };
302 sub get_activity_summary :Path('/ajax/tracking_activity/summary') :Args(1) {
303 my $self = shift;
304 my $c = shift;
305 my $identifier_id = shift;
306 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
308 my @summary = ();
309 my $tracking_activities = $c->config->{tracking_activities};
310 my @activity_types = split ',',$tracking_activities;
312 my $tracking_data_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'tracking_tissue_culture_json', 'stock_property')->cvterm_id();
313 my $activity_info_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $identifier_id, type_id => $tracking_data_json_cvterm_id});
314 if ($activity_info_rs) {
315 my $input;
316 my $activity_json = $activity_info_rs->value();
317 my $info = JSON::Any->jsonToObj($activity_json);
318 my %info_hash = %{$info};
319 foreach my $type (@activity_types){
320 my $empty_string;
321 my @each_type_details = ();
322 my $each_timestamp_string;
323 my $each_type_string;
324 if ($info_hash{$type}) {
325 my $details = {};
326 my %details_hash = ();
327 $details = $info_hash{$type};
328 %details_hash = %{$details};
329 my $input = 0;
330 foreach my $key (keys %details_hash) {
331 $input += $details_hash{$key}{'input'};
333 push @summary, $input;
334 } else {
335 push @summary, $input;
338 } else {
339 foreach my $type (@activity_types) {
340 push @summary, 'NA';
344 my @all_summary;
345 push @all_summary, [@summary];
347 $c->stash->{rest} = { data => \@all_summary };
351 sub get_project_active_identifiers :Path('/ajax/tracking_activity/project_active_identifiers') :Args(1) {
352 my $self = shift;
353 my $c = shift;
354 my $project_id = shift;
355 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
356 my $tracking_activities = $c->config->{tracking_activities};
357 my @activity_types = split ',',$tracking_activities;
359 my $activity_project = CXGN::TrackingActivity::ActivityProject->new(bcs_schema => $schema, trial_id => $project_id);
360 my $all_identifier_info = $activity_project->get_project_active_identifiers();
361 my @all_identifiers;
362 foreach my $identifier_info (@$all_identifier_info) {
363 my @row = ();
364 my $identifier_id = $identifier_info->[0];
365 my $identifier_name = $identifier_info->[1];
366 my $material_id = $identifier_info->[2];
367 my $material_name = $identifier_info->[3];
368 push @row, qq{<a href="/activity/details/$identifier_id">$identifier_name</a>};
369 push @row, qq{<a href="/stock/$material_id/view">$material_name</a>};
370 my $progress = $identifier_info->[5];
371 my $input;
372 if ($progress) {
373 my %progress_hash = %{$progress};
374 foreach my $type (@activity_types){
375 if ($progress_hash{$type}) {
376 my $details = {};
377 my %details_hash = ();
378 $details = $progress_hash{$type};
379 %details_hash = %{$details};
380 my $input = 0;
381 foreach my $key (keys %details_hash) {
382 $input += $details_hash{$key}{'input'};
384 push @row, $input
385 } else {
386 push @row, $input;
389 } else {
390 foreach my $type (@activity_types) {
391 push @row, $input;
395 push @all_identifiers,[@row];
398 $c->stash->{rest} = { data => \@all_identifiers };