debugging
[sgn.git] / lib / SGN / Controller / AJAX / TrackingActivity.pm
blob0a5842fd3ddb2d506057ab9cdd0c8c9d9a2dc9cf
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::TrackingActivity::AddTrackingIdentifier;
14 use CXGN::TrackingActivity::ActivityInfo;
15 use CXGN::TrackingActivity::AddActivityProject;
16 use CXGN::TrackingActivity::ActivityProject;
17 use CXGN::TrackingActivity::TrackingIdentifier;
18 use CXGN::Transformation::CreateAutogeneratedNames;
19 use SGN::Model::Cvterm;
20 use CXGN::Location::LocationLookup;
21 use CXGN::List;
22 use CXGN::Stock::Status;
24 use File::Basename qw | basename dirname|;
25 use File::Copy;
26 use File::Slurp;
27 use File::Spec::Functions;
28 use Digest::MD5;
29 use File::Path qw(make_path);
30 use File::Spec::Functions qw / catfile catdir/;
32 use LWP::UserAgent;
33 use LWP::Simple;
34 use HTML::Entities;
35 use URI::Encode qw(uri_encode uri_decode);
36 use Tie::UrlEncoder; our(%urlencode);
39 BEGIN { extends 'Catalyst::Controller::REST' }
41 __PACKAGE__->config(
42 default => 'application/json',
43 stash_key => 'rest',
44 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
48 sub create_tracking_activity_project : Path('/ajax/tracking_activity/create_tracking_activity_project') : ActionClass('REST'){ }
50 sub create_tracking_activity_project_POST : Args(0) {
51 my $self = shift;
52 my $c = shift;
54 my $user = $c->user();
55 if (!$user) {
56 $c->res->redirect( uri( path => '/user/login', query => { goto_url => $c->req->uri->path_query } ) );
57 return;
60 if (!($user->has_role('submitter') or $user->has_role('curator'))) {
61 $c->stash->{rest} = { error => "You do not have sufficient privileges to create tracking activity project." };
62 return;
65 my $user_id = $c->user()->get_object()->get_sp_person_id();
66 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
67 my $dbh = $c->dbc->dbh;
69 my $project_name = $c->req->param("project_name");
70 my $activity_type = $c->req->param("activity_type");
71 my $breeding_program_id = $c->req->param("breeding_program");
72 my $project_location = $c->req->param("project_location");
73 my $year = $c->req->param("year");
74 my $project_description = $c->req->param("project_description");
76 my $geolocation_lookup = CXGN::Location::LocationLookup->new(schema =>$schema);
77 $geolocation_lookup->set_location_name($project_location);
78 if(!$geolocation_lookup->get_geolocation()){
79 $c->stash->{rest}={error => "Location not found"};
80 return;
83 my $error;
84 eval{
85 my $add_activity_project = CXGN::TrackingActivity::AddActivityProject->new({
86 bcs_schema => $schema,
87 dbh => $dbh,
88 breeding_program_id => $breeding_program_id,
89 year => $year,
90 project_description => $project_description,
91 activity_project_name => $project_name,
92 activity_type => $activity_type,
93 nd_geolocation_id => $geolocation_lookup->get_geolocation()->nd_geolocation_id(),
94 owner_id => $user_id
95 });
97 my $return = $add_activity_project->save_activity_project();
98 if (!$return){
99 $c->stash->{rest} = {error => "Error saving project",};
100 return;
103 # if ($return->{error}){
104 # $error = $return->{error};
108 if ($@) {
109 $c->stash->{rest} = {error => $@};
110 return;
114 if ($error){
115 $c->stash->{rest} = {error => $error};
116 } else {
117 $c->stash->{rest} = {success => 1};
123 sub generate_tracking_identifiers : Path('/ajax/tracking_activity/generate_tracking_identifiers') : ActionClass('REST'){ }
125 sub generate_tracking_identifiers_POST : Args(0) {
126 my $self = shift;
127 my $c = shift;
129 if (!$c->user()) {
130 $c->stash->{rest} = { error_string => "You must be logged in to generate tracking identifiers." };
131 return;
133 if (!($c->user()->has_role('submitter') or $c->user()->has_role('curator'))) {
134 $c->stash->{rest} = { error_string => "You do not have sufficient privileges to generate tracking identifiers." };
135 return;
138 my $user_id = $c->user()->get_object()->get_sp_person_id();
139 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
140 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema");
141 my $dbh = $c->dbc->dbh();
143 my $project_name = $c->req->param("project_name");
144 my $list_id = $c->req->param("list_id");
145 my $project_id;
146 my $project_rs = $schema->resultset("Project::Project")->find( { name => $project_name });
147 if (!$project_rs) {
148 $c->stash->{rest} = { error_string => "Error! Project name: $project_name was not found in the database.\n" };
149 return;
150 } else {
151 $project_id = $project_rs->project_id();
154 my $activity_project = CXGN::TrackingActivity::ActivityProject->new(bcs_schema => $schema, trial_id => $project_id);
155 my $all_identifiers = $activity_project->get_project_active_identifiers();
156 my $last_number = scalar (@$all_identifiers);
158 my $list = CXGN::List->new( { dbh=>$dbh, list_id=>$list_id });
159 my $material_names = $list->elements();
161 my @check_identifier_names;
162 my @tracking_identifiers;
163 my @error_messages;
164 foreach my $name (sort @$material_names) {
165 $last_number++;
166 # my $tracking_id = $project_name.":".$name."_"."T".(sprintf "%04d", $last_number);
167 my $tracking_id = $project_name.":".$name."_"."T".$last_number;
168 push @tracking_identifiers, [$tracking_id, $name];
169 push @check_identifier_names, $tracking_id;
172 my $rs = $schema->resultset("Stock::Stock")->search({
173 'is_obsolete' => { '!=' => 't' },
174 'uniquename' => { -in => \@check_identifier_names }
176 while (my $r=$rs->next){
177 push @error_messages, "Tracking identifier name already exists in database: ".$r->uniquename;
180 if (scalar(@error_messages) >= 1) {
181 $c->stash->{rest} = { error_string => \@error_messages};
182 return;
185 foreach my $identifier_info (@tracking_identifiers) {
186 my $tracking_identifier = $identifier_info->[0];
187 my $material = $identifier_info->[1];
189 my $tracking_obj = CXGN::TrackingActivity::AddTrackingIdentifier->new({
190 schema => $schema,
191 phenome_schema => $phenome_schema,
192 tracking_identifier => $tracking_identifier,
193 material => $material,
194 project_id => $project_id,
195 user_id => $user_id
198 my $return = $tracking_obj->store();
199 if (!$return){
200 $c->stash->{rest} = {error_string => "Error generating tracking identifier",};
201 return;
205 $c->stash->{rest} = { success => 1};
210 sub activity_info_save : Path('/ajax/tracking_activity/save') : ActionClass('REST'){ }
212 sub activity_info_save_POST : Args(0) {
213 my $self = shift;
214 my $c = shift;
216 if (!$c->user()) {
217 $c->stash->{rest} = { error => "You must be logged in to add new information." };
218 return;
220 if (!($c->user()->has_role('submitter') or $c->user()->has_role('curator'))) {
221 $c->stash->{rest} = { error => "You do not have sufficient privileges to record new information." };
222 return;
225 my $user_id = $c->user()->get_object()->get_sp_person_id();
227 my $tracking_identifier = $c->req->param("tracking_identifier_name");
228 my $tracking_identifier_id = $c->req->param("tracking_identifier_id");
229 my $selected_type = $c->req->param("selected_type");
230 my $input = $c->req->param("input");
231 my $notes = $c->req->param("notes");
232 my $record_timestamp = $c->req->param("record_timestamp");
233 my $tracking_transformation = $c->config->{tracking_transformation};
235 if ($selected_type =~ m/number/ && !($input =~ /^\d+?$/) ) {
236 $c->stash->{rest} = {error => "Input is not a positive integer"};
237 return;
240 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
241 my $phenome_schema = $c->dbic_schema("CXGN::Phenome::Schema");
242 my $dbh = $c->dbc->dbh();
243 my $number_of_transformants;
244 if (($selected_type eq 'number_of_transformants') && $tracking_transformation) {
245 $number_of_transformants = $input;
246 my $tracking_identifier_obj = CXGN::TrackingActivity::TrackingIdentifier->new({schema => $schema, dbh => $dbh, tracking_identifier_stock_id => $tracking_identifier_id});
247 my $project_and_program_info = $tracking_identifier_obj->get_associated_project_program();
248 my $program_id = $project_and_program_info->[0]->[2];
249 my $linked_project_id = $project_and_program_info->[0]->[4];
250 my $autogenerated_name_prefix_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'autogenerated_name_prefix', 'project_property')->cvterm_id();
251 my $material_of_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'material_of', 'stock_relationship')->cvterm_id;
252 if ($linked_project_id) {
253 my $new_names_info;
254 my $stored_prefix = $schema->resultset('Project::Projectprop')->find({ project_id => $linked_project_id, type_id => $autogenerated_name_prefix_cvterm_id});
255 if ($stored_prefix) {
256 my $prefix = $stored_prefix->value();
257 my $autogenerated_names = CXGN::Transformation::CreateAutogeneratedNames->new({schema => $schema, dbh => $dbh, breeding_program_id => $program_id, prefix => $prefix, number_of_names => $number_of_transformants });
258 $new_names_info = $autogenerated_names->create_names();
259 print STDERR "NEW NAMES INFO=".Dumper($new_names_info)."\n";
260 my $new_names = $new_names_info->{'new_autogenerated_names'};
261 my $new_current_serial_number = $new_names_info->{'new_current_serial_number'};
263 my $transformation_stock_id = $schema->resultset("Stock::StockRelationship")->find({object_id=>$tracking_identifier_id, type_id=>$material_of_type_id})->subject_id();
264 my $add_transformants = CXGN::Transformation::AddTransformant->new({
265 schema => $schema,
266 phenome_schema => $phenome_schema,
267 dbh => $dbh,
268 transformation_stock_id => $transformation_stock_id,
269 transformant_names => $new_names,
270 owner_id => $user_id,
273 $add_transformants->add_transformant();
275 my $name_metadata_cvterm = SGN::Model::Cvterm->get_cvterm_row($schema, 'autogenerated_name_metadata', 'project_property');
276 my $program = $schema->resultset('Project::Project')->find({ project_id => $program_id});
277 my $metadata_projectprop_rs = $program->projectprops({type_id => $name_metadata_cvterm->cvterm_id});
278 if ($metadata_projectprop_rs->count == 1){
279 my $stored_name_metadata_string = $metadata_projectprop_rs->first->value();
280 my $name_metadata_hash = decode_json $stored_name_metadata_string;
281 print STDERR "PREVIOUS METADATA =".Dumper($name_metadata_hash)."\n";
282 $name_metadata_hash->{$prefix}->{'current_serial_number'} = $new_current_serial_number;
283 my $new_name_metadata_string = encode_json $name_metadata_hash;
284 print STDERR "UPDATED METADATA =".Dumper($name_metadata_hash)."\n";
285 $metadata_projectprop_rs->first->update({value=>$new_name_metadata_string});
292 my $add_activity_info = CXGN::TrackingActivity::ActivityInfo->new({
293 schema => $schema,
294 tracking_identifier => $tracking_identifier,
295 selected_type => $selected_type,
296 input => $input,
297 timestamp => $record_timestamp,
298 operator_id => $user_id,
299 notes => $notes,
301 my $return = $add_activity_info->add_info();
302 # print STDERR "ADD INFO =".Dumper ($add_activity_info->add_info())."\n";
304 if (!$return){
305 $c->stash->{rest} = {error => "Error saving info",};
306 return;
307 } else {
308 $c->stash->{rest} = $return;
314 sub get_activity_details :Path('/ajax/tracking_activity/details') :Args(1) {
315 my $self = shift;
316 my $c = shift;
317 my $identifier_id = shift;
318 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
319 my $dbh = $c->dbc->dbh;
321 my $tracking_identifier_obj = CXGN::TrackingActivity::TrackingIdentifier->new({schema=>$schema, dbh=>$dbh, tracking_identifier_stock_id=>$identifier_id});
322 my $associated_projects = $tracking_identifier_obj->get_associated_project_program();
323 my $tracking_project_id = $associated_projects->[0]->[0];
324 my $tracking_project = CXGN::TrackingActivity::ActivityProject->new(bcs_schema => $schema, trial_id => $tracking_project_id);
325 my $activity_type = $tracking_project->get_project_activity_type();
327 my $tracking_activities;
328 if ($activity_type eq 'tissue_culture') {
329 $tracking_activities = $c->config->{tracking_tissue_culture_info};
330 } elsif ($activity_type eq 'transformation') {
331 $tracking_activities = $c->config->{tracking_transformation_info};
334 my @details;
335 my @activity_types = split ',',$tracking_activities;
337 my $tracking_data_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'tracking_tissue_culture_json', 'stock_property')->cvterm_id();
338 my $activity_info_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $identifier_id, type_id => $tracking_data_json_cvterm_id});
339 if ($activity_info_rs) {
340 my $activity_json = $activity_info_rs->value();
341 my $info = JSON::Any->jsonToObj($activity_json);
342 my %info_hash = %{$info};
343 foreach my $type (@activity_types){
344 my $empty_string;
345 my @each_type_details = ();
346 my $each_timestamp_string;
347 my $each_type_string;
348 if ($info_hash{$type}) {
349 my @each_type_details = ();
350 my $details = {};
351 my %details_hash = ();
352 $details = $info_hash{$type};
353 %details_hash = %{$details};
354 # print STDERR "DETAILS HASH =".Dumper(\%details_hash);
355 foreach my $timestamp (sort keys %details_hash) {
356 my @each_timestamp_details = ();
357 push @each_timestamp_details, "timestamp".":"."".$timestamp;
358 my $operator_id = $details_hash{$timestamp}{'operator_id'};
360 my $person= CXGN::People::Person->new($dbh, $operator_id);
361 my $operator_name = $person->get_first_name()." ".$person->get_last_name();
363 push @each_timestamp_details, "operator".":"."".$operator_name;
364 my $input = $details_hash{$timestamp}{'input'};
365 push @each_timestamp_details, "input".":"."".$input;
366 my $notes = $details_hash{$timestamp}{'notes'};
367 push @each_timestamp_details, "notes".":"."".$notes;
369 push @each_timestamp_details, $empty_string;
371 $each_timestamp_string = join("<br>", @each_timestamp_details);
372 push @each_type_details, $each_timestamp_string;
375 $each_type_string = join("<br>", @each_type_details);
376 # print STDERR "EACH TYPE STRING =".Dumper($each_type_string)."\n";
377 push @details, $each_type_string;
378 } else {
379 my $empty_string;
380 push @details, $empty_string;
383 } else {
384 foreach my $type (@activity_types) {
385 push @details, 'NA';
389 my @all_details;
390 push @all_details, [@details];
392 # print STDERR "ALL DETAILS =".Dumper(\@all_details)."\n";
394 $c->stash->{rest} = { data => \@all_details };
399 sub get_activity_summary :Path('/ajax/tracking_activity/summary') :Args(1) {
400 my $self = shift;
401 my $c = shift;
402 my $identifier_id = shift;
403 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
404 my $dbh = $c->dbc->dbh;
406 my $tracking_identifier_obj = CXGN::TrackingActivity::TrackingIdentifier->new({schema=>$schema, dbh=>$dbh, tracking_identifier_stock_id=>$identifier_id});
407 my $associated_projects = $tracking_identifier_obj->get_associated_project_program();
408 my $tracking_project_id = $associated_projects->[0]->[0];
409 my $tracking_project = CXGN::TrackingActivity::ActivityProject->new(bcs_schema => $schema, trial_id => $tracking_project_id);
410 my $activity_type = $tracking_project->get_project_activity_type();
412 my $tracking_activities;
413 if ($activity_type eq 'tissue_culture') {
414 $tracking_activities = $c->config->{tracking_tissue_culture_info};
415 } elsif ($activity_type eq 'transformation') {
416 $tracking_activities = $c->config->{tracking_transformation_info};
419 my @summary = ();
420 my @activity_types = split ',',$tracking_activities;
422 my $tracking_data_json_cvterm_id = SGN::Model::Cvterm->get_cvterm_row($schema, 'tracking_tissue_culture_json', 'stock_property')->cvterm_id();
423 my $activity_info_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $identifier_id, type_id => $tracking_data_json_cvterm_id});
424 if ($activity_info_rs) {
425 my $activity_json = $activity_info_rs->value();
426 my $info = JSON::Any->jsonToObj($activity_json);
427 my %info_hash = %{$info};
428 foreach my $type (@activity_types){
429 my $input = '';
430 my $empty_string;
431 my @each_type_details = ();
432 my $each_timestamp_string;
433 my $each_type_string;
434 if ($info_hash{$type}) {
435 my $details = {};
436 my %details_hash = ();
437 $details = $info_hash{$type};
438 %details_hash = %{$details};
440 if (($type =~ m/number/) || ($type =~ m/count/)) {
441 $input = 0;
442 foreach my $key (keys %details_hash) {
443 $input += $details_hash{$key}{'input'};
445 push @summary, $input;
446 } elsif ($type =~ m/date/) {
447 foreach my $key (keys %details_hash) {
448 $input = $details_hash{$key}{'input'};
450 push @summary, $input;
452 } else {
453 push @summary, $input;
456 } else {
457 foreach my $type (@activity_types) {
458 push @summary, 'NA';
462 my @all_summary;
463 push @all_summary, [@summary];
465 $c->stash->{rest} = { data => \@all_summary };
469 sub get_project_active_identifiers :Path('/ajax/tracking_activity/project_active_identifiers') :Args(1) {
470 my $self = shift;
471 my $c = shift;
472 my $project_id = shift;
473 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
475 my $tracking_project = CXGN::TrackingActivity::ActivityProject->new(bcs_schema => $schema, trial_id => $project_id);
476 my $activity_type = $tracking_project->get_project_activity_type();
478 my $tracking_activities;
479 if ($activity_type eq 'tissue_culture') {
480 $tracking_activities = $c->config->{tracking_tissue_culture_info};
481 } elsif ($activity_type eq 'transformation') {
482 $tracking_activities = $c->config->{tracking_transformation_info};
485 my @activity_types = split ',',$tracking_activities;
486 my $transformation_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "transformation", 'stock_type')->cvterm_id();
488 my $activity_project = CXGN::TrackingActivity::ActivityProject->new(bcs_schema => $schema, trial_id => $project_id);
489 my $all_identifier_info = $activity_project->get_project_active_identifiers();
490 my @all_identifiers;
491 foreach my $identifier_info (@$all_identifier_info) {
492 my @row = ();
493 my $identifier_id = $identifier_info->[0];
494 my $identifier_name = $identifier_info->[1];
495 push @row, qq{<a href="/activity/details/$identifier_id">$identifier_name</a>};
497 my $material_id = $identifier_info->[2];
498 my $material_rs = $schema->resultset("Stock::Stock")->find( { stock_id => $material_id });
499 my $material_stock_type_id = $material_rs->type_id;
500 my $material_name = $identifier_info->[3];
501 if ($material_stock_type_id == $transformation_type_id) {
502 push @row, qq{<a href="/transformation/$material_id">$material_name</a>}
503 } else {
504 push @row, qq{<a href="/stock/$material_id/view">$material_name</a>};
506 my $progress = $identifier_info->[5];
507 if ($progress) {
508 my $progress_ref = JSON::Any->jsonToObj($progress);
509 my %progress_hash = %{$progress_ref};
510 foreach my $type (@activity_types){
511 my $input = '';
512 if ($progress_hash{$type}) {
513 my $details = {};
514 my %details_hash = ();
515 $details = $progress_hash{$type};
516 %details_hash = %{$details};
518 if (($type =~ m/number/) || ($type =~ m/count/)) {
519 $input = 0;
520 foreach my $key (keys %details_hash) {
521 $input += $details_hash{$key}{'input'};
523 push @row, $input;
524 } elsif ($type =~ m/date/) {
525 foreach my $key (keys %details_hash) {
526 $input = $details_hash{$key}{'input'};
528 push @row, $input;
530 } else {
531 push @row, $input;
534 } else {
535 foreach my $type (@activity_types) {
536 push @row, '';
539 push @row, $identifier_name;
540 push @all_identifiers,[@row];
543 $c->stash->{rest} = { data => \@all_identifiers };
548 sub get_project_active_identifier_names :Path('/ajax/tracking_activity/project_active_identifier_names') :Args(1) {
549 my $self = shift;
550 my $c = shift;
551 my $project_id = shift;
552 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
554 my $activity_project = CXGN::TrackingActivity::ActivityProject->new(bcs_schema => $schema, trial_id => $project_id);
555 my $all_identifier_info = $activity_project->get_project_active_identifiers();
556 my @identifier_names;
557 foreach my $identifier_info (@$all_identifier_info) {
558 push @identifier_names, $identifier_info->[1];
561 $c->stash->{rest} = { data => \@identifier_names };
566 sub update_status : Path('/ajax/tracking_activity/update_status') : ActionClass('REST'){ }
568 sub update_status_POST : Args(0) {
569 my $self = shift;
570 my $c = shift;
571 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
572 my $dbh = $c->dbc->dbh();
573 my $identifier_id = $c->req->param("identifier_id");
574 my $status_type = $c->req->param("status_type");
575 my $comments = $c->req->param("comments");
576 my $material_id = $c->req->param("material_id");
577 my $time = DateTime->now();
578 my $update_date = $time->ymd();
579 my @stocks_to_update;
581 if (!$c->user()){
582 $c->stash->{rest} = { error_string => "You must be logged in to update status" };
583 return;
585 if (!$c->user()->check_roles("curator")) {
586 $c->stash->{rest} = { error_string => "You do not have the correct role to update status. Please contact us." };
587 return;
590 my $user_id = $c->user()->get_object()->get_sp_person_id();
592 my $tracking_identifier_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "tracking_identifier", 'stock_type')->cvterm_id();
593 my $transformation_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "transformation", 'stock_type')->cvterm_id();
595 my $identifier_rs = $schema->resultset("Stock::Stock")->find( { stock_id => $identifier_id, type_id => $tracking_identifier_type_id });
596 if (!$identifier_rs) {
597 $c->stash->{rest} = { error_string => 'Error. No tracking identifier entry found in the database.' };
598 return;
599 } else {
600 push @stocks_to_update, $identifier_id;
603 my $material_stock_type_id;
604 my $material_rs = $schema->resultset("Stock::Stock")->find( { stock_id => $material_id });
605 if (!$material_rs) {
606 $c->stash->{rest} = { error_string => 'Error. No material entry found in the database.' };
607 return;
608 } else {
609 $material_stock_type_id = $material_rs->type_id;
612 if ($material_stock_type_id == $transformation_type_id) {
613 push @stocks_to_update, $material_id;
616 my $completed_metadata;
617 my $terminated_metadata;
618 if ($status_type eq 'completed') {
619 $completed_metadata = 1;
620 } elsif ($status_type eq 'terminated') {
621 $terminated_metadata = 1;
624 foreach my $stock_id (@stocks_to_update) {
625 my $update_status = CXGN::Stock::Status->new({
626 bcs_schema => $schema,
627 parent_id => $stock_id,
628 person_id => $user_id,
629 update_date => $update_date,
630 comments => $comments,
631 completed_metadata => $completed_metadata,
632 terminated_metadata => $terminated_metadata
635 $update_status->store();
637 if (!$update_status->store()){
638 $c->stash->{rest} = {error_string => "Error updating status"};
639 return;
643 $c->stash->{rest} = {success => "1",};
647 sub reverse_status : Path('/ajax/tracking_activity/reverse_status') : ActionClass('REST'){ }
649 sub reverse_status_POST : Args(0) {
650 my $self = shift;
651 my $c = shift;
652 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
653 my $dbh = $c->dbc->dbh();
654 my $identifier_id = $c->req->param("identifier_id");
655 my $updated_status_type = $c->req->param("updated_status_type");
656 my $material_id = $c->req->param("material_id");
658 if (!$c->user()){
659 $c->stash->{rest} = { error_string => "You must be logged in to reverse status of this tracking identifier" };
660 return;
662 if (!$c->user()->check_roles("curator")) {
663 $c->stash->{rest} = { error_string => "You do not have the correct role to reverse status of this tracking identifier. Please contact us." };
664 return;
667 my $user_id = $c->user()->get_object()->get_sp_person_id();
669 my $status_type_id;
670 if ($updated_status_type) {
671 $status_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, $updated_status_type, 'stock_property')->cvterm_id();
672 my $status_stockprop_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $identifier_id, type_id => $status_type_id});
674 if (defined $status_stockprop_rs->stockprop_id) {
675 $status_stockprop_rs->delete();
679 my $material_stock_type_id;
680 my $material_rs = $schema->resultset("Stock::Stock")->find( { stock_id => $material_id });
681 if (!$material_rs) {
682 $c->stash->{rest} = { error_string => 'Error. No material entry found in the database.' };
683 return;
684 } else {
685 $material_stock_type_id = $material_rs->type_id;
688 my $transformation_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "transformation", 'stock_type')->cvterm_id();
690 if ($material_stock_type_id == $transformation_type_id) {
691 if ($status_type_id) {
692 my $transformation_stockprop_rs = $schema->resultset("Stock::Stockprop")->find({stock_id => $material_id, type_id => $status_type_id});
694 if (defined $transformation_stockprop_rs->stockprop_id) {
695 $transformation_stockprop_rs->delete();
700 $c->stash->{rest} = { success => 1 };
705 sub get_project_inactive_identifiers :Path('/ajax/tracking_activity/project_inactive_identifiers') :Args(1) {
706 my $self = shift;
707 my $c = shift;
708 my $project_id = shift;
709 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
711 my $tracking_project = CXGN::TrackingActivity::ActivityProject->new(bcs_schema => $schema, trial_id => $project_id);
712 my $activity_type = $tracking_project->get_project_activity_type();
714 my $tracking_activities;
715 if ($activity_type eq 'tissue_culture') {
716 $tracking_activities = $c->config->{tracking_tissue_culture_info};
717 } elsif ($activity_type eq 'transformation') {
718 $tracking_activities = $c->config->{tracking_transformation_info};
721 my @activity_types = split ',',$tracking_activities;
723 my $transformation_type_id = SGN::Model::Cvterm->get_cvterm_row($schema, "transformation", 'stock_type')->cvterm_id();
725 my $activity_project = CXGN::TrackingActivity::ActivityProject->new(bcs_schema => $schema, trial_id => $project_id);
726 my $all_identifier_info = $activity_project->get_project_inactive_identifiers();
727 my @all_identifiers;
728 foreach my $identifier_info (@$all_identifier_info) {
729 my @row = ();
730 my $identifier_id = $identifier_info->[0];
731 my $identifier_name = $identifier_info->[1];
732 push @row, qq{<a href="/activity/details/$identifier_id">$identifier_name</a>};
734 my $status = $identifier_info->[6];
735 if ($status eq 'terminated_metadata') {
736 $status = '<span style="color:red">'.'TERMINATED'.'</span>';
737 } elsif ($status eq 'completed_metadata') {
738 $status = '<span style="color:red">'.'COMPLETED'.'</span>';
740 push @row, $status;
742 my $material_id = $identifier_info->[2];
743 my $material_name = $identifier_info->[3];
744 my $material_rs = $schema->resultset("Stock::Stock")->find( { stock_id => $material_id });
745 my $material_stock_type_id = $material_rs->type_id;
746 if ($material_stock_type_id == $transformation_type_id) {
747 push @row, qq{<a href="/transformation/$material_id">$material_name</a>}
748 } else {
749 push @row, qq{<a href="/stock/$material_id/view">$material_name</a>};
752 my $progress = $identifier_info->[5];
753 if ($progress) {
754 my $progress_ref = JSON::Any->jsonToObj($progress);
755 my %progress_hash = %{$progress_ref};
756 foreach my $type (@activity_types){
757 my $input = '';
758 if ($progress_hash{$type}) {
759 my $details = {};
760 my %details_hash = ();
761 $details = $progress_hash{$type};
762 %details_hash = %{$details};
764 if (($type =~ m/number/) || ($type =~ m/count/)) {
765 $input = 0;
766 foreach my $key (keys %details_hash) {
767 $input += $details_hash{$key}{'input'};
769 push @row, $input;
770 } elsif ($type =~ m/date/) {
771 foreach my $key (keys %details_hash) {
772 $input = $details_hash{$key}{'input'};
774 push @row, $input;
776 } else {
777 push @row, $input;
780 } else {
781 foreach my $type (@activity_types) {
782 push @row, '';
785 push @row, $identifier_name;
786 push @all_identifiers,[@row];
789 $c->stash->{rest} = { data => \@all_identifiers };