2 package SGN
::Controller
::AJAX
::TrackingActivity
;
5 use CXGN
::Stock
::Order
;
6 use CXGN
::Stock
::OrderBatch
;
10 use CXGN
::People
::Person
;
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
;
21 use File
::Basename qw
| basename dirname
|;
24 use File
::Spec
::Functions
;
26 use File
::Path
qw(make_path);
27 use File
::Spec
::Functions qw
/ catfile catdir/;
32 use URI
::Encode
qw(uri_encode uri_decode);
33 use Tie
::UrlEncoder
; our(%urlencode);
36 BEGIN { extends
'Catalyst::Controller::REST' }
39 default => 'application/json',
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) {
52 $c->stash->{rest
} = { error
=> "You must be logged in to add project." };
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." };
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"};
80 my $add_activity_project = CXGN
::TrackingActivity
::AddActivityProject
->new({
81 bcs_schema
=> $schema,
83 breeding_program_id
=> $breeding_program_id,
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(),
92 my $return = $add_activity_project->save_activity_project();
93 print STDERR
"RETURN =".Dumper
($return)."\n";
95 if ($return->{error
}){
96 $error = $return->{error
};
101 $c->stash->{rest
} = {error
=> $@
};
107 $c->stash->{rest
} = {error
=> $error};
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) {
122 $c->stash->{rest
} = { error
=> "You must be logged in to generate tracking identifiers." };
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." };
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");
138 my $project_rs = $schema->resultset("Project::Project")->find( { name
=> $project_name });
140 print STDERR
"Error! Project name: $project_name was not found in the database.\n";
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({
161 phenome_schema
=> $phenome_schema,
162 tracking_identifier
=> $tracking_identifier,
163 material
=> $material,
164 project_id
=> $project_id,
168 my $return = $tracking_obj->store();
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) {
186 $c->stash->{rest
} = { error
=> "You must be logged in to add properties." };
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." };
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({
213 tracking_identifier
=> $tracking_identifier,
214 selected_type
=> $selected_type,
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",};
227 $c->stash->{rest
} = { success
=> 1};
232 sub get_activity_details
:Path
('/ajax/tracking_activity/details') :Args
(1) {
235 my $identifier_id = shift;
236 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
237 my $dbh = $c->dbc->dbh;
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){
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 = ();
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;
283 push @details, $empty_string;
287 foreach my $type (@activity_types) {
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) {
305 my $identifier_id = shift;
306 my $schema = $c->dbic_schema('Bio::Chado::Schema', 'sgn_chado');
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) {
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){
321 my @each_type_details = ();
322 my $each_timestamp_string;
323 my $each_type_string;
324 if ($info_hash{$type}) {
326 my %details_hash = ();
327 $details = $info_hash{$type};
328 %details_hash = %{$details};
330 foreach my $key (keys %details_hash) {
331 $input += $details_hash{$key}{'input'};
333 push @summary, $input;
335 push @summary, $input;
339 foreach my $type (@activity_types) {
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) {
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();
362 foreach my $identifier_info (@
$all_identifier_info) {
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];
373 my %progress_hash = %{$progress};
374 foreach my $type (@activity_types){
375 if ($progress_hash{$type}) {
377 my %details_hash = ();
378 $details = $progress_hash{$type};
379 %details_hash = %{$details};
381 foreach my $key (keys %details_hash) {
382 $input += $details_hash{$key}{'input'};
390 foreach my $type (@activity_types) {
395 push @all_identifiers,[@row];
398 $c->stash->{rest
} = { data
=> \
@all_identifiers };