2 package SGN
::Controller
::AJAX
::Search
::Trial
;
8 BEGIN { extends
'Catalyst::Controller::REST'; }
11 default => 'application/json',
13 map => { 'application/json' => 'JSON', 'text/html' => 'JSON' },
16 sub search
:Path
('/ajax/search/trials') Args
(0) {
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();
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();
39 my $project_rs = $schema->resultset("Project::Project")->search( {} );
40 while (my $p = $project_rs->next()) {
41 $projects{$p->project_id} = $p->name();
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' ]
51 while (my $row = $parent_project_rs->next()) {
52 $parent_projects{$row->project_id()} = $projects{$row->get_column('parent_project_id')};
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']
69 # make a unique trial list using a hash and filling in auxiliary info...
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";
106 "<a href=\"/breeders_toolbox/trial/$trials{$t}->{project_id}\">$t</a>",
107 $trials{$t}->{project_description
},
108 $trials{$t}->{breeding_program
},
110 $trials{$t}->{location
},
111 $trials{$t}->{project_type
}
115 $c->stash->{rest
} = { data
=> \
@result };
118 sub get_project_year_cvterm_id
{
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
{
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
{
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
{
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();