clean
[sgn.git] / lib / SGN / Controller / AJAX / Search / Trial.pm
blob8b39b0d74e078269340fb4a62412a5510e002365
2 package SGN::Controller::AJAX::Search::Trial;
4 use Moose;
5 use Data::Dumper;
6 use CXGN::Trial;
8 BEGIN { extends 'Catalyst::Controller::REST'; }
10 __PACKAGE__->config(
11 default => 'application/json',
12 stash_key => 'rest',
13 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
16 sub search :Path('/ajax/search/trials') Args(0) {
17 my $self = shift;
18 my $c = shift;
20 my $nd_geolocation= $c->req->param('nd_geolocation');
22 print STDERR "location: ".$nd_geolocation."\n";
24 my $schema = $c->dbic_schema("Bio::Chado::Schema");
26 # pre-fetch some information; more efficient
28 my $breeding_program_cvterm_id = $self->get_breeding_program_cvterm_id($c);
29 my $trial_folder_cvterm_id = $self->get_trial_folder_cvterm_id($c);
30 my $location_cvterm_id = $c->model("Cvterm")->get_cvterm_row($schema, "project location", "project_property")->cvterm_id();
31 my $year_cvterm_id = $c->model("Cvterm")->get_cvterm_row($schema, "project year", "project_property")->cvterm_id();
32 my $project_type_cv_id = $schema->resultset("Cv::Cv")->find( { name => "project_type" } )->cv_id();
33 my %project_types;
34 my $project_type_rs = $schema->resultset("Cv::Cvterm")->search( { cv_id => $project_type_cv_id });
35 while (my $row = $project_type_rs->next()) {
36 $project_types{$row->cvterm_id} = $row->name();
38 my %projects;
39 my $project_rs = $schema->resultset("Project::Project")->search( {} );
40 while (my $p = $project_rs->next()) {
41 $projects{$p->project_id} = $p->name();
44 my %parent_projects;
45 my $parent_project_rs = $schema->resultset("Project::Project")
46 ->search( {}, { join => "project_relationship_subject_projects",
47 '+select' => [ 'project_relationship_subject_projects.object_project_id'],
48 '+as' => [ 'parent_project_id' ]
49 });
51 while (my $row = $parent_project_rs->next()) {
52 $parent_projects{$row->project_id()} = $projects{$row->get_column('parent_project_id')};
55 my %locations;
56 my $location_rs = $schema->resultset("NaturalDiversity::NdGeolocation")->search( { } );
57 while (my $row = $location_rs->next()) {
58 $locations{$row->nd_geolocation_id()} = $row->description();
61 my $projects_rs = $schema->resultset("Project::Project")
62 ->search( {}, { join => 'projectprops',
63 '+select' => [ 'projectprops.type_id', 'projectprops.value' ],
64 '+as' => ['projectprop_type_id', 'projectprop_value']
65 });
67 my @result;
69 # make a unique trial list using a hash and filling in auxiliary info...
71 my %trials = ();
73 while ( my $p = $projects_rs->next() ) {
74 my $project_id = $p->project_id;
75 my $project_name = $p->name();
77 $trials{$project_name}->{project_id} = $p->project_id();
78 $trials{$project_name}->{project_description} = $p->description();
80 my $type_id = $p->get_column('projectprop_type_id');
81 my $value = $p->get_column('projectprop_value');
83 print STDERR "READ: $project_name, $type_id, $value\n";
85 if ($type_id == $trial_folder_cvterm_id) {
86 $trials{$project_name}->{trial_folder} = "FOLDER";
88 if ($type_id == $location_cvterm_id) {
89 $trials{$project_name}->{location} = $locations{$value};
91 if ($type_id == $year_cvterm_id) {
92 $trials{$project_name}->{year} = $value;
95 print "$type_id corresponds to project type $project_types{$type_id}\n";
96 $trials{$project_name}->{project_type} = $project_types{$type_id};
98 $trials{$project_name}->{breeding_program} = $parent_projects{$project_id};
101 foreach my $t (sort(keys(%trials))) {
102 print STDERR "trial location = $trials{$t}->{location} \n";
103 next unless ($nd_geolocation eq 'not_provided' || $trials{$t}->{location} eq $nd_geolocation);
104 print STDERR "matched trial location = $trials{$t}->{location} \n";
105 push @result, [
106 "<a href=\"/breeders_toolbox/trial/$trials{$t}->{project_id}\">$t</a>",
107 $trials{$t}->{project_description},
108 $trials{$t}->{breeding_program},
109 $trials{$t}->{year},
110 $trials{$t}->{location},
111 $trials{$t}->{project_type}
115 $c->stash->{rest} = { data => \@result };
118 sub get_project_year_cvterm_id {
119 my $self = shift;
120 my $c = shift;
122 my $schema = $c->dbic_schema("Bio::Chado::Schema");
124 my $row = $schema->resultset("Cv::Cvterm")->find( { name => 'project year' });
126 return $row->cvterm_id();
129 sub get_project_location_cvterm_id {
130 my $self = shift;
131 my $c = shift;
133 my $schema = $c->dbic_schema("Bio::Chado::Schema");
135 my $row = $schema->resultset("Cv::Cvterm")->find( { name => 'project location' });
137 return $row->cvterm_id();
140 sub get_breeding_program_cvterm_id {
141 my $self = shift;
142 my $c = shift;
143 my $schema = $c->dbic_schema("Bio::Chado::Schema");
144 my $row = $schema->resultset("Cv::Cvterm")->find( { name => 'breeding_program' });
146 return $row->cvterm_id();
150 sub get_trial_folder_cvterm_id {
151 my $self = shift;
152 my $c = shift;
153 my $schema = $c->dbic_schema("Bio::Chado::Schema");
154 my $row = $schema->resultset("Cv::Cvterm")->find( { name => 'trial_folder' });
156 return $row->cvterm_id();